commonmarker 2.10.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +4 -4
- data/README.md +264 -116
- data/UPGRADING.md +102 -0
- data/ext/commonmarker/Cargo.toml +5 -3
- data/ext/commonmarker/src/lib.rs +210 -118
- data/ext/commonmarker/src/nesting.rs +7 -26
- data/ext/commonmarker/src/node.rs +493 -297
- data/ext/commonmarker/src/options.rs +1319 -185
- data/ext/commonmarker/src/plugins/syntax_highlighting.rs +115 -154
- data/ext/commonmarker/src/plugins.rs +115 -1
- data/ext/commonmarker/src/utils.rs +5 -3
- data/lib/commonmarker/errors.rb +1 -1
- data/lib/commonmarker/node/ast.rb +1 -1
- data/lib/commonmarker/node/inspect.rb +15 -10
- data/lib/commonmarker/node.rb +6 -12
- data/lib/commonmarker/version.rb +2 -2
- data/lib/commonmarker.rb +79 -21
- metadata +2 -5
- data/lib/commonmarker/config.rb +0 -119
- data/lib/commonmarker/constants.rb +0 -7
- data/lib/commonmarker/renderer.rb +0 -9
- data/lib/commonmarker/utils.rb +0 -22
|
@@ -1,175 +1,136 @@
|
|
|
1
|
-
use std::
|
|
2
|
-
use std::
|
|
1
|
+
use std::collections::HashMap;
|
|
2
|
+
use std::path::{Path, PathBuf};
|
|
3
|
+
use std::sync::{Arc, Mutex, OnceLock, PoisonError};
|
|
3
4
|
|
|
4
5
|
use comrak::plugins::syntect::{SyntectAdapter, SyntectAdapterBuilder};
|
|
5
6
|
|
|
6
|
-
use magnus::
|
|
7
|
-
use magnus::{RHash, Ruby, TryConvert, Value};
|
|
7
|
+
use magnus::{RHash, Ruby, TryConvert};
|
|
8
8
|
use syntect::highlighting::ThemeSet;
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
/// Adapters built so far, keyed by `(theme, path)`.
|
|
11
|
+
///
|
|
12
|
+
/// Building a `SyntectAdapter` deserializes syntect's bundled syntax and theme
|
|
13
|
+
/// sets, and the syntax regexes are then compiled lazily on first use. That
|
|
14
|
+
/// costs far more than rendering a small document, so an adapter is built once
|
|
15
|
+
/// per theme/path combination and shared for the life of the process. Adapters
|
|
16
|
+
/// hold no Ruby objects and are immutable once built.
|
|
17
|
+
///
|
|
18
|
+
/// The mutex only makes the static `Sync`; every caller holds the GVL, so the
|
|
19
|
+
/// lock is never contended.
|
|
20
|
+
type AdapterCache = Mutex<HashMap<(String, PathBuf), Arc<SyntectAdapter>>>;
|
|
21
|
+
|
|
22
|
+
fn adapter_cache() -> &'static AdapterCache {
|
|
23
|
+
static CACHE: OnceLock<AdapterCache> = OnceLock::new();
|
|
24
|
+
CACHE.get_or_init(|| Mutex::new(HashMap::new()))
|
|
25
|
+
}
|
|
11
26
|
|
|
27
|
+
/// Constructs a syntax highlighter from pre-validated plugin configuration.
|
|
28
|
+
/// Expects the output from format_plugins():
|
|
29
|
+
/// - None: syntax highlighting disabled
|
|
30
|
+
/// - Some(RHash): contains { syntax_highlighter: { theme: String, path: String } }
|
|
12
31
|
pub fn construct_syntax_highlighter_from_plugin(
|
|
13
32
|
ruby: &Ruby,
|
|
14
33
|
rb_plugins: Option<RHash>,
|
|
15
34
|
) -> Result<Option<Arc<SyntectAdapter>>, magnus::Error> {
|
|
16
|
-
match rb_plugins {
|
|
17
|
-
None => Ok(None),
|
|
18
|
-
Some(
|
|
19
|
-
|
|
20
|
-
Some(syntax_highlighter_options) => {
|
|
21
|
-
match fetch_syntax_highlighter_theme(ruby, syntax_highlighter_options) {
|
|
22
|
-
Ok(theme) => theme,
|
|
23
|
-
Err(e) => {
|
|
24
|
-
return Err(e);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
None => None, // no `syntax_highlighter:` defined
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
let adapter: SyntectAdapter;
|
|
32
|
-
|
|
33
|
-
match theme {
|
|
34
|
-
None => Ok(None),
|
|
35
|
-
Some(theme) => {
|
|
36
|
-
if theme.is_empty() {
|
|
37
|
-
// no theme? uss css classes
|
|
38
|
-
// Avoid reloading syntax sets and recompiling regexes for every CSS-mode render.
|
|
39
|
-
static CSS_ADAPTER: OnceLock<Arc<SyntectAdapter>> = OnceLock::new();
|
|
40
|
-
Ok(Some(Arc::clone(
|
|
41
|
-
CSS_ADAPTER.get_or_init(|| Arc::new(SyntectAdapter::new(None))),
|
|
42
|
-
)))
|
|
43
|
-
} else {
|
|
44
|
-
let path = match rb_plugins
|
|
45
|
-
.get(ruby.to_symbol(super::SYNTAX_HIGHLIGHTER_PLUGIN))
|
|
46
|
-
{
|
|
47
|
-
Some(syntax_highlighter_options) => {
|
|
48
|
-
fetch_syntax_highlighter_path(ruby, syntax_highlighter_options)?
|
|
49
|
-
}
|
|
50
|
-
None => PathBuf::from("".to_string()), // no `syntax_highlighter:` defined
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
if path.exists() {
|
|
54
|
-
if !path.is_dir() {
|
|
55
|
-
return Err(magnus::Error::new(
|
|
56
|
-
ruby.exception_arg_error(),
|
|
57
|
-
"`path` needs to be a directory",
|
|
58
|
-
));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
let builder = SyntectAdapterBuilder::new();
|
|
62
|
-
let mut ts = ThemeSet::load_defaults();
|
|
63
|
-
|
|
64
|
-
match ts.add_from_folder(&path) {
|
|
65
|
-
Ok(_) => {}
|
|
66
|
-
Err(e) => {
|
|
67
|
-
return Err(magnus::Error::new(
|
|
68
|
-
ruby.exception_arg_error(),
|
|
69
|
-
format!("failed to load theme set from path: {e}"),
|
|
70
|
-
));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// check if the theme exists in the dir
|
|
75
|
-
match ts.themes.get(&theme) {
|
|
76
|
-
Some(theme) => theme,
|
|
77
|
-
None => {
|
|
78
|
-
return Err(magnus::Error::new(
|
|
79
|
-
ruby.exception_arg_error(),
|
|
80
|
-
format!("theme `{}` does not exist", theme),
|
|
81
|
-
));
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
adapter = builder.theme_set(ts).theme(&theme).build();
|
|
86
|
-
|
|
87
|
-
Ok(Some(Arc::new(adapter)))
|
|
88
|
-
} else {
|
|
89
|
-
// no path? default theme lookup
|
|
90
|
-
ThemeSet::load_defaults()
|
|
91
|
-
.themes
|
|
92
|
-
.get(&theme)
|
|
93
|
-
.ok_or_else(|| {
|
|
94
|
-
magnus::Error::new(
|
|
95
|
-
ruby.exception_arg_error(),
|
|
96
|
-
format!("theme `{}` does not exist", theme),
|
|
97
|
-
)
|
|
98
|
-
})?;
|
|
99
|
-
adapter = SyntectAdapter::new(Some(&theme));
|
|
100
|
-
Ok(Some(Arc::new(adapter)))
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
fn fetch_syntax_highlighter_theme(
|
|
110
|
-
ruby: &Ruby,
|
|
111
|
-
value: Value,
|
|
112
|
-
) -> Result<Option<String>, magnus::Error> {
|
|
113
|
-
if value.is_nil() {
|
|
114
|
-
// `syntax_highlighter: nil`
|
|
115
|
-
return Ok(None);
|
|
116
|
-
}
|
|
35
|
+
let rb_plugins = match rb_plugins {
|
|
36
|
+
None => return Ok(None),
|
|
37
|
+
Some(p) => p,
|
|
38
|
+
};
|
|
117
39
|
|
|
118
|
-
let
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
40
|
+
let syntax_highlighter_hash: RHash = rb_plugins
|
|
41
|
+
.get(ruby.to_symbol(super::SYNTAX_HIGHLIGHTER_PLUGIN))
|
|
42
|
+
.map(TryConvert::try_convert)
|
|
43
|
+
.transpose()?
|
|
44
|
+
.ok_or_else(|| {
|
|
45
|
+
magnus::Error::new(
|
|
46
|
+
ruby.exception_arg_error(),
|
|
47
|
+
"missing syntax_highlighter key in plugins",
|
|
48
|
+
)
|
|
49
|
+
})?;
|
|
50
|
+
|
|
51
|
+
let theme: String = syntax_highlighter_hash
|
|
52
|
+
.get(ruby.to_symbol(super::SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY))
|
|
53
|
+
.map(TryConvert::try_convert)
|
|
54
|
+
.transpose()?
|
|
55
|
+
.unwrap_or_default();
|
|
56
|
+
|
|
57
|
+
let path: String = syntax_highlighter_hash
|
|
58
|
+
.get(ruby.to_symbol(super::SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY))
|
|
59
|
+
.map(TryConvert::try_convert)
|
|
60
|
+
.transpose()?
|
|
61
|
+
.unwrap_or_default();
|
|
62
|
+
|
|
63
|
+
// An empty or missing path means the bundled theme set. A real path is
|
|
64
|
+
// resolved so different spellings of one directory share one adapter.
|
|
65
|
+
let path = PathBuf::from(path);
|
|
66
|
+
let path = if path.as_os_str().is_empty() || !path.exists() {
|
|
67
|
+
PathBuf::new()
|
|
68
|
+
} else {
|
|
69
|
+
path.canonicalize().unwrap_or(path)
|
|
124
70
|
};
|
|
125
71
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
)
|
|
131
|
-
}
|
|
72
|
+
let key = (theme, path);
|
|
73
|
+
|
|
74
|
+
let mut cache = adapter_cache()
|
|
75
|
+
.lock()
|
|
76
|
+
.unwrap_or_else(PoisonError::into_inner);
|
|
132
77
|
|
|
133
|
-
let
|
|
134
|
-
|
|
135
|
-
match syntax_highlighter_plugin.get(theme_key) {
|
|
136
|
-
Some(theme) => {
|
|
137
|
-
if theme.is_nil() {
|
|
138
|
-
return Err(magnus::Error::new(
|
|
139
|
-
ruby.exception_type_error(),
|
|
140
|
-
"theme cannot be nil",
|
|
141
|
-
));
|
|
142
|
-
}
|
|
143
|
-
Ok(TryConvert::try_convert(theme)?)
|
|
144
|
-
}
|
|
145
|
-
None => {
|
|
146
|
-
// `syntax_highlighter: { theme: nil }`
|
|
147
|
-
Ok(None)
|
|
148
|
-
}
|
|
78
|
+
if let Some(adapter) = cache.get(&key) {
|
|
79
|
+
return Ok(Some(Arc::clone(adapter)));
|
|
149
80
|
}
|
|
81
|
+
|
|
82
|
+
let adapter = Arc::new(build_adapter(ruby, &key.0, &key.1)?);
|
|
83
|
+
cache.insert(key, Arc::clone(&adapter));
|
|
84
|
+
|
|
85
|
+
Ok(Some(adapter))
|
|
150
86
|
}
|
|
151
87
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
88
|
+
/// Builds the adapter for one `(theme, path)`; see `adapter_cache` for why this
|
|
89
|
+
/// runs at most once per combination.
|
|
90
|
+
fn build_adapter(ruby: &Ruby, theme: &str, path: &Path) -> Result<SyntectAdapter, magnus::Error> {
|
|
91
|
+
// Empty theme means use CSS classes instead of inline styles
|
|
92
|
+
if theme.is_empty() {
|
|
93
|
+
return Ok(SyntectAdapter::new(None));
|
|
156
94
|
}
|
|
157
95
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
let val: String = TryConvert::try_convert(path)?;
|
|
168
|
-
Ok(PathBuf::from(val))
|
|
169
|
-
}
|
|
170
|
-
None => {
|
|
171
|
-
// `syntax_highlighter: { }`
|
|
172
|
-
Ok(PathBuf::from(EMPTY_STR))
|
|
173
|
-
}
|
|
96
|
+
if path.as_os_str().is_empty() {
|
|
97
|
+
// No custom path, use default themes
|
|
98
|
+
ThemeSet::load_defaults().themes.get(theme).ok_or_else(|| {
|
|
99
|
+
magnus::Error::new(
|
|
100
|
+
ruby.exception_arg_error(),
|
|
101
|
+
format!("theme `{theme}` does not exist"),
|
|
102
|
+
)
|
|
103
|
+
})?;
|
|
104
|
+
return Ok(SyntectAdapter::new(Some(theme)));
|
|
174
105
|
}
|
|
106
|
+
|
|
107
|
+
// Custom theme path provided
|
|
108
|
+
if !path.is_dir() {
|
|
109
|
+
return Err(magnus::Error::new(
|
|
110
|
+
ruby.exception_arg_error(),
|
|
111
|
+
"`path` needs to be a directory",
|
|
112
|
+
));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let mut ts = ThemeSet::load_defaults();
|
|
116
|
+
|
|
117
|
+
ts.add_from_folder(path).map_err(|e| {
|
|
118
|
+
magnus::Error::new(
|
|
119
|
+
ruby.exception_arg_error(),
|
|
120
|
+
format!("failed to load theme set from path: {e}"),
|
|
121
|
+
)
|
|
122
|
+
})?;
|
|
123
|
+
|
|
124
|
+
// Verify theme exists in the loaded set
|
|
125
|
+
ts.themes.get(theme).ok_or_else(|| {
|
|
126
|
+
magnus::Error::new(
|
|
127
|
+
ruby.exception_arg_error(),
|
|
128
|
+
format!("theme `{theme}` does not exist"),
|
|
129
|
+
)
|
|
130
|
+
})?;
|
|
131
|
+
|
|
132
|
+
Ok(SyntectAdapterBuilder::new()
|
|
133
|
+
.theme_set(ts)
|
|
134
|
+
.theme(theme)
|
|
135
|
+
.build())
|
|
175
136
|
}
|
|
@@ -1,6 +1,120 @@
|
|
|
1
1
|
pub mod syntax_highlighting;
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
use magnus::value::ReprValue;
|
|
4
|
+
use magnus::{Error, RHash, Ruby, TryConvert, Value};
|
|
4
5
|
|
|
6
|
+
pub const SYNTAX_HIGHLIGHTER_PLUGIN: &str = "syntax_highlighter";
|
|
5
7
|
pub const SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY: &str = "theme";
|
|
6
8
|
pub const SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY: &str = "path";
|
|
9
|
+
|
|
10
|
+
pub const DEFAULT_THEME: &str = "base16-ocean.dark";
|
|
11
|
+
pub const DEFAULT_PATH: &str = "";
|
|
12
|
+
|
|
13
|
+
/// Formats and validates plugins from Ruby.
|
|
14
|
+
///
|
|
15
|
+
/// No plugin is enabled by default, as in comrak. Returns None unless the Hash
|
|
16
|
+
/// asks for the syntax highlighter (`syntax_highlighter: {}` enables it with
|
|
17
|
+
/// the default theme), and otherwise Some(RHash) holding
|
|
18
|
+
/// `{ syntax_highlighter: { theme: String, path: String } }` with the defaults
|
|
19
|
+
/// filled in.
|
|
20
|
+
pub fn format_plugins(ruby: &Ruby, rb_plugins: Option<Value>) -> Result<Option<RHash>, Error> {
|
|
21
|
+
let rb_plugins = match rb_plugins {
|
|
22
|
+
None => return Ok(None),
|
|
23
|
+
Some(v) if v.is_nil() => return Ok(None),
|
|
24
|
+
Some(v) => v,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let plugins_hash: RHash = TryConvert::try_convert(rb_plugins).map_err(|_| {
|
|
28
|
+
Error::new(
|
|
29
|
+
ruby.exception_type_error(),
|
|
30
|
+
format!(
|
|
31
|
+
"plugins must be a Hash; got {}",
|
|
32
|
+
crate::utils::get_classname(rb_plugins)
|
|
33
|
+
),
|
|
34
|
+
)
|
|
35
|
+
})?;
|
|
36
|
+
|
|
37
|
+
// The syntax highlighter is the only plugin, and it is off unless asked for.
|
|
38
|
+
let syntax_highlighter_value = plugins_hash.get(ruby.to_symbol(SYNTAX_HIGHLIGHTER_PLUGIN));
|
|
39
|
+
|
|
40
|
+
match syntax_highlighter_value {
|
|
41
|
+
None => Ok(None),
|
|
42
|
+
Some(v) if v.is_nil() => Ok(None),
|
|
43
|
+
Some(v) => {
|
|
44
|
+
// Validate it's a hash
|
|
45
|
+
let sh_hash: RHash = TryConvert::try_convert(v).map_err(|_| {
|
|
46
|
+
Error::new(
|
|
47
|
+
ruby.exception_type_error(),
|
|
48
|
+
format!(
|
|
49
|
+
"syntax_highlighter must be a Hash; got {}",
|
|
50
|
+
crate::utils::get_classname(v)
|
|
51
|
+
),
|
|
52
|
+
)
|
|
53
|
+
})?;
|
|
54
|
+
|
|
55
|
+
// Merge with the plugin's defaults; `{}` enables it with the default theme.
|
|
56
|
+
let result = ruby.hash_new();
|
|
57
|
+
let merged_sh = ruby.hash_new();
|
|
58
|
+
|
|
59
|
+
// Get theme - nil is an error, missing key uses default
|
|
60
|
+
let theme: String =
|
|
61
|
+
match sh_hash.get(ruby.to_symbol(SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY)) {
|
|
62
|
+
Some(t) if t.is_nil() => {
|
|
63
|
+
return Err(Error::new(
|
|
64
|
+
ruby.exception_type_error(),
|
|
65
|
+
"syntax_highlighter theme cannot be nil",
|
|
66
|
+
));
|
|
67
|
+
}
|
|
68
|
+
Some(t) => TryConvert::try_convert(t).map_err(|_| {
|
|
69
|
+
Error::new(
|
|
70
|
+
ruby.exception_type_error(),
|
|
71
|
+
format!(
|
|
72
|
+
"syntax_highlighter theme must be a String; got {}",
|
|
73
|
+
crate::utils::get_classname(t)
|
|
74
|
+
),
|
|
75
|
+
)
|
|
76
|
+
})?,
|
|
77
|
+
None => DEFAULT_THEME.to_string(),
|
|
78
|
+
};
|
|
79
|
+
merged_sh.aset(ruby.to_symbol(SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY), theme)?;
|
|
80
|
+
|
|
81
|
+
// Get path (use default if not provided)
|
|
82
|
+
let path: String = match sh_hash.get(ruby.to_symbol(SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY))
|
|
83
|
+
{
|
|
84
|
+
Some(p) if !p.is_nil() => TryConvert::try_convert(p).map_err(|_| {
|
|
85
|
+
Error::new(
|
|
86
|
+
ruby.exception_type_error(),
|
|
87
|
+
format!(
|
|
88
|
+
"syntax_highlighter path must be a String; got {}",
|
|
89
|
+
crate::utils::get_classname(p)
|
|
90
|
+
),
|
|
91
|
+
)
|
|
92
|
+
})?,
|
|
93
|
+
_ => DEFAULT_PATH.to_string(),
|
|
94
|
+
};
|
|
95
|
+
merged_sh.aset(ruby.to_symbol(SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY), path)?;
|
|
96
|
+
|
|
97
|
+
result.aset(ruby.to_symbol(SYNTAX_HIGHLIGHTER_PLUGIN), merged_sh)?;
|
|
98
|
+
Ok(Some(result))
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// Returns the default plugins as a Ruby Hash for introspection: every known
|
|
104
|
+
/// plugin, each `nil` because none is enabled by default.
|
|
105
|
+
pub fn default_plugins_to_hash(ruby: &Ruby) -> Result<RHash, Error> {
|
|
106
|
+
let result = ruby.hash_new();
|
|
107
|
+
result.aset(ruby.to_symbol(SYNTAX_HIGHLIGHTER_PLUGIN), ruby.qnil())?;
|
|
108
|
+
Ok(result)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/// Builds comrak's plugin set around an optional syntax highlighter.
|
|
112
|
+
pub fn comrak_plugins(
|
|
113
|
+
adapter: Option<&comrak::plugins::syntect::SyntectAdapter>,
|
|
114
|
+
) -> comrak::options::Plugins<'_> {
|
|
115
|
+
let mut plugins = comrak::options::Plugins::default();
|
|
116
|
+
if let Some(adapter) = adapter {
|
|
117
|
+
plugins.render.codefence_syntax_highlighter = Some(adapter);
|
|
118
|
+
}
|
|
119
|
+
plugins
|
|
120
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
use magnus::
|
|
1
|
+
use magnus::value::ReprValue;
|
|
2
|
+
use magnus::Value;
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
/// Safely get the Ruby class name of a value.
|
|
5
|
+
pub(crate) fn get_classname(value: Value) -> String {
|
|
6
|
+
unsafe { value.classname() }.into_owned()
|
|
5
7
|
}
|
data/lib/commonmarker/errors.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "pp"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
class Commonmarker
|
|
6
6
|
class Node
|
|
7
7
|
module Inspect
|
|
8
8
|
PP_INDENT_SIZE = 2
|
|
@@ -27,6 +27,7 @@ module Commonmarker
|
|
|
27
27
|
:list_tight,
|
|
28
28
|
:fence_info,
|
|
29
29
|
:alert_type,
|
|
30
|
+
:callout_type,
|
|
30
31
|
].filter_map do |name|
|
|
31
32
|
[name, __send__(name)] if respond_to?(name)
|
|
32
33
|
end
|
|
@@ -36,18 +37,22 @@ module Commonmarker
|
|
|
36
37
|
printer.pp(value)
|
|
37
38
|
end
|
|
38
39
|
|
|
40
|
+
# Show each child's type rather than recursively pretty-printing
|
|
41
|
+
# the full subtree. See GHSA-c4fj-h48v-9fxp: the tree walk, copy, and
|
|
42
|
+
# teardown were all made iterative so deeply nested input cannot
|
|
43
|
+
# overflow the native stack, but `printer.pp` on an array of Node
|
|
44
|
+
# objects re-enters `pretty_print` once per nesting level and still
|
|
45
|
+
# could. Collecting types keeps the output shallow and safe.
|
|
39
46
|
if first_child
|
|
40
47
|
printer.breakable
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
node = node.next_sibling
|
|
47
|
-
end
|
|
48
|
-
printer.text("children=")
|
|
49
|
-
printer.pp(children)
|
|
48
|
+
children = []
|
|
49
|
+
node = first_child
|
|
50
|
+
while node
|
|
51
|
+
children << node.type
|
|
52
|
+
node = node.next_sibling
|
|
50
53
|
end
|
|
54
|
+
printer.text("children=")
|
|
55
|
+
printer.pp(children)
|
|
51
56
|
end
|
|
52
57
|
end
|
|
53
58
|
end
|
data/lib/commonmarker/node.rb
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require "commonmarker/node/ast"
|
|
4
4
|
require "commonmarker/node/inspect"
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
class Commonmarker
|
|
7
7
|
class Node
|
|
8
8
|
include Enumerable
|
|
9
9
|
include Inspect
|
|
@@ -63,28 +63,22 @@ module Commonmarker
|
|
|
63
63
|
# plugins - A {Hash} of additional plugins.
|
|
64
64
|
#
|
|
65
65
|
# Returns a {String} of HTML.
|
|
66
|
-
def to_html(options:
|
|
66
|
+
def to_html(options: {}, plugins: {})
|
|
67
67
|
raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
plugins = Config.process_plugins(plugins)
|
|
71
|
-
|
|
72
|
-
node_to_html(render: opts[:render], parse: opts[:parse], extension: opts[:extension], plugins: plugins).force_encoding("utf-8")
|
|
69
|
+
node_to_html(options: options, plugins: plugins)
|
|
73
70
|
end
|
|
74
71
|
|
|
75
72
|
# Public: Convert the node to a CommonMark string.
|
|
76
73
|
#
|
|
77
|
-
# options - A {
|
|
74
|
+
# options - A {Hash} of render, parse, and extension options.
|
|
78
75
|
# plugins - A {Hash} of additional plugins.
|
|
79
76
|
#
|
|
80
77
|
# Returns a {String}.
|
|
81
|
-
def to_commonmark(options:
|
|
78
|
+
def to_commonmark(options: {}, plugins: {})
|
|
82
79
|
raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
|
|
83
80
|
|
|
84
|
-
|
|
85
|
-
plugins = Config.process_plugins(plugins)
|
|
86
|
-
|
|
87
|
-
node_to_commonmark(render: opts[:render], parse: opts[:parse], extension: opts[:extension], plugins: plugins).force_encoding("utf-8")
|
|
81
|
+
node_to_commonmark(options: options, plugins: plugins)
|
|
88
82
|
end
|
|
89
83
|
end
|
|
90
84
|
end
|
data/lib/commonmarker/version.rb
CHANGED
data/lib/commonmarker.rb
CHANGED
|
@@ -3,28 +3,23 @@
|
|
|
3
3
|
require_relative "commonmarker/extension"
|
|
4
4
|
|
|
5
5
|
require "commonmarker/errors"
|
|
6
|
-
require "commonmarker/utils"
|
|
7
6
|
require "commonmarker/node"
|
|
8
|
-
require "commonmarker/config"
|
|
9
|
-
require "commonmarker/renderer"
|
|
10
7
|
require "commonmarker/version"
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
class Commonmarker
|
|
13
10
|
class << self
|
|
14
|
-
# Public: Parses a CommonMark string into an
|
|
11
|
+
# Public: Parses a CommonMark string into an AST.
|
|
15
12
|
#
|
|
16
13
|
# text - A {String} of text
|
|
17
14
|
# options - A {Hash} of render, parse, and extension options to transform the text.
|
|
18
15
|
#
|
|
19
|
-
#
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
commonmark_parse(text, parse: opts.fetch(:parse, {}), render: opts.fetch(:render, {}), extension: opts.fetch(:extension, {}))
|
|
16
|
+
# The options are processed on every call. To parse many documents with the
|
|
17
|
+
# same options, create an instance with `Commonmarker.new` and call `parse`
|
|
18
|
+
# on it instead.
|
|
19
|
+
#
|
|
20
|
+
# Returns the `document` node.
|
|
21
|
+
def parse(text, options: {})
|
|
22
|
+
new(options: options).parse(text)
|
|
28
23
|
end
|
|
29
24
|
|
|
30
25
|
# Public: Parses a CommonMark string into an HTML string.
|
|
@@ -33,16 +28,79 @@ module Commonmarker
|
|
|
33
28
|
# options - A {Hash} of render, parse, and extension options to transform the text.
|
|
34
29
|
# plugins - A {Hash} of additional plugins.
|
|
35
30
|
#
|
|
31
|
+
# The options and plugins are processed on every call. To convert many
|
|
32
|
+
# documents with the same configuration, create an instance with
|
|
33
|
+
# `Commonmarker.new` and call `to_html` on it instead.
|
|
34
|
+
#
|
|
36
35
|
# Returns a {String} of converted HTML.
|
|
37
|
-
def to_html(text, options:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
def to_html(text, options: {}, plugins: {})
|
|
37
|
+
new(options: options, plugins: plugins).to_html(text)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Public: Creates a converter with a fixed set of options and plugins.
|
|
42
|
+
#
|
|
43
|
+
# options - A {Hash} of render, parse, and extension options to transform the text.
|
|
44
|
+
# plugins - A {Hash} of additional plugins.
|
|
45
|
+
#
|
|
46
|
+
# The options and plugins are validated and processed once, here, so each
|
|
47
|
+
# later `parse`, `to_html`, or `to_commonmark` call skips that work. Changing
|
|
48
|
+
# the hashes after this point has no effect on the instance. Instances hold
|
|
49
|
+
# no mutable state and can be shared across threads.
|
|
50
|
+
def initialize(options: {}, plugins: {})
|
|
51
|
+
raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
|
|
52
|
+
|
|
53
|
+
commonmark_init(options, plugins)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Public: Parses a CommonMark string into an AST using this instance's options.
|
|
57
|
+
#
|
|
58
|
+
# text - A {String} of text
|
|
59
|
+
#
|
|
60
|
+
# Returns the `document` node.
|
|
61
|
+
def parse(text)
|
|
62
|
+
check_text(text)
|
|
41
63
|
|
|
42
|
-
|
|
43
|
-
|
|
64
|
+
commonmark_parse(text)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Public: Converts CommonMark into HTML using this instance's options and plugins.
|
|
68
|
+
#
|
|
69
|
+
# input - A {String} of CommonMark, or a {Commonmarker::Node} to render as-is
|
|
70
|
+
# (for example, a document from `parse` that you have since modified).
|
|
71
|
+
#
|
|
72
|
+
# Returns a {String} of converted HTML.
|
|
73
|
+
def to_html(input)
|
|
74
|
+
return commonmark_render_html(input) if input.is_a?(Node)
|
|
75
|
+
|
|
76
|
+
check_text(input, or_node: true)
|
|
77
|
+
|
|
78
|
+
commonmark_to_html(input)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Public: Converts CommonMark into CommonMark using this instance's options
|
|
82
|
+
# and plugins. Given a {String}, this parses and re-emits it; given a
|
|
83
|
+
# {Commonmarker::Node}, it writes out that node.
|
|
84
|
+
#
|
|
85
|
+
# input - A {String} of CommonMark, or a {Commonmarker::Node}.
|
|
86
|
+
#
|
|
87
|
+
# Returns a {String} of CommonMark.
|
|
88
|
+
def to_commonmark(input)
|
|
89
|
+
return commonmark_render_commonmark(input) if input.is_a?(Node)
|
|
44
90
|
|
|
45
|
-
|
|
91
|
+
check_text(input, or_node: true)
|
|
92
|
+
|
|
93
|
+
commonmark_to_commonmark(input)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
def check_text(text, or_node: false)
|
|
99
|
+
unless text.is_a?(String)
|
|
100
|
+
accepted = or_node ? "a String or a Commonmarker::Node" : "a String"
|
|
101
|
+
raise TypeError, "text must be #{accepted}; got a #{text.class}!"
|
|
46
102
|
end
|
|
103
|
+
|
|
104
|
+
raise TypeError, "text must be UTF-8 encoded; got #{text.encoding}!" unless text.encoding == Encoding::UTF_8
|
|
47
105
|
end
|
|
48
106
|
end
|