commonmarker 2.10.0 → 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.
@@ -1,5 +1,7 @@
1
- use magnus::{TryConvert, Value};
1
+ use magnus::value::ReprValue;
2
+ use magnus::Value;
2
3
 
3
- pub fn try_convert_string(value: Value) -> Option<String> {
4
- TryConvert::try_convert(value).ok()
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
  }
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Commonmarker
4
+ # Raised when a document nests more deeply than the `max_nesting_depth`
5
+ # parse option allows.
6
+ class NestingDepthExceededError < StandardError; end
7
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Commonmarker
3
+ class Commonmarker
4
4
  class Node
5
5
  class Ast
6
6
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "pp"
4
4
 
5
- module Commonmarker
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
- printer.group(PP_INDENT_SIZE) do
42
- children = []
43
- node = first_child
44
- while node
45
- children << node
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
@@ -3,7 +3,7 @@
3
3
  require "commonmarker/node/ast"
4
4
  require "commonmarker/node/inspect"
5
5
 
6
- module Commonmarker
6
+ class Commonmarker
7
7
  class Node
8
8
  include Enumerable
9
9
  include Inspect
@@ -20,15 +20,28 @@ module Commonmarker
20
20
  super
21
21
  end
22
22
 
23
- # Public: An iterator that "walks the tree," descending into children recursively.
23
+ # Public: An iterator that "walks the tree," yielding each node and then
24
+ # its descendants, in document order.
24
25
  #
25
- # blk - A {Proc} representing the action to take for each child
26
+ # The walk is iterative rather than recursive, so arbitrarily deep trees
27
+ # can't exhaust the stack.
28
+ #
29
+ # blk - A {Proc} representing the action to take for each node
26
30
  def walk(&block)
27
31
  return enum_for(:walk) unless block
28
32
 
29
33
  yield self
30
- each do |child|
31
- child.walk(&block)
34
+
35
+ # Like #each, the next sibling is captured before a node is yielded, so
36
+ # the block may detach or replace the node it is given. `pending` holds
37
+ # the sibling to resume at once each ancestor's subtree is done.
38
+ pending = []
39
+ node = first_child
40
+ while node
41
+ next_node = node.next_sibling
42
+ yield node
43
+ pending << next_node if next_node
44
+ node = node.first_child || pending.pop
32
45
  end
33
46
  end
34
47
 
@@ -50,28 +63,22 @@ module Commonmarker
50
63
  # plugins - A {Hash} of additional plugins.
51
64
  #
52
65
  # Returns a {String} of HTML.
53
- def to_html(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
66
+ def to_html(options: {}, plugins: {})
54
67
  raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
55
68
 
56
- opts = Config.process_options(options)
57
- plugins = Config.process_plugins(plugins)
58
-
59
- 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)
60
70
  end
61
71
 
62
72
  # Public: Convert the node to a CommonMark string.
63
73
  #
64
- # options - A {Symbol} or {Array of Symbol}s indicating the render options
74
+ # options - A {Hash} of render, parse, and extension options.
65
75
  # plugins - A {Hash} of additional plugins.
66
76
  #
67
77
  # Returns a {String}.
68
- def to_commonmark(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
78
+ def to_commonmark(options: {}, plugins: {})
69
79
  raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
70
80
 
71
- opts = Config.process_options(options)
72
- plugins = Config.process_plugins(plugins)
73
-
74
- 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)
75
82
  end
76
83
  end
77
84
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Commonmarker
4
- VERSION = "2.10.0"
3
+ class Commonmarker
4
+ VERSION = "3.0.0"
5
5
  end
data/lib/commonmarker.rb CHANGED
@@ -2,28 +2,24 @@
2
2
 
3
3
  require_relative "commonmarker/extension"
4
4
 
5
- require "commonmarker/utils"
5
+ require "commonmarker/errors"
6
6
  require "commonmarker/node"
7
- require "commonmarker/config"
8
- require "commonmarker/renderer"
9
7
  require "commonmarker/version"
10
8
 
11
- module Commonmarker
9
+ class Commonmarker
12
10
  class << self
