diff --git a/objs.mk b/objs.mk index 548ca21..ae2fe44 100644 --- a/objs.mk +++ b/objs.mk @@ -26,6 +26,9 @@ src/external_xemil_src_xpointer.o: external/xemil/src/xpointer.c OBJS += src/src_action_help.o src/src_action_help.o: src/action/help.c $(CC) $(CFLAGS) -c -o $@ src/action/help.c +OBJS += src/src_action_markdown.o +src/src_action_markdown.o: src/action/markdown.c + $(CC) $(CFLAGS) -c -o $@ src/action/markdown.c OBJS += src/src_action_seed.o src/src_action_seed.o: src/action/seed.c $(CC) $(CFLAGS) -c -o $@ src/action/seed.c diff --git a/src/action/markdown.c b/src/action/markdown.c new file mode 100644 index 0000000..57f1b15 --- /dev/null +++ b/src/action/markdown.c @@ -0,0 +1,463 @@ +#include + +static FILE* out; +static int indent = 0; +static int mode = 0; +static char* title = NULL; +static int in_title = 0; +static int in_section = 0; +static int in_pre = 0; +static char** mapping; +static int mapping_len; + +static void print_indent(void) { + int i; + + for(i = 0; i < indent; i++) fprintf(out, "\t"); +} + +static char* encode(const char* str, int len) { + int actlen = (len == -1 ? strlen(str) : len); + char* r = malloc(actlen * 5 + 1); + int n = 0; + int i; + + for(i = 0; i < actlen; i++) { + if(str[i] == '<') { + r[n++] = '&'; + r[n++] = 'l'; + r[n++] = 't'; + r[n++] = ';'; + } else if(str[i] == '>') { + r[n++] = '&'; + r[n++] = 'g'; + r[n++] = 't'; + r[n++] = ';'; + } else if(str[i] == '&') { + r[n++] = '&'; + r[n++] = 'a'; + r[n++] = 'm'; + r[n++] = 'p'; + r[n++] = ';'; + } else { + r[n++] = str[i]; + } + } + r[n] = 0; + + return r; +} + +static int enter_block(MD_BLOCKTYPE type, void* detail, void* user) { + if(mode == 0 && type == MD_BLOCK_H && ((MD_BLOCK_H_DETAIL*)detail)->level == 1 && title == NULL) { + in_title = 1; + } + + if(!mode) return 0; + + if(type == MD_BLOCK_H) { + MD_BLOCK_H_DETAIL* det = detail; + + if(det->level <= 2) { + if(title != NULL) free(title); + title = NULL; + + in_title = 1; + } else { + print_indent(); + fprintf(out, "\n", det->level); + indent++; + } + } else if(type == MD_BLOCK_P) { + print_indent(); + fprintf(out, "

\n"); + indent++; + } else if(type == MD_BLOCK_UL) { + print_indent(); + fprintf(out, "