From c5e7bbfd2d57b19942feca071f587db00e5aadca Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sat, 18 Dec 2021 15:53:00 -0500 Subject: [PATCH] Refactor to combine list implementations (still no mixing) This should get us closer to the mixed lists! Now lists share the type and we just have yet another struct member saying what type of list we're in (for the new/end functions). Supporting mixed lists should now just be a matter of combining the listdepth and ol members into a stack! --- blag.c | 110 ++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------- 1 file changed, 42 insertions(+), 68 deletions(-) diff --git a/blag.c b/blag.c index 6112701..d96860f 100644 --- a/blag.c +++ b/blag.c @@ -11,17 +11,13 @@ enum Block { NONE, - HEADER_PARSE, HEADER, + HEADER_PARSE, PARAGRAPH, CODE, QUOTE, - ULIST, - ULIST_START, - ULIST_PARSE, - OLIST, - OLIST_START, - OLIST_PARSE, + LIST, + LIST_PARSE, LINK_URL_PARSE, LINK_DESC_PARSE, }; @@ -40,6 +36,7 @@ typedef struct State { int previndent; int listdepth; int lastc; + int ol; int linestarted; } state; @@ -54,6 +51,21 @@ void putesc(int c) { } } +void newlist(state *s) { + s->in = LIST; + s->previndent = s->indent; + printf("\n<%s>\n
  • \n", s->ol ? "ol" : "ul"); + s->listdepth++; +} + +int endlist(state *s) { + s->in = LIST; + s->previndent = s->indent; + printf("\n
  • \n\n", s->ol ? "ol" : "ul"); + return --s->listdepth; +} + + void handle_lf(state *s) { s->indent = 0; s->linestarted = 0; @@ -72,13 +84,10 @@ void handle_lf(state *s) { case PARAGRAPH: printf("

    \n"); break; case CODE: printf("\n"); break; case QUOTE: printf("\n"); break; - case OLIST: - case ULIST: + case LIST: s->previndent = 0; - while (s->listdepth > 0) { - printf("\n\n\n", s->in == OLIST ? "ol" : "ul"); - s->listdepth--; - } + while (endlist(s)) + ; break; default: break; /* no op */ @@ -105,6 +114,7 @@ int parse() { .listdepth = 0, .lastc = 0, .linestarted = 0, + .ol = 0, }; while ((c = getchar()) != EOF) { @@ -152,25 +162,23 @@ int parse() { if (s.in == HEADER_PARSE) { printf("", s.hlvl); s.in = HEADER; - } else if (s.in == ULIST_START) { - s.previndent = s.indent; - printf("\n\n"); - s.previndent = s.indent; - s.listdepth--; - s.in = ULIST_PARSE; - } else { - /* next list item */ - s.in = ULIST_PARSE; - } - } else { - putesc(c); - } - break; case '1': case '2': case '3': @@ -283,23 +268,12 @@ int parse() { case '8': case '9': case '.': - if (s.in == NONE) { - s.in = OLIST_START; - } else if (s.in == OLIST_START || s.in == OLIST_PARSE) { + case '-': + if (s.in == NONE || !s.linestarted) { + s.ol = c != '-'; + s.in = LIST_PARSE; + } else if (s.in == LIST_PARSE) { /* no op */ - } else if (!s.linestarted) { - if (s.previndent < s.indent) { - /* new sublist */ - s.in = OLIST_START; - } else if (s.previndent > s.indent) { - /* end of a sublist */ - printf("\n\n\n"); - s.listdepth--; - s.in = OLIST_PARSE; - } else { - /* next list item */ - s.in = OLIST_PARSE; - } } else { putesc(c); } -- libgit2 1.1.1