commonmarker 0.23.10 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +1221 -0
  3. data/Cargo.toml +7 -0
  4. data/README.md +233 -172
  5. data/ext/commonmarker/Cargo.toml +20 -0
  6. data/ext/commonmarker/extconf.rb +3 -6
  7. data/ext/commonmarker/src/lib.rs +103 -0
  8. data/ext/commonmarker/src/node.rs +1160 -0
  9. data/ext/commonmarker/src/options.rs +216 -0
  10. data/ext/commonmarker/src/plugins/syntax_highlighting.rs +166 -0
  11. data/ext/commonmarker/src/plugins.rs +6 -0
  12. data/ext/commonmarker/src/utils.rs +8 -0
  13. data/lib/commonmarker/config.rb +91 -40
  14. data/lib/commonmarker/constants.rb +7 -0
  15. data/lib/commonmarker/extension.rb +14 -0
  16. data/lib/commonmarker/node/ast.rb +8 -0
  17. data/lib/commonmarker/node/inspect.rb +14 -4
  18. data/lib/commonmarker/node.rb +29 -47
  19. data/lib/commonmarker/renderer.rb +1 -127
  20. data/lib/commonmarker/utils.rb +22 -0
  21. data/lib/commonmarker/version.rb +2 -2
  22. data/lib/commonmarker.rb +27 -25
  23. metadata +38 -186
  24. data/Rakefile +0 -109
  25. data/bin/commonmarker +0 -118
  26. data/commonmarker.gemspec +0 -38
  27. data/ext/commonmarker/arena.c +0 -104
  28. data/ext/commonmarker/autolink.c +0 -508
  29. data/ext/commonmarker/autolink.h +0 -8
  30. data/ext/commonmarker/blocks.c +0 -1622
  31. data/ext/commonmarker/buffer.c +0 -278
  32. data/ext/commonmarker/buffer.h +0 -116
  33. data/ext/commonmarker/case_fold_switch.inc +0 -4327
  34. data/ext/commonmarker/chunk.h +0 -135
  35. data/ext/commonmarker/cmark-gfm-core-extensions.h +0 -54
  36. data/ext/commonmarker/cmark-gfm-extension_api.h +0 -737
  37. data/ext/commonmarker/cmark-gfm-extensions_export.h +0 -42
  38. data/ext/commonmarker/cmark-gfm.h +0 -833
  39. data/ext/commonmarker/cmark-gfm_export.h +0 -42
  40. data/ext/commonmarker/cmark-gfm_version.h +0 -7
  41. data/ext/commonmarker/cmark.c +0 -55
  42. data/ext/commonmarker/cmark_ctype.c +0 -44
  43. data/ext/commonmarker/cmark_ctype.h +0 -33
  44. data/ext/commonmarker/commonmark.c +0 -514
  45. data/ext/commonmarker/commonmarker.c +0 -1308
  46. data/ext/commonmarker/commonmarker.h +0 -16
  47. data/ext/commonmarker/config.h +0 -76
  48. data/ext/commonmarker/core-extensions.c +0 -27
  49. data/ext/commonmarker/entities.inc +0 -2138
  50. data/ext/commonmarker/ext_scanners.c +0 -879
  51. data/ext/commonmarker/ext_scanners.h +0 -24
  52. data/ext/commonmarker/footnotes.c +0 -63
  53. data/ext/commonmarker/footnotes.h +0 -27
  54. data/ext/commonmarker/houdini.h +0 -57
  55. data/ext/commonmarker/houdini_href_e.c +0 -100
  56. data/ext/commonmarker/houdini_html_e.c +0 -66
  57. data/ext/commonmarker/houdini_html_u.c +0 -149
  58. data/ext/commonmarker/html.c +0 -502
  59. data/ext/commonmarker/html.h +0 -27
  60. data/ext/commonmarker/inlines.c +0 -1788
  61. data/ext/commonmarker/inlines.h +0 -29
  62. data/ext/commonmarker/iterator.c +0 -159
  63. data/ext/commonmarker/iterator.h +0 -26
  64. data/ext/commonmarker/latex.c +0 -468
  65. data/ext/commonmarker/linked_list.c +0 -37
  66. data/ext/commonmarker/man.c +0 -274
  67. data/ext/commonmarker/map.c +0 -129
  68. data/ext/commonmarker/map.h +0 -44
  69. data/ext/commonmarker/node.c +0 -1045
  70. data/ext/commonmarker/node.h +0 -167
  71. data/ext/commonmarker/parser.h +0 -59
  72. data/ext/commonmarker/plaintext.c +0 -218
  73. data/ext/commonmarker/plugin.c +0 -36
  74. data/ext/commonmarker/plugin.h +0 -34
  75. data/ext/commonmarker/references.c +0 -43
  76. data/ext/commonmarker/references.h +0 -26
  77. data/ext/commonmarker/registry.c +0 -63
  78. data/ext/commonmarker/registry.h +0 -24
  79. data/ext/commonmarker/render.c +0 -213
  80. data/ext/commonmarker/render.h +0 -62
  81. data/ext/commonmarker/scanners.c +0 -14056
  82. data/ext/commonmarker/scanners.h +0 -70
  83. data/ext/commonmarker/scanners.re +0 -341
  84. data/ext/commonmarker/strikethrough.c +0 -167
  85. data/ext/commonmarker/strikethrough.h +0 -9
  86. data/ext/commonmarker/syntax_extension.c +0 -149
  87. data/ext/commonmarker/syntax_extension.h +0 -34
  88. data/ext/commonmarker/table.c +0 -917
  89. data/ext/commonmarker/table.h +0 -12
  90. data/ext/commonmarker/tagfilter.c +0 -60
  91. data/ext/commonmarker/tagfilter.h +0 -8
  92. data/ext/commonmarker/tasklist.c +0 -156
  93. data/ext/commonmarker/tasklist.h +0 -8
  94. data/ext/commonmarker/utf8.c +0 -317
  95. data/ext/commonmarker/utf8.h +0 -35
  96. data/ext/commonmarker/xml.c +0 -182
  97. data/lib/commonmarker/renderer/html_renderer.rb +0 -256
