commonmarker 0.23.10 → 1.1.3

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 +1220 -0
  3. data/Cargo.toml +7 -0
  4. data/README.md +217 -170
  5. data/ext/commonmarker/Cargo.toml +20 -0
  6. data/ext/commonmarker/extconf.rb +3 -6
  7. data/ext/commonmarker/src/lib.rs +183 -0
  8. data/ext/commonmarker/src/node.rs +1112 -0
  9. data/ext/commonmarker/src/options.rs +165 -0
  10. data/ext/commonmarker/src/plugins/syntax_highlighting.rs +74 -0
  11. data/ext/commonmarker/src/plugins.rs +3 -0
  12. data/ext/commonmarker/src/utils.rs +8 -0
  13. data/lib/commonmarker/config.rb +90 -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,183 @@
1
+ extern crate core;
2
+
3
+ use std::path::PathBuf;
4
+
5
+ use ::syntect::highlighting::ThemeSet;
6
+ use comrak::{
7
+ adapters::SyntaxHighlighterAdapter,
8
+ markdown_to_html, markdown_to_html_with_plugins, parse_document,
9
+ plugins::syntect::{SyntectAdapter, SyntectAdapterBuilder},
10
+ ComrakOptions, ComrakPlugins,
11
+ };
12
+ use magnus::{
13
+ define_module, exception, function, r_hash::ForEach, scan_args, Error, RHash, Symbol, Value,
14
+ };
15
+ use node::CommonmarkerNode;
16
+
17
+ mod options;
18
+ use options::iterate_options_hash;
19
+
20
+ mod plugins;
21
+ use plugins::{
22
+ syntax_highlighting::{fetch_syntax_highlighter_path, fetch_syntax_highlighter_theme},
23
+ SYNTAX_HIGHLIGHTER_PLUGIN,
24
+ };
25
+ use typed_arena::Arena;
26
+
27
+ mod node;
28
+ mod utils;
29
+
30
+ pub const EMPTY_STR: &str = "";
31
+
32
+ fn commonmark_parse(args: &[Value]) -> Result<CommonmarkerNode, magnus::Error> {
33
+ let args = scan_args::scan_args::<_, (), (), (), _, ()>(args)?;
34
+ let (rb_commonmark,): (String,) = args.required;
35
+
36
+ let kwargs =
37
+ scan_args::get_kwargs::<_, (), (Option<RHash>,), ()>(args.keywords, &[], &["options"])?;
38
+ let (rb_options,) = kwargs.optional;
39
+
40
+ let mut comrak_options = ComrakOptions::default();
41
+
42
+ if let Some(rb_options) = rb_options {
43
+ rb_options.foreach(|key: Symbol, value: RHash| {
44
+ iterate_options_hash(&mut comrak_options, key, value)?;
45
+ Ok(ForEach::Continue)
46
+ })?;
47
+ }
48
+
49
+ let arena = Arena::new();
50
+ let root = parse_document(&arena, &rb_commonmark, &comrak_options);
51
+
52
+ CommonmarkerNode::new_from_comrak_node(root)
53
+ }
54
+
55
+ fn commonmark_to_html(args: &[Value]) -> Result<String, magnus::Error> {
56
+ let args = scan_args::scan_args::<_, (), (), (), _, ()>(args)?;
57
+ let (rb_commonmark,): (String,) = args.required;
58
+
59
+ let kwargs = scan_args::get_kwargs::<_, (), (Option<RHash>, Option<RHash>), ()>(
60
+ args.keywords,
61
+ &[],
62
+ &["options", "plugins"],
63
+ )?;
64
+ let (rb_options, rb_plugins) = kwargs.optional;
65
+
66
+ let mut comrak_options = ComrakOptions::default();
67
+
68
+ if let Some(rb_options) = rb_options {
69
+ rb_options.foreach(|key: Symbol, value: RHash| {
70
+ iterate_options_hash(&mut comrak_options, key, value)?;
71
+ Ok(ForEach::Continue)
72
+ })?;
73
+ }
74
+
75
+ if let Some(rb_plugins) = rb_plugins {
76
+ let mut comrak_plugins = ComrakPlugins::default();
77
+
78
+ let syntax_highlighter: Option<&dyn SyntaxHighlighterAdapter>;
79
+ let adapter: SyntectAdapter;
80
+
81
+ let theme = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) {
82
+ Some(syntax_highlighter_options) => {
83
+ match fetch_syntax_highlighter_theme(syntax_highlighter_options) {
84
+ Ok(theme) => theme,
85
+ Err(e) => {
86
+ return Err(e);
87
+ }
88
+ }
89
+ }
90
+ None => None, // no `syntax_highlighter:` defined
91
+ };
92
+
93
+ match theme {
94
+ None => syntax_highlighter = None,
95
+ Some(theme) => {
96
+ if theme.is_empty() {
97
+ // no theme? uss css classes
98
+ adapter = SyntectAdapter::new(None);
99
+ syntax_highlighter = Some(&adapter);
100
+ } else {
101
+ let path = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) {
102
+ Some(syntax_highlighter_options) => {
103
+ fetch_syntax_highlighter_path(syntax_highlighter_options)?
104
+ }
105
+ None => PathBuf::from("".to_string()), // no `syntax_highlighter:` defined
106
+ };
107
+
108
+ if path.exists() {
109
+ if !path.is_dir() {
110
+ return Err(Error::new(
111
+ exception::arg_error(),
112
+ "`path` needs to be a directory",
113
+ ));
114
+ }
115
+
116
+ let builder = SyntectAdapterBuilder::new();
117
+ let mut ts = ThemeSet::load_defaults();
118
+
119
+ match ts.add_from_folder(&path) {
120
+ Ok(_) => {}
121
+ Err(e) => {
122
+ return Err(Error::new(
123
+ exception::arg_error(),
124
+ format!("failed to load theme set from path: {e}"),
125
+ ));
126
+ }
127
+ }
128
+
129
+ // check if the theme exists in the dir
130
+ match ts.themes.get(&theme) {
131
+ Some(theme) => theme,
132
+ None => {
133
+ return Err(Error::new(
134
+ exception::arg_error(),
135
+ format!("theme `{}` does not exist", theme),
136
+ ));
137
+ }
138
+ };
139
+
140
+ adapter = builder.theme_set(ts).theme(&theme).build();
141
+
142
+ syntax_highlighter = Some(&adapter);
143
+ } else {
144
+ // no path? default theme lookup
145
+ ThemeSet::load_defaults()
146
+ .themes
147
+ .get(&theme)
148
+ .ok_or_else(|| {
149
+ Error::new(
150
+ exception::arg_error(),
151
+ format!("theme `{}` does not exist", theme),
152
+ )
153
+ })?;
154
+ adapter = SyntectAdapter::new(Some(&theme));
155
+ syntax_highlighter = Some(&adapter);
156
+ }
157
+ }
158
+ }
159
+ }
160
+ comrak_plugins.render.codefence_syntax_highlighter = syntax_highlighter;
161
+
162
+ Ok(markdown_to_html_with_plugins(
163
+ &rb_commonmark,
164
+ &comrak_options,
165
+ &comrak_plugins,
166
+ ))
167
+ } else {
168
+ Ok(markdown_to_html(&rb_commonmark, &comrak_options))
169
+ }
170
+ }
171
+
172
+ #[magnus::init]
173
+ fn init() -> Result<(), Error> {
174
+ let m_commonmarker = define_module("Commonmarker")?;
175
+
176
+ m_commonmarker.define_module_function("commonmark_parse", function!(commonmark_parse, -1))?;
177
+ m_commonmarker
178
+ .define_module_function("commonmark_to_html", function!(commonmark_to_html, -1))?;
179
+
180
+ node::init(m_commonmarker).expect("cannot define Commonmarker::Node class");
181
+
182
+ Ok(())
183
+ }