From 2d522df5a80ae35c7456b0ba9240a5002354eafb Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Mon, 13 Nov 2023 15:11:45 -0800 Subject: [PATCH 1/9] Reintroduce `ruby` platform releases --- Rakefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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] From a2cd884ff34fba65beb69e35ed3585d2b1457ea2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 29 Nov 2023 09:10:02 +0000 Subject: [PATCH 2/9] Bump comrak from 0.19.0 to 0.20.0 Bumps [comrak](https://github.com/kivikakk/comrak) from 0.19.0 to 0.20.0. - [Release notes](https://github.com/kivikakk/comrak/releases) - [Changelog](https://github.com/kivikakk/comrak/blob/main/changelog.txt) - [Commits](https://github.com/kivikakk/comrak/compare/0.19.0...0.20.0) --- updated-dependencies: - dependency-name: comrak dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- Cargo.lock | 4 ++-- ext/commonmarker/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d290626..4cfedc35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,9 +226,9 @@ dependencies = [ [[package]] name = "comrak" -version = "0.19.0" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82c995deda3bfdebd07d0e2af79e9da13e4b1be652b21a746f3f5b24bf0a49ef" +checksum = "9f18e72341e6cdc7489cffb76f993812a14a906db54dedb020044ccc211dcaae" dependencies = [ "clap", "derive_builder", 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] From 3c3929b84249f630f222b897cd4f26d404c2b94b Mon Sep 17 00:00:00 2001 From: "Asherah \"Charlotte\" Connor" Date: Fri, 1 Dec 2023 20:33:03 +1100 Subject: [PATCH 3/9] lib: adjust usage. --- ext/commonmarker/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/commonmarker/src/lib.rs b/ext/commonmarker/src/lib.rs index 989300f4..0a9d5462 100644 --- a/ext/commonmarker/src/lib.rs +++ b/ext/commonmarker/src/lib.rs @@ -125,7 +125,7 @@ fn commonmark_to_html(args: &[Value]) -> Result { format!("theme `{}` does not exist", theme), ) })?; - adapter = SyntectAdapter::new(&theme); + adapter = SyntectAdapter::new(Some(&theme)); syntax_highlighter = Some(&adapter); } From c965fcc1617d4dfb29dc4121330c14ec76470431 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sat, 2 Dec 2023 16:07:54 -0500 Subject: [PATCH 4/9] remove deprecation --- ext/commonmarker/src/plugins/syntax_highlighting.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/commonmarker/src/plugins/syntax_highlighting.rs b/ext/commonmarker/src/plugins/syntax_highlighting.rs index 36f8aa57..960d6851 100644 --- a/ext/commonmarker/src/plugins/syntax_highlighting.rs +++ b/ext/commonmarker/src/plugins/syntax_highlighting.rs @@ -24,7 +24,7 @@ pub fn fetch_syntax_highlighter_theme(value: Value) -> Result()?) + Ok(TryConvert::try_convert(theme)?) } None => { // `syntax_highlighter: { }` From 2d1dffe90a0b513da411b521c2feb5845535221a Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sat, 2 Dec 2023 17:59:13 -0500 Subject: [PATCH 5/9] sure --- .vscode/settings.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 41b3f711..9e9cd3c7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,8 @@ "rust-analyzer.checkOnSave.command": "clippy", "[ruby]": { "editor.defaultFormatter": "Shopify.ruby-lsp" + }, + "[markdown]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" } } From fef7d8786afc8ac595927120c3422735b2eb1980 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sat, 2 Dec 2023 17:59:50 -0500 Subject: [PATCH 6/9] Add the ability to output code using CSS classes --- ext/commonmarker/src/lib.rs | 142 +++++++++--------- .../src/plugins/syntax_highlighting.rs | 31 +++- test/syntax_highlighting_test.rb | 48 +++--- 3 files changed, 119 insertions(+), 102 deletions(-) diff --git a/ext/commonmarker/src/lib.rs b/ext/commonmarker/src/lib.rs index 0a9d5462..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(Some(&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 960d6851..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, magnus::Error> { if value.is_nil() { // `syntax_highlighter: nil` - return Ok(EMPTY_STR.to_string()); + return Ok(None); + } + + let syntax_highlighter_plugin: RHash = match TryConvert::try_convert(value) { + Ok(plugin) => plugin, // `syntax_highlighter: { theme: "" }` + Err(e) => { + // not a hash! + return Err(e); + } + }; + + if syntax_highlighter_plugin.is_nil() || syntax_highlighter_plugin.is_empty() { + return Err(magnus::Error::new( + magnus::exception::type_error(), + "theme cannot be blank hash", + )); } - let syntax_highlighter_plugin: RHash = TryConvert::try_convert(value)?; let theme_key = Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY); match syntax_highlighter_plugin.get(theme_key) { Some(theme) => { if theme.is_nil() { - // `syntax_highlighter: { theme: nil }` - return Ok(EMPTY_STR.to_string()); + return Err(magnus::Error::new( + magnus::exception::type_error(), + "theme cannot be nil", + )); } Ok(TryConvert::try_convert(theme)?) } None => { - // `syntax_highlighter: { }` - Ok(EMPTY_STR.to_string()) + // `syntax_highlighter: { theme: nil }` + Ok(None) } } } diff --git a/test/syntax_highlighting_test.rb b/test/syntax_highlighting_test.rb index eebb9cd7..d178af30 100644 --- a/test/syntax_highlighting_test.rb +++ b/test/syntax_highlighting_test.rb @@ -52,7 +52,7 @@ def hello assert_equal(result, html) end - def test_lack_of_theme_has_no_highlighting + def test_non_hash_value_is_an_error code = <<~CODE ```ruby def hello @@ -61,19 +61,12 @@ def hello ``` CODE - html = Commonmarker.to_html(code, plugins: { syntax_highlighter: {} }) - - result = <<~CODE -
def hello
-        puts "hello"
-      end
-      
- CODE - - assert_equal(result, html) + assert_raises(TypeError) do + Commonmarker.to_html(code, plugins: { syntax_highlighter: "wow!" }) + end end - def test_nil_theme_removes_highlighting + def test_lack_of_theme_is_an_error code = <<~CODE ```ruby def hello @@ -82,19 +75,26 @@ def hello ``` CODE - html = Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: nil } }) + assert_raises(TypeError) do + Commonmarker.to_html(code, plugins: { syntax_highlighter: {} }) + end + end - result = <<~CODE -
def hello
-        puts "hello"
+  def test_nil_theme_is_an_error
+    code = <<~CODE
+      ```ruby
+      def hello
+        puts "hello"
       end
-      
+ ``` CODE - assert_equal(result, html) + assert_raises(TypeError) do + Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: nil } }) + end end - def test_empty_theme_is_no_highlighting + def test_empty_theme_provides_class_highlighting code = <<~CODE ```ruby def hello @@ -106,10 +106,10 @@ def hello html = Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "" } }) result = <<~CODE -
def hello
-        puts "hello"
-      end
-      
+
def hello
+        puts "hello"
+      end
+      
CODE assert_equal(result, html) @@ -152,7 +152,7 @@ def hello CODE assert_raises(ArgumentError) do - Commonmarker.to_html(code, plugins: { syntax_highlighter: { path: "test/fixtures" } }) + Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "WowieZowie", path: "test/fixtures" } }) end end From 82220e0e1bbadfc7732e8d51583501e316350d01 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sat, 2 Dec 2023 17:59:57 -0500 Subject: [PATCH 7/9] Update README for syntax highlighting rules --- README.md | 67 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 939d46d6..d097f049 100644 --- a/README.md +++ b/README.md @@ -105,14 +105,26 @@ providing further niceties. #### Syntax Highlighter Plugin +The library comes with [a set of pre-existing themes](https://docs.rs/syntect/5.0.0/syntect/highlighting/struct.ThemeSet.html#implementations) for highlighting code: + +- `"base16-ocean.dark"` +- `"base16-eighties.dark"` +- `"base16-mocha.dark"` +- `"base16-ocean.light"` +- `"InspiredGitHub"` +- `"Solarized (dark)"` +- `"Solarized (light)"` + ````ruby code = <<~CODE ```ruby def hello - puts "hello" + puts "hello" end + ``` CODE +# pass in a theme name from a pre-existing set puts Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "InspiredGitHub" } }) #

@@ -123,32 +135,49 @@ puts Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Inspire
 # 
```` -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 From 79044af8d1287f08471f8f122d85927069e69627 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sat, 2 Dec 2023 18:17:17 -0500 Subject: [PATCH 8/9] :gem: release 1.0.0.pre12 --- lib/commonmarker/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commonmarker/version.rb b/lib/commonmarker/version.rb index 9b1122b9..2dc90505 100644 --- a/lib/commonmarker/version.rb +++ b/lib/commonmarker/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Commonmarker - VERSION = "1.0.0.pre11" + VERSION = "1.0.0.pre12" end From 404a9d20cd48905c05646b86bfd5cbc4d01bebf4 Mon Sep 17 00:00:00 2001 From: Actions Auto Build Date: Thu, 7 Dec 2023 16:47:18 +0000 Subject: [PATCH 9/9] [auto-docs][skip test]: update changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1372d17b..b83f4b98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## [v1.0.0.pre12] - 07-12-2023 +## What's Changed +* Reintroduce platform releases by @gjtorikian in https://github.com/gjtorikian/commonmarker/pull/260 +* Bump comrak from 0.19.0 to 0.20.0 by @dependabot in https://github.com/gjtorikian/commonmarker/pull/261 +* Classy highlighting by @gjtorikian in https://github.com/gjtorikian/commonmarker/pull/262 + + +**Full Changelog**: https://github.com/gjtorikian/commonmarker/compare/v1.0.0.pre11...v1.0.0.pre12 ## [v1.0.0.pre11] - 12-10-2023 ## What's Changed * Bump syntect from 5.0.0 to 5.1.0 by @dependabot in https://github.com/gjtorikian/commonmarker/pull/248