Added ATX headers

This commit is contained in:
Nate Choe
2022-04-23 17:47:44 -05:00
parent 8a7c1e9bdf
commit ad26f3e7be
3 changed files with 55 additions and 20 deletions

View File

@@ -27,8 +27,14 @@ enum linetype {
HR, HR,
SETEXT1, SETEXT1,
/* === */ /* === */
SETEXT2 SETEXT2,
/* --- */ /* --- */
HEADER
};
struct linedata {
enum linetype type;
int intensity;
}; };
enum nodetype { enum nodetype {
@@ -40,8 +46,8 @@ enum nodetype {
NONE NONE
}; };
enum linetype identifyline(char *line, enum nodetype prev); void identifyline(char *line, enum nodetype prev, struct linedata *ret);
/* prev is almost never used, but sometimes it is. */ /* prev is almost never used, but sometimes it is. */
char *realcontent(char *line, enum linetype type); char *realcontent(char *line, struct linedata *data);
#endif #endif

View File

@@ -24,19 +24,22 @@
static char *truncate(char *str); static char *truncate(char *str);
enum linetype identifyline(char *line, enum nodetype prev) { void identifyline(char *line, enum nodetype prev, struct linedata *ret) {
int i; int i;
if (prev != PARAGRAPH) { if (prev != PARAGRAPH) {
for (i = 0; i < 4; ++i) { for (i = 0; i < 4; ++i) {
if (!isspace(line[i])) if (!isspace(line[i]))
goto notspacecode; goto notspacecode;
} }
return SPACECODE; ret->type = SPACECODE;
return;
} }
notspacecode: notspacecode:
line = truncate(line); line = truncate(line);
if (line[0] == '\0') if (line[0] == '\0') {
return EMPTY; ret->type = EMPTY;
return;
}
{ {
int hrcount; int hrcount;
if (strchr("-*_=", line[0]) == NULL) if (strchr("-*_=", line[0]) == NULL)
@@ -55,11 +58,14 @@ notspacecode:
if (hrcount >= 3) { if (hrcount >= 3) {
switch (line[0]) { switch (line[0]) {
case '=': case '=':
return SETEXT1; ret->type = SETEXT1;
return;
case '-': case '-':
return SETEXT2; ret->type = SETEXT2;
return;
default: default:
return HR; ret->type = HR;
return;
} }
} }
/* There has to be at least 3 delimiter characters */ /* There has to be at least 3 delimiter characters */
@@ -69,10 +75,23 @@ nothr:
if (line[i] != '`') if (line[i] != '`')
goto notfencedcode; goto notfencedcode;
} }
return FENCECODE; ret->type = FENCECODE;
return;
notfencedcode: notfencedcode:
return PLAIN; if (line[0] == '#') {
int pcount;
for (pcount = 0; line[pcount] == '#'; ++pcount) ;
if (line[pcount] != ' ' && line[pcount] != '\0')
goto notheader;
ret->type = HEADER;
ret->intensity = pcount;
return;
}
notheader:
ret->type = PLAIN;
return;
} }
/* TODO: Finish this */ /* TODO: Finish this */
@@ -82,14 +101,16 @@ static char *truncate(char *str) {
return str; return str;
} }
char *realcontent(char *line, enum linetype type) { char *realcontent(char *line, struct linedata *data) {
switch (type) { switch (data->type) {
case EMPTY: case HR: case SETEXT1: case SETEXT2: case FENCECODE: case EMPTY: case HR: case SETEXT1: case SETEXT2: case FENCECODE:
return NULL; return NULL;
case PLAIN: case PLAIN:
return line; return line;
case SPACECODE: case SPACECODE:
return line + 4; return line + 4;
case HEADER:
return truncate(line + data->intensity);
} }
return NULL; return NULL;
} }

View File

@@ -63,12 +63,12 @@ int parsetemplate(FILE *infile, FILE *outfile) {
} }
static int parseline(char *line, struct parsestate *currstate, FILE *out) { static int parseline(char *line, struct parsestate *currstate, FILE *out) {
enum linetype type; struct linedata type;
type = identifyline(line, currstate->type); identifyline(line, currstate->type, &type);
if (currstate->type == CODEBLOCK) { if (currstate->type == CODEBLOCK) {
if (type == FENCECODE) { if (type.type == FENCECODE) {
currstate->type = NONE; currstate->type = NONE;
fputs("</code>", out); fputs("</code>", out);
return 0; return 0;
@@ -80,7 +80,7 @@ static int parseline(char *line, struct parsestate *currstate, FILE *out) {
return 0; return 0;
} }
switch (type) { switch (type.type) {
case EMPTY: case EMPTY:
endpara(currstate, out); endpara(currstate, out);
currstate->type = NONE; currstate->type = NONE;
@@ -115,7 +115,7 @@ static int parseline(char *line, struct parsestate *currstate, FILE *out) {
} }
else else
appendcharstring(currstate->para, ' '); appendcharstring(currstate->para, ' ');
appendstrstring(currstate->para, realcontent(line, type)); appendstrstring(currstate->para, realcontent(line, &type));
return 0; return 0;
/* According to the commonmark spec, this markdown: /* According to the commonmark spec, this markdown:
@@ -147,7 +147,15 @@ static int parseline(char *line, struct parsestate *currstate, FILE *out) {
} }
else else
fputs("<br>", out); fputs("<br>", out);
fputs(realcontent(line, type), out); fputs(realcontent(line, &type), out);
break;
case HEADER:
endpara(currstate, out);
fprintf(out, "<h%d>%s</h%d>",
type.intensity,
realcontent(line, &type),
type.intensity);
currstate->type = NONE;
break; break;
} }
return 0; return 0;