From 3efd22a207d0f330f400d821671a55e3bc5d2041 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Thu, 23 Dec 2021 19:55:26 -0500 Subject: [PATCH] Refactor in_link to have its own enum state It's not really a Block type in the regular sense! --- blag.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/blag.c b/blag.c index 8992d79..d092a38 100644 --- 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("", 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(""); @@ -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(""); @@ -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(""); @@ -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("%s", 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(""); } else { putesc(c); -- libgit2 1.1.1