Fix footnotes. Again.
This commit is contained in:
+4
-4
@@ -28,12 +28,12 @@ The **VARIABLE** is $(variable)
|
|||||||
- List item 1
|
- List item 1
|
||||||
. List item 2
|
. List item 2
|
||||||
|
|
||||||
1. Ordered list item [#1]
|
1. Ordered list item [&1]
|
||||||
|
|
||||||
> This is a sample blockquote
|
> This is a sample blockquote
|
||||||
> Bon apetit!
|
> Bon apetit!
|
||||||
|
|
||||||
Text under a blockquote [#named]
|
Text under a blockquote [&named]
|
||||||
|
|
||||||
**This *is* bold *and* italics *mixed** TOGETHER*
|
**This *is* bold *and* italics *mixed** TOGETHER*
|
||||||
|
|
||||||
@@ -56,8 +56,8 @@ Here is some __underlined__ text
|
|||||||
|
|
||||||
[Go to Top](#Heading)
|
[Go to Top](#Heading)
|
||||||
|
|
||||||
[&1] Ordered list item 2 not included
|
[&&1] Ordered list item 2 not included
|
||||||
[&named] This is the definition for a named footnote
|
[&&named] This is the definition for a named footnote
|
||||||
|
|
||||||
!! Variable demo
|
!! Variable demo
|
||||||
|
|
||||||
|
|||||||
+26
-3
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
import "core:unicode"
|
||||||
|
|
||||||
escape_html :: proc(s: string, allocator := context.temp_allocator) -> string {
|
escape_html :: proc(s: string, allocator := context.temp_allocator) -> string {
|
||||||
b := strings.builder_make(allocator)
|
b := strings.builder_make(allocator)
|
||||||
@@ -62,8 +63,30 @@ check_only_var_defs :: proc(token: ^Token) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
slugifyifyifyify :: proc(text: string) -> string {
|
slugifyifyifyify :: proc(text: string) -> string {
|
||||||
lower := strings.to_lower(text, context.temp_allocator)
|
b := strings.builder_make(context.temp_allocator)
|
||||||
res, _ := strings.replace_all(lower, " ", "-", context.temp_allocator)
|
|
||||||
|
for ch in text {
|
||||||
|
lower_ch := unicode.to_lower(ch)
|
||||||
|
|
||||||
|
if unicode.is_letter(lower_ch) || unicode.is_digit(lower_ch) {
|
||||||
|
strings.write_rune(&b, lower_ch)
|
||||||
|
} else if lower_ch == ' ' || lower_ch == '_' || lower_ch == '-' {
|
||||||
|
strings.write_rune(&b, '-')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res := strings.to_string(b)
|
||||||
|
|
||||||
|
for {
|
||||||
|
new_res, replaced := strings.replace_all(res, "--", "-", context.temp_allocator)
|
||||||
|
if !replaced {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
res = new_res
|
||||||
|
}
|
||||||
|
|
||||||
|
res = strings.trim(res, "-")
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +114,7 @@ print_html :: proc(token: ^Token) {
|
|||||||
}
|
}
|
||||||
slug := slugifyifyifyify(strings.to_string(b))
|
slug := slugifyifyifyify(strings.to_string(b))
|
||||||
|
|
||||||
fmt.printf("<h%d id=\"%s\">", level, slug)
|
fmt.printf("<h%d id=\"%s\">", level, escape_html(slug))
|
||||||
for child in token.children {
|
for child in token.children {
|
||||||
print_html(child)
|
print_html(child)
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-5
@@ -1,6 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "core:fmt"
|
|
||||||
import "core:strconv"
|
import "core:strconv"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
@@ -161,10 +160,10 @@ parse_header :: proc(line: string) -> (int, string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parse_footnote_def :: proc(line: string) -> (string, string, bool) {
|
parse_footnote_def :: proc(line: string) -> (string, string, bool) {
|
||||||
if strings.has_prefix(line, "[&") {
|
if strings.has_prefix(line, "[&&") {
|
||||||
idx := strings.index_byte(line, ']')
|
idx := strings.index_byte(line, ']')
|
||||||
if idx != -1 {
|
if idx != -1 {
|
||||||
id := line[2:idx]
|
id := line[3:idx]
|
||||||
text := strings.trim_space(line[idx + 1:])
|
text := strings.trim_space(line[idx + 1:])
|
||||||
return id, text, true
|
return id, text, true
|
||||||
}
|
}
|
||||||
@@ -335,7 +334,7 @@ parse_inline_footnotes :: proc(token: ^Token) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
start_idx := strings.index(str, "[#")
|
start_idx := strings.index(str, "[&")
|
||||||
if start_idx == -1 {
|
if start_idx == -1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1042,10 +1041,10 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token {
|
|||||||
|
|
||||||
for token in root.children {
|
for token in root.children {
|
||||||
parse_inline_code(token)
|
parse_inline_code(token)
|
||||||
|
parse_inline_footnotes(token)
|
||||||
parse_links(token)
|
parse_links(token)
|
||||||
parse_variables(token)
|
parse_variables(token)
|
||||||
parse_embeds(token)
|
parse_embeds(token)
|
||||||
parse_inline_footnotes(token)
|
|
||||||
pass_through_children(token)
|
pass_through_children(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user