From 41927764fc6f79240295f4a75f0c6cf3654af59a Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 22 Feb 2025 18:39:41 -0500 Subject: [PATCH] Continue highlights across code block lines. --- TODO.org | 4 ++-- src/intermediate/src_block.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/TODO.org b/TODO.org index 46cb300..cb2226c 100644 --- a/TODO.org +++ b/TODO.org @@ -1,4 +1,4 @@ -* Things to do [3/14] +* Things to do [4/14] ** DONE If the paragraph only contains an image, text-align center ** DONE Syntax highlighting for code blocks ** TODO Render gnuplot @@ -14,4 +14,4 @@ ** TODO Add highlighting for languages [0/2] *** TODO bash *** TODO gnuplot -** TODO Bug: carry over highlight starts when breaking lines +** DONE Bug: carry over highlight starts when breaking lines diff --git a/src/intermediate/src_block.rs b/src/intermediate/src_block.rs index 7b357ab..4588256 100644 --- a/src/intermediate/src_block.rs +++ b/src/intermediate/src_block.rs @@ -169,6 +169,7 @@ where let mut highlighted_text: Vec = Vec::with_capacity(lines.len()); let mut current_line = ISrcLine::new(); + let mut highlight_stack: Vec<&str> = Vec::new(); for event in highlights { match event.unwrap() { HighlightEvent::Source { start, end } => { @@ -178,8 +179,22 @@ where current_line .children .push(ISrcSegment::RawText(first_line.to_owned())); + current_line.children.extend( + highlight_stack + .iter() + .map(|_name| ISrcSegment::HighlightEnd), + ); highlighted_text.push(current_line); current_line = ISrcLine::new(); + current_line + .children + .extend( + highlight_stack + .iter() + .map(|name| ISrcSegment::HighlightStart { + name: (*name).into(), + }), + ); span = &span[(line_break_index + 1)..]; } if !span.is_empty() { @@ -189,16 +204,20 @@ where } } HighlightEvent::HighlightStart(s) => { + highlight_stack.push(highlight_names[s.0]); current_line.children.push(ISrcSegment::HighlightStart { name: highlight_names[s.0].into(), }); } HighlightEvent::HighlightEnd => { + highlight_stack.pop(); current_line.children.push(ISrcSegment::HighlightEnd); } } } + debug_assert!(highlight_stack.is_empty()); + Ok(highlighted_text) }