From 4cc04bda461087cd9ad963f36210ac2dc4972e4a Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 22 Feb 2025 16:04:47 -0500 Subject: [PATCH] Update the render context to use the new src block format. --- src/context/src_block.rs | 43 +++++++++++++++++++++++++++++++--- src/intermediate/mod.rs | 1 + src/intermediate/src_block.rs | 44 ++++++++++++++++------------------- 3 files changed, 61 insertions(+), 27 deletions(-) diff --git a/src/context/src_block.rs b/src/context/src_block.rs index 4602a45..1d03eef 100644 --- a/src/context/src_block.rs +++ b/src/context/src_block.rs @@ -3,6 +3,7 @@ use serde::Serialize; use super::render_context::RenderContext; use crate::error::CustomError; use crate::intermediate::ISrcBlock; +use crate::intermediate::ISrcSegment; use super::macros::render; @@ -10,15 +11,51 @@ use super::macros::render; #[serde(tag = "type")] #[serde(rename = "src_block")] pub(crate) struct RenderSrcBlock { - lines: Vec, + lines: Vec, language: Option, post_blank: organic::types::PostBlank, } +#[derive(Debug, Serialize)] +#[serde(tag = "type")] +#[serde(rename = "src_block")] +pub(crate) struct RenderSrcLine { + children: Vec, +} + +#[derive(Debug, Serialize)] +pub(crate) enum RenderSrcSegment { + #[serde(rename = "raw_text")] + RawText(String), + + #[serde(rename = "highlight_start")] + HighlightStart { name: String }, + + #[serde(rename = "highlight_end")] + HighlightEnd, +} + render!(RenderSrcBlock, ISrcBlock, original, _render_context, { + let lines = original + .lines + .iter() + .map(|original_line| { + let children = original_line + .children + .iter() + .map(|original_segment| match original_segment { + ISrcSegment::RawText(body) => RenderSrcSegment::RawText(body.to_owned()), + ISrcSegment::HighlightStart { name } => RenderSrcSegment::HighlightStart { + name: name.to_owned(), + }, + ISrcSegment::HighlightEnd => RenderSrcSegment::HighlightEnd, + }) + .collect(); + RenderSrcLine { children } + }) + .collect(); Ok(RenderSrcBlock { - // lines: original.lines.clone(), - lines: Vec::new(), + lines, language: original.language.clone(), post_blank: original.post_blank, }) diff --git a/src/intermediate/mod.rs b/src/intermediate/mod.rs index 44c257f..41950f1 100644 --- a/src/intermediate/mod.rs +++ b/src/intermediate/mod.rs @@ -127,6 +127,7 @@ pub(crate) use regular_link::LinkTarget; pub(crate) use section::ISection; pub(crate) use special_block::ISpecialBlock; pub(crate) use src_block::ISrcBlock; +pub(crate) use src_block::ISrcSegment; pub(crate) use statistics_cookie::IStatisticsCookie; pub(crate) use strike_through::IStrikeThrough; pub(crate) use subscript::ISubscript; diff --git a/src/intermediate/src_block.rs b/src/intermediate/src_block.rs index 64cda52..ad18df7 100644 --- a/src/intermediate/src_block.rs +++ b/src/intermediate/src_block.rs @@ -11,18 +11,18 @@ use tree_sitter_highlight::Highlighter; #[derive(Debug, Clone)] pub(crate) struct ISrcBlock { - pub(crate) lines: Vec, + pub(crate) lines: Vec, pub(crate) language: Option, pub(crate) post_blank: organic::types::PostBlank, } #[derive(Debug, Clone)] -pub(crate) struct HighlightedSrcLine { - pub(crate) children: Vec, +pub(crate) struct ISrcLine { + pub(crate) children: Vec, } #[derive(Debug, Clone)] -pub(crate) enum HighlightedSrcSegment { +pub(crate) enum ISrcSegment { RawText(String), HighlightStart { name: String }, HighlightEnd, @@ -101,9 +101,9 @@ intermediate!( } ); -impl HighlightedSrcLine { - pub(crate) fn new() -> HighlightedSrcLine { - HighlightedSrcLine { +impl ISrcLine { + pub(crate) fn new() -> ISrcLine { + ISrcLine { children: Vec::new(), } } @@ -119,21 +119,21 @@ fn ascii_whitespace_value(c: char) -> usize { } } -fn highlight_plain(lines: &[L]) -> Result, CustomError> +fn highlight_plain(lines: &[L]) -> Result, CustomError> where std::string::String: for<'a> From<&'a L>, { Ok(lines .into_iter() .map(|l| { - let mut line = HighlightedSrcLine::new(); - line.children.push(HighlightedSrcSegment::RawText(l.into())); + let mut line = ISrcLine::new(); + line.children.push(ISrcSegment::RawText(l.into())); line }) .collect()) } -fn highlight_nix(lines: &[L]) -> Result, CustomError> +fn highlight_nix(lines: &[L]) -> Result, CustomError> where L: Borrow, { @@ -152,8 +152,8 @@ where .highlight(&config, combined_text.as_bytes(), None, |_| None) .unwrap(); - let mut highlighted_text: Vec = Vec::with_capacity(lines.len()); - let mut current_line = HighlightedSrcLine::new(); + let mut highlighted_text: Vec = Vec::with_capacity(lines.len()); + let mut current_line = ISrcLine::new(); for event in highlights { match event.unwrap() { HighlightEvent::Source { start, end } => { @@ -162,28 +162,24 @@ where let first_line = &span[..(line_break_index + 1)]; current_line .children - .push(HighlightedSrcSegment::RawText(first_line.to_owned())); + .push(ISrcSegment::RawText(first_line.to_owned())); highlighted_text.push(current_line); - current_line = HighlightedSrcLine::new(); + current_line = ISrcLine::new(); span = &span[(line_break_index + 1)..]; } if !span.is_empty() { current_line .children - .push(HighlightedSrcSegment::RawText(span.to_owned())); + .push(ISrcSegment::RawText(span.to_owned())); } } HighlightEvent::HighlightStart(s) => { - current_line - .children - .push(HighlightedSrcSegment::HighlightStart { - name: highlight_names[s.0].to_owned(), - }); + current_line.children.push(ISrcSegment::HighlightStart { + name: highlight_names[s.0].to_owned(), + }); } HighlightEvent::HighlightEnd => { - current_line - .children - .push(HighlightedSrcSegment::HighlightEnd); + current_line.children.push(ISrcSegment::HighlightEnd); } } }