Update the render context to use the new src block format.

This commit is contained in:
Tom Alexander
2025-02-22 16:04:47 -05:00
parent 7e934cd360
commit 4cc04bda46
3 changed files with 61 additions and 27 deletions

View File

@@ -11,18 +11,18 @@ use tree_sitter_highlight::Highlighter;
#[derive(Debug, Clone)]
pub(crate) struct ISrcBlock {
pub(crate) lines: Vec<HighlightedSrcLine>,
pub(crate) lines: Vec<ISrcLine>,
pub(crate) language: Option<String>,
pub(crate) post_blank: organic::types::PostBlank,
}
#[derive(Debug, Clone)]
pub(crate) struct HighlightedSrcLine {
pub(crate) children: Vec<HighlightedSrcSegment>,
pub(crate) struct ISrcLine {
pub(crate) children: Vec<ISrcSegment>,
}
#[derive(Debug, Clone)]
pub(crate) enum HighlightedSrcSegment {
pub(crate) enum ISrcSegment {
RawText(String),
HighlightStart { name: String },
HighlightEnd,
@@ -101,9 +101,9 @@ intermediate!(
}
);
impl HighlightedSrcLine {
pub(crate) fn new() -> HighlightedSrcLine {
HighlightedSrcLine {
impl ISrcLine {
pub(crate) fn new() -> ISrcLine {
ISrcLine {
children: Vec::new(),
}
}
@@ -119,21 +119,21 @@ fn ascii_whitespace_value(c: char) -> usize {
}
}
fn highlight_plain<L>(lines: &[L]) -> Result<Vec<HighlightedSrcLine>, CustomError>
fn highlight_plain<L>(lines: &[L]) -> Result<Vec<ISrcLine>, 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()));
let mut line = ISrcLine::new();
line.children.push(ISrcSegment::RawText(l.into()));
line
})
.collect())
}
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<HighlightedSrcLine>, CustomError>
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
where
L: Borrow<str>,
{
@@ -152,8 +152,8 @@ where
.highlight(&config, combined_text.as_bytes(), None, |_| None)
.unwrap();
let mut highlighted_text: Vec<HighlightedSrcLine> = Vec::with_capacity(lines.len());
let mut current_line = HighlightedSrcLine::new();
let mut highlighted_text: Vec<ISrcLine> = Vec::with_capacity(lines.len());
let mut current_line = ISrcLine::new();
for event in highlights {
match event.unwrap() {
HighlightEvent::Source { start, end } => {
@@ -162,28 +162,24 @@ where
let first_line = &span[..(line_break_index + 1)];
current_line
.children
.push(HighlightedSrcSegment::RawText(first_line.to_owned()));
.push(ISrcSegment::RawText(first_line.to_owned()));
highlighted_text.push(current_line);
current_line = HighlightedSrcLine::new();
current_line = ISrcLine::new();
span = &span[(line_break_index + 1)..];
}
if !span.is_empty() {
current_line
.children
.push(HighlightedSrcSegment::RawText(span.to_owned()));
.push(ISrcSegment::RawText(span.to_owned()));
}
}
HighlightEvent::HighlightStart(s) => {
current_line
.children
.push(HighlightedSrcSegment::HighlightStart {
name: highlight_names[s.0].to_owned(),
});
current_line.children.push(ISrcSegment::HighlightStart {
name: highlight_names[s.0].to_owned(),
});
}
HighlightEvent::HighlightEnd => {
current_line
.children
.push(HighlightedSrcSegment::HighlightEnd);
current_line.children.push(ISrcSegment::HighlightEnd);
}
}
}