This commit is contained in:
2025-10-23 08:29:10 +02:00
parent bc26c88188
commit 581ae4808c

View File

@@ -9,21 +9,14 @@ import incubaid.herolib.core.texttools
fn (mut paragraph Paragraph) paragraph_parse() ! { fn (mut paragraph Paragraph) paragraph_parse() ! {
mut parser := parser_char_new_text(paragraph.content) mut parser := parser_char_new_text(paragraph.content)
// SAFETY: Ensure children list is not empty // mut d := para.doc or { panic('no doc') }
if paragraph.children.len == 0 { paragraph.text_new(mut paragraph.parent_doc(), '') // the initial one
paragraph.text_new(mut paragraph.parent_doc(), '')
}
mut potential_link := false mut potential_link := false
mut link_in_link := false mut link_in_link := false
for { for {
// Add guard at loop start mut llast := paragraph.children.last()
if paragraph.children.len == 0 {
return error('paragraph children list became empty during parsing')
}
mut llast := paragraph.get_last_safe()!
mut char_ := parser.char_current() mut char_ := parser.char_current()
// console.print_debug("[[[${char_}]]]") // console.print_debug("[[[${char_}]]]")
@@ -31,16 +24,12 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
if mut llast is Def { if mut llast is Def {
if (char_ == '' || char_ == ' ' || char_ == '\n') && parser.char_prev() != '*' { if (char_ == '' || char_ == ' ' || char_ == '\n') && parser.char_prev() != '*' {
if llast.content.len < 3 { if llast.content.len < 3 {
saved_content := llast.content
paragraph.children.pop() paragraph.children.pop()
if paragraph.children.len == 0 { mut llast2 := paragraph.children.last()
paragraph.text_new(mut paragraph.parent_doc(), '')
}
mut llast2 := paragraph.get_last_safe()!
if mut llast2 is Text { if mut llast2 is Text {
llast2.content += saved_content + char_ llast2.content += llast.content + char_
} else { } else {
paragraph.text_new(mut paragraph.parent_doc(), saved_content + char_) paragraph.text_new(mut paragraph.parent_doc(), llast.content + char_)
} }
parser.next() parser.next()
char_ = '' char_ = ''
@@ -57,19 +46,15 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
} else if !(texttools.is_upper_text(char_) || char_ == '_') { } else if !(texttools.is_upper_text(char_) || char_ == '_') {
// this means it wasn't a def, we need to add text // this means it wasn't a def, we need to add text
// console.print_debug(' -- no def: ${char_}') // console.print_debug(' -- no def: ${char_}')
saved_content := llast.content
paragraph.children.pop() paragraph.children.pop()
if paragraph.children.len == 0 {
paragraph.text_new(mut paragraph.parent_doc(), '')
}
// console.print_debug(' -- no def: ${paragraph.children.last()}') // console.print_debug(' -- no def: ${paragraph.children.last()}')
mut llast2 := paragraph.get_last_safe()! mut llast2 := paragraph.children.last()
if mut llast2 is Text { if mut llast2 is Text {
llast2_content := llast2.content llast2_content := llast2.content
llast2.content = llast2_content + saved_content + char_ llast2.content = llast2_content + llast.content + char_
// llast2.content += llast.content + char_ // llast2.content += llast.content + char_
} else { } else {
paragraph.text_new(mut paragraph.parent_doc(), saved_content + char_) paragraph.text_new(mut paragraph.parent_doc(), llast.content + char_)
} }
parser.next() parser.next()
char_ = '' char_ = ''
@@ -123,10 +108,7 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
mut c := llast.content mut c := llast.content
paragraph.children.delete_last() // remove the link paragraph.children.delete_last() // remove the link
paragraph.text_new(mut paragraph.parent_doc(), '') paragraph.text_new(mut paragraph.parent_doc(), '')
if paragraph.children.len == 0 { llast = paragraph.children.last() // fetch last again
paragraph.text_new(mut paragraph.parent_doc(), '')
}
llast = paragraph.get_last_safe()! // fetch last again
llast_content := llast.content llast_content := llast.content
llast.content = llast_content + c + char_ // need to add current content llast.content = llast_content + c + char_ // need to add current content
parser.next() parser.next()
@@ -157,12 +139,12 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
if mut llast is Text { if mut llast is Text {
if char_ != '' { if char_ != '' {
if char_ == '*' { // if char_ == '*' {
paragraph.def_new(mut paragraph.parent_doc(), '*') // paragraph.def_new(mut paragraph.parent_doc(), '*')
parser.next() // parser.next()
char_ = '' // char_ = ''
continue // continue
} // }
// check for comments start // check for comments start
for totry in ['<!--', '//'] { for totry in ['<!--', '//'] {
// TODO: this is a quick fix for now (https:// is being parsed as comment) // TODO: this is a quick fix for now (https:// is being parsed as comment)
@@ -170,7 +152,7 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
if parser.text_next_is(totry, 0) && !is_url { if parser.text_next_is(totry, 0) && !is_url {
// we are now in comment // we are now in comment
paragraph.comment_new(mut paragraph.parent_doc(), '') paragraph.comment_new(mut paragraph.parent_doc(), '')
mut llast2 := paragraph.get_last_safe()! mut llast2 := paragraph.children.last()
if totry == '//' { if totry == '//' {
if mut llast2 is Comment { if mut llast2 is Comment {
llast2.singleline = true llast2.singleline = true
@@ -198,10 +180,3 @@ fn (mut paragraph Paragraph) paragraph_parse() ! {
paragraph.remove_empty_children() paragraph.remove_empty_children()
// console.print_debug("[[[[[DONE]]]]]") // console.print_debug("[[[[[DONE]]]]]")
} }
fn (mut paragraph Paragraph) get_last_safe() !Element {
if paragraph.children.len == 0 {
return error('paragraph has no children')
}
return paragraph.children.last()
}