changed whole newline handling
This commit is contained in:
parent
9f24a7669b
commit
e1fc11840a
1 changed files with 75 additions and 66 deletions
139
cmarkdown.c
139
cmarkdown.c
|
@ -15,7 +15,7 @@
|
||||||
{ b = realloc(b,((i + 1)+ BUFFERSIZE) * sizeof(b)); if(!b) eprint("Malloc failed."); }; b[i+1] = '\0'; b[i]
|
{ b = realloc(b,((i + 1)+ BUFFERSIZE) * sizeof(b)); if(!b) eprint("Malloc failed."); }; b[i+1] = '\0'; b[i]
|
||||||
|
|
||||||
|
|
||||||
typedef unsigned int (*Parser)(const char *, const char *);
|
typedef unsigned int (*Parser)(const char *, const char *, int);
|
||||||
struct Tag {
|
struct Tag {
|
||||||
char *search;
|
char *search;
|
||||||
int process;
|
int process;
|
||||||
|
@ -25,29 +25,32 @@ struct Tag {
|
||||||
|
|
||||||
void eprint(const char *format, ...); /* Prints error and exits */
|
void eprint(const char *format, ...); /* Prints error and exits */
|
||||||
void hprint(const char *begin, const char *end); /* escapes HTML and prints it to stdout*/
|
void hprint(const char *begin, const char *end); /* escapes HTML and prints it to stdout*/
|
||||||
unsigned int doamp(const char *begin, const char *end); /* Parser for & */
|
unsigned int doamp(const char *begin, const char *end, int first);
|
||||||
unsigned int dohtml(const char *begin, const char *end);
|
/* Parser for & */
|
||||||
|
unsigned int dohtml(const char *begin, const char *end, int first);
|
||||||
/* Parser for html */
|
/* Parser for html */
|
||||||
unsigned int dolineprefix(const char *begin, const char *end);
|
unsigned int dolineprefix(const char *begin, const char *end, int first);
|
||||||
/* Parser for line prefix tags */
|
/* Parser for line prefix tags */
|
||||||
unsigned int dolink(const char *begin, const char *end);
|
unsigned int dolink(const char *begin, const char *end, int first);
|
||||||
/* Parser for links and images */
|
/* Parser for links and images */
|
||||||
unsigned int dolist(const char *begin, const char *end);
|
unsigned int dolist(const char *begin, const char *end, int first);
|
||||||
/* Parser for lists */
|
/* Parser for lists */
|
||||||
unsigned int doparagraph(const char *begin, const char *end);
|
unsigned int doparagraph(const char *begin, const char *end, int first);
|
||||||
/* Parser for paragraphs */
|
/* Parser for paragraphs */
|
||||||
unsigned int doreplace(const char *begin, const char *end);
|
unsigned int doreplace(const char *begin, const char *end, int first);
|
||||||
/* Parser for simple replaces */
|
/* Parser for simple replaces */
|
||||||
unsigned int doshortlink(const char *begin, const char *end);
|
unsigned int doshortlink(const char *begin, const char *end, int first);
|
||||||
/* Parser for links and images */
|
/* Parser for links and images */
|
||||||
unsigned int dosurround(const char *begin, const char *end);
|
unsigned int dosurround(const char *begin, const char *end, int first);
|
||||||
/* Parser for surrounding tags */
|
/* Parser for surrounding tags */
|
||||||
unsigned int dounderline(const char *begin, const char *end);
|
unsigned int dounderline(const char *begin, const char *end, int first);
|
||||||
/* Parser for underline tags */
|
/* Parser for underline tags */
|
||||||
void process(const char *begin, const char *end); /* Processes range between begin and end. */
|
void process(const char *begin, const char *end, int isblock);
|
||||||
|
/* Processes range between begin and end. */
|
||||||
|
|
||||||
Parser parsers[] = { dounderline, dohtml, dolineprefix, dolist, doparagraph,
|
Parser parsers[] = { dounderline, dohtml, dolineprefix, dolist, doparagraph,
|
||||||
dosurround, dolink, doshortlink, doamp, doreplace }; /* list of parsers */
|
dosurround, dolink, doshortlink, doamp, doreplace }; /* list of parsers */
|
||||||
|
//Parser parsers[] = { doparagraph };
|
||||||
FILE *source;
|
FILE *source;
|
||||||
unsigned int bsize = 0, nohtml = 0;
|
unsigned int bsize = 0, nohtml = 0;
|
||||||
struct Tag lineprefix[] = {
|
struct Tag lineprefix[] = {
|
||||||
|
@ -114,7 +117,7 @@ hprint(const char *begin, const char *end) {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
doamp(const char *begin, const char *end) {
|
doamp(const char *begin, const char *end, int first) {
|
||||||
const char *p;
|
const char *p;
|
||||||
|
|
||||||
if(*begin != '&')
|
if(*begin != '&')
|
||||||
|
@ -128,7 +131,7 @@ doamp(const char *begin, const char *end) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
unsigned int
|
unsigned int
|
||||||
dohtml(const char *begin, const char *end) {
|
dohtml(const char *begin, const char *end, int first) {
|
||||||
const char *p, *tag, *tagend;
|
const char *p, *tag, *tagend;
|
||||||
if(nohtml || *begin != '\n' || !*begin)
|
if(nohtml || *begin != '\n' || !*begin)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -153,15 +156,15 @@ dohtml(const char *begin, const char *end) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
unsigned int
|
unsigned int
|
||||||
dolineprefix(const char *begin, const char *end) {
|
dolineprefix(const char *begin, const char *end, int first) {
|
||||||
unsigned int i, j, l;
|
unsigned int i, j, l;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
const char *p;
|
const char *p;
|
||||||
|
|
||||||
if(*begin != '\n' || !*begin)
|
if(*begin != '\n' && !first)
|
||||||
return 0;
|
return 0;
|
||||||
p = begin;
|
p = begin;
|
||||||
if(p[1] == '\n')
|
if(!first)
|
||||||
p++;
|
p++;
|
||||||
for(i = 0; i < LENGTH(lineprefix); i++) {
|
for(i = 0; i < LENGTH(lineprefix); i++) {
|
||||||
l = strlen(lineprefix[i].search);
|
l = strlen(lineprefix[i].search);
|
||||||
|
@ -183,7 +186,7 @@ dolineprefix(const char *begin, const char *end) {
|
||||||
}
|
}
|
||||||
ADDC(buffer,j) = '\0';
|
ADDC(buffer,j) = '\0';
|
||||||
if(lineprefix[i].process)
|
if(lineprefix[i].process)
|
||||||
process(buffer,buffer+strlen(buffer));
|
process(buffer,buffer+strlen(buffer),1);
|
||||||
else
|
else
|
||||||
hprint(buffer,buffer+strlen(buffer));
|
hprint(buffer,buffer+strlen(buffer));
|
||||||
printf("</%s>\n",lineprefix[i].tag);
|
printf("</%s>\n",lineprefix[i].tag);
|
||||||
|
@ -194,7 +197,7 @@ dolineprefix(const char *begin, const char *end) {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
dolink(const char *begin, const char *end) {
|
dolink(const char *begin, const char *end, int first) {
|
||||||
int img;
|
int img;
|
||||||
const char *desc, *link, *p, *q, *r, *descend, *linkend;
|
const char *desc, *link, *p, *q, *r, *descend, *linkend;
|
||||||
|
|
||||||
|
@ -217,50 +220,49 @@ dolink(const char *begin, const char *end) {
|
||||||
linkend = p;
|
linkend = p;
|
||||||
if(img) {
|
if(img) {
|
||||||
fputs("<img src=\"",stdout);
|
fputs("<img src=\"",stdout);
|
||||||
process(link,linkend);
|
hprint(link,linkend);
|
||||||
fputs("\" alt=\"",stdout);
|
fputs("\" alt=\"",stdout);
|
||||||
process(desc,descend);
|
hprint(desc,descend);
|
||||||
fputs("\" />",stdout);
|
fputs("\" />",stdout);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fputs("<a href=\"",stdout);
|
fputs("<a href=\"",stdout);
|
||||||
process(link,linkend);
|
hprint(link,linkend);
|
||||||
fputs("\">",stdout);
|
fputs("\">",stdout);
|
||||||
process(desc,descend);
|
process(desc,descend,0);
|
||||||
fputs("</a>",stdout);
|
fputs("</a>",stdout);
|
||||||
}
|
}
|
||||||
return p + 1 - begin;
|
return p + 1 - begin;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
dolist(const char *begin, const char *end) {
|
dolist(const char *begin, const char *end, int first) {
|
||||||
unsigned int i,j,k,indent,run,ul;
|
unsigned int i,j,indent,run,ul, isblock;
|
||||||
const char *p, *q;
|
const char *p, *q;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
|
|
||||||
if(*begin != '\n')
|
if(*begin != '\n' && !first)
|
||||||
return 0;
|
return 0;
|
||||||
p = begin;
|
p = begin;
|
||||||
if(p[1] == '\n')
|
if(!first)
|
||||||
p++;
|
p++;
|
||||||
q = p;
|
q = p;
|
||||||
if((p[1] == '-' || p[1] == '*' || p[1] == '+') && p[2] == ' ') {
|
if((*p == '-' || *p == '*' || *p == '+') && p[1] == ' ') {
|
||||||
ul = 1;
|
ul = 1;
|
||||||
p++;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ul = 0;
|
ul = 0;
|
||||||
for(p++; *p && p != end && *p >= '0' && *p <= '9';p++);
|
for(; *p && p != end && *p >= '0' && *p <= '9';p++);
|
||||||
if(!*p || p[0] != '.' || p[1] != ' ')
|
if(!*p || p[0] != '.' || p[1] != ' ')
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for(p++; *p && p != end && *p == ' '; p++);
|
for(p++; *p && p != end && *p == ' '; p++);
|
||||||
indent = p - q - 1;
|
indent = p - q;
|
||||||
|
|
||||||
if(!(buffer = malloc(BUFFERSIZE)))
|
if(!(buffer = malloc(BUFFERSIZE)))
|
||||||
eprint("Malloc failed.");
|
eprint("Malloc failed.");
|
||||||
|
|
||||||
puts(ul ? "\n<ul>" : "\n<ol>");
|
fputs(ul ? "<ul>\n" : "<ol>\n",stdout);
|
||||||
run = 1;
|
run = 1;
|
||||||
for(i = 0; *p && p < end && run; p++) {
|
for(i = 0; *p && p < end && run; p++) {
|
||||||
buffer[0] = '\0';
|
buffer[0] = '\0';
|
||||||
|
@ -298,36 +300,39 @@ dolist(const char *begin, const char *end) {
|
||||||
ADDC(buffer,i) = *p;
|
ADDC(buffer,i) = *p;
|
||||||
}
|
}
|
||||||
fputs("<li>",stdout);
|
fputs("<li>",stdout);
|
||||||
process(buffer,buffer+i);
|
process(buffer,buffer+i,0); //TODO
|
||||||
fputs("</li>\n",stdout);
|
fputs("</li>\n",stdout);
|
||||||
}
|
}
|
||||||
puts(ul ? "</ul>" : "</ol>");
|
fputs(ul ? "</ul>\n" : "</ol>\n",stdout);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
p--;
|
p--;
|
||||||
while(*(--p) == '\n');
|
while(*(--p) == '\n');
|
||||||
return p + 1 - begin;
|
return p - begin + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
doparagraph(const char *begin, const char *end) {
|
doparagraph(const char *begin, const char *end, int first) {
|
||||||
const char *p;
|
const char *p, *q;
|
||||||
|
|
||||||
if(strncmp(begin,"\n\n",2))
|
if(first)
|
||||||
|
p = begin;
|
||||||
|
else if(*begin == '\n')
|
||||||
|
p = begin + 1;
|
||||||
|
else
|
||||||
return 0;
|
return 0;
|
||||||
if(!(p = strstr(begin + 2,"\n\n")))
|
q = strchr(p, '\n');
|
||||||
p = end - 1;
|
if(!q || q > end)
|
||||||
if(p > end)
|
q = end;
|
||||||
|
if(q - begin <= 1)
|
||||||
return 0;
|
return 0;
|
||||||
if(p - begin - 2 <= 0)
|
fputs("<p>",stdout);
|
||||||
return 0;
|
process(p,q,0);
|
||||||
fputs("\n<p>",stdout);
|
|
||||||
process(begin+2,p);
|
|
||||||
fputs("</p>\n",stdout);
|
fputs("</p>\n",stdout);
|
||||||
return p - begin;
|
return q - begin;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
doreplace(const char *begin, const char *end) {
|
doreplace(const char *begin, const char *end, int first) {
|
||||||
unsigned int i, l;
|
unsigned int i, l;
|
||||||
|
|
||||||
for(i = 0; i < LENGTH(insert); i++)
|
for(i = 0; i < LENGTH(insert); i++)
|
||||||
|
@ -346,7 +351,7 @@ doreplace(const char *begin, const char *end) {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
doshortlink(const char *begin, const char *end) {
|
doshortlink(const char *begin, const char *end, int first) {
|
||||||
const char *p, *c;
|
const char *p, *c;
|
||||||
int ismail = 0;
|
int ismail = 0;
|
||||||
|
|
||||||
|
@ -390,7 +395,7 @@ doshortlink(const char *begin, const char *end) {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
dosurround(const char *begin, const char *end) {
|
dosurround(const char *begin, const char *end, int first) {
|
||||||
unsigned int i,l;
|
unsigned int i,l;
|
||||||
const char *p,*ps;
|
const char *p,*ps;
|
||||||
|
|
||||||
|
@ -408,7 +413,7 @@ dosurround(const char *begin, const char *end) {
|
||||||
continue;
|
continue;
|
||||||
printf("<%s>",surround[i].tag);
|
printf("<%s>",surround[i].tag);
|
||||||
if(surround[i].process)
|
if(surround[i].process)
|
||||||
process(begin + strlen(surround[i].search), p);
|
process(begin + strlen(surround[i].search), p,0);
|
||||||
else
|
else
|
||||||
hprint(begin + strlen(surround[i].search), p);
|
hprint(begin + strlen(surround[i].search), p);
|
||||||
printf("</%s>",surround[i].tag);
|
printf("</%s>",surround[i].tag);
|
||||||
|
@ -418,31 +423,30 @@ dosurround(const char *begin, const char *end) {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
dounderline(const char *begin, const char *end) {
|
dounderline(const char *begin, const char *end, int first) {
|
||||||
unsigned int i, j, l, nl;
|
unsigned int i, j, l, nl;
|
||||||
const char *p;
|
const char *p;
|
||||||
|
|
||||||
if(*begin != '\n' && *begin != '\0')
|
if(*begin != '\n' && !first)
|
||||||
return 0;
|
return 0;
|
||||||
nl = 0;
|
|
||||||
p = begin;
|
p = begin;
|
||||||
if(p[1] == '\n') {
|
nl = 0;
|
||||||
|
if(!first) {
|
||||||
nl = 1;
|
nl = 1;
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
for(p++,l = 0; p[l] != '\n' && p[l] && p+l != end; l++);
|
for(l = 0; p[l] != '\n' && p[l] && p+l != end; l++);
|
||||||
p += l + 1;
|
p += l + 1;
|
||||||
if(l == 0)
|
if(l == 0)
|
||||||
return 0;
|
return 0;
|
||||||
for(i = 0; i < LENGTH(underline); i++) {
|
for(i = 0; i < LENGTH(underline); i++) {
|
||||||
for(j = 0; p[j] != '\n' && p[j] == underline[i].search[0] && p+j != end; j++);
|
for(j = 0; p[j] != '\n' && p[j] == underline[i].search[0] && p+j != end; j++);
|
||||||
if(j >= l) {
|
if(j >= l) {
|
||||||
putchar('\n');
|
|
||||||
printf("<%s>",underline[i].tag);
|
printf("<%s>",underline[i].tag);
|
||||||
if(underline[i].process)
|
if(underline[i].process)
|
||||||
process(begin + 1 + nl, begin + l + 1 + nl);
|
process(begin + nl, begin + l + nl, 0);
|
||||||
else
|
else
|
||||||
hprint(begin + 1 + nl, begin + l + 1 + nl);
|
hprint(begin + nl, begin + l + nl);
|
||||||
printf("</%s>\n",underline[i].tag);
|
printf("</%s>\n",underline[i].tag);
|
||||||
return j + p - begin;
|
return j + p - begin;
|
||||||
}
|
}
|
||||||
|
@ -451,17 +455,21 @@ dounderline(const char *begin, const char *end) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
process(const char *begin, const char *end) {
|
process(const char *begin, const char *end, int isblock) {
|
||||||
const char *p, *q;
|
const char *p, *q;
|
||||||
int affected;
|
int affected;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for(p = begin; *p && p < end;) {
|
for(p = begin; *p && *p == '\n' && p < end;p++);
|
||||||
|
for(; *p && p < end;) {
|
||||||
affected = 0;
|
affected = 0;
|
||||||
for(i = 0; i < LENGTH(parsers) && affected == 0; i++)
|
for(i = 0; i < LENGTH(parsers) && affected == 0; i++)
|
||||||
affected = parsers[i](p, end);
|
affected = parsers[i](p, end, isblock);
|
||||||
if(affected)
|
if(affected) {
|
||||||
p += affected;
|
p += affected;
|
||||||
|
while(*p+1 == '\n')
|
||||||
|
p++;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
for(q = p; *q == '\n' && q != end; q++);
|
for(q = p; *q == '\n' && q != end; q++);
|
||||||
if(q != end && *q) {
|
if(q != end && *q) {
|
||||||
|
@ -472,6 +480,7 @@ process(const char *begin, const char *end) {
|
||||||
}
|
}
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
|
isblock = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -492,8 +501,8 @@ main(int argc, char *argv[]) {
|
||||||
if(!(buffer = malloc(BUFFERSIZE)))
|
if(!(buffer = malloc(BUFFERSIZE)))
|
||||||
eprint("Malloc failed.");
|
eprint("Malloc failed.");
|
||||||
bsize = BUFFERSIZE;
|
bsize = BUFFERSIZE;
|
||||||
/* needed to properly process first line */
|
buffer[0] = '\0';
|
||||||
strcpy(buffer,"\n");
|
|
||||||
|
|
||||||
p = buffer+strlen(buffer);
|
p = buffer+strlen(buffer);
|
||||||
while(s = fread(p, sizeof(char),BUFFERSIZE, source)) {
|
while(s = fread(p, sizeof(char),BUFFERSIZE, source)) {
|
||||||
|
@ -505,7 +514,7 @@ main(int argc, char *argv[]) {
|
||||||
eprint("Malloc failed.");
|
eprint("Malloc failed.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
process(buffer,buffer+strlen(buffer));
|
process(buffer,buffer+strlen(buffer),1);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
free(buffer);
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue