feat(ast): add list html export

main
Jef Roosens 2024-03-19 15:09:51 +01:00
parent e0852de230
commit 2dfbf8a986
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 44 additions and 3 deletions

View File

@ -1,16 +1,36 @@
#include <assert.h>
#include <stdio.h>
#include <sys/stat.h>
#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);

10
example/example.md 100644
View File

@ -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

View File

@ -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, "</p>", 4));
break;
case mrk_ast_node_type_list:
MRK_RES(mrk_buf_append_n(buf, child->d.list.ordered ? "<ol>" : "<ul>", 4))
MRK_RES(__mrk_ast_to_html(buf, orig, child));
MRK_RES(
mrk_buf_append_n(buf, child->d.list.ordered ? "</ol>" : "</ul>", 5))
break;
case mrk_ast_node_type_list_item:
MRK_RES(mrk_buf_append_n(buf, "<li>", 4));
MRK_RES(__mrk_ast_to_html(buf, orig, child));
MRK_RES(mrk_buf_append_n(buf, "</li>", 5));
break;
case mrk_ast_node_type_none:
break;
}