Removed the enum and made plain src blocks just highlighted src blocks with only plain text in them.

This commit is contained in:
Tom Alexander 2025-02-22 15:55:16 -05:00
parent e34e2ef75f
commit 7e934cd360
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 31 additions and 43 deletions

View File

@ -6,38 +6,20 @@ use crate::intermediate::ISrcBlock;
use super::macros::render; use super::macros::render;
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub(crate) enum RenderSrcBlock {
Plain(RenderPlainSrcBlock),
Highlighted(RenderHighlightedSrcBlock),
}
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
#[serde(rename = "src_block")] #[serde(rename = "src_block")]
pub(crate) struct RenderPlainSrcBlock { pub(crate) struct RenderSrcBlock {
lines: Vec<String>,
language: Option<String>,
post_blank: organic::types::PostBlank,
}
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
#[serde(rename = "highlighted_src_block")]
pub(crate) struct RenderHighlightedSrcBlock {
lines: Vec<String>, lines: Vec<String>,
language: Option<String>, language: Option<String>,
post_blank: organic::types::PostBlank, post_blank: organic::types::PostBlank,
} }
render!(RenderSrcBlock, ISrcBlock, original, _render_context, { render!(RenderSrcBlock, ISrcBlock, original, _render_context, {
match original { Ok(RenderSrcBlock {
ISrcBlock::Plain(plain_src_block) => Ok(RenderSrcBlock::Plain(RenderPlainSrcBlock { // lines: original.lines.clone(),
lines: plain_src_block.lines.clone(), lines: Vec::new(),
language: plain_src_block.language.clone(), language: original.language.clone(),
post_blank: plain_src_block.post_blank, post_blank: original.post_blank,
})), })
ISrcBlock::Highlighted(_) => todo!(),
}
}); });

View File

@ -10,20 +10,7 @@ use tree_sitter_highlight::HighlightEvent;
use tree_sitter_highlight::Highlighter; use tree_sitter_highlight::Highlighter;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) enum ISrcBlock { pub(crate) struct ISrcBlock {
Plain(PlainSrcBlock),
Highlighted(HighlightedSrcBlock),
}
#[derive(Debug, Clone)]
pub(crate) struct PlainSrcBlock {
pub(crate) lines: Vec<String>,
pub(crate) language: Option<String>,
pub(crate) post_blank: organic::types::PostBlank,
}
#[derive(Debug, Clone)]
pub(crate) struct HighlightedSrcBlock {
pub(crate) lines: Vec<HighlightedSrcLine>, pub(crate) lines: Vec<HighlightedSrcLine>,
pub(crate) language: Option<String>, pub(crate) language: Option<String>,
pub(crate) post_blank: organic::types::PostBlank, pub(crate) post_blank: organic::types::PostBlank,
@ -96,16 +83,21 @@ intermediate!(
Some("nix") => { Some("nix") => {
let highlighted = highlight_nix(&lines); let highlighted = highlight_nix(&lines);
if let Ok(highlighted) = highlighted { 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 { let highlighted = highlight_plain(&lines)?;
lines, Ok(ISrcBlock {
lines: highlighted,
language, language,
post_blank: original.get_post_blank(), post_blank: original.get_post_blank(),
})) })
} }
); );
@ -127,6 +119,20 @@ fn ascii_whitespace_value(c: char) -> usize {
} }
} }
fn highlight_plain<L>(lines: &[L]) -> Result<Vec<HighlightedSrcLine>, 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<L>(lines: &[L]) -> Result<Vec<HighlightedSrcLine>, CustomError> fn highlight_nix<L>(lines: &[L]) -> Result<Vec<HighlightedSrcLine>, CustomError>
where where
L: Borrow<str>, L: Borrow<str>,