Files
TypoML/src/parser.v
T
2026-07-11 19:55:19 +02:00

48 lines
849 B
V

fn replace(text string, start int, len int, repl string) string {
return text[..start] + repl + text[start + len..]
}
fn parse_header(line string) (int, string) {
if line.len == 0 {
return 0, line
}
mut lvl := 0
for lvl < line.len && line[lvl] == '!'.bytes()[0] {
lvl++
}
if lvl == 0 || lvl >= line.len || line[lvl] != ' '.bytes()[0] {
return 0, line
}
return lvl, line[lvl + 1..]
}
fn parse(lines []string) string {
mut output := '<p>'
for line in lines {
mut trimmed := line.trim_space()
if trimmed.len == 0 {
output += '<br>\n'
continue
}
lvl, mut text := parse_header(trimmed)
is_header := lvl > 0
if is_header {
output += '<h' + lvl.str() + '>'
output += text + '</h' + lvl.str() + '>'
} else {
output += text
output += '<br>'
}
output += '\n'
}
output += '</p>'
return output
}