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
2 changed files with 31 additions and 43 deletions

View File

@@ -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<String>,
pub(crate) language: Option<String>,
pub(crate) post_blank: organic::types::PostBlank,
}
#[derive(Debug, Clone)]
pub(crate) struct HighlightedSrcBlock {
pub(crate) struct ISrcBlock {
pub(crate) lines: Vec<HighlightedSrcLine>,
pub(crate) language: Option<String>,
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<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>
where
L: Borrow<str>,