````
-To disable this plugin, pass `nil`:
+By default, the plugin uses the `"base16-ocean.dark"` theme to syntax highlight code.
+
+To disable this plugin, set the value to `nil`:
+
+````ruby
+code = <<~CODE
+ ```ruby
+ def hello
+ puts "hello"
+ end
+ ```
+CODE
-```ruby
Commonmarker.to_html(code, plugins: { syntax_highlighter: nil })
-# or
-Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: nil } })
-```
-You can also provide a `path` to a directory containing `.tmtheme` files to load:
+#
def hello
+# puts "hello"
+# end
+#
+````
+
+To output CSS classes instead of `style` attributes, set the `theme` key to `""`:
-```ruby
+````ruby
+code = <<~CODE
+ ```ruby
+ def hello
+ puts "hello"
+ end
+CODE
-Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Monokai", path: "./themes" } })
-```
+Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "" } })
-##### Available themes
+#
def # hello
+# puts"hello"
+# end\n
+````
-Here's [a list of themes available by default](https://docs.rs/syntect/5.0.0/syntect/highlighting/struct.ThemeSet.html#implementations):
+To use a custom theme, you can provide a `path` to a directory containing `.tmtheme` files to load:
-- `"base16-ocean.dark"`
-- `"base16-eighties.dark"`
-- `"base16-mocha.dark"`
-- `"base16-ocean.light"`
-- `"InspiredGitHub"`
-- `"Solarized (dark)"`
-- `"Solarized (light)"`
+```ruby
+Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Monokai", path: "./themes" } })
+```
## Output formats
diff --git a/Rakefile b/Rakefile
index 049a64fe..da560e0d 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,8 +1,11 @@
# frozen_string_literal: true
# Gem Spec
-require "bundler"
+require "bundler/gem_tasks"
COMMONMARKER_SPEC = Bundler.load_gemspec("commonmarker.gemspec")
# Packaging
-require "bundler/gem_tasks"
+require "rubygems/package_task"
+gem_path = Gem::PackageTask.new(COMMONMARKER_SPEC).define
+desc "Package the Ruby gem"
+task "package" => [gem_path]
diff --git a/ext/commonmarker/Cargo.toml b/ext/commonmarker/Cargo.toml
index baaa016e..5eeedf6c 100644
--- a/ext/commonmarker/Cargo.toml
+++ b/ext/commonmarker/Cargo.toml
@@ -5,7 +5,7 @@ edition = "2021"
[dependencies]
magnus = "0.6"
-comrak = { version = "0.19", features = ["shortcodes"] }
+comrak = { version = "0.20", features = ["shortcodes"] }
syntect = { version = "5.1", features = ["plist-load"] }
[lib]
diff --git a/ext/commonmarker/src/lib.rs b/ext/commonmarker/src/lib.rs
index 989300f4..2475c7c5 100644
--- a/ext/commonmarker/src/lib.rs
+++ b/ext/commonmarker/src/lib.rs
@@ -18,10 +18,7 @@ use options::iterate_options_hash;
mod plugins;
use plugins::{
- syntax_highlighting::{
- fetch_syntax_highlighter_path, fetch_syntax_highlighter_theme,
- SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME,
- },
+ syntax_highlighting::{fetch_syntax_highlighter_path, fetch_syntax_highlighter_theme},
SYNTAX_HIGHLIGHTER_PLUGIN,
};
@@ -57,78 +54,83 @@ fn commonmark_to_html(args: &[Value]) -> Result {
let theme = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) {
Some(syntax_highlighter_options) => {
- fetch_syntax_highlighter_theme(syntax_highlighter_options)?
- }
- None => SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME.to_string(), // no `syntax_highlighter:` defined
- };
-
- let path = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) {
- Some(syntax_highlighter_options) => {
- fetch_syntax_highlighter_path(syntax_highlighter_options)?
+ match fetch_syntax_highlighter_theme(syntax_highlighter_options) {
+ Ok(theme) => theme,
+ Err(e) => {
+ return Err(e);
+ }
+ }
}
- None => PathBuf::from("".to_string()), // no `syntax_highlighter:` defined
+ None => None, // no `syntax_highlighter:` defined
};
- if !path.eq(&PathBuf::from("".to_string())) && !path.exists() {
- return Err(Error::new(
- exception::arg_error(),
- "path does not exist".to_string(),
- ));
- }
-
- if theme.is_empty() && path.exists() {
- return Err(Error::new(
- exception::arg_error(),
- "`path` also needs `theme` passed into the `syntax_highlighter`",
- ));
- }
- if path.exists() && !path.is_dir() {
- return Err(Error::new(
- exception::arg_error(),
- "`path` needs to be a directory",
- ));
- }
-
- if path.exists() {
- let builder = SyntectAdapterBuilder::new();
- let mut ts = ThemeSet::load_defaults();
-
- match ts.add_from_folder(&path) {
- Ok(_) => {}
- Err(e) => {
- return Err(Error::new(
- exception::arg_error(),
- format!("failed to load theme set from path: {e}"),
- ));
+ match theme {
+ None => syntax_highlighter = None,
+ Some(theme) => {
+ if theme.is_empty() {
+ // no theme? uss css classes
+ adapter = SyntectAdapter::new(None);
+ syntax_highlighter = Some(&adapter);
+ } else {
+ let path = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) {
+ Some(syntax_highlighter_options) => {
+ fetch_syntax_highlighter_path(syntax_highlighter_options)?
+ }
+ None => PathBuf::from("".to_string()), // no `syntax_highlighter:` defined
+ };
+
+ if path.exists() {
+ if !path.is_dir() {
+ return Err(Error::new(
+ exception::arg_error(),
+ "`path` needs to be a directory",
+ ));
+ }
+
+ let builder = SyntectAdapterBuilder::new();
+ let mut ts = ThemeSet::load_defaults();
+
+ match ts.add_from_folder(&path) {
+ Ok(_) => {}
+ Err(e) => {
+ return Err(Error::new(
+ exception::arg_error(),
+ format!("failed to load theme set from path: {e}"),
+ ));
+ }
+ }
+
+ // check if the theme exists in the dir
+ match ts.themes.get(&theme) {
+ Some(theme) => theme,
+ None => {
+ return Err(Error::new(
+ exception::arg_error(),
+ format!("theme `{}` does not exist", theme),
+ ));
+ }
+ };
+
+ adapter = builder.theme_set(ts).theme(&theme).build();
+
+ syntax_highlighter = Some(&adapter);
+ } else {
+ // no path? default theme lookup
+ ThemeSet::load_defaults()
+ .themes
+ .get(&theme)
+ .ok_or_else(|| {
+ Error::new(
+ exception::arg_error(),
+ format!("theme `{}` does not exist", theme),
+ )
+ })?;
+ adapter = SyntectAdapter::new(Some(&theme));
+ syntax_highlighter = Some(&adapter);
+ }
}
}
-
- ts.themes.get(&theme).ok_or_else(|| {
- Error::new(
- exception::arg_error(),
- format!("theme `{}` does not exist", theme),
- )
- })?;
-
- adapter = builder.theme_set(ts).theme(&theme).build();
-
- syntax_highlighter = Some(&adapter);
- } else if theme.is_empty() || theme == "none" {
- syntax_highlighter = None;
- } else {
- ThemeSet::load_defaults()
- .themes
- .get(&theme)
- .ok_or_else(|| {
- Error::new(
- exception::arg_error(),
- format!("theme `{}` does not exist", theme),
- )
- })?;
- adapter = SyntectAdapter::new(&theme);
- syntax_highlighter = Some(&adapter);
}
-
comrak_plugins.render.codefence_syntax_highlighter = syntax_highlighter;
Ok(markdown_to_html_with_plugins(
diff --git a/ext/commonmarker/src/plugins/syntax_highlighting.rs b/ext/commonmarker/src/plugins/syntax_highlighting.rs
index 36f8aa57..6e466d20 100644
--- a/ext/commonmarker/src/plugins/syntax_highlighting.rs
+++ b/ext/commonmarker/src/plugins/syntax_highlighting.rs
@@ -7,28 +7,43 @@ use crate::EMPTY_STR;
pub const SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY: &str = "theme";
pub const SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY: &str = "path";
-pub const SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME: &str = "base16-ocean.dark";
-pub fn fetch_syntax_highlighter_theme(value: Value) -> Result {
+pub fn fetch_syntax_highlighter_theme(value: Value) -> Result