diff --git a/src/context/src_block.rs b/src/context/src_block.rs index f6af667..4602a45 100644 --- a/src/context/src_block.rs +++ b/src/context/src_block.rs @@ -6,38 +6,20 @@ use crate::intermediate::ISrcBlock; use super::macros::render; -#[derive(Debug, Serialize)] -#[serde(untagged)] -pub(crate) enum RenderSrcBlock { - Plain(RenderPlainSrcBlock), - Highlighted(RenderHighlightedSrcBlock), -} - #[derive(Debug, Serialize)] #[serde(tag = "type")] #[serde(rename = "src_block")] -pub(crate) struct RenderPlainSrcBlock { - lines: Vec, - language: Option, - post_blank: organic::types::PostBlank, -} - -#[derive(Debug, Serialize)] -#[serde(tag = "type")] -#[serde(rename = "highlighted_src_block")] -pub(crate) struct RenderHighlightedSrcBlock { +pub(crate) struct RenderSrcBlock { lines: Vec, language: Option, post_blank: organic::types::PostBlank, } render!(RenderSrcBlock, ISrcBlock, original, _render_context, { - match original { - ISrcBlock::Plain(plain_src_block) => Ok(RenderSrcBlock::Plain(RenderPlainSrcBlock { - lines: plain_src_block.lines.clone(), - language: plain_src_block.language.clone(), - post_blank: plain_src_block.post_blank, - })), - ISrcBlock::Highlighted(_) => todo!(), - } + Ok(RenderSrcBlock { + // lines: original.lines.clone(), + lines: Vec::new(), + language: original.language.clone(), + post_blank: original.post_blank, + }) }); diff --git a/src/intermediate/src_block.rs b/src/intermediate/src_block.rs index ca88164..64cda52 100644 --- a/src/intermediate/src_block.rs +++ b/src/intermediate/src_block.rs @@ -10,20 +10,7 @@ use tree_sitter_highlight::HighlightEvent; use tree_sitter_highlight::Highlighter; #[derive(Debug, Clone)] -pub(crate) enum ISrcBlock { - Plain(PlainSrcBlock), - Highlighted(HighlightedSrcBlock), -} - -#[derive(Debug, Clone)] -pub(crate) struct PlainSrcBlock { - pub(crate) lines: Vec, - pub(crate) language: Option, - pub(crate) post_blank: organic::types::PostBlank, -} - -#[derive(Debug, Clone)] -pub(crate) struct HighlightedSrcBlock { +pub(crate) struct ISrcBlock { pub(crate) lines: Vec, pub(crate) language: Option, pub(crate) post_blank: organic::types::PostBlank, @@ -96,16 +83,21 @@ intermediate!( Some("nix") => { let highlighted = highlight_nix(&lines); if let Ok(highlighted) = highlighted { - // return the other type + return Ok(ISrcBlock { + lines: highlighted, + language, + post_blank: original.get_post_blank(), + }); } } _ => {} }; - Ok(ISrcBlock::Plain(PlainSrcBlock { - lines, + let highlighted = highlight_plain(&lines)?; + Ok(ISrcBlock { + lines: highlighted, language, post_blank: original.get_post_blank(), - })) + }) } ); @@ -127,6 +119,20 @@ fn ascii_whitespace_value(c: char) -> usize { } } +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())); + line + }) + .collect()) +} + fn highlight_nix(lines: &[L]) -> Result, CustomError> where L: Borrow,