From 4f66907d08a8ed18b41e70188b112d7c915aa0bc Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Tue, 14 Dec 2021 14:24:02 +0100 Subject: Add Code Block syntax highlighting --- src/eval/mod.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 105 insertions(+), 6 deletions(-) (limited to 'src/eval/mod.rs') diff --git a/src/eval/mod.rs b/src/eval/mod.rs index c16c2208..2759e0d5 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -30,21 +30,36 @@ use std::collections::HashMap; use std::io; use std::mem; use std::path::PathBuf; +use std::sync::Mutex; + +use once_cell::sync::Lazy; +use syntect::easy::HighlightLines; +use syntect::highlighting::{FontStyle, Highlighter, Style as SynStyle, Theme, ThemeSet}; +use syntect::parsing::SyntaxSet; use unicode_segmentation::UnicodeSegmentation; use crate::diag::{At, Error, StrResult, Trace, Tracepoint, TypResult}; -use crate::geom::{Angle, Fractional, Length, Relative}; +use crate::geom::{Angle, Fractional, Length, Paint, Relative, RgbaColor}; use crate::image::ImageStore; use crate::layout::RootNode; -use crate::library::{self, TextNode}; +use crate::library::{self, Decoration, TextNode}; use crate::loading::Loader; +use crate::parse; use crate::source::{SourceId, SourceStore}; +use crate::syntax; use crate::syntax::ast::*; -use crate::syntax::{Span, Spanned}; +use crate::syntax::{RedNode, Span, Spanned}; use crate::util::{EcoString, RefMutExt}; use crate::Context; +static THEME: Lazy> = Lazy::new(|| { + Mutex::new(ThemeSet::load_defaults().themes.remove("InspiredGitHub").unwrap()) +}); + +static SYNTAXES: Lazy> = + Lazy::new(|| Mutex::new(SyntaxSet::load_defaults_newlines())); + /// An evaluated module, ready for importing or conversion to a root layout /// tree. #[derive(Debug, Default, Clone)] @@ -209,15 +224,99 @@ impl Eval for RawNode { type Output = Node; fn eval(&self, _: &mut EvalContext) -> TypResult { - let text = Node::Text(self.text.clone()).monospaced(); + let code = self.highlighted(); Ok(if self.block { - Node::Block(text.into_block()) + Node::Block(code.into_block()) } else { - text + code }) } } +impl RawNode { + /// Styled node for a code block, with optional syntax highlighting. + pub fn highlighted(&self) -> Node { + let mut sequence: Vec> = vec![]; + let syntaxes = SYNTAXES.lock().unwrap(); + + let syntax = if let Some(syntax) = self + .lang + .as_ref() + .and_then(|token| syntaxes.find_syntax_by_token(&token)) + { + Some(syntax) + } else if matches!( + self.lang.as_ref().map(|s| s.to_ascii_lowercase()).as_deref(), + Some("typ" | "typst") + ) { + None + } else { + return Node::Text(self.text.clone()).monospaced(); + }; + + let theme = THEME.lock().unwrap(); + let foreground = theme + .settings + .foreground + .map(RgbaColor::from) + .unwrap_or(RgbaColor::BLACK) + .into(); + + match syntax { + Some(syntax) => { + let mut highlighter = HighlightLines::new(syntax, &theme); + for (i, line) in self.text.lines().enumerate() { + if i != 0 { + sequence.push(Styled::bare(Node::Linebreak)); + } + + for (style, line) in highlighter.highlight(line, &syntaxes) { + sequence.push(Self::styled_line(style, line, foreground)); + } + } + } + None => { + let red_tree = + RedNode::from_root(parse::parse(&self.text), SourceId::from_raw(0)); + let highlighter = Highlighter::new(&theme); + + syntax::highlight_syntect( + red_tree.as_ref(), + &self.text, + &highlighter, + &mut |style, line| { + sequence.push(Self::styled_line(style, line, foreground)); + }, + ) + } + } + + Node::Sequence(sequence).monospaced() + } + + fn styled_line(style: SynStyle, line: &str, foreground: Paint) -> Styled { + let paint = style.foreground.into(); + let text_node = Node::Text(line.into()); + let mut style_map = StyleMap::new(); + + if paint != foreground { + style_map.set(TextNode::FILL, paint); + } + + if style.font_style.contains(FontStyle::BOLD) { + style_map.set(TextNode::STRONG, true); + } + if style.font_style.contains(FontStyle::ITALIC) { + style_map.set(TextNode::EMPH, true); + } + if style.font_style.contains(FontStyle::UNDERLINE) { + style_map.set(TextNode::LINES, vec![Decoration::underline()]); + } + + Styled::new(text_node, style_map) + } +} + impl Eval for MathNode { type Output = Node; -- cgit v1.2.3 From c183ed3c15110e11bffd40fad5c5fdfb4d1a5814 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 27 Jan 2022 23:07:10 +0100 Subject: Mutex comes from tex and we don't want any --- src/eval/mod.rs | 59 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'src/eval/mod.rs') diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 2759e0d5..58400f5a 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -30,7 +30,6 @@ use std::collections::HashMap; use std::io; use std::mem; use std::path::PathBuf; -use std::sync::Mutex; use once_cell::sync::Lazy; use syntect::easy::HighlightLines; @@ -43,7 +42,7 @@ use crate::diag::{At, Error, StrResult, Trace, Tracepoint, TypResult}; use crate::geom::{Angle, Fractional, Length, Paint, Relative, RgbaColor}; use crate::image::ImageStore; use crate::layout::RootNode; -use crate::library::{self, Decoration, TextNode}; +use crate::library::{self, DecoLine, TextNode}; use crate::loading::Loader; use crate::parse; use crate::source::{SourceId, SourceStore}; @@ -53,12 +52,10 @@ use crate::syntax::{RedNode, Span, Spanned}; use crate::util::{EcoString, RefMutExt}; use crate::Context; -static THEME: Lazy> = Lazy::new(|| { - Mutex::new(ThemeSet::load_defaults().themes.remove("InspiredGitHub").unwrap()) -}); +static THEME: Lazy = + Lazy::new(|| ThemeSet::load_defaults().themes.remove("InspiredGitHub").unwrap()); -static SYNTAXES: Lazy> = - Lazy::new(|| Mutex::new(SyntaxSet::load_defaults_newlines())); +static SYNTAXES: Lazy = Lazy::new(|| SyntaxSet::load_defaults_newlines()); /// An evaluated module, ready for importing or conversion to a root layout /// tree. @@ -237,12 +234,11 @@ impl RawNode { /// Styled node for a code block, with optional syntax highlighting. pub fn highlighted(&self) -> Node { let mut sequence: Vec> = vec![]; - let syntaxes = SYNTAXES.lock().unwrap(); let syntax = if let Some(syntax) = self .lang .as_ref() - .and_then(|token| syntaxes.find_syntax_by_token(&token)) + .and_then(|token| SYNTAXES.find_syntax_by_token(&token)) { Some(syntax) } else if matches!( @@ -254,8 +250,7 @@ impl RawNode { return Node::Text(self.text.clone()).monospaced(); }; - let theme = THEME.lock().unwrap(); - let foreground = theme + let foreground = THEME .settings .foreground .map(RgbaColor::from) @@ -264,28 +259,31 @@ impl RawNode { match syntax { Some(syntax) => { - let mut highlighter = HighlightLines::new(syntax, &theme); + let mut highlighter = HighlightLines::new(syntax, &THEME); for (i, line) in self.text.lines().enumerate() { if i != 0 { sequence.push(Styled::bare(Node::Linebreak)); } - for (style, line) in highlighter.highlight(line, &syntaxes) { - sequence.push(Self::styled_line(style, line, foreground)); + for (style, line) in highlighter.highlight(line, &SYNTAXES) { + sequence.push(Self::styled_piece(style, line, foreground)); } } } None => { - let red_tree = - RedNode::from_root(parse::parse(&self.text), SourceId::from_raw(0)); - let highlighter = Highlighter::new(&theme); + let green = parse::parse(&self.text); + let red = RedNode::from_root(green, SourceId::from_raw(0)); + let highlighter = Highlighter::new(&THEME); syntax::highlight_syntect( - red_tree.as_ref(), - &self.text, + red.as_ref(), &highlighter, - &mut |style, line| { - sequence.push(Self::styled_line(style, line, foreground)); + &mut |range, style| { + sequence.push(Self::styled_piece( + style, + &self.text[range], + foreground, + )); }, ) } @@ -294,26 +292,29 @@ impl RawNode { Node::Sequence(sequence).monospaced() } - fn styled_line(style: SynStyle, line: &str, foreground: Paint) -> Styled { + fn styled_piece(style: SynStyle, piece: &str, foreground: Paint) -> Styled { let paint = style.foreground.into(); - let text_node = Node::Text(line.into()); - let mut style_map = StyleMap::new(); + let node = Node::Text(piece.into()); + + let mut styles = StyleMap::new(); if paint != foreground { - style_map.set(TextNode::FILL, paint); + styles.set(TextNode::FILL, paint); } if style.font_style.contains(FontStyle::BOLD) { - style_map.set(TextNode::STRONG, true); + styles.set(TextNode::STRONG, true); } + if style.font_style.contains(FontStyle::ITALIC) { - style_map.set(TextNode::EMPH, true); + styles.set(TextNode::EMPH, true); } + if style.font_style.contains(FontStyle::UNDERLINE) { - style_map.set(TextNode::LINES, vec![Decoration::underline()]); + styles.set(TextNode::LINES, vec![DecoLine::Underline.into()]); } - Styled::new(text_node, style_map) + Styled::new(node, styles) } } -- cgit v1.2.3