Skip to content

Commit

Permalink
syntect: minor cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Nov 29, 2023
1 parent 985e49a commit 4b9d838
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
9 changes: 7 additions & 2 deletions examples/syntect.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//! This example shows how to use the bundled syntect plugin.
use comrak::plugins::syntect::SyntectAdapter;
use comrak::plugins::syntect::SyntectAdapterBuilder;
use comrak::{markdown_to_html_with_plugins, Options, Plugins};

fn main() {
let adapter = SyntectAdapter::new(Some("base16-ocean.dark"));
run_with(SyntectAdapterBuilder::new().theme("base16-ocean.dark"));
run_with(SyntectAdapterBuilder::new().css());
}

fn run_with(builder: SyntectAdapterBuilder) {
let adapter = builder.build();
let options = Options::default();
let mut plugins = Plugins::default();

Expand Down
52 changes: 31 additions & 21 deletions src/plugins/syntect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@ pub struct SyntectAdapter {

impl SyntectAdapter {
/// Construct a new `SyntectAdapter` object and set the syntax highlighting theme.
/// If None is specified, apply CSS classes instead.
pub fn new(theme: Option<&str>) -> Self {
SyntectAdapter {
theme: match theme {
Some(t) => Some(t.into()),
None => None,
},
theme: theme.map(String::from),
syntax_set: SyntaxSet::load_defaults_newlines(),
theme_set: ThemeSet::load_defaults(),
}
}

fn highlight_html(&self, code: &str, syntax: &SyntaxReference) -> Result<String, Error> {
let mut output = String::new();

match &self.theme {
Some(theme) => {
// syntect::html::highlighted_html_for_string, without the opening/closing <pre>.
Expand All @@ -45,6 +41,7 @@ impl SyntectAdapter {

let bg = theme.settings.background.unwrap_or(Color::WHITE);

let mut output = String::new();
for line in LinesWithEndings::from(code) {
let regions = highlighter.highlight_line(line, &self.syntax_set)?;
append_highlighted_html_for_styled_line(
Expand All @@ -53,23 +50,21 @@ impl SyntectAdapter {
&mut output,
)?;
}
Ok(output)
}
None => {
// fall back to HTML classes
// fall back to HTML classes.
let mut html_generator = ClassedHTMLGenerator::new_with_class_style(
syntax,
&self.syntax_set,
ClassStyle::Spaced,
);
for line in LinesWithEndings::from(code) {
html_generator
.parse_html_for_line_which_includes_newline(line)
.unwrap();
html_generator.parse_html_for_line_which_includes_newline(line)?;
}
output = html_generator.finalize();
Ok(html_generator.finalize())
}
}
Ok(output)
}
}

Expand Down Expand Up @@ -121,9 +116,8 @@ impl SyntaxHighlighterAdapter for SyntectAdapter {
html::write_opening_tag(output, "pre", pre_attributes.iter_mut())
}
None => {
let mut attributes: HashMap<String, String> = HashMap::new();
attributes.insert(String::from("class"), String::from("syntax-highlighting"));

let mut attributes: HashMap<&str, &str> = HashMap::new();
attributes.insert("class", "syntax-highlighting");
html::write_opening_tag(output, "pre", attributes)
}
}
Expand Down Expand Up @@ -186,7 +180,7 @@ impl<'a> Iterator for SyntectPreAttributesIter<'a> {
}
}

#[derive(Debug, Default)]
#[derive(Debug)]
/// A builder for [`SyntectAdapter`].
///
/// Allows customization of `Theme`, [`ThemeSet`], and [`SyntaxSet`].
Expand All @@ -196,25 +190,41 @@ pub struct SyntectAdapterBuilder {
theme_set: Option<ThemeSet>,
}

impl Default for SyntectAdapterBuilder {
fn default() -> Self {
SyntectAdapterBuilder {
theme: Some("InspiredGitHub".into()),
syntax_set: None,
theme_set: None,
}
}
}

impl SyntectAdapterBuilder {
/// Creates a new empty [`SyntectAdapterBuilder`]
/// Create a new empty [`SyntectAdapterBuilder`].
pub fn new() -> Self {
Default::default()
}

/// Sets the theme
/// Set the theme.
pub fn theme(mut self, s: &str) -> Self {
self.theme.replace(s.into());
self
}

/// Sets the syntax set
/// Uses CSS classes instead of a Syntect theme.
pub fn css(mut self) -> Self {
self.theme = None;
self
}

/// Set the syntax set.
pub fn syntax_set(mut self, s: SyntaxSet) -> Self {
self.syntax_set.replace(s);
self
}

/// Sets the theme set
/// Set the theme set.
pub fn theme_set(mut self, s: ThemeSet) -> Self {
self.theme_set.replace(s);
self
Expand All @@ -226,7 +236,7 @@ impl SyntectAdapterBuilder {
/// - `theme_set`: [`ThemeSet::load_defaults()`]
pub fn build(self) -> SyntectAdapter {
SyntectAdapter {
theme: Some(self.theme.unwrap_or_else(|| "InspiredGitHub".into())),
theme: self.theme,
syntax_set: self
.syntax_set
.unwrap_or_else(SyntaxSet::load_defaults_newlines),
Expand Down

0 comments on commit 4b9d838

Please sign in to comment.