Introduce an enum for a separate highlighted src block type.

This commit is contained in:
Tom Alexander 2025-02-22 15:22:35 -05:00
parent b06424cb17
commit c067ca9cc8
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 52 additions and 14 deletions

View File

@ -6,19 +6,38 @@ 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 RenderSrcBlock {
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 {
lines: Vec<String>,
language: Option<String>,
post_blank: organic::types::PostBlank,
}
render!(RenderSrcBlock, ISrcBlock, original, _render_context, {
Ok(RenderSrcBlock {
lines: original.lines.clone(),
language: original.language.clone(),
post_blank: original.post_blank,
})
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!(),
}
});

View File

@ -1,3 +1,4 @@
use std::borrow::Borrow;
use std::borrow::Cow;
use super::macros::intermediate;
@ -9,7 +10,20 @@ use tree_sitter_highlight::HighlightEvent;
use tree_sitter_highlight::Highlighter;
#[derive(Debug, Clone)]
pub(crate) struct ISrcBlock {
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) lines: Vec<String>,
pub(crate) language: Option<String>,
pub(crate) post_blank: organic::types::PostBlank,
@ -66,18 +80,20 @@ intermediate!(
.collect();
let language = original.language.map(str::to_owned);
let lines = match language.as_ref().map(String::as_str) {
match language.as_ref().map(String::as_str) {
Some("nix") => {
// foo
highlight_nix(lines)?
let highlighted = highlight_nix(&lines);
if let Ok(highlighted) = highlighted {
// return the other type
}
_ => lines,
}
_ => {}
};
Ok(ISrcBlock {
Ok(ISrcBlock::Plain(PlainSrcBlock {
lines,
language,
post_blank: original.get_post_blank(),
})
}))
}
);
@ -91,7 +107,10 @@ fn ascii_whitespace_value(c: char) -> usize {
}
}
fn highlight_nix(lines: Vec<String>) -> Result<Vec<String>, CustomError> {
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<String>, CustomError>
where
L: Borrow<str>,
{
let highlight_names = ["comment", "keyword"];
// Need 1 highlighter per thread
let mut highlighter = Highlighter::new();