commonmarker 1.0.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Cargo.lock +52 -21
- data/README.md +150 -45
- data/ext/commonmarker/Cargo.toml +4 -2
- data/ext/commonmarker/src/lib.rs +33 -3
- data/ext/commonmarker/src/node.rs +1095 -0
- data/ext/commonmarker/src/options.rs +21 -0
- data/lib/commonmarker/config.rb +5 -0
- data/lib/commonmarker/node/ast.rb +8 -0
- data/lib/commonmarker/node/inspect.rb +57 -0
- data/lib/commonmarker/node.rb +65 -0
- data/lib/commonmarker/version.rb +1 -1
- data/lib/commonmarker.rb +17 -0
- metadata +6 -2
@@ -10,6 +10,7 @@ use crate::utils::try_convert_string;
|
|
10
10
|
|
11
11
|
const PARSE_SMART: &str = "smart";
|
12
12
|
const PARSE_DEFAULT_INFO_STRING: &str = "default_info_string";
|
13
|
+
const PARSE_RELAXED_AUTOLINKS: &str = "relaxed_autolinks";
|
13
14
|
|
14
15
|
fn iterate_parse_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
|
15
16
|
options_hash
|
@@ -21,6 +22,9 @@ fn iterate_parse_options(comrak_options: &mut ComrakOptions, options_hash: RHash
|
|
21
22
|
Ok(Cow::Borrowed(PARSE_DEFAULT_INFO_STRING)) => {
|
22
23
|
comrak_options.parse.default_info_string = try_convert_string(value);
|
23
24
|
}
|
25
|
+
Ok(Cow::Borrowed(PARSE_RELAXED_AUTOLINKS)) => {
|
26
|
+
comrak_options.parse.relaxed_autolinks = TryConvert::try_convert(value)?;
|
27
|
+
}
|
24
28
|
_ => {}
|
25
29
|
}
|
26
30
|
Ok(ForEach::Continue)
|
@@ -34,6 +38,7 @@ const RENDER_WIDTH: &str = "width";
|
|
34
38
|
const RENDER_UNSAFE: &str = "unsafe";
|
35
39
|
const RENDER_ESCAPE: &str = "escape";
|
36
40
|
const RENDER_SOURCEPOS: &str = "sourcepos";
|
41
|
+
const RENDER_ESCAPED_CHAR_SPANS: &str = "escaped_char_spans";
|
37
42
|
|
38
43
|
fn iterate_render_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
|
39
44
|
options_hash
|
@@ -57,6 +62,9 @@ fn iterate_render_options(comrak_options: &mut ComrakOptions, options_hash: RHas
|
|
57
62
|
Ok(Cow::Borrowed(RENDER_SOURCEPOS)) => {
|
58
63
|
comrak_options.render.sourcepos = TryConvert::try_convert(value)?;
|
59
64
|
}
|
65
|
+
Ok(Cow::Borrowed(RENDER_ESCAPED_CHAR_SPANS)) => {
|
66
|
+
comrak_options.render.escaped_char_spans = TryConvert::try_convert(value)?;
|
67
|
+
}
|
60
68
|
_ => {}
|
61
69
|
}
|
62
70
|
Ok(ForEach::Continue)
|
@@ -75,6 +83,9 @@ const EXTENSION_FOOTNOTES: &str = "footnotes";
|
|
75
83
|
const EXTENSION_DESCRIPTION_LISTS: &str = "description_lists";
|
76
84
|
const EXTENSION_FRONT_MATTER_DELIMITER: &str = "front_matter_delimiter";
|
77
85
|
const EXTENSION_SHORTCODES: &str = "shortcodes";
|
86
|
+
const EXTENSION_MULTILINE_BLOCK_QUOTES: &str = "multiline_block_quotes";
|
87
|
+
const EXTENSION_MATH_DOLLARS: &str = "math_dollars";
|
88
|
+
const EXTENSION_MATH_CODE: &str = "math_code";
|
78
89
|
|
79
90
|
fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
|
80
91
|
options_hash
|
@@ -117,6 +128,16 @@ fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: R
|
|
117
128
|
Ok(Cow::Borrowed(EXTENSION_SHORTCODES)) => {
|
118
129
|
comrak_options.extension.shortcodes = TryConvert::try_convert(value)?;
|
119
130
|
}
|
131
|
+
Ok(Cow::Borrowed(EXTENSION_MULTILINE_BLOCK_QUOTES)) => {
|
132
|
+
comrak_options.extension.multiline_block_quotes =
|
133
|
+
TryConvert::try_convert(value)?;
|
134
|
+
}
|
135
|
+
Ok(Cow::Borrowed(EXTENSION_MATH_DOLLARS)) => {
|
136
|
+
comrak_options.extension.math_dollars = TryConvert::try_convert(value)?;
|
137
|
+
}
|
138
|
+
Ok(Cow::Borrowed(EXTENSION_MATH_CODE)) => {
|
139
|
+
comrak_options.extension.math_code = TryConvert::try_convert(value)?;
|
140
|
+
}
|
120
141
|
_ => {}
|
121
142
|
}
|
122
143
|
Ok(ForEach::Continue)
|
data/lib/commonmarker/config.rb
CHANGED
@@ -8,6 +8,7 @@ module Commonmarker
|
|
8
8
|
parse: {
|
9
9
|
smart: false,
|
10
10
|
default_info_string: "",
|
11
|
+
relaxed_autolinks: false,
|
11
12
|
}.freeze,
|
12
13
|
render: {
|
13
14
|
hardbreaks: true,
|
@@ -16,6 +17,7 @@ module Commonmarker
|
|
16
17
|
unsafe: false,
|
17
18
|
escape: false,
|
18
19
|
sourcepos: false,
|
20
|
+
escaped_char_spans: true,
|
19
21
|
}.freeze,
|
20
22
|
extension: {
|
21
23
|
strikethrough: true,
|
@@ -29,6 +31,9 @@ module Commonmarker
|
|
29
31
|
description_lists: false,
|
30
32
|
front_matter_delimiter: "",
|
31
33
|
shortcodes: true,
|
34
|
+
multiline_block_quotes: false,
|
35
|
+
math_dollars: false,
|
36
|
+
math_code: false,
|
32
37
|
},
|
33
38
|
format: [:html].freeze,
|
34
39
|
}.freeze
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "pp"
|
4
|
+
|
5
|
+
module Commonmarker
|
6
|
+
class Node
|
7
|
+
module Inspect
|
8
|
+
PP_INDENT_SIZE = 2
|
9
|
+
|
10
|
+
def inspect
|
11
|
+
PP.pp(self, +"", Float::INFINITY)
|
12
|
+
end
|
13
|
+
|
14
|
+
# @param printer [PrettyPrint] pp
|
15
|
+
def pretty_print(printer)
|
16
|
+
printer.group(PP_INDENT_SIZE, "#<#{self.class}(#{type}):", ">") do
|
17
|
+
printer.breakable
|
18
|
+
|
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|
|
30
|
+
[name, __send__(name)]
|
31
|
+
rescue StandardError
|
32
|
+
nil
|
33
|
+
end.compact
|
34
|
+
|
35
|
+
printer.seplist(attrs) do |name, value|
|
36
|
+
printer.text("#{name}=")
|
37
|
+
printer.pp(value)
|
38
|
+
end
|
39
|
+
|
40
|
+
if first_child
|
41
|
+
printer.breakable
|
42
|
+
printer.group(PP_INDENT_SIZE) do
|
43
|
+
children = []
|
44
|
+
node = first_child
|
45
|
+
while node
|
46
|
+
children << node
|
47
|
+
node = node.next_sibling
|
48
|
+
end
|
49
|
+
printer.text("children=")
|
50
|
+
printer.pp(children)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "commonmarker/node/ast"
|
4
|
+
require "commonmarker/node/inspect"
|
5
|
+
|
6
|
+
module Commonmarker
|
7
|
+
class Node
|
8
|
+
include Enumerable
|
9
|
+
include Inspect
|
10
|
+
|
11
|
+
# Public: An iterator that "walks the tree," descending into children recursively.
|
12
|
+
#
|
13
|
+
# blk - A {Proc} representing the action to take for each child
|
14
|
+
def walk(&block)
|
15
|
+
return enum_for(:walk) unless block
|
16
|
+
|
17
|
+
yield self
|
18
|
+
each do |child|
|
19
|
+
child.walk(&block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Public: Iterate over the children (if any) of the current pointer.
|
24
|
+
def each
|
25
|
+
return enum_for(:each) unless block_given?
|
26
|
+
|
27
|
+
child = first_child
|
28
|
+
while child
|
29
|
+
next_child = child.next_sibling
|
30
|
+
yield child
|
31
|
+
child = next_child
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Public: Converts a node to an HTML string.
|
36
|
+
#
|
37
|
+
# options - A {Hash} of render, parse, and extension options to transform the text.
|
38
|
+
# plugins - A {Hash} of additional plugins.
|
39
|
+
#
|
40
|
+
# Returns a {String} of HTML.
|
41
|
+
def to_html(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
|
42
|
+
raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
|
43
|
+
|
44
|
+
opts = Config.process_options(options)
|
45
|
+
plugins = Config.process_plugins(plugins)
|
46
|
+
|
47
|
+
node_to_html(options: opts, plugins: plugins).force_encoding("utf-8")
|
48
|
+
end
|
49
|
+
|
50
|
+
# Public: Convert the node to a CommonMark string.
|
51
|
+
#
|
52
|
+
# options - A {Symbol} or {Array of Symbol}s indicating the render options
|
53
|
+
# plugins - A {Hash} of additional plugins.
|
54
|
+
#
|
55
|
+
# Returns a {String}.
|
56
|
+
def to_commonmark(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
|
57
|
+
raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
|
58
|
+
|
59
|
+
opts = Config.process_options(options)
|
60
|
+
plugins = Config.process_plugins(plugins)
|
61
|
+
|
62
|
+
node_to_commonmark(options: opts, plugins: plugins).force_encoding("utf-8")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/commonmarker/version.rb
CHANGED
data/lib/commonmarker.rb
CHANGED
@@ -3,12 +3,29 @@
|
|
3
3
|
require_relative "commonmarker/extension"
|
4
4
|
|
5
5
|
require "commonmarker/utils"
|
6
|
+
require "commonmarker/node"
|
6
7
|
require "commonmarker/config"
|
7
8
|
require "commonmarker/renderer"
|
8
9
|
require "commonmarker/version"
|
9
10
|
|
10
11
|
module Commonmarker
|
11
12
|
class << self
|
13
|
+
# Public: Parses a CommonMark string into an HTML string.
|
14
|
+
#
|
15
|
+
# text - A {String} of text
|
16
|
+
# options - A {Hash} of render, parse, and extension options to transform the text.
|
17
|
+
#
|
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, options: opts)
|
27
|
+
end
|
28
|
+
|
12
29
|
# Public: Parses a CommonMark string into an HTML string.
|
13
30
|
#
|
14
31
|
# text - A {String} of text
|
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: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Garen Torikian
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-
|
12
|
+
date: 2024-04-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rb_sys
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- ext/commonmarker/Cargo.toml
|
70
70
|
- ext/commonmarker/extconf.rb
|
71
71
|
- ext/commonmarker/src/lib.rs
|
72
|
+
- ext/commonmarker/src/node.rs
|
72
73
|
- ext/commonmarker/src/options.rs
|
73
74
|
- ext/commonmarker/src/plugins.rs
|
74
75
|
- ext/commonmarker/src/plugins/syntax_highlighting.rs
|
@@ -77,6 +78,9 @@ files:
|
|
77
78
|
- lib/commonmarker/config.rb
|
78
79
|
- lib/commonmarker/constants.rb
|
79
80
|
- lib/commonmarker/extension.rb
|
81
|
+
- lib/commonmarker/node.rb
|
82
|
+
- lib/commonmarker/node/ast.rb
|
83
|
+
- lib/commonmarker/node/inspect.rb
|
80
84
|
- lib/commonmarker/renderer.rb
|
81
85
|
- lib/commonmarker/utils.rb
|
82
86
|
- lib/commonmarker/version.rb
|