commit c5e7bbfd2d57b19942feca071f587db00e5aadca (patch)
parent c75dd84b276e33b70613d251eb0a3b9782d8bc96
Author: Alex Karle <alex@alexkarle.com>
Date: Sat, 18 Dec 2021 15:53:00 -0500
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!
Diffstat:
M | blag.c | | | 110 | ++++++++++++++++++++++++++++++------------------------------------------------- |
1 file changed, 42 insertions(+), 68 deletions(-)
diff --git 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<li>\n", s->ol ? "ol" : "ul");
+ s->listdepth++;
+}
+
+int endlist(state *s) {
+ s->in = LIST;
+ s->previndent = s->indent;
+ printf("\n</li>\n</%s>\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("</p>\n"); break;
case CODE: printf("</pre></code>\n"); break;
case QUOTE: printf("</blockquote>\n"); break;
- case OLIST:
- case ULIST:
+ case LIST:
s->previndent = 0;
- while (s->listdepth > 0) {
- printf("\n</li>\n</%s>\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("<h%d>", s.hlvl);
s.in = HEADER;
- } else if (s.in == ULIST_START) {
- s.previndent = s.indent;
- printf("\n<ul>\n<li>\n");
- s.in = ULIST;
- s.listdepth++;
- } else if (s.in == ULIST_PARSE) {
- printf("\n</li>\n<li>\n");
- s.in = ULIST;
- } else if (s.in == OLIST_START) {
- s.previndent = s.indent;
- printf("\n<ol>\n<li>\n");
- s.in = OLIST;
- s.listdepth++;
- } else if (s.in == OLIST_PARSE) {
- printf("\n</li>\n<li>\n");
- s.in = OLIST;
} else if (s.in_link == LINK_URL_PARSE) {
s.in_link = LINK_DESC_PARSE;
printf("\">");
+ } else if (s.in == LIST_PARSE) {
+ if (!s.listdepth) {
+ newlist(&s);
+ } else {
+ if (s.previndent < s.indent) {
+ newlist(&s);
+ } else if (s.previndent > s.indent) {
+ endlist(&s);
+ printf("\n</li>\n<li>\n");
+ } else {
+ s.in = LIST;
+ printf("\n</li>\n<li>\n");
+ }
+ }
} else {
putesc(c);
}
@@ -250,29 +258,6 @@ int parse() {
putesc(c);
}
break;
- case '-':
- if (s.in == NONE) {
- s.in = ULIST_START;
- } else if (s.in == ULIST_START || s.in == ULIST_PARSE) {
- /* no op */
- } else if (!s.linestarted) {
- if (s.previndent < s.indent) {
- /* new sublist */
- s.in = ULIST_START;
- } else if (s.previndent > s.indent) {
- /* end of a sublist */
- printf("\n</li>\n</ul>\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</li>\n</ol>\n");
- s.listdepth--;
- s.in = OLIST_PARSE;
- } else {
- /* next list item */
- s.in = OLIST_PARSE;
- }
} else {
putesc(c);
}