This has to be the worst-looking code yet
This commit is contained in:
+45
-1
@@ -1,6 +1,24 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strings"
|
||||
|
||||
get_text_content :: proc(token: ^Token, b: ^strings.Builder) {
|
||||
if token.type == .Text {
|
||||
if v, ok := token.value.(string); ok {
|
||||
strings.write_string(b, v)
|
||||
}
|
||||
}
|
||||
for child in token.children {
|
||||
get_text_content(child, b)
|
||||
}
|
||||
}
|
||||
|
||||
slugifyifyifyify :: proc(text: string) -> string {
|
||||
lower := strings.to_lower(text, context.temp_allocator)
|
||||
res, _ := strings.replace_all(lower, " ", "-", context.temp_allocator)
|
||||
return res
|
||||
}
|
||||
|
||||
print_html :: proc(token: ^Token) {
|
||||
switch token.type {
|
||||
@@ -19,7 +37,14 @@ print_html :: proc(token: ^Token) {
|
||||
if v, ok := token.value.(int); ok {
|
||||
level = v
|
||||
}
|
||||
fmt.printf("<h%d>", level)
|
||||
|
||||
b := strings.builder_make(context.temp_allocator)
|
||||
for child in token.children {
|
||||
get_text_content(child, &b)
|
||||
}
|
||||
slug := slugifyifyifyify(strings.to_string(b))
|
||||
|
||||
fmt.printf("<h%d id=\"%s\">", level, slug)
|
||||
for child in token.children {
|
||||
print_html(child)
|
||||
}
|
||||
@@ -234,6 +259,25 @@ print_html :: proc(token: ^Token) {
|
||||
}
|
||||
fmt.print("</sub>")
|
||||
|
||||
case TokenType.HeaderLink:
|
||||
if target, ok := token.value.(string); ok {
|
||||
slug := slugifyifyifyify(target)
|
||||
fmt.printf("<a href=\"#%s\">", slug)
|
||||
for child in token.children {
|
||||
print_html(child)
|
||||
}
|
||||
fmt.print("</a>")
|
||||
}
|
||||
|
||||
case TokenType.Link:
|
||||
if url, ok := token.value.(string); ok {
|
||||
fmt.printf("<a href=\"%s\">", url)
|
||||
for child in token.children {
|
||||
print_html(child)
|
||||
}
|
||||
fmt.print("</a>")
|
||||
}
|
||||
|
||||
case TokenType.BreakLine:
|
||||
fmt.print("<br>\n")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user