commonmarker 0.23.10 → 2.8.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.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +1164 -0
  3. data/Cargo.toml +12 -0
  4. data/README.md +252 -176
  5. data/ext/commonmarker/Cargo.toml +21 -0
  6. data/ext/commonmarker/build.rs +9 -0
  7. data/ext/commonmarker/extconf.rb +3 -6
  8. data/ext/commonmarker/src/lib.rs +168 -0
  9. data/ext/commonmarker/src/node.rs +1263 -0
  10. data/ext/commonmarker/src/options.rs +241 -0
  11. data/ext/commonmarker/src/plugins/syntax_highlighting.rs +171 -0
  12. data/ext/commonmarker/src/plugins.rs +6 -0
  13. data/ext/commonmarker/src/utils.rs +5 -0
  14. data/lib/commonmarker/config.rb +103 -40
  15. data/lib/commonmarker/constants.rb +7 -0
  16. data/lib/commonmarker/extension.rb +14 -0
  17. data/lib/commonmarker/node/ast.rb +8 -0
  18. data/lib/commonmarker/node/inspect.rb +14 -4
  19. data/lib/commonmarker/node.rb +29 -47
  20. data/lib/commonmarker/renderer.rb +1 -127
  21. data/lib/commonmarker/utils.rb +22 -0
  22. data/lib/commonmarker/version.rb +2 -2
  23. data/lib/commonmarker.rb +27 -25
  24. metadata +38 -187
  25. data/Rakefile +0 -109
  26. data/bin/commonmarker +0 -118
  27. data/commonmarker.gemspec +0 -38
  28. data/ext/commonmarker/arena.c +0 -104
  29. data/ext/commonmarker/autolink.c +0 -508
  30. data/ext/commonmarker/autolink.h +0 -8
  31. data/ext/commonmarker/blocks.c +0 -1622
  32. data/ext/commonmarker/buffer.c +0 -278
  33. data/ext/commonmarker/buffer.h +0 -116
  34. data/ext/commonmarker/case_fold_switch.inc +0 -4327
  35. data/ext/commonmarker/chunk.h +0 -135
  36. data/ext/commonmarker/cmark-gfm-core-extensions.h +0 -54
  37. data/ext/commonmarker/cmark-gfm-extension_api.h +0 -737
  38. data/ext/commonmarker/cmark-gfm-extensions_export.h +0 -42
  39. data/ext/commonmarker/cmark-gfm.h +0 -833
  40. data/ext/commonmarker/cmark-gfm_export.h +0 -42
  41. data/ext/commonmarker/cmark-gfm_version.h +0 -7
  42. data/ext/commonmarker/cmark.c +0 -55
  43. data/ext/commonmarker/cmark_ctype.c +0 -44
  44. data/ext/commonmarker/cmark_ctype.h +0 -33
  45. data/ext/commonmarker/commonmark.c +0 -514
  46. data/ext/commonmarker/commonmarker.c +0 -1308
  47. data/ext/commonmarker/commonmarker.h +0 -16
  48. data/ext/commonmarker/config.h +0 -76
  49. data/ext/commonmarker/core-extensions.c +0 -27
  50. data/ext/commonmarker/entities.inc +0 -2138
  51. data/ext/commonmarker/ext_scanners.c +0 -879
  52. data/ext/commonmarker/ext_scanners.h +0 -24
  53. data/ext/commonmarker/footnotes.c +0 -63
  54. data/ext/commonmarker/footnotes.h +0 -27
  55. data/ext/commonmarker/houdini.h +0 -57
  56. data/ext/commonmarker/houdini_href_e.c +0 -100
  57. data/ext/commonmarker/houdini_html_e.c +0 -66
  58. data/ext/commonmarker/houdini_html_u.c +0 -149
  59. data/ext/commonmarker/html.c +0 -502
  60. data/ext/commonmarker/html.h +0 -27
  61. data/ext/commonmarker/inlines.c +0 -1788
  62. data/ext/commonmarker/inlines.h +0 -29
  63. data/ext/commonmarker/iterator.c +0 -159
  64. data/ext/commonmarker/iterator.h +0 -26
  65. data/ext/commonmarker/latex.c +0 -468
  66. data/ext/commonmarker/linked_list.c +0 -37
  67. data/ext/commonmarker/man.c +0 -274
  68. data/ext/commonmarker/map.c +0 -129
  69. data/ext/commonmarker/map.h +0 -44
  70. data/ext/commonmarker/node.c +0 -1045
  71. data/ext/commonmarker/node.h +0 -167
  72. data/ext/commonmarker/parser.h +0 -59
  73. data/ext/commonmarker/plaintext.c +0 -218
  74. data/ext/commonmarker/plugin.c +0 -36
  75. data/ext/commonmarker/plugin.h +0 -34
  76. data/ext/commonmarker/references.c +0 -43
  77. data/ext/commonmarker/references.h +0 -26
  78. data/ext/commonmarker/registry.c +0 -63
  79. data/ext/commonmarker/registry.h +0 -24
  80. data/ext/commonmarker/render.c +0 -213
  81. data/ext/commonmarker/render.h +0 -62
  82. data/ext/commonmarker/scanners.c +0 -14056
  83. data/ext/commonmarker/scanners.h +0 -70
  84. data/ext/commonmarker/scanners.re +0 -341
  85. data/ext/commonmarker/strikethrough.c +0 -167
  86. data/ext/commonmarker/strikethrough.h +0 -9
  87. data/ext/commonmarker/syntax_extension.c +0 -149
  88. data/ext/commonmarker/syntax_extension.h +0 -34
  89. data/ext/commonmarker/table.c +0 -917
  90. data/ext/commonmarker/table.h +0 -12
  91. data/ext/commonmarker/tagfilter.c +0 -60
  92. data/ext/commonmarker/tagfilter.h +0 -8
  93. data/ext/commonmarker/tasklist.c +0 -156
  94. data/ext/commonmarker/tasklist.h +0 -8
  95. data/ext/commonmarker/utf8.c +0 -317
  96. data/ext/commonmarker/utf8.h +0 -35
  97. data/ext/commonmarker/xml.c +0 -182
  98. data/lib/commonmarker/renderer/html_renderer.rb +0 -256
