Update the render context to use the new src block format.
This commit is contained in:
parent
7e934cd360
commit
4cc04bda46
@ -3,6 +3,7 @@ use serde::Serialize;
|
|||||||
use super::render_context::RenderContext;
|
use super::render_context::RenderContext;
|
||||||
use crate::error::CustomError;
|
use crate::error::CustomError;
|
||||||
use crate::intermediate::ISrcBlock;
|
use crate::intermediate::ISrcBlock;
|
||||||
|
use crate::intermediate::ISrcSegment;
|
||||||
|
|
||||||
use super::macros::render;
|
use super::macros::render;
|
||||||
|
|
||||||
@ -10,15 +11,51 @@ use super::macros::render;
|
|||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
#[serde(rename = "src_block")]
|
#[serde(rename = "src_block")]
|
||||||
pub(crate) struct RenderSrcBlock {
|
pub(crate) struct RenderSrcBlock {
|
||||||
lines: Vec<String>,
|
lines: Vec<RenderSrcLine>,
|
||||||
language: Option<String>,
|
language: Option<String>,
|
||||||
post_blank: organic::types::PostBlank,
|
post_blank: organic::types::PostBlank,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
#[serde(rename = "src_block")]
|
||||||
|
pub(crate) struct RenderSrcLine {
|
||||||
|
children: Vec<RenderSrcSegment>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub(crate) enum RenderSrcSegment {
|
||||||
|
#[serde(rename = "raw_text")]
|
||||||
|
RawText(String),
|
||||||
|
|
||||||
|
#[serde(rename = "highlight_start")]
|
||||||
|
HighlightStart { name: String },
|
||||||
|
|
||||||
|
#[serde(rename = "highlight_end")]
|
||||||
|
HighlightEnd,
|
||||||
|
}
|
||||||
|
|
||||||
render!(RenderSrcBlock, ISrcBlock, original, _render_context, {
|
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(body.to_owned()),
|
||||||
|
ISrcSegment::HighlightStart { name } => RenderSrcSegment::HighlightStart {
|
||||||
|
name: name.to_owned(),
|
||||||
|
},
|
||||||
|
ISrcSegment::HighlightEnd => RenderSrcSegment::HighlightEnd,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
RenderSrcLine { children }
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
Ok(RenderSrcBlock {
|
Ok(RenderSrcBlock {
|
||||||
// lines: original.lines.clone(),
|
lines,
|
||||||
lines: Vec::new(),
|
|
||||||
language: original.language.clone(),
|
language: original.language.clone(),
|
||||||
post_blank: original.post_blank,
|
post_blank: original.post_blank,
|
||||||
})
|
})
|
||||||
|
@ -127,6 +127,7 @@ pub(crate) use regular_link::LinkTarget;
|
|||||||
pub(crate) use section::ISection;
|
pub(crate) use section::ISection;
|
||||||
pub(crate) use special_block::ISpecialBlock;
|
pub(crate) use special_block::ISpecialBlock;
|
||||||
pub(crate) use src_block::ISrcBlock;
|
pub(crate) use src_block::ISrcBlock;
|
||||||
|
pub(crate) use src_block::ISrcSegment;
|
||||||
pub(crate) use statistics_cookie::IStatisticsCookie;
|
pub(crate) use statistics_cookie::IStatisticsCookie;
|
||||||
pub(crate) use strike_through::IStrikeThrough;
|
pub(crate) use strike_through::IStrikeThrough;
|
||||||
pub(crate) use subscript::ISubscript;
|
pub(crate) use subscript::ISubscript;
|
||||||
|
@ -11,18 +11,18 @@ use tree_sitter_highlight::Highlighter;
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct ISrcBlock {
|
pub(crate) struct ISrcBlock {
|
||||||
pub(crate) lines: Vec<HighlightedSrcLine>,
|
pub(crate) lines: Vec<ISrcLine>,
|
||||||
pub(crate) language: Option<String>,
|
pub(crate) language: Option<String>,
|
||||||
pub(crate) post_blank: organic::types::PostBlank,
|
pub(crate) post_blank: organic::types::PostBlank,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct HighlightedSrcLine {
|
pub(crate) struct ISrcLine {
|
||||||
pub(crate) children: Vec<HighlightedSrcSegment>,
|
pub(crate) children: Vec<ISrcSegment>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) enum HighlightedSrcSegment {
|
pub(crate) enum ISrcSegment {
|
||||||
RawText(String),
|
RawText(String),
|
||||||
HighlightStart { name: String },
|
HighlightStart { name: String },
|
||||||
HighlightEnd,
|
HighlightEnd,
|
||||||
@ -101,9 +101,9 @@ intermediate!(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
impl HighlightedSrcLine {
|
impl ISrcLine {
|
||||||
pub(crate) fn new() -> HighlightedSrcLine {
|
pub(crate) fn new() -> ISrcLine {
|
||||||
HighlightedSrcLine {
|
ISrcLine {
|
||||||
children: Vec::new(),
|
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
|
where
|
||||||
std::string::String: for<'a> From<&'a L>,
|
std::string::String: for<'a> From<&'a L>,
|
||||||
{
|
{
|
||||||
Ok(lines
|
Ok(lines
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|l| {
|
.map(|l| {
|
||||||
let mut line = HighlightedSrcLine::new();
|
let mut line = ISrcLine::new();
|
||||||
line.children.push(HighlightedSrcSegment::RawText(l.into()));
|
line.children.push(ISrcSegment::RawText(l.into()));
|
||||||
line
|
line
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<HighlightedSrcLine>, CustomError>
|
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
|
||||||
where
|
where
|
||||||
L: Borrow<str>,
|
L: Borrow<str>,
|
||||||
{
|
{
|
||||||
@ -152,8 +152,8 @@ where
|
|||||||
.highlight(&config, combined_text.as_bytes(), None, |_| None)
|
.highlight(&config, combined_text.as_bytes(), None, |_| None)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut highlighted_text: Vec<HighlightedSrcLine> = Vec::with_capacity(lines.len());
|
let mut highlighted_text: Vec<ISrcLine> = Vec::with_capacity(lines.len());
|
||||||
let mut current_line = HighlightedSrcLine::new();
|
let mut current_line = ISrcLine::new();
|
||||||
for event in highlights {
|
for event in highlights {
|
||||||
match event.unwrap() {
|
match event.unwrap() {
|
||||||
HighlightEvent::Source { start, end } => {
|
HighlightEvent::Source { start, end } => {
|
||||||
@ -162,28 +162,24 @@ where
|
|||||||
let first_line = &span[..(line_break_index + 1)];
|
let first_line = &span[..(line_break_index + 1)];
|
||||||
current_line
|
current_line
|
||||||
.children
|
.children
|
||||||
.push(HighlightedSrcSegment::RawText(first_line.to_owned()));
|
.push(ISrcSegment::RawText(first_line.to_owned()));
|
||||||
highlighted_text.push(current_line);
|
highlighted_text.push(current_line);
|
||||||
current_line = HighlightedSrcLine::new();
|
current_line = ISrcLine::new();
|
||||||
span = &span[(line_break_index + 1)..];
|
span = &span[(line_break_index + 1)..];
|
||||||
}
|
}
|
||||||
if !span.is_empty() {
|
if !span.is_empty() {
|
||||||
current_line
|
current_line
|
||||||
.children
|
.children
|
||||||
.push(HighlightedSrcSegment::RawText(span.to_owned()));
|
.push(ISrcSegment::RawText(span.to_owned()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
HighlightEvent::HighlightStart(s) => {
|
HighlightEvent::HighlightStart(s) => {
|
||||||
current_line
|
current_line.children.push(ISrcSegment::HighlightStart {
|
||||||
.children
|
name: highlight_names[s.0].to_owned(),
|
||||||
.push(HighlightedSrcSegment::HighlightStart {
|
});
|
||||||
name: highlight_names[s.0].to_owned(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
HighlightEvent::HighlightEnd => {
|
HighlightEvent::HighlightEnd => {
|
||||||
current_line
|
current_line.children.push(ISrcSegment::HighlightEnd);
|
||||||
.children
|
|
||||||
.push(HighlightedSrcSegment::HighlightEnd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user