commit 2c0827e183e223d0efd935d037b5d324a7ae1e1f
parent 997be2c014d5746dea5439c3a1abd404216de524
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Sat, 25 Sep 2021 22:45:00 +0000
Markdown: parse underline style headers
Diffstat:
M | extra/Markdown.c | | | 84 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
1 file changed, 78 insertions(+), 6 deletions(-)
diff --git a/extra/Markdown.c b/extra/Markdown.c
@@ -12,6 +12,7 @@ enum {
TEOF = 0,
TH0, TH1, TH2, TH3, TH4, TH5, TH6,
TWORD, TWBRK, TPBRK,
+ TLINK,
TUNDEF = -1,
};
@@ -32,6 +33,8 @@ void lnewline(void);
void lheader(void);
void lhspace(void);
void lhword(void);
+void lhline(void);
+void lsubhline(void);
void lword(void);
void lspace(void);
@@ -85,7 +88,10 @@ main(int argc, char **argv)
tok.type = TEOF;
emit();
- for (i = 0; i < tokn; i++) printtoken(tokens[i]);
+ for (i = 0; i < tokn; i++) {
+ if (tokens[i].type == TEOF) break;
+ printtoken(tokens[i]);
+ }
}
void
@@ -107,6 +113,14 @@ lnewline(void)
consume();
tok.type = TH0;
break;
+ case '=':
+ lex = lhline;
+ consume();
+ break;
+ case '-':
+ lex =lsubhline;
+ consume();
+ break;
default:
lex = lword;
emitwbrk();
@@ -132,10 +146,11 @@ lword(void)
break;
case ' ':
lex = lspace;
+ s_putc(tok.s, c);
consume();
- emit();
- emitwbrk();
- tok.type = TWORD;
+ //emit();
+ //emitwbrk();
+ //tok.type = TWORD;
break;
default:
s_putc(tok.s, c);
@@ -150,13 +165,17 @@ lspace(void)
c = peek(0);
switch (c) {
case ' ':
+ consume();
+ break;
case '\n':
- lex = lheader;
+ tok.type = TWORD;
+ lex = lnewline;
consume();
+ emit();
+ tok.type = TUNDEF;
break;
default:
lex = lword;
- tok.type = TWORD;
}
}
@@ -215,6 +234,7 @@ lhword(void)
switch(c) {
case 0:
lex = nil;
+ break;
case ' ':
s_putc(tok.s, c);
consume();
@@ -232,6 +252,58 @@ lhword(void)
}
}
+void
+lhline(void)
+{
+ char c;
+ c = peek(0);
+ switch (c) {
+ case 0:
+ lex = nil;
+ break;
+ case '=':
+ consume();
+ s_putc(tok.s, c);
+ break;
+ case '\n':
+ lex = lnewline;
+ consume();
+ tokens[tokn - 1].type = TH1;
+ break;
+ default:
+ lex = lword;
+ consume();
+ tok.type = TWORD;
+ emit();
+ };
+}
+
+void
+lsubhline(void)
+{
+ char c;
+ c = peek(0);
+ switch (c){
+ case 0:
+ lex = nil;
+ break;
+ case '-':
+ consume();
+ s_putc(tok.s, c);
+ break;
+ case '\n':
+ lex = lnewline;
+ tokens[tokn - 1].type = TH2;
+ consume();
+ break;
+ default:
+ lex = lword;
+ tok.type = TWORD;
+ consume();
+ emit();
+ }
+}
+
char
consume(void)
{