13
- # Public: Parses a CommonMark string into an HTML string.
11
+ # Public: Parses a CommonMark string into an AST.
14
12
  #
15
13
  # text - A {String} of text
16
14
  # options - A {Hash} of render, parse, and extension options to transform the text.
17
15
  #
18
- # Returns the `parser` node.
19
- def parse(text, options: Commonmarker::Config::OPTIONS)
20
- raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
21
- raise TypeError, "text must be UTF-8 encoded; got #{text.encoding}!" unless text.encoding.name == "UTF-8"
22
- raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
23
-
24
- opts = Config.process_options(options)
25
-
26
- 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)
27
23
  end
28
24
 
29
25
  # Public: Parses a CommonMark string into an HTML string.
@@ -32,16 +28,79 @@ module Commonmarker
32
28
  # options - A {Hash} of render, parse, and extension options to transform the text.
33
29
  # plugins - A {Hash} of additional plugins.
34
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
+ #
35
35
  # Returns a {String} of converted HTML.
36
- def to_html(text, options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
37
- raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
38
- raise TypeError, "text must be UTF-8 encoded; got #{text.encoding}!" unless text.encoding.name == "UTF-8"
39
- raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
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)
40
63
 
41
- opts = Config.process_options(options)
42
- plugins = Config.process_plugins(plugins)
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)
43
90
 
44
- commonmark_to_html(text, parse: opts.fetch(:parse, {}), render: opts.fetch(:render, {}), extension: opts.fetch(:extension, {}), plugins: plugins)
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}!"
45
102
  end
103
+
104
+ raise TypeError, "text must be UTF-8 encoded; got #{text.encoding}!" unless text.encoding == Encoding::UTF_8
46
105
  end
47
106
  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.10.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen Torikian
@@ -35,24 +35,24 @@ files:
35
35
  - Cargo.toml
36
36
  - LICENSE.txt
37
37
  - README.md
38
+ - UPGRADING.md
38
39
  - ext/commonmarker/Cargo.toml
39
40
  - ext/commonmarker/build.rs
40
41
  - ext/commonmarker/extconf.rb
41
42
  - ext/commonmarker/src/lib.rs
43
+ - ext/commonmarker/src/nesting.rs
42
44
  - ext/commonmarker/src/node.rs
43
45
  - ext/commonmarker/src/options.rs
44
46
  - ext/commonmarker/src/plugins.rs
45
47
  - ext/commonmarker/src/plugins/syntax_highlighting.rs
48
+ - ext/commonmarker/src/rctree.rs
46
49
  - ext/commonmarker/src/utils.rs
47
50
  - lib/commonmarker.rb
48
- - lib/commonmarker/config.rb
49
- - lib/commonmarker/constants.rb
51
+ - lib/commonmarker/errors.rb
50
52
  - lib/commonmarker/extension.rb
51
53
  - lib/commonmarker/node.rb
52
54
  - lib/commonmarker/node/ast.rb
53
55
  - lib/commonmarker/node/inspect.rb
54
- - lib/commonmarker/renderer.rb
55
- - lib/commonmarker/utils.rb
56
56
  - lib/commonmarker/version.rb
57
57
  homepage: https://github.com/gjtorikian/commonmarker
58
58
  licenses:
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  - !ruby/object:Gem::Version
80
80
  version: '3.4'
81
81
  requirements: []
82
- rubygems_version: 4.0.10
82
+ rubygems_version: 4.0.20
83
83
  specification_version: 4
84
84
  summary: CommonMark parser and renderer. Written in Rust, wrapped in Ruby.
