nihdoc

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

commit 3efd22a207d0f330f400d821671a55e3bc5d2041 (patch)
parent db1d1cb328a53fa466c9f53e8c84e0f2b8befca3
Author: Alex Karle <alex@alexkarle.com>
Date:   Thu, 23 Dec 2021 19:55:26 -0500

Refactor in_link to have its own enum state

It's not really a Block type in the regular sense!

Diffstat:
Mblag.c | 38+++++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/blag.c b/blag.c @@ -18,13 +18,17 @@ enum Block { QUOTE, LIST, LIST_PARSE, - LINK_URL_PARSE, - LINK_DESC_PARSE, +}; + +enum Link { + NOL, + URL_PARSE, + DESC_PARSE, }; typedef struct State { enum Block in; - enum Block in_link; + enum Link in_link; int hlvl; int in_code; int in_ital; @@ -84,7 +88,7 @@ void handle_lf(state *s) { } /* Guard against newlines when in parsing types */ - if (s->in_link == LINK_URL_PARSE || s->in_link == LINK_DESC_PARSE) { + if (s->in_link == URL_PARSE || s->in_link == DESC_PARSE) { errx(1, "newline detected while processing link"); } @@ -111,7 +115,7 @@ int parse() { int c; state s = { .in = NONE, - .in_link = NONE, + .in_link = NOL, .hlvl = 0, .in_code = 0, .in_ital = 0, @@ -142,7 +146,7 @@ int parse() { } /* Store links as we go */ - if (s.in_link == LINK_URL_PARSE && c != ']') { + if (s.in_link == URL_PARSE && c != ']') { s.lnkbuf[s.lnkidx++] = c; } @@ -172,8 +176,8 @@ int parse() { if (s.in == HEADER_PARSE) { printf("<h%d>", s.hlvl); s.in = HEADER; - } else if (s.in_link == LINK_URL_PARSE) { - s.in_link = LINK_DESC_PARSE; + } else if (s.in_link == URL_PARSE) { + s.in_link = DESC_PARSE; printf("\">"); } else if (s.in == LIST_PARSE) { if (!s.listdepth) { @@ -194,7 +198,7 @@ int parse() { } break; case '`': - if (s.in_link != LINK_URL_PARSE && s.in != CODE) { + if (s.in_link != URL_PARSE && s.in != CODE) { maybe_startp(&s); if (s.in_code) { printf("</code>"); @@ -208,7 +212,7 @@ int parse() { } break; case '*': - if (!s.in_code && s.in_link != LINK_URL_PARSE && s.in != CODE) { + if (!s.in_code && s.in_link != URL_PARSE && s.in != CODE) { maybe_startp(&s); if (s.in_bold) { printf("</strong>"); @@ -222,7 +226,7 @@ int parse() { } break; case '_': - if (!s.in_code && s.in_link != LINK_URL_PARSE && s.in != CODE) { + if (!s.in_code && s.in_link != URL_PARSE && s.in != CODE) { maybe_startp(&s); if (s.in_ital) { printf("</em>"); @@ -247,9 +251,9 @@ int parse() { } break; case '[': - if (s.in_link == NONE && !s.in_code) { + if (s.in_link == NOL && !s.in_code) { maybe_startp(&s); - s.in_link = LINK_URL_PARSE; + s.in_link = URL_PARSE; s.lnkidx = 0; printf("<a href=\""); } else { @@ -257,13 +261,13 @@ int parse() { } break; case ']': - if (s.in_link == LINK_URL_PARSE) { + if (s.in_link == URL_PARSE) { /* no description */ - s.in_link = NONE; + s.in_link = NOL; s.lnkbuf[s.lnkidx] = '\0'; printf("\">%s</a>", s.lnkbuf); - } else if (s.in_link == LINK_DESC_PARSE) { - s.in_link = NONE; + } else if (s.in_link == DESC_PARSE) { + s.in_link = NOL; printf("</a>"); } else { putesc(c);