From a5809b29a67f629537f77d448a53b4089ab5d272 Mon Sep 17 00:00:00 2001 From: PoiScript Date: Wed, 9 Oct 2019 11:03:17 +0800 Subject: [PATCH] feat(export): able to specify syntect theme and background --- orgize/src/export/html.rs | 89 ++++++++++++++++++++++++++++++--------- orgize/src/lib.rs | 2 + 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/orgize/src/export/html.rs b/orgize/src/export/html.rs index 4ed3587..d597263 100644 --- a/orgize/src/export/html.rs +++ b/orgize/src/export/html.rs @@ -199,13 +199,14 @@ pub trait HtmlHandler>: Default { } } +/// Default Html Handler #[derive(Default)] pub struct DefaultHtmlHandler; impl HtmlHandler for DefaultHtmlHandler {} #[cfg(feature = "syntect")] -pub mod syntect_feature { +mod syntect_handler { use super::*; use std::marker::PhantomData; @@ -216,31 +217,68 @@ pub mod syntect_feature { parsing::SyntaxSet, }; + /// Syntect Html Handler + /// + /// Simple Usage: + /// + /// ```rust + /// use orgize::Org; + /// use orgize::export::html::{DefaultHtmlHandler, SyntectHtmlHandler}; + /// + /// let mut handler = SyntectHtmlHandler::new(DefaultHtmlHandler); + /// let org = Org::parse("src_rust{println!(\"Hello\")}"); + /// + /// let mut vec = vec![]; + /// + /// org.html_with_handler(&mut vec, &mut handler).unwrap(); + /// ``` + /// + /// Customize: + /// + /// ```rust,no_run + /// // orgize has re-exported the whole syntect crate + /// use orgize::syntect::parsing::SyntaxSet; + /// use orgize::export::html::{DefaultHtmlHandler, SyntectHtmlHandler}; + /// + /// let mut handler = SyntectHtmlHandler { + /// syntax_set: { + /// let set = SyntaxSet::load_defaults_newlines(); + /// let mut builder = set.into_builder(); + /// // add extra language syntax + /// builder.add_from_folder("path/to/syntax/dir", true).unwrap(); + /// builder.build() + /// }, + /// // specify theme + /// theme: String::from("Solarized (dark)"), + /// inner: DefaultHtmlHandler, + /// ..Default::default() + /// }; + /// + /// // Make sure to check if theme presents or it will painc at runtime + /// if handler.theme_set.themes.contains_key("dont-exists") { + /// + /// } + /// ``` pub struct SyntectHtmlHandler, H: HtmlHandler> { + /// syntax set, default is `SyntaxSet::load_defaults_newlines()` pub syntax_set: SyntaxSet, + /// theme set, default is `ThemeSet::load_defaults()` pub theme_set: ThemeSet, + /// theme used for highlighting, default is `"InspiredGitHub"` + pub theme: String, + /// inner html handler pub inner: H, - error_type: PhantomData, - } - - impl Default for SyntectHtmlHandler { - fn default() -> Self { - SyntectHtmlHandler { - syntax_set: SyntaxSet::load_defaults_newlines(), - theme_set: ThemeSet::load_defaults(), - inner: DefaultHtmlHandler, - error_type: PhantomData, - } - } + /// background color, default is `IncludeBackground::No` + pub background: IncludeBackground, + /// handler error type + pub error_type: PhantomData, } impl, H: HtmlHandler> SyntectHtmlHandler { pub fn new(inner: H) -> Self { SyntectHtmlHandler { - syntax_set: SyntaxSet::load_defaults_newlines(), - theme_set: ThemeSet::load_defaults(), inner, - error_type: PhantomData, + ..Default::default() } } @@ -249,10 +287,23 @@ pub mod syntect_feature { language .and_then(|lang| self.syntax_set.find_syntax_by_token(lang)) .unwrap_or_else(|| self.syntax_set.find_syntax_plain_text()), - &self.theme_set.themes["InspiredGitHub"], + &self.theme_set.themes[&self.theme], ); let regions = highlighter.highlight(content, &self.syntax_set); - styled_line_to_highlighted_html(®ions[..], IncludeBackground::No) + styled_line_to_highlighted_html(®ions[..], self.background) + } + } + + impl, H: HtmlHandler> Default for SyntectHtmlHandler { + fn default() -> Self { + SyntectHtmlHandler { + syntax_set: SyntaxSet::load_defaults_newlines(), + theme_set: ThemeSet::load_defaults(), + theme: String::from("InspiredGitHub"), + inner: H::default(), + background: IncludeBackground::No, + error_type: PhantomData, + } } } @@ -294,4 +345,4 @@ pub mod syntect_feature { } #[cfg(feature = "syntect")] -pub use syntect_feature::*; +pub use syntect_handler::SyntectHtmlHandler; diff --git a/orgize/src/lib.rs b/orgize/src/lib.rs index c6fc34c..09a7660 100644 --- a/orgize/src/lib.rs +++ b/orgize/src/lib.rs @@ -231,6 +231,8 @@ mod error; // Re-export of the indextree crate. pub use indextree; +#[cfg(feature = "syntect")] +pub use syntect; pub use config::ParseConfig; pub use elements::Element;