commonmarker 1.0.0.pre11 → 1.0.0.pre12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8625202aca416574d42bd5ca2dea12d42c3d91708bda01a9bbcfbbfc21a18cae
4
- data.tar.gz: 51b1a7df2e9249f52e6ca7298035e64e3e758ecf0c71b872709c566752e7941f
3
+ metadata.gz: 66ae092bc1da22502edf2dccb8709f14c87128503eedf76e1c17aa348686dfe9
4
+ data.tar.gz: 20cebb526aa41fb1f1f98ad4958e943e5c5e6dcefcea0620732ac01553a0fd2e
5
5
  SHA512:
6
- metadata.gz: baed4aa07a0f802b1ddf7ed20732983d29f1703a63d6896fa48d9352ee73aa3476387085b78b02ff69a6d4be2ae8d5cda7bf22d25b64e69885306a4a56d22c40
7
- data.tar.gz: ad2af728905d0e1a80332f34142b2ab59b451dae278021e444c501f345a2d65485f2ee13063b7211cdc019a54e6107f74b04948cc61205d1e94f13855282b1dc
6
+ metadata.gz: 31dad33f45ec8106299d7089ff1e72971b202ab853b83f3fcb884ac640f760941575c6f7494dd13e3d1acf6a449cfc48a1d2cb0b3123b149c996824ce771142b
7
+ data.tar.gz: 28e6ae936f089c690fc1b18bc03ec8a1ba4e247ef5e55ecd46e37eebf2223bd8a298e096a81e46a9968ab467196ae9d5d952b5586254c2dbcbabcbd745834e84
data/Cargo.lock CHANGED
@@ -226,9 +226,9 @@ dependencies = [
226
226
 
227
227
  [[package]]
228
228
  name = "comrak"
229
- version = "0.19.0"
229
+ version = "0.20.0"
230
230
  source = "registry+https://github.com/rust-lang/crates.io-index"
231
- checksum = "82c995deda3bfdebd07d0e2af79e9da13e4b1be652b21a746f3f5b24bf0a49ef"
231
+ checksum = "9f18e72341e6cdc7489cffb76f993812a14a906db54dedb020044ccc211dcaae"
232
232
  dependencies = [
233
233
  "clap",
234
234
  "derive_builder",
data/README.md CHANGED
@@ -105,14 +105,26 @@ providing further niceties.
105
105
 
106
106
  #### Syntax Highlighter Plugin
107
107
 
108
+ 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:
109
+
110
+ - `"base16-ocean.dark"`
111
+ - `"base16-eighties.dark"`
112
+ - `"base16-mocha.dark"`
113
+ - `"base16-ocean.light"`
114
+ - `"InspiredGitHub"`
115
+ - `"Solarized (dark)"`
116
+ - `"Solarized (light)"`
117
+
108
118
  ````ruby
109
119
  code = <<~CODE
110
120
  ```ruby
111
121
  def hello
112
- puts "hello"
122
+ puts "hello"
113
123
  end
124
+ ```
114
125
  CODE
115
126
 
127
+ # pass in a theme name from a pre-existing set
116
128
  puts Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "InspiredGitHub" } })
117
129
 
118
130
  # <pre style="background-color:#ffffff;" lang="ruby"><code>
@@ -123,32 +135,49 @@ puts Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Inspire
123
135
  # </code></pre>
124
136
  ````
125
137
 
126
- To disable this plugin, pass `nil`:
138
+ By default, the plugin uses the `"base16-ocean.dark"` theme to syntax highlight code.
139
+
140
+ To disable this plugin, set the value to `nil`:
141
+
142
+ ````ruby
143
+ code = <<~CODE
144
+ ```ruby
145
+ def hello
146
+ puts "hello"
147
+ end
148
+ ```
149
+ CODE
127
150
 
128
- ```ruby
129
151
  Commonmarker.to_html(code, plugins: { syntax_highlighter: nil })
130
- # or
131
- Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: nil } })
132
- ```
133
152
 
