diff --git a/Cargo.lock b/Cargo.lock index 727edc5..30ee343 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -444,7 +444,7 @@ dependencies = [ [[package]] name = "snekdown" -version = "0.2.0" +version = "0.2.1" dependencies = [ "crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index c6567e7..401a4e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snekdown" -version = "0.2.0" +version = "0.2.1" authors = ["trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/src/format/html.rs b/src/format/html.rs index 61d2997..288a2d9 100644 --- a/src/format/html.rs +++ b/src/format/html.rs @@ -2,6 +2,7 @@ use crate::elements::*; use htmlescape::{encode_attribute, encode_minimal}; use minify::html::minify; use rayon::prelude::*; +use std::cell::RefCell; use syntect::highlighting::ThemeSet; use syntect::html::highlighted_html_for_string; use syntect::parsing::SyntaxSet; @@ -183,31 +184,41 @@ impl ToHtml for Cell { } } +thread_local! {static PS: RefCell = RefCell::new(SyntaxSet::load_defaults_nonewlines());} +thread_local! {static TS: RefCell = RefCell::new(ThemeSet::load_defaults());} + impl ToHtml for CodeBlock { fn to_html(&self) -> String { if self.language.len() > 0 { - let ps = SyntaxSet::load_defaults_nonewlines(); - let ts = ThemeSet::load_defaults(); - if let Some(syntax) = ps.find_syntax_by_token(self.language.as_str()) { - format!( - "
{}
", - encode_attribute(self.language.clone().as_str()), - highlighted_html_for_string( - self.code.as_str(), - &ps, - syntax, - &ts.themes["InspiredGitHub"] + PS.with(|ps_cell| { + let ps = ps_cell.borrow(); + if let Some(syntax) = ps.find_syntax_by_token(self.language.as_str()) { + TS.with(|ts_cell| { + let ts = ts_cell.borrow(); + format!( + "
{}
", + encode_attribute(self.language.clone().as_str()), + highlighted_html_for_string( + encode_minimal(self.code.as_str()).as_str(), + &ps, + syntax, + &ts.themes["InspiredGitHub"] + ) + ) + }) + } else { + format!( + "
{}
", + encode_attribute(self.language.clone().as_str()), + encode_minimal(self.code.as_str()) ) - ) - } else { - format!( - "
{}
", - encode_attribute(self.language.clone().as_str()), - self.code.clone() - ) - } + } + }) } else { - format!("
{}
", self.code.clone()) + format!( + "
{}
", + encode_minimal(self.code.as_str()) + ) } } }