use zero-copy traits for inline code detection

This commit is contained in:
yggverse 2025-03-17 02:20:36 +02:00
parent e2012dee99
commit d8f692d807

View file

@ -104,7 +104,8 @@ impl Gemini {
let is_multiline_enabled = { let is_multiline_enabled = {
let mut t: usize = 0; let mut t: usize = 0;
for l in gemtext.lines() { for l in gemtext.lines() {
if (l.starts_with(code::TAG) || l.ends_with(code::TAG)) && Inline::from(l).is_none() if (l.starts_with(code::TAG) || l.ends_with(code::TAG))
&& inline::Gemtext::as_value(l).is_none()
{ {
t += 1; t += 1;
} }
@ -115,9 +116,10 @@ impl Gemini {
// Parse gemtext lines // Parse gemtext lines
for line in gemtext.lines() { for line in gemtext.lines() {
// Is inline code // Is inline code
if let Some(code) = Inline::from(line) { {
if let Some(code) = inline::Gemtext::as_value(line) {
// Try auto-detect code syntax for given `value` @TODO optional // Try auto-detect code syntax for given `value` @TODO optional
match syntax.highlight(&code.value, None) { match syntax.highlight(code, None) {
Ok(highlight) => { Ok(highlight) => {
for (syntax_tag, entity) in highlight { for (syntax_tag, entity) in highlight {
// Register new tag // Register new tag
@ -134,19 +136,24 @@ impl Gemini {
} }
Err(_) => { Err(_) => {
// Try ANSI/SGR format (terminal emulation) @TODO optional // Try ANSI/SGR format (terminal emulation) @TODO optional
for (ansi_tag, entity) in ansi::format(&code.value) { for (ansi_tag, entity) in ansi::format(code) {
// Register new tag // Register new tag
if !tag.text_tag_table.add(&ansi_tag) { if !tag.text_tag_table.add(&ansi_tag) {
todo!() todo!()
} }
// Append tag to buffer // Append tag to buffer
buffer.insert_with_tags(&mut buffer.end_iter(), &entity, &[&ansi_tag]); buffer.insert_with_tags(
&mut buffer.end_iter(),
&entity,
&[&ansi_tag],
);
} }
} // @TODO handle } // @TODO handle
} }
buffer.insert(&mut buffer.end_iter(), NEW_LINE); buffer.insert(&mut buffer.end_iter(), NEW_LINE);
continue; continue;
} }
}
// Is multiline code // Is multiline code
if is_multiline_enabled { if is_multiline_enabled {