bbcoder 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +13 -3
- data/lib/bbcoder/base.rb +3 -3
- data/lib/bbcoder/buffer_tags.rb +4 -1
- data/lib/bbcoder/configuration.rb +1 -0
- data/lib/bbcoder/tag.rb +20 -5
- data/lib/bbcoder/version.rb +1 -1
- data/lib/bbcoder.rb +8 -0
- data/spec/base_spec.rb +4 -0
- data/spec/bbcoder_spec.rb +14 -0
- metadata +7 -7
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -6,9 +6,9 @@ Features
|
|
6
6
|
* Generates good html even from bad input
|
7
7
|
* Easy configuration to add new tags
|
8
8
|
|
9
|
-
* Tags supported:
|
9
|
+
* Tags supported by default:
|
10
10
|
|
11
|
-
p, b, i, u, s, del, ins, ol, ul, li, dl, dt, dd, quote, code, spoiler, url, img
|
11
|
+
p, b, i, u, s, del, ins, ol, ul, li, dl, dt, dd, quote, code, spoiler, url, img, youtube, sub, sup
|
12
12
|
|
13
13
|
Usage
|
14
14
|
--------
|
@@ -23,12 +23,20 @@ Install
|
|
23
23
|
gem install bbcoder
|
24
24
|
|
25
25
|
|
26
|
-
Configuration
|
26
|
+
Configuration Examples
|
27
27
|
-----------------------
|
28
28
|
|
29
29
|
BBCoder.configure do
|
30
30
|
tag :b, :as => :strong
|
31
31
|
|
32
|
+
tag :sub, :singular => true do
|
33
|
+
%(<sub>#{singular? ? meta : content}</sub>)
|
34
|
+
end
|
35
|
+
|
36
|
+
tag :sup, :singular => true do
|
37
|
+
%(<sup>#{singular? ? meta : content}</sup>)
|
38
|
+
end
|
39
|
+
|
32
40
|
tag :ul
|
33
41
|
tag :ol
|
34
42
|
tag :li, :parents => [:ol, :ul]
|
@@ -60,12 +68,14 @@ Options for #tag
|
|
60
68
|
* :as (symbol) -> use this as the html element ([b] becomes strong)
|
61
69
|
* :match (regex) -> convert this tag and its content to html only if the content matches the regex
|
62
70
|
* :parents ([symbol]) -> ignore this tag if there is no open tag that matches its parents
|
71
|
+
* :singular (true|false) -> use this if the tag does not require an ending tag
|
63
72
|
|
64
73
|
|
65
74
|
When you pass a block to #tag it is expecting you to return a string. You have two variables available to your block:
|
66
75
|
|
67
76
|
* meta -> Everything after the '=' in the opening tag (with [quote="Legendary"] meta returns '"Legendary"' and with [quote] meta returns nil)
|
68
77
|
* content -> Everything between the two tags (with [b]strong arm[/b] content returns 'strong arm')
|
78
|
+
* singular? -> Tells you if this tag is being parsed in singular form or if it had an ending tag (affects if content has any data)
|
69
79
|
|
70
80
|
|
71
81
|
Author
|
data/lib/bbcoder/base.rb
CHANGED
@@ -21,11 +21,11 @@ class BBCoder
|
|
21
21
|
raw.each do |data|
|
22
22
|
case data
|
23
23
|
when /\[\/([^\]]+)\]/
|
24
|
-
buffer.tags.pop($1)
|
24
|
+
buffer.tags.pop($1) # popping end tag
|
25
25
|
when /\[([^\]]+)\]/
|
26
|
-
buffer.tags.push($1)
|
26
|
+
buffer.tags.push($1) # pushing start tag
|
27
27
|
else
|
28
|
-
buffer.push(data)
|
28
|
+
buffer.push(data) # content
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
data/lib/bbcoder/buffer_tags.rb
CHANGED
@@ -27,6 +27,8 @@ class BBCoder
|
|
27
27
|
# logic when popping specific tag
|
28
28
|
def pop(tag)
|
29
29
|
tag = tag.downcase.to_sym
|
30
|
+
|
31
|
+
# no more tags left to pop || this tag isn't in the list
|
30
32
|
if empty? || !include?(tag)
|
31
33
|
buffer.push("[/#{tag}]")
|
32
34
|
elsif last == tag
|
@@ -43,7 +45,8 @@ class BBCoder
|
|
43
45
|
limit = size if limit.nil?
|
44
46
|
|
45
47
|
1.upto(limit).to_a.collect do
|
46
|
-
|
48
|
+
# singular tags are caught in the handle method
|
49
|
+
[BBCoder::Tag.handle(_internal.pop, _meta.delete(size+1)), buffer.pop(+1)].join # +1 depth modifier for the buffer
|
47
50
|
end.reverse.join
|
48
51
|
end
|
49
52
|
|
data/lib/bbcoder/tag.rb
CHANGED
@@ -4,25 +4,26 @@ class BBCoder
|
|
4
4
|
|
5
5
|
def initialize(name, options = {})
|
6
6
|
@name = name
|
7
|
-
@options = options.merge(:as => (options[:as] || name))
|
7
|
+
@options = options.merge(:as => (options[:as] || name), :singular => (options[:singular] || false))
|
8
8
|
end
|
9
9
|
|
10
|
-
def to_html(meta, content)
|
10
|
+
def to_html(meta, content, singularity = false)
|
11
11
|
return self.class.reform(name, meta, content, true) unless content_valid?(content)
|
12
12
|
|
13
|
-
|
14
13
|
if options[:block].nil?
|
15
14
|
"<#{options[:as]}>#{content}</#{options[:as]}>"
|
16
15
|
else
|
17
16
|
options[:block].binding.eval <<-EOS
|
18
17
|
@meta = %Q{#{Regexp.escape(meta.to_s)}}
|
19
18
|
@content = %Q{#{Regexp.escape(content.to_s)}}
|
19
|
+
@singularity = #{singularity.to_s}
|
20
20
|
EOS
|
21
21
|
options[:block].call
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
def content_valid?(content)
|
26
|
+
return true if content.nil? && options[:singular]
|
26
27
|
return false if content.nil?
|
27
28
|
return true if options[:match].nil?
|
28
29
|
|
@@ -33,9 +34,23 @@ class BBCoder
|
|
33
34
|
options[:parents] || []
|
34
35
|
end
|
35
36
|
|
37
|
+
def singular?
|
38
|
+
options[:singular]
|
39
|
+
end
|
40
|
+
|
36
41
|
module ClassMethods
|
37
|
-
def to_html(tag, meta, content)
|
38
|
-
BBCoder.configuration[tag].to_html(meta, content)
|
42
|
+
def to_html(tag, meta, content, singularity = false)
|
43
|
+
BBCoder.configuration[tag].to_html(meta, content, singularity)
|
44
|
+
end
|
45
|
+
|
46
|
+
# need #handle so we don't get into a reform loop from to_html
|
47
|
+
# checking content validity
|
48
|
+
def handle(tag, meta, content = nil, force_end = false)
|
49
|
+
if BBCoder.configuration[tag] && BBCoder.configuration[tag].singular?
|
50
|
+
to_html(tag, meta, content, true)
|
51
|
+
else
|
52
|
+
reform(tag, meta, content, force_end)
|
53
|
+
end
|
39
54
|
end
|
40
55
|
|
41
56
|
def reform(tag, meta, content = nil, force_end = false)
|
data/lib/bbcoder/version.rb
CHANGED
data/lib/bbcoder.rb
CHANGED
@@ -65,5 +65,13 @@ BBCoder.configure do
|
|
65
65
|
<iframe width="560" height="349" src="http://www.youtube.com/embed/#{content}" frameborder="0" allowfullscreen></iframe>
|
66
66
|
EOS
|
67
67
|
end
|
68
|
+
|
69
|
+
tag :sub, :singular => true do
|
70
|
+
%(<sub>#{singular? ? meta : content}</sub>)
|
71
|
+
end
|
72
|
+
|
73
|
+
tag :sup, :singular => true do
|
74
|
+
%(<sup>#{singular? ? meta : content}</sup>)
|
75
|
+
end
|
68
76
|
end
|
69
77
|
|
data/spec/base_spec.rb
CHANGED
@@ -32,6 +32,10 @@ describe BBCoder do
|
|
32
32
|
it "should split tags up properly" do
|
33
33
|
subject.raw.should == ["[p]", "Text and now ", "[b]", "bolded.", "[/b]", "[/p]"]
|
34
34
|
end
|
35
|
+
|
36
|
+
it "should split tags up properly without content" do
|
37
|
+
BBCoder.new("[b][/b][u][/u]").raw.should == ["[b]", "[/b]", "[u]", "[/u]"]
|
38
|
+
end
|
35
39
|
end
|
36
40
|
|
37
41
|
context "#parse" do
|
data/spec/bbcoder_spec.rb
CHANGED
@@ -103,5 +103,19 @@ EOS
|
|
103
103
|
"[p]Text and now [B]nested.[/b][/P]".bbcode_to_html.should == "<p>Text and now <strong>nested.</strong></p>"
|
104
104
|
end
|
105
105
|
end
|
106
|
+
|
107
|
+
context "with singular tags" do
|
108
|
+
it "should handle one singular tag" do
|
109
|
+
"[sub=subscriptage]".bbcode_to_html.should == "<sub>subscriptage</sub>"
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should handle multiple singular tags" do
|
113
|
+
"[p]I [sub=me] think [sub=thought]; therefore [sup=well] I am[/p]".bbcode_to_html.should == "<p>I <sub>me</sub> think <sub>thought</sub>; therefore <sup>well</sup> I am</p>"
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should handle singular tags in non singular form" do
|
117
|
+
"[p]I [sub]me[/sub] think [sub]thought[/sub]; therefore [sup]well[/sup] I am[/p]".bbcode_to_html.should == "<p>I <sub>me</sub> think <sub>thought</sub>; therefore <sup>well</sup> I am</p>"
|
118
|
+
end
|
119
|
+
end
|
106
120
|
end
|
107
121
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bbcoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-13 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &8964480 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *8964480
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rr
|
27
|
-
requirement: &
|
27
|
+
requirement: &8964060 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *8964060
|
36
36
|
description: A gem for parsing bbcode that doesn't rely on regular expressions
|
37
37
|
email:
|
38
38
|
- machinist@asceth.com
|
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
81
|
version: '0'
|
82
82
|
requirements: []
|
83
83
|
rubyforge_project: bbcoder
|
84
|
-
rubygems_version: 1.8.
|
84
|
+
rubygems_version: 1.8.10
|
85
85
|
signing_key:
|
86
86
|
specification_version: 3
|
87
87
|
summary: BBCode parser
|