rbbcode 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rbbcode/schema.rb +25 -0
- data/lib/rbbcode/tree_maker.rb +14 -5
- data/spec/parser_spec.rb +6 -0
- data/spec/tree_maker_spec.rb +21 -6
- metadata +28 -13
data/lib/rbbcode/schema.rb
CHANGED
@@ -35,6 +35,16 @@ module RbbCode
|
|
35
35
|
end
|
36
36
|
|
37
37
|
class SchemaTag < SchemaNode
|
38
|
+
def closes_twins
|
39
|
+
@schema.close_twins(@name)
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def does_not_close_twins
|
44
|
+
@schema.dont_close_twins(@name)
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
38
48
|
def initialize(schema, name)
|
39
49
|
@schema = schema
|
40
50
|
@name = name
|
@@ -143,6 +153,19 @@ module RbbCode
|
|
143
153
|
@forbidden_descent = {}
|
144
154
|
@required_parents = {}
|
145
155
|
@no_text = []
|
156
|
+
@twin_closers = []
|
157
|
+
end
|
158
|
+
|
159
|
+
def close_twins(tag_name)
|
160
|
+
@twin_closers << tag_name.to_s unless @twin_closers.include?(tag_name.to_s)
|
161
|
+
end
|
162
|
+
|
163
|
+
def close_twins?(tag_name)
|
164
|
+
@twin_closers.include?(tag_name.to_s)
|
165
|
+
end
|
166
|
+
|
167
|
+
def does_not_close_twins(tag_name)
|
168
|
+
@twin_closers.delete(tag_name.to_s)
|
146
169
|
end
|
147
170
|
|
148
171
|
def forbid_children_except(parent, children)
|
@@ -175,6 +198,7 @@ module RbbCode
|
|
175
198
|
@child_requirements = {}
|
176
199
|
@never_empty = []
|
177
200
|
@no_text = []
|
201
|
+
@twin_closers = []
|
178
202
|
use_defaults
|
179
203
|
end
|
180
204
|
|
@@ -248,6 +272,7 @@ module RbbCode
|
|
248
272
|
tag('code').may_not_be_nested
|
249
273
|
tag('p').may_not_be_nested
|
250
274
|
tag('*').must_be_child_of('list')
|
275
|
+
tag('*').closes_twins
|
251
276
|
tag('list').may_not_descend_from('p')
|
252
277
|
tag('list').may_only_be_parent_of('*')
|
253
278
|
tag('list').may_not_contain_text
|
data/lib/rbbcode/tree_maker.rb
CHANGED
@@ -100,7 +100,7 @@ module RbbCode
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def make_tree(str)
|
103
|
-
|
103
|
+
delete_junk_breaks!(
|
104
104
|
delete_invalid_empty_tags!(
|
105
105
|
parse_str(str)
|
106
106
|
)
|
@@ -130,14 +130,18 @@ module RbbCode
|
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
|
-
|
133
|
+
# Delete empty paragraphs and line breaks at the end of block-level elements
|
134
|
+
def delete_junk_breaks!(node)
|
134
135
|
node.children.reject! do |child|
|
135
136
|
if child.is_a?(TagNode)
|
136
137
|
if !child.children.empty?
|
137
|
-
|
138
|
+
delete_junk_breaks!(child)
|
138
139
|
false
|
139
140
|
elsif child.tag_name == @schema.paragraph_tag_name
|
140
|
-
# It's an empty paragraph tag
|
141
|
+
# It's an empty paragraph tag
|
142
|
+
true
|
143
|
+
elsif @schema.block_level?(node.tag_name) and child.tag_name == @schema.line_break_tag_name and node.children.last == child
|
144
|
+
# It's a line break a the end of the block-level element
|
141
145
|
true
|
142
146
|
else
|
143
147
|
false
|
@@ -217,7 +221,7 @@ module RbbCode
|
|
217
221
|
current_token << char
|
218
222
|
end
|
219
223
|
when :break
|
220
|
-
if
|
224
|
+
if char_code == CR_CODE or char_code == LF_CODE
|
221
225
|
current_token << char
|
222
226
|
else
|
223
227
|
if break_type(current_token) == :paragraph
|
@@ -289,6 +293,11 @@ module RbbCode
|
|
289
293
|
current_parent << tag_node
|
290
294
|
current_parent = tag_node
|
291
295
|
# If all of this results in empty paragraph tags, no worries: they will be deleted later.
|
296
|
+
elsif tag_node.tag_name == current_parent.tag_name and @schema.close_twins?(tag_node.tag_name)
|
297
|
+
# The current tag and the tag we're now opening are of the same type, and this kind of tag auto-closes its twins
|
298
|
+
# (E.g. * tags in the default config.)
|
299
|
+
current_parent.parent << tag_node
|
300
|
+
current_parent = tag_node
|
292
301
|
elsif @schema.tag(tag_node.tag_name).valid_in_context?(*ancestor_list(current_parent))
|
293
302
|
current_parent << tag_node
|
294
303
|
current_parent = tag_node
|
data/spec/parser_spec.rb
CHANGED
@@ -95,5 +95,11 @@ describe RbbCode::Parser do
|
|
95
95
|
@parser.parse('[url=http://www.google.com][img]http://www.123.com/123.png[/img][/url]').should ==
|
96
96
|
'<p><a href="http://www.google.com"><img src="http://www.123.com/123.png" alt=""/></a></p>'
|
97
97
|
end
|
98
|
+
|
99
|
+
it 'can parse phpBB-style [*] tags' do
|
100
|
+
# Thanks to motiv for finding this
|
101
|
+
@parser.parse("[list]\n[*]one\n[*]two\n[/list]"
|
102
|
+
).should == '<ul><li>one</li><li>two</li></ul>'
|
103
|
+
end
|
98
104
|
end
|
99
105
|
end
|
data/spec/tree_maker_spec.rb
CHANGED
@@ -16,7 +16,7 @@ describe RbbCode::TreeMaker do
|
|
16
16
|
@tree_maker = RbbCode::TreeMaker.new(@schema)
|
17
17
|
end
|
18
18
|
|
19
|
-
it '
|
19
|
+
it 'makes a tree from a string with one tag' do
|
20
20
|
str = 'This is [b]bold[/b] text'
|
21
21
|
|
22
22
|
expect_tree(str) do
|
@@ -28,7 +28,7 @@ describe RbbCode::TreeMaker do
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
it '
|
31
|
+
it 'ignores tags that are invalid in their context' do
|
32
32
|
@schema.tag('u').may_not_descend_from('b')
|
33
33
|
|
34
34
|
str = 'This is [b]bold and [u]underlined[/u][/b] text'
|
@@ -45,7 +45,7 @@ describe RbbCode::TreeMaker do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
it '
|
48
|
+
it 'creates paragraph tags' do
|
49
49
|
str = "This is a paragraph.\n\nThis is another."
|
50
50
|
|
51
51
|
expect_tree(str) do
|
@@ -58,7 +58,7 @@ describe RbbCode::TreeMaker do
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
it '
|
61
|
+
it 'does not put block-level elements inside paragraph tags' do
|
62
62
|
str = "This is a list:\n\n[list]\n\n[*]Foo[/i]\n\n[/list]\n\nwith some text after it"
|
63
63
|
|
64
64
|
expect_tree(str) do
|
@@ -74,7 +74,7 @@ describe RbbCode::TreeMaker do
|
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
|
-
it '
|
77
|
+
it 'does not insert br tags in the midst of block-level elements' do
|
78
78
|
str = "List:\n[list]\n[*]Foo[/*]\n[*]Bar[/*]\n[/list]\nText after list"
|
79
79
|
|
80
80
|
expect_tree(str) do
|
@@ -91,7 +91,7 @@ describe RbbCode::TreeMaker do
|
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
|
-
it '
|
94
|
+
it 'stores tag values' do
|
95
95
|
str = 'This is a [url=http://google.com]link[/url]'
|
96
96
|
|
97
97
|
expect_tree(str) do
|
@@ -103,5 +103,20 @@ describe RbbCode::TreeMaker do
|
|
103
103
|
end
|
104
104
|
end
|
105
105
|
end
|
106
|
+
|
107
|
+
it 'auto-closes twins when the schema says it should' do
|
108
|
+
str = "[list]\n[*]One\n[*]Two\n[/list]"
|
109
|
+
|
110
|
+
expect_tree(str) do
|
111
|
+
tag('list') do
|
112
|
+
tag('*') do
|
113
|
+
text 'One'
|
114
|
+
end
|
115
|
+
tag('*') do
|
116
|
+
text 'Two'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
106
121
|
end
|
107
122
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbbcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 6
|
9
|
+
version: 0.1.6
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Jarrett Colby
|
@@ -9,29 +14,37 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-03-07 00:00:00 -06:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: rspec
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
30
|
+
- 0
|
23
31
|
version: 1.3.0
|
24
|
-
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
25
34
|
- !ruby/object:Gem::Dependency
|
26
35
|
name: sanitize-url
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 1
|
44
|
+
- 3
|
33
45
|
version: 0.1.3
|
34
|
-
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
35
48
|
description: RbbCode is a customizable Ruby library for parsing BB Code. RbbCode validates and cleans input. It supports customizable schemas so you can set rules about what tags are allowed where. The default rules are designed to ensure valid HTML output.
|
36
49
|
email: jarrett@jarrettcolby.com
|
37
50
|
executables: []
|
@@ -60,18 +73,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
73
|
requirements:
|
61
74
|
- - ">="
|
62
75
|
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
63
78
|
version: "0"
|
64
|
-
version:
|
65
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
80
|
requirements:
|
67
81
|
- - ">="
|
68
82
|
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
69
85
|
version: "0"
|
70
|
-
version:
|
71
86
|
requirements: []
|
72
87
|
|
73
88
|
rubyforge_project:
|
74
|
-
rubygems_version: 1.3.
|
89
|
+
rubygems_version: 1.3.6
|
75
90
|
signing_key:
|
76
91
|
specification_version: 3
|
77
92
|
summary: Ruby BB Code parser
|