rbbcode 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rbbcode/node_extensions.rb +47 -47
- data/lib/rbbcode.rb +13 -5
- metadata +24 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3894f3d4d56f04b0c0d7ad5fa194eef37e9933f49993dc9ddeb243b4bdf1bda9
|
4
|
+
data.tar.gz: f0167cb15f14b2c37dbbd55205e4e3b08275f26b8d8c9d9e9a95c684741c451f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a40401398844f12b7b874cedfc173bb98a887bc4dad718d2d30ece44b338060c84441e87265b12b91e777608a94b29f7eb9f074f49f810acb1f2dc1de9e01be
|
7
|
+
data.tar.gz: f0712fb419b27353fd569ce150adf74c47ef2d45cc6ed16dc80bcb6bd759354e4b3233596dfa942cda9845cb288d6a720b7a73dc2dff78b16538c17a8736b315
|
@@ -7,7 +7,7 @@ class RbbCode
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module RecursiveConversion
|
10
|
-
def recursively_convert(node, output_method, depth = 0)
|
10
|
+
def recursively_convert(node, output_method, options, depth = 0)
|
11
11
|
if node.terminal?
|
12
12
|
if node.respond_to?(output_method)
|
13
13
|
# This is a terminal node with a custom implementation of the output
|
@@ -23,12 +23,12 @@ class RbbCode
|
|
23
23
|
if node.respond_to?(output_method)
|
24
24
|
# This is a non-terminal node with a custom implementation of the
|
25
25
|
# output method.
|
26
|
-
node.send(output_method)
|
26
|
+
node.send(output_method, options)
|
27
27
|
else
|
28
28
|
# This is a non-terminal node without a custom implementation of the
|
29
29
|
# output method. Convert all its child nodes and concatenate the results.
|
30
30
|
node.elements.collect do |sub_node|
|
31
|
-
recursively_convert(sub_node, output_method, depth + 1)
|
31
|
+
recursively_convert(sub_node, output_method, options, depth + 1)
|
32
32
|
end.join
|
33
33
|
end
|
34
34
|
end
|
@@ -36,32 +36,32 @@ class RbbCode
|
|
36
36
|
end
|
37
37
|
|
38
38
|
module DocumentNode
|
39
|
-
def to_html
|
40
|
-
contents.elements.collect { |p| p.to_html }.join
|
39
|
+
def to_html(options)
|
40
|
+
contents.elements.collect { |p| p.to_html(options) }.join
|
41
41
|
end
|
42
42
|
|
43
|
-
def to_markdown
|
44
|
-
contents.elements.collect { |p| p.to_markdown }.join
|
43
|
+
def to_markdown(options)
|
44
|
+
contents.elements.collect { |p| p.to_markdown(options) }.join
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
module ParagraphNode
|
49
49
|
include RecursiveConversion
|
50
50
|
|
51
|
-
def to_html
|
51
|
+
def to_html(options)
|
52
52
|
# Convert all child nodes, concatenate the results,
|
53
53
|
# and wrap the concatenated HTML in <p> tags.
|
54
54
|
html = elements.collect do |node|
|
55
|
-
recursively_convert(node, :to_html)
|
55
|
+
recursively_convert(node, :to_html, options)
|
56
56
|
end.join
|
57
57
|
"\n<p>" + html + "</p>\n"
|
58
58
|
end
|
59
59
|
|
60
|
-
def to_markdown
|
60
|
+
def to_markdown(options)
|
61
61
|
# Convert all child nodes, concatenate the results,
|
62
62
|
# and append newline characters.
|
63
63
|
markdown = elements.collect do |node|
|
64
|
-
recursively_convert(node, :to_markdown)
|
64
|
+
recursively_convert(node, :to_markdown, options)
|
65
65
|
end.join
|
66
66
|
markdown + "\n\n"
|
67
67
|
end
|
@@ -70,12 +70,12 @@ class RbbCode
|
|
70
70
|
module BlockquoteNode
|
71
71
|
include RecursiveConversion
|
72
72
|
|
73
|
-
def to_html
|
73
|
+
def to_html(options)
|
74
74
|
# Detect paragraph breaks and wrap the result in <blockquote> tags.
|
75
75
|
paragraphs = []
|
76
76
|
cur_para = ''
|
77
77
|
lines.elements.each do |line|
|
78
|
-
inner = recursively_convert(line, :to_html)
|
78
|
+
inner = recursively_convert(line, :to_html, options)
|
79
79
|
unless inner.blank?
|
80
80
|
cur_para << inner
|
81
81
|
if line.post_breaks == 1
|
@@ -93,11 +93,11 @@ class RbbCode
|
|
93
93
|
"\n<blockquote>" + inner + "</blockquote>\n"
|
94
94
|
end
|
95
95
|
|
96
|
-
def to_markdown
|
96
|
+
def to_markdown(options)
|
97
97
|
# Add a > character per line, preserving linebreaks as they are in the source.
|
98
98
|
# Then append two newlines.
|
99
99
|
'> ' + lines.elements.inject('') do |output, line|
|
100
|
-
inner_markdown = recursively_convert(line.contents, :to_markdown)
|
100
|
+
inner_markdown = recursively_convert(line.contents, :to_markdown, options)
|
101
101
|
output + inner_markdown + ("\n> " * line.post_breaks)
|
102
102
|
end + "\n\n"
|
103
103
|
end
|
@@ -114,33 +114,33 @@ class RbbCode
|
|
114
114
|
module ListNode
|
115
115
|
include RecursiveConversion
|
116
116
|
|
117
|
-
def to_html
|
117
|
+
def to_html(options)
|
118
118
|
# Convert the :contents child node (defined in the .treetop file)
|
119
119
|
# and wrap the result in <ul> tags.
|
120
|
-
"\n<ul>" + recursively_convert(items, :to_html) + "</ul>\n"
|
120
|
+
"\n<ul>" + recursively_convert(items, :to_html, options) + "</ul>\n"
|
121
121
|
end
|
122
122
|
|
123
|
-
def to_markdown
|
123
|
+
def to_markdown(options)
|
124
124
|
# Convert the :contents child node (defined in the .treetop file).
|
125
125
|
# (Unlike with HTML, no outer markup needed.) Then append an extra
|
126
126
|
# newline, for a total of two at the end.
|
127
|
-
recursively_convert(items, :to_markdown) + "\n"
|
127
|
+
recursively_convert(items, :to_markdown, options) + "\n"
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
131
131
|
module ListItemNode
|
132
132
|
include RecursiveConversion
|
133
133
|
|
134
|
-
def to_html
|
134
|
+
def to_html(options)
|
135
135
|
# Convert the :contents child node (defined in the .treetop file)
|
136
136
|
# and wrap the result in <li> tags.
|
137
|
-
"\n<li>" + recursively_convert(contents, :to_html) + "</li>\n"
|
137
|
+
"\n<li>" + recursively_convert(contents, :to_html, options) + "</li>\n"
|
138
138
|
end
|
139
139
|
|
140
|
-
def to_markdown
|
140
|
+
def to_markdown(options)
|
141
141
|
# Convert the :contents child node (defined in the .treetop file)
|
142
142
|
# and add * characters.
|
143
|
-
"* " + recursively_convert(contents, :to_html) + "\n"
|
143
|
+
"* " + recursively_convert(contents, :to_html, options) + "\n"
|
144
144
|
end
|
145
145
|
end
|
146
146
|
|
@@ -150,7 +150,7 @@ class RbbCode
|
|
150
150
|
module URLTagNode
|
151
151
|
include Attributes
|
152
152
|
|
153
|
-
def url_to_html
|
153
|
+
def url_to_html(options)
|
154
154
|
# The :url child node (defined in the .treetop file) may or may not exist,
|
155
155
|
# depending on how the link is formatted in the BBCode source.
|
156
156
|
if respond_to?(:url) and respond_to?(:text)
|
@@ -162,7 +162,7 @@ class RbbCode
|
|
162
162
|
end
|
163
163
|
end
|
164
164
|
|
165
|
-
def url_to_markdown
|
165
|
+
def url_to_markdown(options)
|
166
166
|
if respond_to?(:url) and respond_to?(:text)
|
167
167
|
# This is a URL tag formatted like [url=http://example.com]Example[/url].
|
168
168
|
'[' + text.text_value + '](' + strip_quotes(url.text_value) + ')'
|
@@ -176,17 +176,17 @@ class RbbCode
|
|
176
176
|
# You won't find this module in the .treetop file. Instead, it's effectively a specialization
|
177
177
|
# of TagNode, which calls to ImgTagNode when processing an img tag.
|
178
178
|
module ImgTagNode
|
179
|
-
def img_to_html
|
179
|
+
def img_to_html(options)
|
180
180
|
'<img src="' + inner_bbcode + '" alt="Image"/>'
|
181
181
|
end
|
182
182
|
|
183
|
-
def img_to_markdown
|
183
|
+
def img_to_markdown(options)
|
184
184
|
"![Image](#{inner_bbcode})"
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
188
188
|
module UTagNode
|
189
|
-
def u_to_markdown
|
189
|
+
def u_to_markdown(options)
|
190
190
|
# Underlining is unsupported in Markdown. So we just ignore [u] tags.
|
191
191
|
inner_bbcode
|
192
192
|
end
|
@@ -216,27 +216,27 @@ class RbbCode
|
|
216
216
|
contents.elements.collect { |e| e.text_value }.join
|
217
217
|
end
|
218
218
|
|
219
|
-
def inner_html
|
219
|
+
def inner_html(options)
|
220
220
|
contents.elements.collect do |node|
|
221
|
-
recursively_convert(node, :to_html)
|
221
|
+
recursively_convert(node, :to_html, options)
|
222
222
|
end.join
|
223
223
|
end
|
224
224
|
|
225
|
-
def inner_markdown
|
225
|
+
def inner_markdown(options)
|
226
226
|
contents.elements.collect do |node|
|
227
|
-
recursively_convert(node, :to_markdown)
|
227
|
+
recursively_convert(node, :to_markdown, options)
|
228
228
|
end.join
|
229
229
|
end
|
230
230
|
|
231
|
-
def wrap_html(t)
|
232
|
-
"<#{t}>" + inner_html + "</#{t}>"
|
231
|
+
def wrap_html(t, options)
|
232
|
+
"<#{t}>" + inner_html(options) + "</#{t}>"
|
233
233
|
end
|
234
234
|
|
235
|
-
def wrap_markdown(t)
|
236
|
-
t + inner_markdown + t
|
235
|
+
def wrap_markdown(t, options)
|
236
|
+
t + inner_markdown(options) + t
|
237
237
|
end
|
238
238
|
|
239
|
-
def convert(output_format)
|
239
|
+
def convert(output_format, options)
|
240
240
|
# Consult TAG_MAPPINGS to decide how to process this type of tag.
|
241
241
|
t = TAG_MAPPINGS[output_format][tag_name]
|
242
242
|
if t.nil?
|
@@ -245,41 +245,41 @@ class RbbCode
|
|
245
245
|
# This type of tag requires more than just a simple mapping from one tag name
|
246
246
|
# to another. So we invoke a separate Ruby module.
|
247
247
|
extend(t)
|
248
|
-
send("#{tag_name}_to_#{output_format}")
|
248
|
+
send("#{tag_name}_to_#{output_format}", options)
|
249
249
|
# Thus, if our tag_name is"url, and TAG_MAPPINGS points us to URLTagNode,
|
250
250
|
# that module must define url_to_html.
|
251
251
|
else
|
252
252
|
# For this type of tag, a simple mapping from the tag name to a string (such as
|
253
253
|
# <i>) suffices.
|
254
|
-
send("wrap_#{output_format}", t)
|
254
|
+
send("wrap_#{output_format}", t, options)
|
255
255
|
end
|
256
256
|
end
|
257
257
|
|
258
|
-
def to_html
|
259
|
-
convert :html
|
258
|
+
def to_html(options)
|
259
|
+
convert :html, options
|
260
260
|
end
|
261
261
|
|
262
|
-
def to_markdown
|
263
|
-
convert :markdown
|
262
|
+
def to_markdown(options)
|
263
|
+
convert :markdown, options
|
264
264
|
end
|
265
265
|
end
|
266
266
|
|
267
267
|
module SingleBreakNode
|
268
|
-
def to_html
|
268
|
+
def to_html(options)
|
269
269
|
'<br/>'
|
270
270
|
end
|
271
271
|
|
272
|
-
def to_markdown
|
272
|
+
def to_markdown(options)
|
273
273
|
"\n"
|
274
274
|
end
|
275
275
|
end
|
276
276
|
|
277
277
|
module LiteralTextNode
|
278
|
-
def to_html
|
278
|
+
def to_html(options)
|
279
279
|
text_value
|
280
280
|
end
|
281
281
|
|
282
|
-
def to_markdown
|
282
|
+
def to_markdown(options)
|
283
283
|
text_value
|
284
284
|
end
|
285
285
|
end
|
data/lib/rbbcode.rb
CHANGED
@@ -29,23 +29,31 @@ class RbbCode
|
|
29
29
|
def initialize(options = {})
|
30
30
|
@options = {
|
31
31
|
:output_format => :html,
|
32
|
+
:emoticons => false,
|
32
33
|
:sanitize => true,
|
33
34
|
:sanitize_config => RbbCode::DEFAULT_SANITIZE_CONFIG
|
34
35
|
}.merge(options)
|
35
36
|
end
|
36
37
|
|
37
|
-
def convert(bb_code)
|
38
|
+
def convert(bb_code, options = {})
|
39
|
+
# Options passed to #convert will override any options passed to .new.
|
40
|
+
options = @options.merge(options)
|
41
|
+
output_format = options.delete(:output_format)
|
42
|
+
emoticons = options.delete(:emoticons)
|
43
|
+
sanitize = options.delete(:sanitize)
|
44
|
+
sanitize_config = options.delete(:sanitize_config)
|
45
|
+
|
38
46
|
# Collapse CRLFs to LFs. Then replace any solitary CRs with LFs.
|
39
47
|
bb_code = bb_code.gsub("\r\n", "\n").gsub("\r", "\n")
|
40
48
|
# Add linebreaks before and after so that paragraphs etc. can be recognized.
|
41
49
|
bb_code = "\n\n" + bb_code + "\n\n"
|
42
|
-
output = self.class.parser_class.new.parse(bb_code).send("to_#{
|
43
|
-
if
|
50
|
+
output = self.class.parser_class.new.parse(bb_code).send("to_#{output_format}", options)
|
51
|
+
if emoticons
|
44
52
|
output = convert_emoticons(output)
|
45
53
|
end
|
46
54
|
# Sanitization works for HTML only.
|
47
|
-
if
|
48
|
-
Sanitize.clean(output,
|
55
|
+
if output_format == :html and sanitize
|
56
|
+
Sanitize.clean(output, sanitize_config)
|
49
57
|
else
|
50
58
|
output
|
51
59
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbbcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarrett Colby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: treetop
|
@@ -28,72 +28,78 @@ dependencies:
|
|
28
28
|
name: sanitize
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6'
|
31
34
|
- - ">="
|
32
35
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
36
|
+
version: 6.0.2
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '6'
|
38
44
|
- - ">="
|
39
45
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
46
|
+
version: 6.0.2
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: minitest
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
44
50
|
requirements:
|
45
|
-
- - "
|
51
|
+
- - "~>"
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
53
|
+
version: '5'
|
48
54
|
type: :development
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
51
57
|
requirements:
|
52
|
-
- - "
|
58
|
+
- - "~>"
|
53
59
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
60
|
+
version: '5'
|
55
61
|
- !ruby/object:Gem::Dependency
|
56
62
|
name: minitest-reporters
|
57
63
|
requirement: !ruby/object:Gem::Requirement
|
58
64
|
requirements:
|
59
|
-
- - "
|
65
|
+
- - "~>"
|
60
66
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
67
|
+
version: '1'
|
62
68
|
type: :development
|
63
69
|
prerelease: false
|
64
70
|
version_requirements: !ruby/object:Gem::Requirement
|
65
71
|
requirements:
|
66
|
-
- - "
|
72
|
+
- - "~>"
|
67
73
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
74
|
+
version: '1'
|
69
75
|
- !ruby/object:Gem::Dependency
|
70
76
|
name: lorax
|
71
77
|
requirement: !ruby/object:Gem::Requirement
|
72
78
|
requirements:
|
73
|
-
- -
|
79
|
+
- - '='
|
74
80
|
- !ruby/object:Gem::Version
|
75
81
|
version: 0.3.0.rc2
|
76
82
|
type: :development
|
77
83
|
prerelease: false
|
78
84
|
version_requirements: !ruby/object:Gem::Requirement
|
79
85
|
requirements:
|
80
|
-
- -
|
86
|
+
- - '='
|
81
87
|
- !ruby/object:Gem::Version
|
82
88
|
version: 0.3.0.rc2
|
83
89
|
- !ruby/object:Gem::Dependency
|
84
90
|
name: rake
|
85
91
|
requirement: !ruby/object:Gem::Requirement
|
86
92
|
requirements:
|
87
|
-
- - "
|
93
|
+
- - "~>"
|
88
94
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
95
|
+
version: '13'
|
90
96
|
type: :development
|
91
97
|
prerelease: false
|
92
98
|
version_requirements: !ruby/object:Gem::Requirement
|
93
99
|
requirements:
|
94
|
-
- - "
|
100
|
+
- - "~>"
|
95
101
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
102
|
+
version: '13'
|
97
103
|
description: Converts BBCode to HTML. Gracefully handles invalid input.
|
98
104
|
email: jarrett@madebyhq.com
|
99
105
|
executables: []
|