From 2dfbf8a9864c56d00e1c514648563b9c56115d50 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 19 Mar 2024 15:09:51 +0100 Subject: [PATCH] feat(ast): add list html export --- example/basic.c | 26 +++++++++++++++++++++++--- example/example.md | 10 ++++++++++ src/ast/html.c | 11 +++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 example/example.md diff --git a/example/basic.c b/example/basic.c index 1bfe08b..e2d6707 100644 --- a/example/basic.c +++ b/example/basic.c @@ -1,16 +1,36 @@ #include #include +#include #include "mrk/lexer.h" #include "mrk/parser.h" #include "mrk/ast.h" -int main() { - const char *buf = "# this is a header\n\nthis is a paragraph with a [link](https://example.com)"; +int main(int argc, char **argv) { + if (argc == 1) { + fprintf(stderr, "No file provided."); + return 1; + } + + struct stat stat_s; + + assert(stat(argv[1], &stat_s) == 0); + + char *buf = malloc(stat_s.st_size); + assert(buf); + + long read = 0; + + FILE *f = fopen(argv[1], "rb"); + assert(f); + + while (read < stat_s.st_size) { + read += fread(buf + read, 1, stat_s.st_size - read, f); + } mrk_lexer *lexer; assert(mrk_lexer_init(&lexer) == mrk_err_ok); - mrk_lexer_open(lexer, buf, 0); + mrk_lexer_open(lexer, buf, stat_s.st_size); mrk_parser *parser; assert(mrk_parser_init(&parser) == mrk_err_ok); diff --git a/example/example.md b/example/example.md new file mode 100644 index 0000000..39c38c5 --- /dev/null +++ b/example/example.md @@ -0,0 +1,10 @@ +## This is header + +This is a paragraph with a [link](https://example.com). + +1. list item 1 + 1. sublist item 1 + this is a continuation block + + another paragraph +2. list item 2 diff --git a/src/ast/html.c b/src/ast/html.c index e3a66f4..095e57b 100644 --- a/src/ast/html.c +++ b/src/ast/html.c @@ -47,6 +47,17 @@ mrk_err __mrk_ast_to_html(mrk_buf *buf, const char *orig, mrk_ast_node *root) { MRK_RES(__mrk_ast_to_html(buf, orig, child)); MRK_RES(mrk_buf_append_n(buf, "

", 4)); break; + case mrk_ast_node_type_list: + MRK_RES(mrk_buf_append_n(buf, child->d.list.ordered ? "
    " : "
      ", 4)) + MRK_RES(__mrk_ast_to_html(buf, orig, child)); + MRK_RES( + mrk_buf_append_n(buf, child->d.list.ordered ? "
" : "", 5)) + break; + case mrk_ast_node_type_list_item: + MRK_RES(mrk_buf_append_n(buf, "
  • ", 4)); + MRK_RES(__mrk_ast_to_html(buf, orig, child)); + MRK_RES(mrk_buf_append_n(buf, "
  • ", 5)); + break; case mrk_ast_node_type_none: break; }