134
- You can also provide a `path` to a directory containing `.tmtheme` files to load:
153
+ # <pre lang="ruby"><code>def hello
154
+ # puts &quot;hello&quot;
155
+ # end
156
+ # </code></pre>
157
+ ````
158
+
159
+ To output CSS classes instead of `style` attributes, set the `theme` key to `""`:
135
160
 
136
- ```ruby
161
+ ````ruby
162
+ code = <<~CODE
163
+ ```ruby
164
+ def hello
165
+ puts "hello"
166
+ end
167
+ CODE
137
168
 
138
- Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Monokai", path: "./themes" } })
139
- ```
169
+ Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "" } })
140
170
 
141
- ##### Available themes
171
+ # <pre class="syntax-highlighting"><code><span class="source ruby"><span class="meta function ruby"><span class="keyword control def ruby">def</span></span><span class="meta function ruby"> # <span class="entity name function ruby">hello</span></span>
172
+ # <span class="support function builtin ruby">puts</span> <span class="string quoted double ruby"><span class="punctuation definition string begin ruby">&quot;</span>hello<span class="punctuation definition string end ruby">&quot;</span></span>
173
+ # <span class="keyword control ruby">end</span>\n</span></code></pre>
174
+ ````
142
175
 
143
- Here's [a list of themes available by default](https://docs.rs/syntect/5.0.0/syntect/highlighting/struct.ThemeSet.html#implementations):
176
+ To use a custom theme, you can provide a `path` to a directory containing `.tmtheme` files to load:
144
177
 
145
- - `"base16-ocean.dark"`
146
- - `"base16-eighties.dark"`
147
- - `"base16-mocha.dark"`
148
- - `"base16-ocean.light"`
149
- - `"InspiredGitHub"`
150
- - `"Solarized (dark)"`
151
- - `"Solarized (light)"`
178
+ ```ruby
179
+ Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Monokai", path: "./themes" } })
180
+ ```
152
181
 
153
182
  ## Output formats
154
183
 
@@ -5,7 +5,7 @@ edition = "2021"
5
5
 
6
6
  [dependencies]
7
7
  magnus = "0.6"
8
- comrak = { version = "0.19", features = ["shortcodes"] }
8
+ comrak = { version = "0.20", features = ["shortcodes"] }
9
9
  syntect = { version = "5.1", features = ["plist-load"] }
10
10
 
11
11
  [lib]
@@ -18,10 +18,7 @@ use options::iterate_options_hash;
18
18
 
19
19
  mod plugins;
20
20
  use plugins::{
21
- syntax_highlighting::{
22
- fetch_syntax_highlighter_path, fetch_syntax_highlighter_theme,
23
- SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME,
24
- },
21
+ syntax_highlighting::{fetch_syntax_highlighter_path, fetch_syntax_highlighter_theme},
25
22
  SYNTAX_HIGHLIGHTER_PLUGIN,
26
23
  };
27
24
 
@@ -57,78 +54,83 @@ fn commonmark_to_html(args: &[Value]) -> Result<String, magnus::Error> {
57
54
 
58
55
  let theme = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) {
59
56
  Some(syntax_highlighter_options) => {
60
- fetch_syntax_highlighter_theme(syntax_highlighter_options)?
61
- }
62
- None => SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME.to_string(), // no `syntax_highlighter:` defined
63
- };
64
-
65
- let path = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) {
66
- Some(syntax_highlighter_options) => {
67
- fetch_syntax_highlighter_path(syntax_highlighter_options)?
57
+ match fetch_syntax_highlighter_theme(syntax_highlighter_options) {
58
+ Ok(theme) => theme,
59
+ Err(e) => {
60
+ return Err(e);
61
+ }
62
+ }
68
63
  }
69
- None => PathBuf::from("".to_string()), // no `syntax_highlighter:` defined
64
+ None => None, // no `syntax_highlighter:` defined
70
65
  };
71
66
 
72
- if !path.eq(&PathBuf::from("".to_string())) && !path.exists() {
73
- return Err(Error::new(
74
- exception::arg_error(),
75
- "path does not exist".to_string(),
76
- ));
77
- }
78
-
79
- if theme.is_empty() && path.exists() {
80
- return Err(Error::new(
81
- exception::arg_error(),
82
- "`path` also needs `theme` passed into the `syntax_highlighter`",
83
- ));
84
- }
85
- if path.exists() && !path.is_dir() {
86
- return Err(Error::new(
87
- exception::arg_error(),
88
- "`path` needs to be a directory",
89
- ));
90
- }
91
-
92
- if path.exists() {
93
- let builder = SyntectAdapterBuilder::new();
94
- let mut ts = ThemeSet::load_defaults();
95
-
96
- match ts.add_from_folder(&path) {
97
- Ok(_) => {}
98
- Err(e) => {
99
- return Err(Error::new(
100
- exception::arg_error(),
101
- format!("failed to load theme set from path: {e}"),
102
- ));
67
+ match theme {
68
+ None => syntax_highlighter = None,
69
+ Some(theme) => {
70
+ if theme.is_empty() {
71
+ // no theme? uss css classes
72
+ adapter = SyntectAdapter::new(None);
73
+ syntax_highlighter = Some(&adapter);
74
+ } else {
75
+ let path = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) {
76
+ Some(syntax_highlighter_options) => {
77
+ fetch_syntax_highlighter_path(syntax_highlighter_options)?
78
+ }
79
+ None => PathBuf::from("".to_string()), // no `syntax_highlighter:` defined
80
+ };
81
+
82
+ if path.exists() {
83
+ if !path.is_dir() {
84
+ return Err(Error::new(
85
+ exception::arg_error(),
86
+ "`path` needs to be a directory",
87
+ ));
88
+ }
89
+
90
+ let builder = SyntectAdapterBuilder::new();
91
+ let mut ts = ThemeSet::load_defaults();
92
+
93
+ match ts.add_from_folder(&path) {
94
+ Ok(_) => {}
95
+ Err(e) => {
96
+ return Err(Error::new(
97
+ exception::arg_error(),
98
+ format!("failed to load theme set from path: {e}"),
99
+ ));
100
+ }
101
+ }
102
+
103
+ // check if the theme exists in the dir
104
+ match ts.themes.get(&theme) {
105
+ Some(theme) => theme,
106
+ None => {
107
+ return Err(Error::new(
108
+ exception::arg_error(),
109
+ format!("theme `{}` does not exist", theme),
110
+ ));
111
+ }
112
+ };
113
+
114
+ adapter = builder.theme_set(ts).theme(&theme).build();
115
+
116
+ syntax_highlighter = Some(&adapter);
117
+ } else {
118
+ // no path? default theme lookup
119
+ ThemeSet::load_defaults()
120
+ .themes
121
+ .get(&theme)
122
+ .ok_or_else(|| {
123
+ Error::new(
124
+ exception::arg_error(),
125
+ format!("theme `{}` does not exist", theme),
126
+ )
127
+ })?;
128
+ adapter = SyntectAdapter::new(Some(&theme));
129
+ syntax_highlighter = Some(&adapter);
130
+ }
103
131
  }
104
132
  }
105
-
106
- ts.themes.get(&theme).ok_or_else(|| {
107
- Error::new(
108
- exception::arg_error(),
109
- format!("theme `{}` does not exist", theme),
110
- )
111
- })?;
112
-
113
- adapter = builder.theme_set(ts).theme(&theme).build();
114
-
115
- syntax_highlighter = Some(&adapter);
116
- } else if theme.is_empty() || theme == "none" {
117
- syntax_highlighter = None;
118
- } else {
119
- ThemeSet::load_defaults()
120
- .themes
121
- .get(&theme)
122
- .ok_or_else(|| {
123
- Error::new(
124
- exception::arg_error(),
125
- format!("theme `{}` does not exist", theme),
126
- )
127
- })?;
128
- adapter = SyntectAdapter::new(&theme);
129
- syntax_highlighter = Some(&adapter);
130
133
  }
131
-
132
134
  comrak_plugins.render.codefence_syntax_highlighter = syntax_highlighter;
133
135
 
134
136
  Ok(markdown_to_html_with_plugins(
@@ -7,28 +7,43 @@ use crate::EMPTY_STR;
7
7
 
8
8
  pub const SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY: &str = "theme";
9
9
  pub const SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY: &str = "path";
10
- pub const SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME: &str = "base16-ocean.dark";
11
10
 
12
- pub fn fetch_syntax_highlighter_theme(value: Value) -> Result<String, magnus::Error> {
11
+ pub fn fetch_syntax_highlighter_theme(value: Value) -> Result<Option<String>, magnus::Error> {
13
12
  if value.is_nil() {
14
13
  // `syntax_highlighter: nil`
15
- return Ok(EMPTY_STR.to_string());
14
+ return Ok(None);
15
+ }
16
+
17
+ let syntax_highlighter_plugin: RHash = match TryConvert::try_convert(value) {
18
+ Ok(plugin) => plugin, // `syntax_highlighter: { theme: "<something>" }`
19
+ Err(e) => {
20
+ // not a hash!
21
+ return Err(e);
22
+ }
23
+ };
24
+
25
+ if syntax_highlighter_plugin.is_nil() || syntax_highlighter_plugin.is_empty() {
26
+ return Err(magnus::Error::new(
27
+ magnus::exception::type_error(),
28
+ "theme cannot be blank hash",
29
+ ));
16
30
  }
17
31
 
18
- let syntax_highlighter_plugin: RHash = TryConvert::try_convert(value)?;
19
32
  let theme_key = Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY);
20
33
 
21
34
  match syntax_highlighter_plugin.get(theme_key) {
22
35
  Some(theme) => {
23
36
  if theme.is_nil() {
24
- // `syntax_highlighter: { theme: nil }`
25
- return Ok(EMPTY_STR.to_string());
37
+ return Err(magnus::Error::new(
38
+ magnus::exception::type_error(),
39
+ "theme cannot be nil",
40
+ ));
26
41
  }
27
- Ok(theme.try_convert::<String>()?)
42
+ Ok(TryConvert::try_convert(theme)?)
28
43
  }
29
44
  None => {
30
- // `syntax_highlighter: { }`
31
- Ok(EMPTY_STR.to_string())
45
+ // `syntax_highlighter: { theme: nil }`
46
+ Ok(None)
32
47
  }
33
48
  }
34
49
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Commonmarker
4
- VERSION = "1.0.0.pre11"
4
+ VERSION = "1.0.0.pre12"
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commonmarker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre11
4
+ version: 1.0.0.pre12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen Torikian
8
8
  - Ashe Connor
9
- autorequire:
9
+ autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-11-13 00:00:00.000000000 Z
12
+ date: 2023-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rb_sys
@@ -56,7 +56,7 @@ dependencies:
56
56
  version: '1.2'
57
57
  description: A fast, safe, extensible parser for CommonMark. This wraps the comrak
58
58
  Rust crate.
59
- email:
59
+ email:
60
60
  executables: []
61
61
  extensions:
62
62
  - ext/commonmarker/extconf.rb
@@ -88,7 +88,7 @@ metadata:
88
88
  funding_uri: https://github.com/sponsors/gjtorikian/
89
89
  source_code_uri: https://github.com/gjtorikian/commonmarker
90
90
  rubygems_mfa_required: 'true'
91
- post_install_message:
91
+ post_install_message:
92
92
  rdoc_options: []
93
93
  require_paths:
94
94
  - lib
@@ -103,8 +103,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  - !ruby/object:Gem::Version
104
104
  version: 3.3.22
105
105
  requirements: []
106
- rubygems_version: 3.4.21
107
- signing_key:
106
+ rubygems_version: 3.4.22
107
+ signing_key:
108
108
  specification_version: 4
109
109
  summary: CommonMark parser and renderer. Written in Rust, wrapped in Ruby.
110
110
  test_files: []