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;
#[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<String>,
language: Option<String>,
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<String>,
language: Option<String>,
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,
})
});

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>,