refactor: Improve frontmatter and def parsing logic

- Save content before modifying
- Handle '*' character for defs correctly
- Re-enable frontmatter parsing for '---' and '+++'
- Re-enable frontmatter parsing for '---' and '+++' in paragraphs
This commit is contained in:
Mahmoud-Emad
2025-10-22 21:31:49 +03:00
parent 4f3a81b097
commit afbfa11516
2 changed files with 32 additions and 30 deletions

View File

@@ -24,12 +24,13 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
if mut llast is Def {
if (char_ == '' || char_ == ' ' || char_ == '\n') && parser.char_prev() != '*' {
if llast.content.len < 3 {
saved_content := llast.content
paragraph.children.pop()
mut llast2 := paragraph.children.last()
if mut llast2 is Text {
llast2.content += llast.content + char_
llast2.content += saved_content + char_
} else {
paragraph.text_new(mut paragraph.parent_doc(), llast.content + char_)
paragraph.text_new(mut paragraph.parent_doc(), saved_content + char_)
}
parser.next()
char_ = ''
@@ -46,15 +47,16 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
} else if !(texttools.is_upper_text(char_) || char_ == '_') {
// this means it wasn't a def, we need to add text
// console.print_debug(' -- no def: ${char_}')
saved_content := llast.content
paragraph.children.pop()
// console.print_debug(' -- no def: ${paragraph.children.last()}')
mut llast2 := paragraph.children.last()
if mut llast2 is Text {
llast2_content := llast2.content
llast2.content = llast2_content + llast.content + char_
llast2.content = llast2_content + saved_content + char_
// llast2.content += llast.content + char_
} else {
paragraph.text_new(mut paragraph.parent_doc(), llast.content + char_)
paragraph.text_new(mut paragraph.parent_doc(), saved_content + char_)
}
parser.next()
char_ = ''
@@ -139,12 +141,12 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
if mut llast is Text {
if char_ != '' {
// if char_ == '*' {
// paragraph.def_new(mut paragraph.parent_doc(), '*')
// parser.next()
// char_ = ''
// continue
// }
if char_ == '*' {
paragraph.def_new(mut paragraph.parent_doc(), '*')
parser.next()
char_ = ''
continue
}
// check for comments start
for totry in ['<!--', '//'] {
// TODO: this is a quick fix for now (https:// is being parsed as comment)

View File

@@ -77,16 +77,16 @@ pub fn parse_doc(mut doc elements.Doc) ! {
continue
}
// if mut llast is elements.Frontmatter || mut llast is elements.Frontmatter2 {
// if trimmed_line == '---' || trimmed_line == '+++' {
// parser.next_start_lf()!
// parser.frontmatter = true
// continue
// }
// llast.content += '${line}\n'
// parser.next()
// continue
// }
if mut llast is elements.Frontmatter || mut llast is elements.Frontmatter2 {
if trimmed_line == '---' || trimmed_line == '+++' {
parser.next_start_lf()!
parser.frontmatter = true
continue
}
llast.content += '${line}\n'
parser.next()
continue
}
if mut llast is elements.Paragraph {
if elements.line_is_list(line) {
@@ -110,17 +110,17 @@ pub fn parse_doc(mut doc elements.Doc) ! {
continue
}
// if line.starts_with('+++') && parser.frontmatter == false {
// mut e := doc.frontmatter_new(mut &doc, '')
// parser.next()
// continue
// }
if line.starts_with('+++') && parser.frontmatter == false {
mut e := doc.frontmatter_new(mut &doc, '')
parser.next()
continue
}
// if line.starts_with('---') && parser.frontmatter == false {
// mut e := doc.frontmatter2_new(mut &doc, '')
// parser.next()
// continue
// }
if line.starts_with('---') && parser.frontmatter == false {
mut e := doc.frontmatter2_new(mut &doc, '')
parser.next()
continue
}
// process headers (# is 35)
if line.len > 0 && line[0] == 35 {