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("", level) + for child in token.children { + print_html(child) + } + fmt.printf("\n", level) + + case TokenType.Paragraph: + fmt.print("

") + 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("\n") + + case TokenType.OrderedList: + fmt.print("
    \n") + for child in token.children { + print_html(child) + } + fmt.print("
\n") + + case TokenType.ListItem: + fmt.print("
  • ") + for child in token.children { + print_html(child) + } + fmt.print("
  • \n") + + case TokenType.Bold: + fmt.print("") + for child in token.children { + print_html(child) + } + fmt.print("") + + case TokenType.Italics: + fmt.print("") + for child in token.children { + print_html(child) + } + fmt.print("") + + case TokenType.BreakLine: + fmt.print("
    \n") + + case TokenType.Text: + if v, ok := token.value.(string); ok { + fmt.print(v) + } + for child in token.children { + print_html(child) + } + } +} diff --git a/src/main.odin b/src/main.odin index 6699d97..861081c 100644 --- a/src/main.odin +++ b/src/main.odin @@ -62,7 +62,7 @@ print_token :: proc(token: ^Token, depth: int = 0) { main :: proc() { lines := read_stdin() parsed := parse(lines) - // print_html(&parsed) + print_html(&parsed) fmt.println("
    ") fmt.println("

    Token Tree

    ") fmt.println("

    ") 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) }