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,
SETEXT1,
/* === */
SETEXT2
SETEXT2,
/* --- */
HEADER
};
struct linedata {
enum linetype type;
int intensity;
};
enum nodetype {
@@ -40,8 +46,8 @@ enum nodetype {
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. */
char *realcontent(char *line, enum linetype type);
char *realcontent(char *line, struct linedata *data);
#endif

View File

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

View File

@@ -63,12 +63,12 @@ int parsetemplate(FILE *infile, FILE *outfile) {
}
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 (type == FENCECODE) {
if (type.type == FENCECODE) {
currstate->type = NONE;
fputs("</code>", out);
return 0;
@@ -80,7 +80,7 @@ static int parseline(char *line, struct parsestate *currstate, FILE *out) {
return 0;
}
switch (type) {
switch (type.type) {
case EMPTY:
endpara(currstate, out);
currstate->type = NONE;
@@ -115,7 +115,7 @@ static int parseline(char *line, struct parsestate *currstate, FILE *out) {
}
else
appendcharstring(currstate->para, ' ');
appendstrstring(currstate->para, realcontent(line, type));
appendstrstring(currstate->para, realcontent(line, &type));
return 0;
/* According to the commonmark spec, this markdown:
@@ -147,7 +147,15 @@ static int parseline(char *line, struct parsestate *currstate, FILE *out) {
}
else
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;
}
return 0;