Support for Backslashing. Correcting lineprefixes. dolist supports \t; correction in doparagraph; correction in dosurround
This commit is contained in:
parent
e1fc11840a
commit
604eff6c9e
1 changed files with 41 additions and 21 deletions
62
cmarkdown.c
62
cmarkdown.c
|
@ -50,13 +50,12 @@ void process(const char *begin, const char *end, int isblock);
|
||||||
|
|
||||||
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[] = {
|
||||||
{ " ", 0, "pre" },
|
{ " ", 0, "pre" },
|
||||||
{ "\t", 0, "pre" },
|
{ "\t", 0, "pre" },
|
||||||
{ "> ", 1, "blockquote" },
|
{ "> ", 2, "blockquote" },
|
||||||
{ "###### ", 1, "h6" },
|
{ "###### ", 1, "h6" },
|
||||||
{ "##### ", 1, "h5" },
|
{ "##### ", 1, "h5" },
|
||||||
{ "#### ", 1, "h4" },
|
{ "#### ", 1, "h4" },
|
||||||
|
@ -88,6 +87,21 @@ char * replace[][2] = {
|
||||||
{ " #\n", "\n" },
|
{ " #\n", "\n" },
|
||||||
{ " >", ">" },
|
{ " >", ">" },
|
||||||
{ "< ", "<" },
|
{ "< ", "<" },
|
||||||
|
{ "\\\\", "\\" },
|
||||||
|
{ "\\`", "`" },
|
||||||
|
{ "\\*", "*" },
|
||||||
|
{ "\\_", "_" },
|
||||||
|
{ "\\{", "{" },
|
||||||
|
{ "\\}", "}" },
|
||||||
|
{ "\\[", "[" },
|
||||||
|
{ "\\]", "]" },
|
||||||
|
{ "\\(", "(" },
|
||||||
|
{ "\\)", ")" },
|
||||||
|
{ "\\#", "#" },
|
||||||
|
{ "\\+", "+" },
|
||||||
|
{ "\\-", "-" },
|
||||||
|
{ "\\.", "." },
|
||||||
|
{ "\\!", "!" },
|
||||||
};
|
};
|
||||||
char * insert[][2] = {
|
char * insert[][2] = {
|
||||||
{ " \n", "<br />" },
|
{ " \n", "<br />" },
|
||||||
|
@ -163,20 +177,23 @@ dolineprefix(const char *begin, const char *end, int first) {
|
||||||
|
|
||||||
if(*begin != '\n' && !first)
|
if(*begin != '\n' && !first)
|
||||||
return 0;
|
return 0;
|
||||||
p = begin;
|
if(first)
|
||||||
if(!first)
|
p = begin;
|
||||||
p++;
|
else
|
||||||
|
p = begin + 1;
|
||||||
for(i = 0; i < LENGTH(lineprefix); i++) {
|
for(i = 0; i < LENGTH(lineprefix); i++) {
|
||||||
l = strlen(lineprefix[i].search);
|
l = strlen(lineprefix[i].search);
|
||||||
if(end - p+1 < l)
|
if(end - p < l)
|
||||||
continue;
|
continue;
|
||||||
if(strncmp(lineprefix[i].search,p+1,l))
|
if(!first && *begin != '\n')
|
||||||
|
continue;
|
||||||
|
if(strncmp(lineprefix[i].search,p,l))
|
||||||
continue;
|
continue;
|
||||||
if(!(buffer = malloc(BUFFERSIZE)))
|
if(!(buffer = malloc(BUFFERSIZE)))
|
||||||
eprint("Malloc failed.");
|
eprint("Malloc failed.");
|
||||||
buffer[0] = '\0';
|
buffer[0] = '\0';
|
||||||
printf("\n<%s>",lineprefix[i].tag);
|
printf("<%s>\n",lineprefix[i].tag);
|
||||||
for(j = 0; p != end; p++, j++) {
|
for(j = 0, p += l; p != end; p++, j++) {
|
||||||
ADDC(buffer,j) = *p;
|
ADDC(buffer,j) = *p;
|
||||||
if(*p == '\n') {
|
if(*p == '\n') {
|
||||||
if(strncmp(lineprefix[i].search,p+1,l) != 0)
|
if(strncmp(lineprefix[i].search,p+1,l) != 0)
|
||||||
|
@ -186,7 +203,7 @@ dolineprefix(const char *begin, const char *end, int first) {
|
||||||
}
|
}
|
||||||
ADDC(buffer,j) = '\0';
|
ADDC(buffer,j) = '\0';
|
||||||
if(lineprefix[i].process)
|
if(lineprefix[i].process)
|
||||||
process(buffer,buffer+strlen(buffer),1);
|
process(buffer,buffer+strlen(buffer), lineprefix[i].process >= 2);
|
||||||
else
|
else
|
||||||
hprint(buffer,buffer+strlen(buffer));
|
hprint(buffer,buffer+strlen(buffer));
|
||||||
printf("</%s>\n",lineprefix[i].tag);
|
printf("</%s>\n",lineprefix[i].tag);
|
||||||
|
@ -247,16 +264,16 @@ dolist(const char *begin, const char *end, int first) {
|
||||||
if(!first)
|
if(!first)
|
||||||
p++;
|
p++;
|
||||||
q = p;
|
q = p;
|
||||||
if((*p == '-' || *p == '*' || *p == '+') && p[1] == ' ') {
|
if((*p == '-' || *p == '*' || *p == '+') && (p[1] == ' ' || p[1] == '\t')) {
|
||||||
ul = 1;
|
ul = 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ul = 0;
|
ul = 0;
|
||||||
for(; *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] != ' ' && p[1] != '\t'))
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for(p++; *p && p != end && *p == ' '; p++);
|
for(p++; *p && p != end && (*p == ' ' || *p == '\t'); p++);
|
||||||
indent = p - q;
|
indent = p - q;
|
||||||
|
|
||||||
if(!(buffer = malloc(BUFFERSIZE)))
|
if(!(buffer = malloc(BUFFERSIZE)))
|
||||||
|
@ -285,13 +302,13 @@ dolist(const char *begin, const char *end, int first) {
|
||||||
else
|
else
|
||||||
j = 0;
|
j = 0;
|
||||||
}
|
}
|
||||||
for(;q[j] == ' ' && j < indent; j++);
|
for(;(q[j] == ' ' || *q == '\t') && j < indent; j++);
|
||||||
if(j == indent) {
|
if(j == indent) {
|
||||||
ADDC(buffer,i) = '\n';
|
ADDC(buffer,i) = '\n';
|
||||||
i++;
|
i++;
|
||||||
p += indent;
|
p += indent;
|
||||||
run = 1;
|
run = 1;
|
||||||
if(q[0] == ' ')
|
if(*q == ' ' || *q == '\t')
|
||||||
p++;
|
p++;
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
|
@ -316,11 +333,11 @@ doparagraph(const char *begin, const char *end, int first) {
|
||||||
|
|
||||||
if(first)
|
if(first)
|
||||||
p = begin;
|
p = begin;
|
||||||
else if(*begin == '\n')
|
else if(*begin == '\n' && begin[1] == '\n')
|
||||||
p = begin + 1;
|
p = begin + 2;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
q = strchr(p, '\n');
|
q = strstr(p, "\n\n");
|
||||||
if(!q || q > end)
|
if(!q || q > end)
|
||||||
q = end;
|
q = end;
|
||||||
if(q - begin <= 1)
|
if(q - begin <= 1)
|
||||||
|
@ -397,7 +414,7 @@ doshortlink(const char *begin, const char *end, int first) {
|
||||||
unsigned int
|
unsigned int
|
||||||
dosurround(const char *begin, const char *end, int first) {
|
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, *q, *qend;
|
||||||
|
|
||||||
for(i = 0; i < LENGTH(surround); i++) {
|
for(i = 0; i < LENGTH(surround); i++) {
|
||||||
l = strlen(surround[i].search);
|
l = strlen(surround[i].search);
|
||||||
|
@ -412,10 +429,12 @@ dosurround(const char *begin, const char *end, int first) {
|
||||||
if(!p || p > end)
|
if(!p || p > end)
|
||||||
continue;
|
continue;
|
||||||
printf("<%s>",surround[i].tag);
|
printf("<%s>",surround[i].tag);
|
||||||
|
for(q = begin + strlen(surround[i].search); *q == ' '; q++);
|
||||||
|
for(qend = p-1; *qend == ' '; qend--);
|
||||||
if(surround[i].process)
|
if(surround[i].process)
|
||||||
process(begin + strlen(surround[i].search), p,0);
|
process(q, qend+1,0);
|
||||||
else
|
else
|
||||||
hprint(begin + strlen(surround[i].search), p);
|
hprint(q, qend+1);
|
||||||
printf("</%s>",surround[i].tag);
|
printf("</%s>",surround[i].tag);
|
||||||
return p - begin + l;
|
return p - begin + l;
|
||||||
}
|
}
|
||||||
|
@ -518,3 +537,4 @@ main(int argc, char *argv[]) {
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
free(buffer);
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue