Ordered lists

This commit is contained in:
2026-07-14 14:31:37 +02:00
parent b4f5285a99
commit 4f03c5bf9a
+36 -16
View File
@@ -1,5 +1,6 @@
package main
import "core:strconv"
import "core:strings"
TokenType :: enum {
@@ -58,12 +59,8 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token {
line := strings.trim_space(untrimmed_line)
if len(line) == 0 {
if line_index != len(lines) - 1 {
current_token = push_token(&root, TokenType.Paragraph, nil)
current_token = &root
continue
} else {
continue
}
}
header_level, text := parse_header(line)
@@ -77,8 +74,10 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token {
if strings.has_prefix(text, "> ") {
if current_token.type != TokenType.Blockquote {
current_token = push_token(&root, TokenType.Blockquote, nil)
push_token(current_token, TokenType.Paragraph, nil)
}
push_token(current_token, TokenType.Text, text[2:])
last_p := current_token.children[len(current_token.children) - 1]
push_token(last_p, TokenType.Text, text[2:])
continue
}
@@ -86,25 +85,46 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token {
if current_token.type != TokenType.UnorderedList {
current_token = push_token(&root, TokenType.UnorderedList, nil)
}
current_token = push_token(current_token, TokenType.ListItem, nil)
push_token(current_token, TokenType.Text, text[2:])
current_token = current_token.parent
li := push_token(current_token, TokenType.ListItem, nil)
push_token(li, TokenType.Text, text[2:])
continue
}
if len(text) >= 3 {
item := 1
idx := 0
for idx < len(text) && text[idx] >= '0' && text[idx] <= '9' {
idx += 1
}
if idx > 0 && idx + 1 < len(text) && text[idx] == '.' && text[idx + 1] == ' ' {
item, _ = strconv.parse_int(text[:idx])
text = text[idx + 2:]
if current_token.type != TokenType.OrderedList {
current_token = push_token(&root, TokenType.OrderedList, item)
}
li := push_token(current_token, TokenType.ListItem, nil)
push_token(li, TokenType.Text, text)
continue
}
}
// LAST-CASE SCENARIO: NORMAL TEXT
if current_token.parent == nil {
if current_token.type == .Blockquote {
last_p := current_token.children[len(current_token.children) - 1]
push_token(last_p, TokenType.Text, line)
} else if current_token.type == .UnorderedList || current_token.type == .OrderedList {
last_li := current_token.children[len(current_token.children) - 1]
push_token(last_li, TokenType.Text, line)
} else {
if current_token.type != .Paragraph {
current_token = push_token(&root, TokenType.Paragraph, nil)
}
#partial switch current_token.parent.type {
case TokenType.Blockquote:
current_token = current_token.parent
}
push_token(current_token, TokenType.Text, line)
}
}
return root
}