nihdoc

WIP markup parser (txt -> html)
git clone git://git.alexkarle.com.com/blag
Log | Files | Refs | README | LICENSE

commit 1f0e043f48b8261563d10f8d7249a78290075f30 (patch)
parent e8150fca9f396da1eb1ae369f636c66622065a6e
Author: Alex Karle <alex@alexkarle.com>
Date:   Mon, 20 Dec 2021 00:54:03 -0500

Fix inline formatting at beginning of paragraph

Paragraphs start usually when a non special character is entered in the
NONE state. Previous to this change, they were not starting until that
non-special character, meaning lines starting with formatting would have
a strange skew:

    <em><p>italics</em></p>

This patch encapsulates the "start a paragraph if it's NONE" and
triggers it for all inline formatting as well as non-special chars

Diffstat:
Mblag.c | 22++++++++++++----------
Mtest/big.html | 12++++++++++++
Mtest/big.txt | 6++++++
3 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/blag.c b/blag.c @@ -65,6 +65,13 @@ int endlist(state *s) { return --s->listdepth; } +void maybe_startp(state *s) { + /* All inline types should start the paragraph if no other major type present*/ + if (s->in == NONE) { + s->in = PARAGRAPH; + printf("<p>\n"); + } +} void handle_lf(state *s) { s->indent = 0; @@ -188,6 +195,7 @@ int parse() { break; case '`': if (s.in_link != LINK_URL_PARSE && s.in != CODE) { + maybe_startp(&s); if (s.in_code) { printf("</code>"); s.in_code = 0; @@ -201,6 +209,7 @@ int parse() { break; case '*': if (!s.in_code && s.in_link != LINK_URL_PARSE && s.in != CODE) { + maybe_startp(&s); if (s.in_bold) { printf("</strong>"); s.in_bold = 0; @@ -214,6 +223,7 @@ int parse() { break; case '_': if (!s.in_code && s.in_link != LINK_URL_PARSE && s.in != CODE) { + maybe_startp(&s); if (s.in_ital) { printf("</em>"); s.in_ital = 0; @@ -238,11 +248,7 @@ int parse() { break; case '[': if (s.in_link == NONE && !s.in_code) { - if (s.in == NONE) { - /* Assume this is a new paragraph */ - s.in = PARAGRAPH; - printf("<p>\n"); - } + maybe_startp(&s); s.in_link = LINK_URL_PARSE; s.lnkidx = 0; printf("<a href=\""); @@ -288,11 +294,7 @@ int parse() { putesc(c); break; default: - if (s.in == NONE) { - /* nothing else was matched -> assume new <p> */ - s.in = PARAGRAPH; - printf("<p>\n"); - } + maybe_startp(&s); putesc(c); break; } diff --git a/test/big.html b/test/big.html @@ -4,6 +4,18 @@ This is an example blag file! </p> +<p> +<em>This should be emphasized within a paragraph</em> +</p> + +<p> +<strong>As should this bold</strong> +</p> + +<p> +<code>and code!</code> +</p> + <h2>Sub Header 2</h2> <h3>Woah a third header?</h3> diff --git a/test/big.txt b/test/big.txt @@ -2,6 +2,12 @@ This is an example blag file! +_This should be emphasized within a paragraph_ + +*As should this bold* + +`and code!` + ## Sub Header 2 ### Woah a third header?