natter/src/context/src_block.rs
Tom Alexander 8d85d5ef79
All checks were successful
format Build format has succeeded
clippy Build clippy has succeeded
rust-test Build rust-test has succeeded
Fix clippy.
2025-02-22 19:42:24 -05:00

96 lines
2.5 KiB
Rust

use serde::Serialize;
use super::render_context::RenderContext;
use crate::error::CustomError;
use crate::intermediate::ISrcBlock;
use crate::intermediate::ISrcSegment;
use super::macros::render;
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
#[serde(rename = "src_block")]
pub(crate) struct RenderSrcBlock {
lines: Vec<RenderSrcLine>,
language: Option<String>,
post_blank: organic::types::PostBlank,
}
#[derive(Debug, Serialize)]
pub(crate) struct RenderSrcLine {
children: Vec<RenderSrcSegment>,
}
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
pub(crate) enum RenderSrcSegment {
#[serde(rename = "raw_text")]
RawText { content: String },
#[serde(rename = "highlight_start")]
HighlightStart { name: String },
#[serde(rename = "highlight_end")]
HighlightEnd,
}
render!(RenderSrcBlock, ISrcBlock, original, render_context, {
let lines = original
.lines
.iter()
.map(|original_line| {
let children = original_line
.children
.iter()
.map(|original_segment| match original_segment {
ISrcSegment::RawText(body) => RenderSrcSegment::RawText {
content: body.to_owned(),
},
ISrcSegment::HighlightStart { name } => RenderSrcSegment::HighlightStart {
name: css_safe_name(name),
},
ISrcSegment::HighlightEnd => RenderSrcSegment::HighlightEnd,
})
.collect();
RenderSrcLine { children }
})
.collect();
match original.language.as_deref() {
Some("bash") => {
render_context
.dependency_manager
.lock()
.unwrap()
.include_css("language_bash.css")?;
}
Some("nix") => {
render_context
.dependency_manager
.lock()
.unwrap()
.include_css("language_nix.css")?;
}
Some("python") => {
render_context
.dependency_manager
.lock()
.unwrap()
.include_css("language_python.css")?;
}
_ => {}
};
Ok(RenderSrcBlock {
lines,
language: original.language.clone(),
post_blank: original.post_blank,
})
});
fn css_safe_name<S>(inp: S) -> String
where
std::string::String: From<S>,
{
let inp: String = inp.into();
inp.replace(".", "_")
}