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 crate::error::CustomError; | ||||
| use crate::intermediate::ISrcBlock; | ||||
| use crate::intermediate::ISrcSegment; | ||||
| 
 | ||||
| use super::macros::render; | ||||
| 
 | ||||
| @ -10,15 +11,51 @@ use super::macros::render; | ||||
| #[serde(tag = "type")] | ||||
| #[serde(rename = "src_block")] | ||||
| pub(crate) struct RenderSrcBlock { | ||||
|     lines: Vec<String>, | ||||
|     lines: Vec<RenderSrcLine>, | ||||
|     language: Option<String>, | ||||
|     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, { | ||||
|     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 { | ||||
|         // lines: original.lines.clone(),
 | ||||
|         lines: Vec::new(), | ||||
|         lines, | ||||
|         language: original.language.clone(), | ||||
|         post_blank: original.post_blank, | ||||
|     }) | ||||
|  | ||||
| @ -127,6 +127,7 @@ pub(crate) use regular_link::LinkTarget; | ||||
| pub(crate) use section::ISection; | ||||
| pub(crate) use special_block::ISpecialBlock; | ||||
| pub(crate) use src_block::ISrcBlock; | ||||
| pub(crate) use src_block::ISrcSegment; | ||||
| pub(crate) use statistics_cookie::IStatisticsCookie; | ||||
| pub(crate) use strike_through::IStrikeThrough; | ||||
| pub(crate) use subscript::ISubscript; | ||||
|  | ||||
| @ -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); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander