commonmarker 2.9.0 → 2.10.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 051a8f17a827d1329de0031ac4f933b3a78aee387707562979698f810bb64a87
4
- data.tar.gz: e4f6e3c99d4c680db8d41f6d2bea08fb394db91cd628fed345a47c8b2f078e9f
3
+ metadata.gz: 23a7f4c3d26ba5e9b9301c9af8db67520fdda959f6cf8faab5564e9276d8d7bc
4
+ data.tar.gz: 7ee7de8566813969b327b94d3e0c8b6a8e536a38906522cb7eb9f13206186811
5
5
  SHA512:
6
- metadata.gz: dce8bf8e72268b9304ad04bc91d741be82f6e35de32a6914cacbce2be17b89ea1349c04dc45e83b06f91eeecb3eb36b697d099044b53926a1cafdb9bf7003894
7
- data.tar.gz: 9dbcbfea2785a3e5567f605e0b96ff87e78cee59aa4219b524a55fd1d4241261acbc76fd5d3950565ca457795e0b957a3b19c5a27f6284eddce227de41653cff
6
+ metadata.gz: eafbbb68ab07ac0ab161437cb6327ea55e232d6c412ea1be6f7d4b6474fae61607d905efc028000c5be2a9c193719e7b5dda16b69d4be2dbcdef112c65c82ed5
7
+ data.tar.gz: f2c79a17c4dec72cdd65979d59952097a286f125a593935986088788cb936f1a99e0dc13f2493afc8c96e309dc175900989956e39fa0044bc9bc8bbd893578c2
data/README.md CHANGED
@@ -140,6 +140,24 @@ doc.to_commonmark
140
140
  # => # The site\n\nGitHub\n
141
141
  ```
142
142
 
143
+ ### Reading and writing node content
144
+
145
+ `string_content` reads and writes the text of nodes whose content is plain text (like `:text`, `:code`, and `:code_block`).
146
+
147
+ `literal` reads and writes the raw string a node carries, for every node type that has one (like `:text`, `:code`, `:code_block`, `:html_block`, `:html_inline`, `:raw`, `:math`, and `:frontmatter`).
148
+
149
+ The two exist separately because they mean different things. Rewriting `string_content` only ever swaps text for text. Rewriting `literal` on an `:html_block`, `:html_inline`, or `:raw` node writes markup that is emitted unescaped:
150
+
151
+ ```ruby
152
+ doc = Commonmarker.parse("A <b>bold</b> claim")
153
+
154
+ doc.walk do |node|
155
+ node.literal = "<i>" if node.type == :html_inline && node.literal == "<b>"
156
+ end
157
+ ```
158
+
159
+ Note that a `:frontmatter` node's literal includes its delimiters and trailing newlines, so anything you assign must include them too.
160
+
143
161
  ## Options and plugins
144
162
 
145
163
  ### Options
@@ -169,21 +187,21 @@ Note that there is a distinction in comrak for "parse" options and "render" opti
169
187
 
170
188
  ### Render options
171
189
 
172
- | Name | Description | Default |
173
- | -------------------- | ------------------------------------------------------------------------------------------------------ | ------- |
174
- | `hardbreaks` | [Soft line breaks](http://spec.commonmark.org/0.27/#soft-line-breaks) translate into hard line breaks. | `true` |
175
- | `github_pre_lang` | GitHub-style `<pre lang="xyz">` is used for fenced code blocks with info tags. | `true` |
176
- | `full_info_string` | Gives info string data after a space in a `data-meta` attribute on code blocks. | `false` |
177
- | `width` | The wrap column when outputting CommonMark. | `80` |
178
- | `unsafe` | Allow rendering of raw HTML and potentially dangerous links. | `false` |
179
- | `escape` | Escape raw HTML instead of clobbering it. | `false` |
180
- | `sourcepos` | Include source position attribute in HTML and XML output. | `false` |
181
- | `escaped_char_spans` | Wrap escaped characters in span tags. | `true` |
182
- | `ignore_empty_links` | Ignores empty links, leaving the Markdown text in place. | `false` |
183
- | `gfm_quirks` | Outputs HTML with GFM-style quirks; namely, not nesting `<strong>` inlines. | `false` |
184
- | `prefer_fenced` | Always output fenced code blocks, even where an indented one could be used. | `false` |
185
- | `tasklist_classes` | Add CSS classes to the HTML output of the tasklist extension | `false` |
186
- | `compact_html` | Suppress newlines in pretty-printed HTML output. | `false` |
190
+ | Name | Description | Default |
191
+ | -------------------- | ------------------------------------------------------------------------------------------------------------------------ | ------------ |
192
+ | `hardbreaks` | [Soft line breaks](http://spec.commonmark.org/0.27/#soft-line-breaks) translate into hard line breaks. | `true` |
193
+ | `github_pre_lang` | GitHub-style `<pre lang="xyz">` is used for fenced code blocks with info tags. | `true` |
194
+ | `full_info_string` | Gives info string data after a space in a `data-meta` attribute on code blocks. | `false` |
195
+ | `width` | The wrap column when outputting CommonMark. | `80` |
196
+ | `unsafe` | Allow rendering of raw HTML and potentially dangerous links. | `false` |
197
+ | `escape` | Escape raw HTML instead of clobbering it. | `false` |
198
+ | `sourcepos` | Include source position attribute in HTML and XML output. | `false` |
199
+ | `escaped_char_spans` | Wrap escaped characters in span tags. | `true` |
200
+ | `ignore_empty_links` | Ignores empty links, leaving the Markdown text in place. | `false` |
201
+ | `gfm_quirks` | Outputs HTML with GFM-style quirks; namely, not nesting `<strong>` inlines. | `false` |
202
+ | `prefer_fenced` | Always output fenced code blocks, even where an indented one could be used. | `false` |
203
+ | `tasklist_classes` | Add CSS classes to the HTML output of the tasklist extension | `false` |
204
+ | `compact_html` | Suppress newlines in pretty-printed HTML output. | `false` |
187
205
  | `alert_style` | The style of alert output: `"specific"` (`<div class="markdown-alert">`) or `"semantic"` (`<aside class="admonition">`). | `"specific"` |
188
206
 
189
207
  As well, there are several extensions which you can toggle in the same manner:
@@ -31,6 +31,39 @@ pub struct CommonmarkerNode {
31
31
  /// SAFETY: This is safe because we only access this data when the GVL is held.
32
32
  unsafe impl Send for CommonmarkerNode {}
33
33
 
34
+ /// Reports whether a node value supports a given property, where "property" is
35
+ /// a method name with any trailing `=` or `?` removed.
36
+ ///
37
+ /// `None` means the property is not type-dependent at all
38
+ /// (`walk`, `type`, `delete`, ...).
39
+ fn value_supports(value: &ComrakNodeValue, property: &str) -> Option<bool> {
40
+ let supported = match property {
41
+ "string_content" => matches!(
42
+ value,
43
+ ComrakNodeValue::Text(_) | ComrakNodeValue::Code(_) | ComrakNodeValue::CodeBlock(_)
44
+ ),
45
+ "literal" => matches!(
46
+ value,
47
+ ComrakNodeValue::Text(_)
48
+ | ComrakNodeValue::Code(_)
49
+ | ComrakNodeValue::CodeBlock(_)
50
+ | ComrakNodeValue::HtmlBlock(_)
51
+ | ComrakNodeValue::HtmlInline(_)
52
+ | ComrakNodeValue::Raw(_)
53
+ | ComrakNodeValue::Math(_)
54
+ | ComrakNodeValue::FrontMatter(_)
55
+ ),
56
+ "url" | "title" => matches!(value, ComrakNodeValue::Link(_) | ComrakNodeValue::Image(_)),
57
+ "header_level" => matches!(value, ComrakNodeValue::Heading(_)),
58
+ "list_type" | "list_start" | "list_tight" => matches!(value, ComrakNodeValue::List(_)),
59
+ "fenced" | "fence_info" => matches!(value, ComrakNodeValue::CodeBlock(_)),
60
+ "alert_type" => matches!(value, ComrakNodeValue::Alert(_)),
61
+ _ => return None,
62
+ };
63
+
64
+ Some(supported)
65
+ }
66
+
34
67
  impl CommonmarkerNode {
35
68
  pub fn new(ruby: &Ruby, args: &[Value]) -> Result<Self, magnus::Error> {
36
69
  let args = scan_args::scan_args::<_, (), (), (), _, ()>(args)?;
@@ -784,6 +817,63 @@ impl CommonmarkerNode {
784
817
  }
785
818
  }
786
819
 
820
+ /// Returns `true` or `false` if the given method is only available for
821
+ /// certain node types, and `nil` if its availability does not depend on
822
+ /// the node's type. Backs `Node#respond_to?`.
823
+ fn supports_method(rb_self: &Self, name: Symbol) -> Option<bool> {
824
+ let node = rb_self.inner.borrow();
825
+ let name = name.to_string();
826
+ let property = name.trim_end_matches('=').trim_end_matches('?');
827
+
828
+ value_supports(&node.data.value, property)
829
+ }
830
+
831
+ fn get_literal(ruby: &Ruby, rb_self: &Self) -> Result<String, magnus::Error> {
832
+ let node = rb_self.inner.borrow();
833
+
834
+ match &node.data.value {
835
+ ComrakNodeValue::Text(text) => Ok(text.to_string()),
836
+ ComrakNodeValue::Code(code) => Ok(code.literal.to_string()),
837
+ ComrakNodeValue::CodeBlock(code_block) => Ok(code_block.literal.to_string()),
838
+ ComrakNodeValue::HtmlBlock(html_block) => Ok(html_block.literal.to_string()),
839
+ ComrakNodeValue::HtmlInline(html_inline) => Ok(html_inline.to_string()),
840
+ ComrakNodeValue::Raw(raw) => Ok(raw.to_string()),
841
+ ComrakNodeValue::Math(math) => Ok(math.literal.to_string()),
842
+ ComrakNodeValue::FrontMatter(front_matter) => Ok(front_matter.to_string()),
843
+ _ => Err(magnus::Error::new(
844
+ ruby.exception_type_error(),
845
+ "node does not have a literal",
846
+ )),
847
+ }
848
+ }
849
+
850
+ fn set_literal(
851
+ ruby: &Ruby,
852
+ rb_self: &Self,
853
+ new_literal: String,
854
+ ) -> Result<bool, magnus::Error> {
855
+ let mut node = rb_self.inner.borrow_mut();
856
+
857
+ match node.data.value {
858
+ ComrakNodeValue::Text(ref mut text) => *text = new_literal.into(),
859
+ ComrakNodeValue::Code(ref mut code) => code.literal = new_literal,
860
+ ComrakNodeValue::CodeBlock(ref mut code_block) => code_block.literal = new_literal,
861
+ ComrakNodeValue::HtmlBlock(ref mut html_block) => html_block.literal = new_literal,
862
+ ComrakNodeValue::HtmlInline(ref mut html_inline) => *html_inline = new_literal,
863
+ ComrakNodeValue::Raw(ref mut raw) => *raw = new_literal,
864
+ ComrakNodeValue::Math(ref mut math) => math.literal = new_literal,
865
+ ComrakNodeValue::FrontMatter(ref mut front_matter) => *front_matter = new_literal,
866
+ _ => {
867
+ return Err(magnus::Error::new(
868
+ ruby.exception_type_error(),
869
+ "node does not have a literal",
870
+ ))
871
+ }
872
+ }
873
+
874
+ Ok(true)
875
+ }
876
+
787
877
  fn get_title(ruby: &Ruby, rb_self: &Self) -> Result<String, magnus::Error> {
788
878
  let node = rb_self.inner.borrow();
789
879
 
@@ -1292,6 +1382,14 @@ pub fn init(ruby: &Ruby, m_commonmarker: RModule) -> Result<(), magnus::Error> {
1292
1382
  method!(CommonmarkerNode::set_string_content, 1),
1293
1383
  )?;
1294
1384
 
1385
+ c_node.define_method("literal", method!(CommonmarkerNode::get_literal, 0))?;
1386
+ c_node.define_method("literal=", method!(CommonmarkerNode::set_literal, 1))?;
1387
+
1388
+ c_node.define_method(
1389
+ "node_supports?",
1390
+ method!(CommonmarkerNode::supports_method, 1),
1391
+ )?;
1392
+
1295
1393
  c_node.define_method("url", method!(CommonmarkerNode::get_url, 0))?;
1296
1394
  c_node.define_method("url=", method!(CommonmarkerNode::set_url, 1))?;
1297
1395
  c_node.define_method("title", method!(CommonmarkerNode::get_title, 0))?;
@@ -68,8 +68,8 @@ module Commonmarker
68
68
  syntax_highlighter: {
69
69
  theme: "base16-ocean.dark",
70
70
  path: "",
71
- },
72
- }
71
+ }.freeze,
72
+ }.freeze
73
73
 