85
85
  test_files: []
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Commonmarker
4
- module Config
5
- # For details, see
6
- # https://github.com/kivikakk/comrak/blob/162ef9354deb2c9b4a4e05be495aa372ba5bb696/src/main.rs#L201
7
- OPTIONS = {
8
- parse: {
9
- smart: false,
10
- default_info_string: "",
11
- relaxed_tasklist_matching: false,
12
- relaxed_autolinks: false,
13
- leave_footnote_definitions: false,
14
- ignore_setext: false,
15
- sourcepos_chars: false,
16
- }.freeze,
17
- render: {
18
- hardbreaks: true,
19
- github_pre_lang: true,
20
- full_info_string: false,
21
- width: 80,
22
- unsafe: false,
23
- escape: false,
24
- sourcepos: false,
25
- escaped_char_spans: true,
26
- ignore_empty_links: false,
27
- gfm_quirks: false,
28
- prefer_fenced: false,
29
- tasklist_classes: false,
30
- compact_html: false,
31
- alert_style: "specific",
32
- }.freeze,
33
- extension: {
34
- strikethrough: true,
35
- tagfilter: true,
36
- table: true,
37
- autolink: true,
38
- tasklist: true,
39
- superscript: false,
40
- header_ids: "",
41
- header_id_prefix_in_href: false,
42
- footnotes: false,
43
- inline_footnotes: false,
44
- description_lists: false,
45
- front_matter_delimiter: "",
46
- multiline_block_quotes: false,
47
- math_dollars: false,
48
- math_code: false,
49
- math_latex: false,
50
- shortcodes: true,
51
- wikilinks_title_before_pipe: false,
52
- wikilinks_title_after_pipe: false,
53
- underline: false,
54
- spoiler: false,
55
- greentext: false,
56
- subscript: false,
57
- subtext: false,
58
- alerts: false,
59
- cjk_friendly_emphasis: false,
60
- highlight: false,
61
- insert: false,
62
- block_directive: false,
63
- }.freeze,
64
- format: [:html].freeze,
65
- }.freeze
66
-
67
- PLUGINS = {
68
- syntax_highlighter: {
69
- theme: "base16-ocean.dark",
70
- path: "",
71
- }.freeze,
72
- }.freeze
73
-
74
- class << self
75
- include Commonmarker::Utils
76
-
77
- def process_options(options)
78
- {
79
- parse: process_parse_options(options[:parse].dup),
80
- render: process_render_options(options[:render].dup),
81
- extension: process_extension_options(options[:extension].dup),
82
- }
83
- end
84
-
85
- def process_plugins(plugins)
86
- {
87
- syntax_highlighter: process_syntax_highlighter_plugin(plugins&.fetch(:syntax_highlighter, nil)),
88
- }
89
- end
90
- end
91
-
92
- [:parse, :render, :extension].each do |type|
93
- define_singleton_method :"process_#{type}_options" do |options|
94
- Commonmarker::Config::OPTIONS[type].each_with_object({}) do |(key, value), hash|
95
- if options.nil? || !options.key?(key) # option not provided, use the default
96
- hash[key] = value
97
- next
98
- end
99
-
100
- if options[key].nil? # # option explicitly not included, remove it
101
- options.delete(key)
102
- next
103
- end
104
-
105
- hash[key] = fetch_kv(options, key, value, type)
106
- end
107
- end
108
- end
109
-
110
- define_singleton_method :process_syntax_highlighter_plugin do |options|
111
- return if options.nil? # plugin explicitly nil, remove it
112
-
113
- raise TypeError, "Expected a Hash for syntax_highlighter plugin, got #{options.class}" unless options.is_a?(Hash)
114
- raise TypeError, "Expected a Hash for syntax_highlighter plugin, got nothing" if options.empty?
115
-
116
- Commonmarker::Config::PLUGINS[:syntax_highlighter].merge(options)
117
- end
118
- end
119
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Commonmarker
4
- module Constants
5
- BOOLS = [true, false].freeze
6
- end
7
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "set"
4
- require "stringio"
5
-
6
- module Commonmarker
7
- class Renderer
8
- end
9
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "commonmarker/constants"
4
-
5
- module Commonmarker
6
- module Utils
7
- include Commonmarker::Constants
8
-
9
- def fetch_kv(options, key, value, type)
10
- value_klass = value.class
11
-
12
- if Constants::BOOLS.include?(value) && BOOLS.include?(options[key])
13
- options[key]
14
- elsif options[key].is_a?(value_klass)
15
- options[key]
16
- else
17
- expected_type = Constants::BOOLS.include?(value) ? "Boolean" : value_klass.to_s
18
- raise TypeError, "#{type} option `:#{key}` must be #{expected_type}; got #{options[key].class}"
19
- end
20
- end
21
- end
22
- end