diff --git a/src/html.odin b/src/html.odin
new file mode 100644
index 0000000..f348f97
--- /dev/null
+++ b/src/html.odin
@@ -0,0 +1,83 @@
+package main
+
+import "core:fmt"
+
+print_html :: proc(token: ^Token) {
+ switch token.type {
+ case TokenType.Root:
+ for child in token.children {
+ print_html(child)
+ }
+
+ case TokenType.Header:
+ level := 1
+ if v, ok := token.value.(int); ok {
+ level = v
+ }
+ fmt.printf("
") + for child in token.children { + print_html(child) + } + fmt.print("
\n") + + case TokenType.Blockquote: + fmt.print("\n") + for child in token.children { + print_html(child) + } + fmt.print("\n") + + case TokenType.UnorderedList: + fmt.print("
") diff --git a/src/parser.odin b/src/parser.odin index f6f00a2..d65d03f 100644 --- a/src/parser.odin +++ b/src/parser.odin @@ -122,6 +122,27 @@ pass_through_children :: proc(token: ^Token) { } } +add_breaklines :: proc(token: ^Token) { + if token.children == nil { + return + } + + new_children: [dynamic]^Token + for i := 0; i < len(token.children); i += 1 { + child := token.children[i] + add_breaklines(child) + append(&new_children, child) + if child.type == TokenType.Text && + i + 1 < len(token.children) && + token.children[i + 1].type == TokenType.Text { + br := new(Token) + br^ = Token{TokenType.BreakLine, nil, nil, token} + append(&new_children, br) + } + } + token.children = new_children +} + parse :: proc(lines: []string, allocator := context.allocator) -> Token { root := Token{TokenType.Root, nil, nil, nil} current_token := &root @@ -201,6 +222,12 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token { // SECOND PASS + for token in root.children { + add_breaklines(token) + } + + // THIRD PASS + for token in root.children { pass_through_children(token) }