diff --git a/src/include/mdutil.h b/src/include/mdutil.h index 7a93827..96efd95 100644 --- a/src/include/mdutil.h +++ b/src/include/mdutil.h @@ -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 diff --git a/src/mdutil.c b/src/mdutil.c index 3e27666..98a7731 100644 --- a/src/mdutil.c +++ b/src/mdutil.c @@ -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; } diff --git a/src/template.c b/src/template.c index d18f047..5ca2717 100644 --- a/src/template.c +++ b/src/template.c @@ -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("", 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("
", out); - fputs(realcontent(line, type), out); + fputs(realcontent(line, &type), out); + break; + case HEADER: + endpara(currstate, out); + fprintf(out, "%s", + type.intensity, + realcontent(line, &type), + type.intensity); + currstate->type = NONE; break; } return 0;