@@ -0,0 +1,168 @@
1
+ extern crate core;
2
+
3
+ use std::fmt::{self, Write};
4
+
5
+ use comrak::parse_document;
6
+ use magnus::{function, scan_args, RHash, RString, Ruby, Value};
7
+ use node::CommonmarkerNode;
8
+ use plugins::syntax_highlighting::construct_syntax_highlighter_from_plugin;
9
+
10
+ mod options;
11
+
12
+ mod plugins;
13
+
14
+ use rb_allocator::ruby_global_allocator;
15
+ use typed_arena::Arena;
16
+
17
+ use crate::options::{iterate_extension_options, iterate_parse_options, iterate_render_options};
18
+
19
+ mod node;
20
+ mod utils;
21
+
22
+ pub const EMPTY_STR: &str = "";
23
+
24
+ // Inform Ruby's GC about memory allocations.
25
+ ruby_global_allocator!();
26
+
27
+ /// A writer that writes directly to a Ruby String, avoiding intermediate Rust allocations.
28
+ struct RStringWriter(RString);
29
+
30
+ impl Write for RStringWriter {
31
+ fn write_str(&mut self, s: &str) -> fmt::Result {
32
+ self.0.cat(s);
33
+ Ok(())
34
+ }
35
+ }
36
+
37
+ fn commonmark_parse(ruby: &Ruby, args: &[Value]) -> Result<CommonmarkerNode, magnus::Error> {
38
+ let args = scan_args::scan_args::<_, (), (), (), _, ()>(args)?;
39
+ let (rb_commonmark,): (RString,) = args.required;
40
+
41
+ // SAFETY: We hold the GVL and rb_commonmark won't be modified until we return
42
+ let commonmark_str = unsafe {
43
+ rb_commonmark.as_str().map_err(|e| {
44
+ magnus::Error::new(
45
+ ruby.exception_encoding_error(),
46
+ format!("invalid UTF-8: {}", e),
47
+ )
48
+ })?
49
+ };
50
+
51
+ let kwargs = scan_args::get_kwargs::<_, (), (Option<RHash>, Option<RHash>, Option<RHash>), ()>(
52
+ args.keywords,
53
+ &[],
54
+ &["parse", "render", "extension"],
55
+ )?;
56
+ let (rb_parse, rb_render, rb_extension) = kwargs.optional;
57
+
58
+ let mut comrak_parse_options = comrak::options::Parse::default();
59
+ let mut comrak_render_options = comrak::options::Render::default();
60
+ let mut comrak_extension_options = comrak::options::Extension::default();
61
+
62
+ if let Some(rb_parse) = rb_parse {
63
+ iterate_parse_options(&mut comrak_parse_options, rb_parse);
64
+ }
65
+ if let Some(rb_render) = rb_render {
66
+ iterate_render_options(&mut comrak_render_options, rb_render);
67
+ }
68
+ if let Some(rb_extension) = rb_extension {
69
+ iterate_extension_options(&mut comrak_extension_options, rb_extension);
70
+ }
71
+
72
+ let comrak_options = comrak::Options {
73
+ parse: comrak_parse_options,
74
+ render: comrak_render_options,
75
+ extension: comrak_extension_options,
76
+ };
77
+
78
+ let arena = Arena::new();
79
+ let root = parse_document(&arena, commonmark_str, &comrak_options);
80
+
81
+ CommonmarkerNode::new_from_comrak_node(root)
82
+ }
83
+
84
+ fn commonmark_to_html(ruby: &Ruby, args: &[Value]) -> Result<RString, magnus::Error> {
85
+ let args = scan_args::scan_args::<_, (), (), (), _, ()>(args)?;
86
+ let (rb_commonmark,): (RString,) = args.required;
87
+
88
+ // SAFETY: We hold the GVL and rb_commonmark won't be modified until we return
89
+ let commonmark_str = unsafe {
90
+ rb_commonmark.as_str().map_err(|e| {
91
+ magnus::Error::new(
92
+ ruby.exception_encoding_error(),
93
+ format!("invalid UTF-8: {}", e),
94
+ )
95
+ })?
96
+ };
97
+
98
+ let kwargs = scan_args::get_kwargs::<
99
+ _,
100
+ (),
101
+ (Option<RHash>, Option<RHash>, Option<RHash>, Option<RHash>),
102
+ (),
103
+ >(
104
+ args.keywords,
105
+ &[],
106
+ &["render", "parse", "extension", "plugins"],
107
+ )?;
108
+ let (rb_render, rb_parse, rb_extension, rb_plugins) = kwargs.optional;
109
+
110
+ let mut comrak_parse_options = comrak::options::Parse::default();
111
+ let mut comrak_render_options = comrak::options::Render::default();
112
+ let mut comrak_extension_options = comrak::options::Extension::default();
113
+
114
+ if let Some(rb_parse) = rb_parse {
115
+ iterate_parse_options(&mut comrak_parse_options, rb_parse);
116
+ }
117
+ if let Some(rb_render) = rb_render {
118
+ iterate_render_options(&mut comrak_render_options, rb_render);
119
+ }
120
+ if let Some(rb_extension) = rb_extension {
121
+ iterate_extension_options(&mut comrak_extension_options, rb_extension);
122
+ }
123
+
124
+ let mut comrak_plugins = comrak::options::Plugins::default();
125
+
126
+ let syntect_adapter = match construct_syntax_highlighter_from_plugin(ruby, rb_plugins) {
127
+ Ok(Some(adapter)) => Some(adapter),
128
+ Ok(None) => None,
129
+ Err(err) => return Err(err),
130
+ };
131
+
132
+ match syntect_adapter {
133
+ Some(ref adapter) => comrak_plugins.render.codefence_syntax_highlighter = Some(adapter),
134
+ None => comrak_plugins.render.codefence_syntax_highlighter = None,
135
+ }
136
+
137
+ let comrak_options = comrak::Options {
138
+ parse: comrak_parse_options,
139
+ render: comrak_render_options,
140
+ extension: comrak_extension_options,
141
+ };
142
+
143
+ // Pre-allocate Ruby string with estimated capacity (assume HTML is typically 2x commonmark size)
144
+ let output = ruby.str_with_capacity(commonmark_str.len() * 2);
145
+ let mut writer = RStringWriter(output);
146
+
147
+ // Parse and render directly to Ruby string, avoiding intermediate Rust String allocation
148
+ let arena = Arena::new();
149
+ let root = parse_document(&arena, commonmark_str, &comrak_options);
150
+
151
+ comrak::html::format_document_with_plugins(root, &comrak_options, &mut writer, &comrak_plugins)
152
+ .map_err(|e| magnus::Error::new(ruby.exception_runtime_error(), e.to_string()))?;
153
+
154
+ Ok(output)
155
+ }
156
+
157
+ #[magnus::init]
158
+ fn init(ruby: &Ruby) -> Result<(), magnus::Error> {
159
+ let m_commonmarker = ruby.define_module("Commonmarker")?;
160
+
161
+ m_commonmarker.define_module_function("commonmark_parse", function!(commonmark_parse, -1))?;
162
+ m_commonmarker
163
+ .define_module_function("commonmark_to_html", function!(commonmark_to_html, -1))?;
164
+
165
+ node::init(ruby, m_commonmarker).expect("cannot define Commonmarker::Node class");
166
+
167
+ Ok(())
168
+ }