@@ -0,0 +1,216 @@
1
+ use std::borrow::Cow;
2
+
3
+ use comrak::ComrakOptions;
4
+
5
+ use magnus::value::ReprValue;
6
+ use magnus::TryConvert;
7
+ use magnus::{class, r_hash::ForEach, Error, RHash, Symbol, Value};
8
+
9
+ use crate::utils::try_convert_string;
10
+
11
+ const PARSE_SMART: &str = "smart";
12
+ const PARSE_DEFAULT_INFO_STRING: &str = "default_info_string";
13
+ const PARSE_RELAXED_TASKLIST_MATCHING: &str = "relaxed_tasklist_matching";
14
+ const PARSE_RELAXED_AUTOLINKS: &str = "relaxed_autolinks";
15
+
16
+ fn iterate_parse_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
17
+ options_hash
18
+ .foreach(|key: Symbol, value: Value| {
19
+ match key.name() {
20
+ Ok(Cow::Borrowed(PARSE_SMART)) => {
21
+ comrak_options.parse.smart = TryConvert::try_convert(value)?;
22
+ }
23
+ Ok(Cow::Borrowed(PARSE_DEFAULT_INFO_STRING)) => {
24
+ comrak_options.parse.default_info_string = try_convert_string(value);
25
+ }
26
+ Ok(Cow::Borrowed(PARSE_RELAXED_TASKLIST_MATCHING)) => {
27
+ comrak_options.parse.relaxed_tasklist_matching =
28
+ TryConvert::try_convert(value)?;
29
+ }
30
+ Ok(Cow::Borrowed(PARSE_RELAXED_AUTOLINKS)) => {
31
+ comrak_options.parse.relaxed_autolinks = TryConvert::try_convert(value)?;
32
+ }
33
+ _ => {}
34
+ }
35
+ Ok(ForEach::Continue)
36
+ })
37
+ .unwrap();
38
+ }
39
+
40
+ const RENDER_HARDBREAKS: &str = "hardbreaks";
41
+ const RENDER_GITHUB_PRE_LANG: &str = "github_pre_lang";
42
+ const RENDER_FULL_INFO_STRING: &str = "full_info_string";
43
+ const RENDER_WIDTH: &str = "width";
44
+ const RENDER_UNSAFE: &str = "unsafe";
45
+ const RENDER_ESCAPE: &str = "escape";
46
+ const RENDER_SOURCEPOS: &str = "sourcepos";
47
+ const RENDER_ESCAPED_CHAR_SPANS: &str = "escaped_char_spans";
48
+ const RENDER_IGNORE_SETEXT: &str = "ignore_setext";
49
+ const RENDER_IGNORE_EMPTY_LINKS: &str = "ignore_empty_links";
50
+ const RENDER_GFM_QUIRKS: &str = "gfm_quirks";
51
+ const RENDER_PREFER_FENCED: &str = "prefer_fenced";
52
+
53
+ fn iterate_render_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
54
+ options_hash
55
+ .foreach(|key: Symbol, value: Value| {
56
+ match key.name() {
57
+ Ok(Cow::Borrowed(RENDER_HARDBREAKS)) => {
58
+ comrak_options.render.hardbreaks = TryConvert::try_convert(value)?;
59
+ }
60
+ Ok(Cow::Borrowed(RENDER_GITHUB_PRE_LANG)) => {
61
+ comrak_options.render.github_pre_lang = TryConvert::try_convert(value)?;
62
+ }
63
+ Ok(Cow::Borrowed(RENDER_FULL_INFO_STRING)) => {
64
+ comrak_options.render.full_info_string = TryConvert::try_convert(value)?;
65
+ }
66
+ Ok(Cow::Borrowed(RENDER_WIDTH)) => {
67
+ comrak_options.render.width = TryConvert::try_convert(value)?;
68
+ }
69
+ Ok(Cow::Borrowed(RENDER_UNSAFE)) => {
70
+ comrak_options.render.unsafe_ = TryConvert::try_convert(value)?;
71
+ }
72
+ Ok(Cow::Borrowed(RENDER_ESCAPE)) => {
73
+ comrak_options.render.escape = TryConvert::try_convert(value)?;
74
+ }
75
+ Ok(Cow::Borrowed(RENDER_SOURCEPOS)) => {
76
+ comrak_options.render.sourcepos = TryConvert::try_convert(value)?;
77
+ }
78
+ Ok(Cow::Borrowed(RENDER_ESCAPED_CHAR_SPANS)) => {
79
+ comrak_options.render.escaped_char_spans = TryConvert::try_convert(value)?;
80
+ }
81
+ Ok(Cow::Borrowed(RENDER_IGNORE_SETEXT)) => {
82
+ comrak_options.render.ignore_setext = TryConvert::try_convert(value)?;
83
+ }
84
+ Ok(Cow::Borrowed(RENDER_IGNORE_EMPTY_LINKS)) => {
85
+ comrak_options.render.ignore_empty_links = TryConvert::try_convert(value)?;
86
+ }
87
+ Ok(Cow::Borrowed(RENDER_GFM_QUIRKS)) => {
88
+ comrak_options.render.gfm_quirks = TryConvert::try_convert(value)?;
89
+ }
90
+ Ok(Cow::Borrowed(RENDER_PREFER_FENCED)) => {
91
+ comrak_options.render.prefer_fenced = TryConvert::try_convert(value)?;
92
+ }
93
+ _ => {}
94
+ }
95
+ Ok(ForEach::Continue)
96
+ })
97
+ .unwrap();
98
+ }
99
+
100
+ const EXTENSION_STRIKETHROUGH: &str = "strikethrough";
101
+ const EXTENSION_TAGFILTER: &str = "tagfilter";
102
+ const EXTENSION_TABLE: &str = "table";
103
+ const EXTENSION_AUTOLINK: &str = "autolink";
104
+ const EXTENSION_TASKLIST: &str = "tasklist";
105
+ const EXTENSION_SUPERSCRIPT: &str = "superscript";
106
+ const EXTENSION_HEADER_IDS: &str = "header_ids";
107
+ const EXTENSION_FOOTNOTES: &str = "footnotes";
108
+ const EXTENSION_DESCRIPTION_LISTS: &str = "description_lists";
109
+ const EXTENSION_FRONT_MATTER_DELIMITER: &str = "front_matter_delimiter";
110
+ const EXTENSION_MULTILINE_BLOCK_QUOTES: &str = "multiline_block_quotes";
111
+ const EXTENSION_MATH_DOLLARS: &str = "math_dollars";
112
+ const EXTENSION_MATH_CODE: &str = "math_code";
113
+ const EXTENSION_SHORTCODES: &str = "shortcodes";
114
+ const EXTENSION_WIKILINKS_TITLE_AFTER_PIPE: &str = "wikilinks_title_after_pipe";
115
+ const EXTENSION_WIKILINKS_TITLE_BEFORE_PIPE: &str = "wikilinks_title_before_pipe";
116
+ const EXTENSION_UNDERLINE: &str = "underline";
117
+ const EXTENSION_SPOILER: &str = "spoiler";
118
+ const EXTENSION_GREENTEXT: &str = "greentext";
119
+ const EXTENSION_SUBSCRIPT: &str = "subscript";
120
+
121
+ fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
122
+ options_hash
123
+ .foreach(|key: Symbol, value: Value| {
124
+ match key.name() {
125
+ Ok(Cow::Borrowed(EXTENSION_STRIKETHROUGH)) => {
126
+ comrak_options.extension.strikethrough = TryConvert::try_convert(value)?;
127
+ }
128
+ Ok(Cow::Borrowed(EXTENSION_TAGFILTER)) => {
129
+ comrak_options.extension.tagfilter = TryConvert::try_convert(value)?;
130
+ }
131
+ Ok(Cow::Borrowed(EXTENSION_TABLE)) => {
132
+ comrak_options.extension.table = TryConvert::try_convert(value)?;
133
+ }
134
+ Ok(Cow::Borrowed(EXTENSION_AUTOLINK)) => {
135
+ comrak_options.extension.autolink = TryConvert::try_convert(value)?;
136
+ }
137
+ Ok(Cow::Borrowed(EXTENSION_TASKLIST)) => {
138
+ comrak_options.extension.tasklist = TryConvert::try_convert(value)?;
139
+ }
140
+ Ok(Cow::Borrowed(EXTENSION_SUPERSCRIPT)) => {
141
+ comrak_options.extension.superscript = TryConvert::try_convert(value)?;
142
+ }
143
+ Ok(Cow::Borrowed(EXTENSION_HEADER_IDS)) => {
144
+ comrak_options.extension.header_ids = try_convert_string(value);
145
+ }
146
+ Ok(Cow::Borrowed(EXTENSION_FOOTNOTES)) => {
147
+ comrak_options.extension.footnotes = TryConvert::try_convert(value)?;
148
+ }
149
+ Ok(Cow::Borrowed(EXTENSION_DESCRIPTION_LISTS)) => {
150
+ comrak_options.extension.description_lists = TryConvert::try_convert(value)?;
151
+ }
152
+ Ok(Cow::Borrowed(EXTENSION_FRONT_MATTER_DELIMITER)) => {
153
+ if let Some(option) = try_convert_string(value) {
154
+ if !option.is_empty() {
155
+ comrak_options.extension.front_matter_delimiter = Some(option);
156
+ }
157
+ }
158
+ }
159
+ Ok(Cow::Borrowed(EXTENSION_MULTILINE_BLOCK_QUOTES)) => {
160
+ comrak_options.extension.multiline_block_quotes =
161
+ TryConvert::try_convert(value)?;
162
+ }
163
+ Ok(Cow::Borrowed(EXTENSION_MATH_DOLLARS)) => {
164
+ comrak_options.extension.math_dollars = TryConvert::try_convert(value)?;
165
+ }
166
+ Ok(Cow::Borrowed(EXTENSION_MATH_CODE)) => {
167
+ comrak_options.extension.math_code = TryConvert::try_convert(value)?;
168
+ }
169
+ Ok(Cow::Borrowed(EXTENSION_SHORTCODES)) => {
170
+ comrak_options.extension.shortcodes = TryConvert::try_convert(value)?;
171
+ }
172
+ Ok(Cow::Borrowed(EXTENSION_WIKILINKS_TITLE_AFTER_PIPE)) => {
173
+ comrak_options.extension.wikilinks_title_after_pipe =
174
+ TryConvert::try_convert(value)?;
175
+ }
176
+ Ok(Cow::Borrowed(EXTENSION_WIKILINKS_TITLE_BEFORE_PIPE)) => {
177
+ comrak_options.extension.wikilinks_title_before_pipe =
178
+ TryConvert::try_convert(value)?;
179
+ }
180
+ Ok(Cow::Borrowed(EXTENSION_UNDERLINE)) => {
181
+ comrak_options.extension.underline = TryConvert::try_convert(value)?;
182
+ }
183
+ Ok(Cow::Borrowed(EXTENSION_SPOILER)) => {
184
+ comrak_options.extension.spoiler = TryConvert::try_convert(value)?;
185
+ }
186
+ Ok(Cow::Borrowed(EXTENSION_GREENTEXT)) => {
187
+ comrak_options.extension.greentext = TryConvert::try_convert(value)?;
188
+ }
189
+ Ok(Cow::Borrowed(EXTENSION_SUBSCRIPT)) => {
190
+ comrak_options.extension.subscript = TryConvert::try_convert(value)?;
191
+ }
192
+ _ => {}
193
+ }
194
+ Ok(ForEach::Continue)
195
+ })
196
+ .unwrap();
197
+ }
198
+
199
+ pub fn iterate_options_hash(
200
+ comrak_options: &mut ComrakOptions,
201
+ key: Symbol,
202
+ value: RHash,
203
+ ) -> Result<ForEach, Error> {
204
+ assert!(value.is_kind_of(class::hash()));
205
+
206
+ if key.name().unwrap() == "parse" {
207
+ iterate_parse_options(comrak_options, value);
208
+ }
209
+ if key.name().unwrap() == "render" {
210
+ iterate_render_options(comrak_options, value);
211
+ }
212
+ if key.name().unwrap() == "extension" {
213
+ iterate_extension_options(comrak_options, value);
214
+ }
215
+ Ok(ForEach::Continue)
216
+ }
@@ -0,0 +1,166 @@
1
+ use std::path::PathBuf;
2
+
3
+ use comrak::plugins::syntect::{SyntectAdapter, SyntectAdapterBuilder};
4
+
5
+ use magnus::value::ReprValue;
6
+ use magnus::{exception, RHash, Symbol, TryConvert, Value};
7
+ use syntect::highlighting::ThemeSet;
8
+
9
+ use crate::EMPTY_STR;
10
+
11
+ pub fn construct_syntax_highlighter_from_plugin(
12
+ rb_plugins: Option<RHash>,
13
+ ) -> Result<Option<SyntectAdapter>, magnus::Error> {
14
+ match rb_plugins {
15
+ None => Ok(None),
16
+ Some(rb_plugins) => {
17
+ let theme = match rb_plugins.get(Symbol::new(super::SYNTAX_HIGHLIGHTER_PLUGIN)) {
18
+ Some(syntax_highlighter_options) => {
19
+ match fetch_syntax_highlighter_theme(syntax_highlighter_options) {
20
+ Ok(theme) => theme,
21
+ Err(e) => {
22
+ return Err(e);
23
+ }
24
+ }
25
+ }
26
+ None => None, // no `syntax_highlighter:` defined
27
+ };
28
+
29
+ let adapter: SyntectAdapter;
30
+
31
+ match theme {
32
+ None => Ok(None),
33
+ Some(theme) => {
34
+ if theme.is_empty() {
35
+ // no theme? uss css classes
36
+ adapter = SyntectAdapter::new(None);
37
+ Ok(Some(adapter))
38
+ } else {
39
+ let path =
40
+ match rb_plugins.get(Symbol::new(super::SYNTAX_HIGHLIGHTER_PLUGIN)) {
41
+ Some(syntax_highlighter_options) => {
42
+ fetch_syntax_highlighter_path(syntax_highlighter_options)?
43
+ }
44
+ None => PathBuf::from("".to_string()), // no `syntax_highlighter:` defined
45
+ };
46
+
47
+ if path.exists() {
48
+ if !path.is_dir() {
49
+ return Err(magnus::Error::new(
50
+ exception::arg_error(),
51
+ "`path` needs to be a directory",
52
+ ));
53
+ }
54
+
55
+ let builder = SyntectAdapterBuilder::new();
56
+ let mut ts = ThemeSet::load_defaults();
57
+
58
+ match ts.add_from_folder(&path) {
59
+ Ok(_) => {}
60
+ Err(e) => {
61
+ return Err(magnus::Error::new(
62
+ exception::arg_error(),
63
+ format!("failed to load theme set from path: {e}"),
64
+ ));
65
+ }
66
+ }
67
+
68
+ // check if the theme exists in the dir
69
+ match ts.themes.get(&theme) {
70
+ Some(theme) => theme,
71
+ None => {
72
+ return Err(magnus::Error::new(
73
+ exception::arg_error(),
74
+ format!("theme `{}` does not exist", theme),
75
+ ));
76
+ }
77
+ };
78
+
79
+ adapter = builder.theme_set(ts).theme(&theme).build();
80
+
81
+ Ok(Some(adapter))
82
+ } else {
83
+ // no path? default theme lookup
84
+ ThemeSet::load_defaults()
85
+ .themes
86
+ .get(&theme)
87
+ .ok_or_else(|| {
88
+ magnus::Error::new(
89
+ exception::arg_error(),
90
+ format!("theme `{}` does not exist", theme),
91
+ )
92
+ })?;
93
+ adapter = SyntectAdapter::new(Some(&theme));
94
+ Ok(Some(adapter))
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+
103
+ fn fetch_syntax_highlighter_theme(value: Value) -> Result<Option<String>, magnus::Error> {
104
+ if value.is_nil() {
105
+ // `syntax_highlighter: nil`
106
+ return Ok(None);
107
+ }
108
+
109
+ let syntax_highlighter_plugin: RHash = match TryConvert::try_convert(value) {
110
+ Ok(plugin) => plugin, // `syntax_highlighter: { theme: "<something>" }`
111
+ Err(e) => {
112
+ // not a hash!
113
+ return Err(e);
114
+ }
115
+ };
116
+
117
+ if syntax_highlighter_plugin.is_nil() || syntax_highlighter_plugin.is_empty() {
118
+ return Err(magnus::Error::new(
119
+ magnus::exception::type_error(),
120
+ "theme cannot be blank hash",
121
+ ));
122
+ }
123
+
124
+ let theme_key = Symbol::new(super::SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY);
125
+
126
+ match syntax_highlighter_plugin.get(theme_key) {
127
+ Some(theme) => {
128
+ if theme.is_nil() {
129
+ return Err(magnus::Error::new(
130
+ magnus::exception::type_error(),
131
+ "theme cannot be nil",
132
+ ));
133
+ }
134
+ Ok(TryConvert::try_convert(theme)?)
135
+ }
136
+ None => {
137
+ // `syntax_highlighter: { theme: nil }`
138
+ Ok(None)
139
+ }
140
+ }
141
+ }
142
+
143
+ fn fetch_syntax_highlighter_path(value: Value) -> Result<PathBuf, magnus::Error> {
144
+ if value.is_nil() {
145
+ // `syntax_highlighter: nil`
146
+ return Ok(PathBuf::from(EMPTY_STR));
147
+ }
148
+
149
+ let syntax_highlighter_plugin: RHash = TryConvert::try_convert(value)?;
150
+ let path_key = Symbol::new(super::SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY);
151
+
152
+ match syntax_highlighter_plugin.get(path_key) {
153
+ Some(path) => {
154
+ if path.is_nil() {
155
+ // `syntax_highlighter: { path: nil }`
156
+ return Ok(PathBuf::from(EMPTY_STR));
157
+ }
158
+ let val: String = TryConvert::try_convert(path)?;
159
+ Ok(PathBuf::from(val))
160
+ }
161
+ None => {
162
+ // `syntax_highlighter: { }`
163
+ Ok(PathBuf::from(EMPTY_STR))
164
+ }
165
+ }
166
+ }
@@ -0,0 +1,6 @@
1
+ pub mod syntax_highlighting;
2
+
3
+ pub const SYNTAX_HIGHLIGHTER_PLUGIN: &str = "syntax_highlighter";
4
+
5
+ pub const SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY: &str = "theme";
6
+ pub const SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY: &str = "path";
@@ -0,0 +1,8 @@
1
+ use magnus::{TryConvert, Value};
2
+
3
+ pub fn try_convert_string(value: Value) -> Option<String> {
4
+ match TryConvert::try_convert(value) {
5
+ Ok(s) => Some(s),
6
+ Err(_) => None,
7
+ }
8
+ }
@@ -1,54 +1,105 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module CommonMarker
4
- # For Ruby::Enum, these must be classes, not modules
3
+ module Commonmarker
5
4
  module Config
6
- # See https://github.com/github/cmark-gfm/blob/master/src/cmark-gfm.h#L673
7
- OPTS = {
5
+ # For details, see
6
+ # https://github.com/kivikakk/comrak/blob/162ef9354deb2c9b4a4e05be495aa372ba5bb696/src/main.rs#L201
7
+ OPTIONS = {
8
8
  parse: {
9
- DEFAULT: 0,
10
- SOURCEPOS: (1 << 1),
11
- UNSAFE: (1 << 17),
12
- VALIDATE_UTF8: (1 << 9),
13
- SMART: (1 << 10),
14
- LIBERAL_HTML_TAG: (1 << 12),
15
- FOOTNOTES: (1 << 13),
16
- STRIKETHROUGH_DOUBLE_TILDE: (1 << 14),
9
+ smart: false,
10
+ default_info_string: "",
11
+ relaxed_tasklist_matching: false,
12
+ relaxed_autolinks: false,
17
13
  }.freeze,
18
14
  render: {
19
- DEFAULT: 0,
20
- SOURCEPOS: (1 << 1),
21
- HARDBREAKS: (1 << 2),
22
- UNSAFE: (1 << 17),
23
- NOBREAKS: (1 << 4),
24
- VALIDATE_UTF8: (1 << 9),
25
- SMART: (1 << 10),
26
- GITHUB_PRE_LANG: (1 << 11),
27
- LIBERAL_HTML_TAG: (1 << 12),
28
- FOOTNOTES: (1 << 13),
29
- STRIKETHROUGH_DOUBLE_TILDE: (1 << 14),
30
- TABLE_PREFER_STYLE_ATTRIBUTES: (1 << 15),
31
- FULL_INFO_STRING: (1 << 16),
15
+ hardbreaks: true,
16
+ github_pre_lang: true,
17
+ full_info_string: false,
18
+ width: 80,
19
+ unsafe: false,
20
+ escape: false,
21
+ sourcepos: false,
22
+ escaped_char_spans: true,
23
+ ignore_setext: false,
24
+ ignore_empty_links: false,
25
+ gfm_quirks: false,
26
+ prefer_fenced: false,
32
27
  }.freeze,
33
- format: [:html, :xml, :commonmark, :plaintext].freeze,
28
+ extension: {
29
+ strikethrough: true,
30
+ tagfilter: true,
31
+ table: true,
32
+ autolink: true,
33
+ tasklist: true,
34
+ superscript: false,
35
+ header_ids: "",
36
+ footnotes: false,
37
+ description_lists: false,
38
+ front_matter_delimiter: "",
39
+ multiline_block_quotes: false,
40
+ math_dollars: false,
41
+ math_code: false,
42
+ shortcodes: true,
43
+ wikilinks_title_before_pipe: false,
44
+ wikilinks_title_after_pipe: false,
45
+ underline: false,
46
+ spoiler: false,
47
+ greentext: false,
48
+ subscript: false,
49
+ }.freeze,
50
+ format: [:html].freeze,
34
51
  }.freeze
35
52
 
53
+ PLUGINS = {
54
+ syntax_highlighter: {
55
+ theme: "base16-ocean.dark",
56
+ path: "",
57
+ },
58
+ }
59
+
36
60
  class << self
37
- def process_options(option, type)
38
- case option
39
- when Symbol
40
- OPTS.fetch(type).fetch(option)
41
- when Array
42
- raise TypeError if option.none?
43
-
44
- # neckbearding around. the map will both check the opts and then bitwise-OR it
45
- OPTS.fetch(type).fetch_values(*option).inject(0, :|)
46
- else
47
- raise TypeError, "option type must be a valid symbol or array of symbols within the #{name}::OPTS[:#{type}] context"
61
+ include Commonmarker::Utils
62
+
63
+ def process_options(options)
64
+ {
65
+ parse: process_parse_options(options[:parse]),
66
+ render: process_render_options(options[:render]),
67
+ extension: process_extension_options(options[:extension]),
68
+ }
69
+ end
70
+
71
+ def process_plugins(plugins)
72
+ {
73
+ syntax_highlighter: process_syntax_highlighter_plugin(plugins&.fetch(:syntax_highlighter, nil)),
74
+ }
75
+ end
76
+ end
77
+
78
+ [:parse, :render, :extension].each do |type|
79
+ define_singleton_method :"process_#{type}_options" do |options|
80
+ Commonmarker::Config::OPTIONS[type].each_with_object({}) do |(key, value), hash|
81
+ if options.nil? || !options.key?(key) # option not provided, use the default
82
+ hash[key] = value
83
+ next
84
+ end
85
+
86
+ if options[key].nil? # # option explicitly not included, remove it
87
+ options.delete(key)
88
+ next
89
+ end
90
+
91
+ hash[key] = fetch_kv(options, key, value, type)
48
92
  end
49
- rescue KeyError => e
50
- raise TypeError, "option ':#{e.key}' does not exist for #{name}::OPTS[:#{type}]"
51
93
  end
52
- end
94
+ end
95
+
96
+ define_singleton_method :process_syntax_highlighter_plugin do |options|
97
+ return if options.nil? # plugin explicitly nil, remove it
98
+
99
+ raise TypeError, "Expected a Hash for syntax_highlighter plugin, got #{options.class}" unless options.is_a?(Hash)
100
+ raise TypeError, "Expected a Hash for syntax_highlighter plugin, got nothing" if options.empty?
101
+
102
+ Commonmarker::Config::PLUGINS[:syntax_highlighter].merge(options)
103
+ end
53
104
  end
54
105
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commonmarker
4
+ module Constants
5
+ BOOLS = [true, false].freeze
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ # native precompiled gems package shared libraries in <gem_dir>/lib/commonmarker/<ruby_version>
5
+ # load the precompiled extension file
6
+ ruby_version = /\d+\.\d+/.match(RUBY_VERSION)
7
+ require_relative "#{ruby_version}/commonmarker"
8
+ rescue LoadError
9
+ # fall back to the extension compiled upon installation.
10
+ # use "require" instead of "require_relative" because non-native gems will place C extension files
11
+ # in Gem::BasicSpecification#extension_dir after compilation (during normal installation), which
12
+ # is in $LOAD_PATH but not necessarily relative to this file (see nokogiri#2300)
13
+ require "commonmarker/commonmarker"
14
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commonmarker
4
+ class Node
5
+ class Ast
6
+ end
7
+ end
8
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "pp"
4
4
 
5
- module CommonMarker
5
+ module Commonmarker
6
6
  class Node
7
7
  module Inspect
8
8
  PP_INDENT_SIZE = 2
@@ -16,9 +16,19 @@ module CommonMarker
16
16
  printer.group(PP_INDENT_SIZE, "#<#{self.class}(#{type}):", ">") do
17
17
  printer.breakable
18
18
 
19
- attrs = [:sourcepos, :string_content, :url, :title, :header_level, :list_type, :list_start, :list_tight, :fence_info].map do |name|
19
+ attrs = [
20
+ :source_position,
21
+ :string_content,
22
+ :url,
23
+ :title,
24
+ :header_level,
25
+ :list_type,
26
+ :list_start,
27
+ :list_tight,
28
+ :fence_info,
29
+ ].filter_map do |name|
20
30
  [name, __send__(name)]
21
- rescue NodeError
31
+ rescue StandardError
22
32
  nil
23
33
  end.compact
24
34
 
@@ -34,7 +44,7 @@ module CommonMarker
34
44
  node = first_child
35
45
  while node
36
46
  children << node
37
- node = node.next
47
+ node = node.next_sibling
38
48
  end
39
49
  printer.text("children=")
40
50
  printer.pp(children)