74
74
  class << self
75
75
  include Commonmarker::Utils
@@ -28,10 +28,8 @@ module Commonmarker
28
28
  :fence_info,
29
29
  :alert_type,
30
30
  ].filter_map do |name|
31
- [name, __send__(name)]
32
- rescue StandardError
33
- nil
34
- end.compact
31
+ [name, __send__(name)] if respond_to?(name)
32
+ end
35
33
 
36
34
  printer.seplist(attrs) do |name, value|
37
35
  printer.text("#{name}=")
@@ -8,6 +8,18 @@ module Commonmarker
8
8
  include Enumerable
9
9
  include Inspect
10
10
 
11
+ # Public: Whether this node responds to the given method.
12
+ #
13
+ # name - A {Symbol} or {String} naming the method.
14
+ # include_all - A {Boolean} indicating whether to consider private methods.
15
+ #
16
+ # Returns a {Boolean}.
17
+ def respond_to?(name, include_all = false)
18
+ return false if node_supports?(name.to_sym) == false
19
+
20
+ super
21
+ end
22
+
11
23
  # Public: An iterator that "walks the tree," descending into children recursively.
12
24
  #
13
25
  # blk - A {Proc} representing the action to take for each child
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Commonmarker
4
- VERSION = "2.9.0"
4
+ VERSION = "2.10.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commonmarker
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.0
4
+ version: 2.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen Torikian
@@ -24,34 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.9'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '13.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '13.0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake-compiler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.2'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.2'
55
27
  description: A fast, safe, extensible parser for CommonMark. This wraps the comrak
56
28
  Rust crate.
57
29
  executables: []