bb-ruby 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +8 -0
- data/PostInstall.txt +2 -0
- data/README.rdoc +113 -0
- data/Rakefile +25 -0
- data/lib/bb-ruby.rb +247 -0
- data/test/test_bb-ruby.rb +209 -0
- data/test/test_helper.rb +3 -0
- metadata +85 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
= bb-ruby
|
2
|
+
|
3
|
+
* http://bb-ruby.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
bb-ruby is a BBCode (http://www.bbcode.org) implementation for Ruby. It will convert strings with BBCode markup to their HTML equivalent.
|
8
|
+
|
9
|
+
== INSTALL:
|
10
|
+
|
11
|
+
To install as a gem:
|
12
|
+
|
13
|
+
sudo gem install bb-ruby
|
14
|
+
|
15
|
+
To Install as a plugin:
|
16
|
+
|
17
|
+
./script/plugin install git://github.com/cpjolicoeur/bb-ruby.git
|
18
|
+
|
19
|
+
== USAGE:
|
20
|
+
|
21
|
+
require 'bb-ruby' # (only needed if installed as a gem)
|
22
|
+
|
23
|
+
BBRuby has been included directly into the String class for use on any string object:
|
24
|
+
|
25
|
+
text = "[b]Here is some bold text[/b] followed by some [u]underlined text[/u]"
|
26
|
+
output = text.bbcode_to_html
|
27
|
+
text.bbcode_to_html!
|
28
|
+
|
29
|
+
BBRuby will auto-escape HTML tags. To prevent this just pass false as the second param:
|
30
|
+
|
31
|
+
output = text.bbcode_to_html({}, false)
|
32
|
+
|
33
|
+
Only allow certain tags:
|
34
|
+
|
35
|
+
output = text.bbcode_to_html({}, true, :enable, :image, :bold, :quote)
|
36
|
+
|
37
|
+
Disable certain tags:
|
38
|
+
|
39
|
+
output = text.bbcode_to_html({}, true, :disable, :image, :bold, :quote)
|
40
|
+
|
41
|
+
Alternative Direct usage:
|
42
|
+
|
43
|
+
output = BBRuby.to_html(bbcode_markup)
|
44
|
+
|
45
|
+
Define your own translation, in order to be more flexible:
|
46
|
+
|
47
|
+
my_blockquote = {
|
48
|
+
'Quote' => [
|
49
|
+
/\[quote(:.*)?=(.*?)\](.*?)\[\/quote\1?\]/mi,
|
50
|
+
'<div class="quote"><p><cite>\2</cite></p><blockquote>\3</blockquote></div>',
|
51
|
+
'Quote with citation',
|
52
|
+
'[quote=mike]please quote me[/quote]',
|
53
|
+
:quote
|
54
|
+
],
|
55
|
+
}
|
56
|
+
|
57
|
+
text.bbcode_to_html(my_blockquote)
|
58
|
+
|
59
|
+
== TAGS PROCESSED:
|
60
|
+
|
61
|
+
The following is the list of BBCode tags processed by BBRuby and their associated symbol for enabling/disabling them
|
62
|
+
|
63
|
+
* [b] :bold
|
64
|
+
* [i] :italics
|
65
|
+
* [u] :underline
|
66
|
+
* [s] :strikeout
|
67
|
+
* [del] :delete
|
68
|
+
* [ins] :insert
|
69
|
+
* [code] :code
|
70
|
+
* [size] :size
|
71
|
+
* [color] :color
|
72
|
+
* [ol] :orderedlist
|
73
|
+
* [ul] :unorderedlist
|
74
|
+
* [li] :listitem
|
75
|
+
* [*] :listitem
|
76
|
+
* [list] :listitem
|
77
|
+
* [list=1] :listitem
|
78
|
+
* [list=a] :listitem
|
79
|
+
* [dl] :definelist
|
80
|
+
* [dt] :defineterm
|
81
|
+
* [dd] :definition
|
82
|
+
* [quote] :quote
|
83
|
+
* [quote=source] :quote
|
84
|
+
* [url=link] :link
|
85
|
+
* [url] :link
|
86
|
+
* [img size=] :image
|
87
|
+
* [img=] :image
|
88
|
+
* [img] :image
|
89
|
+
* [youtube] :video
|
90
|
+
* [gvideo] :video
|
91
|
+
* [email] :email
|
92
|
+
|
93
|
+
== LICENSE:
|
94
|
+
|
95
|
+
Copyright (c) 2008 Craig P Jolicoeur, Fernando Blat
|
96
|
+
|
97
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
98
|
+
of this software and associated documentation files (the "Software"), to deal
|
99
|
+
in the Software without restriction, including without limitation the rights
|
100
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
101
|
+
copies of the Software, and to permit persons to whom the Software is
|
102
|
+
furnished to do so, subject to the following conditions:
|
103
|
+
|
104
|
+
The above copyright notice and this permission notice shall be included in
|
105
|
+
all copies or substantial portions of the Software.
|
106
|
+
|
107
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
108
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
109
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
110
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
111
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
112
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
113
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/bb-ruby'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('bb-ruby', BBRuby::VERSION) do |p|
|
7
|
+
p.developer('Craig P Jolicoeur', 'cpjolicoeur@gmail.com')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
10
|
+
p.rubyforge_name = p.name # TODO this is default value
|
11
|
+
p.extra_dev_deps = [
|
12
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
13
|
+
]
|
14
|
+
|
15
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
16
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
17
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
18
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
22
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
23
|
+
|
24
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
25
|
+
# task :default => [:spec, :features]
|
data/lib/bb-ruby.rb
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
module BBRuby
|
5
|
+
VERSION = '0.8.2'
|
6
|
+
|
7
|
+
@@imageformats = 'png|bmp|jpg|gif|jpeg'
|
8
|
+
@@tags = {
|
9
|
+
# tag name => [regex, replace, description, example, enable/disable symbol]
|
10
|
+
'Bold' => [
|
11
|
+
/\[b(:.*)?\](.*?)\[\/b\1?\]/mi,
|
12
|
+
'<strong>\2</strong>',
|
13
|
+
'Embolden text',
|
14
|
+
'Look [b]here[/b]',
|
15
|
+
:bold],
|
16
|
+
'Italics' => [
|
17
|
+
/\[i(:.+)?\](.*?)\[\/i\1?\]/mi,
|
18
|
+
'<em>\2</em>',
|
19
|
+
'Italicize or emphasize text',
|
20
|
+
'Even my [i]cat[/i] was chasing the mailman!',
|
21
|
+
:italics],
|
22
|
+
'Underline' => [
|
23
|
+
/\[u(:.+)?\](.*?)\[\/u\1?\]/mi,
|
24
|
+
'<u>\2</u>',
|
25
|
+
'Underline',
|
26
|
+
'Use it for [u]important[/u] things or something',
|
27
|
+
:underline],
|
28
|
+
'Strikeout' => [
|
29
|
+
/\[s(:.+)?\](.*?)\[\/s\1?\]/mi,
|
30
|
+
'<del>\2</del>',
|
31
|
+
'Strikeout',
|
32
|
+
'[s]nevermind[/s]',
|
33
|
+
:strikeout],
|
34
|
+
'Delete' => [
|
35
|
+
/\[del(:.+)?\](.*?)\[\/del\1?\]/mi,
|
36
|
+
'<del>\2</del>',
|
37
|
+
'Deleted text',
|
38
|
+
'[del]deleted text[/del]',
|
39
|
+
:delete],
|
40
|
+
'Insert' => [
|
41
|
+
/\[ins(:.+)?\](.*?)\[\/ins\1?\]/mi,
|
42
|
+
'<ins>\2</ins>',
|
43
|
+
'Inserted Text',
|
44
|
+
'[ins]inserted text[/del]',
|
45
|
+
:insert],
|
46
|
+
'Code' => [
|
47
|
+
/\[code(:.+)?\](.*?)\[\/code\1?\]/mi,
|
48
|
+
'<code>\2</code>',
|
49
|
+
'Code Text',
|
50
|
+
'[code]some code[/code]',
|
51
|
+
:code],
|
52
|
+
'Size' => [
|
53
|
+
/\[size=['"]?(.*?)['"]?\](.*?)\[\/size\]/im,
|
54
|
+
'<span style="font-size: \1px;">\2</span>',
|
55
|
+
'Change text size',
|
56
|
+
'[size=20]Here is some larger text[/size]',
|
57
|
+
:size],
|
58
|
+
'Color' => [
|
59
|
+
/\[color=['"]?(\w+|\#\w{6})['"]?(:.+)?\](.*?)\[\/color\2?\]/im,
|
60
|
+
'<span style="color: \1;">\3</span>',
|
61
|
+
'Change text color',
|
62
|
+
'[color=red]This is red text[/color]',
|
63
|
+
:color],
|
64
|
+
'Ordered List' => [
|
65
|
+
/\[ol\](.*?)\[\/ol\]/mi,
|
66
|
+
'<ol>\1</ol>',
|
67
|
+
'Ordered list',
|
68
|
+
'My favorite people (alphabetical order): [ol][li]Jenny[/li][li]Alex[/li][li]Beth[/li][/ol]',
|
69
|
+
:orderedlist],
|
70
|
+
'Unordered List' => [
|
71
|
+
/\[ul\](.*?)\[\/ul\]/mi,
|
72
|
+
'<ul>\1</ul>',
|
73
|
+
'Unordered list',
|
74
|
+
'My favorite people (order of importance): [ul][li]Jenny[/li][li]Alex[/li][li]Beth[/li][/ul]',
|
75
|
+
:unorderedlist],
|
76
|
+
'List Item' => [
|
77
|
+
/\[li\](.*?)\[\/li\]/mi,
|
78
|
+
'<li>\1</li>',
|
79
|
+
'List item',
|
80
|
+
'See ol or ul',
|
81
|
+
:listitem],
|
82
|
+
'List Item (alternative)' => [
|
83
|
+
/\[\*(:[^\[]+)?\]([^(\[|\<)]+)/mi,
|
84
|
+
'<li>\2</li>',
|
85
|
+
'List item (alternative)',
|
86
|
+
'[*]list item',
|
87
|
+
:listitem],
|
88
|
+
'Unordered list (alternative)' => [
|
89
|
+
/\[list(:.*)?\]((?:(?!list).)*)\[\/list(:.)?\1?\]/mi,
|
90
|
+
'<ul>\2</ul>',
|
91
|
+
'Unordered list item',
|
92
|
+
'[list][*]item 1[*] item2[/list]',
|
93
|
+
:list],
|
94
|
+
'Ordered list (numerical)' => [
|
95
|
+
/\[list=1(:.*)?\](.+)\[\/list(:.)?\1?\]/mi,
|
96
|
+
'<ol>\2</ol>',
|
97
|
+
'Ordered list numerically',
|
98
|
+
'[list=1][*]item 1[*] item2[/list]',
|
99
|
+
:list],
|
100
|
+
'Ordered list (alphabetical)' => [
|
101
|
+
/\[list=a(:.*)?\](.+)\[\/list(:.)?\1?\]/mi,
|
102
|
+
'<ol sytle="list-style-type: lower-alpha;">\2</ol>',
|
103
|
+
'Ordered list alphabetically',
|
104
|
+
'[list=a][*]item 1[*] item2[/list]',
|
105
|
+
:list],
|
106
|
+
'Definition List' => [
|
107
|
+
/\[dl\](.*?)\[\/dl\]/im,
|
108
|
+
'<dl>\1</dl>',
|
109
|
+
'List of terms/items and their definitions',
|
110
|
+
'[dl][dt]Fusion Reactor[/dt][dd]Chamber that provides power to your... nerd stuff[/dd][dt]Mass Cannon[/dt][dd]A gun of some sort[/dd][/dl]',
|
111
|
+
:definelist],
|
112
|
+
'Definition Term' => [
|
113
|
+
/\[dt\](.*?)\[\/dt\]/mi,
|
114
|
+
'<dt>\1</dt>',
|
115
|
+
'List of definition terms',
|
116
|
+
'[dt]definition term[/dt]',
|
117
|
+
:defineterm],
|
118
|
+
'Definition Definition' => [
|
119
|
+
/\[dd\](.*?)\[\/dd\]/mi,
|
120
|
+
'<dd>\1</dd>',
|
121
|
+
'Definition definitions',
|
122
|
+
'[dd]my definition[/dd',
|
123
|
+
:definition],
|
124
|
+
'Quote' => [
|
125
|
+
/\[quote(:.*)?="?(.*?)"?\](.*?)\[\/quote\1?\]/mi,
|
126
|
+
'<fieldset><legend>\2</legend><blockquote>\3</blockquote></fieldset>',
|
127
|
+
'Quote with citation',
|
128
|
+
"[quote=mike]Now is the time...[/quote]",
|
129
|
+
:quote],
|
130
|
+
'Quote (Sourceless)' => [
|
131
|
+
/\[quote(:.*)?\](.*?)\[\/quote\1?\]/mi,
|
132
|
+
'<fieldset><blockquote>\2</blockquote></fieldset>',
|
133
|
+
'Quote (sourceclass)',
|
134
|
+
"[quote]Now is the time...[/quote]",
|
135
|
+
:quote],
|
136
|
+
'Link' => [
|
137
|
+
/\[url=(.*?)\](.*?)\[\/url\]/mi,
|
138
|
+
'<a href="\1">\2</a>',
|
139
|
+
'Hyperlink to somewhere else',
|
140
|
+
'Maybe try looking on [url=http://google.com]Google[/url]?',
|
141
|
+
:link],
|
142
|
+
'Link (Implied)' => [
|
143
|
+
/\[url\](.*?)\[\/url\]/mi,
|
144
|
+
'<a href="\1">\1</a>',
|
145
|
+
'Hyperlink (implied)',
|
146
|
+
"Maybe try looking on [url]http://google.com[/url]",
|
147
|
+
:link],
|
148
|
+
#
|
149
|
+
# TODO: fix automatic links
|
150
|
+
#
|
151
|
+
# 'Link (Automatic)' => [
|
152
|
+
# /http:\/\/(.*?)[^<\/a>]/,
|
153
|
+
# '<a href="\1">\1</a>',
|
154
|
+
# 'Hyperlink (automatic)',
|
155
|
+
# nil, nil,
|
156
|
+
# :link],
|
157
|
+
'Image (Resized)' => [
|
158
|
+
/\[img(:.+)? size=(['"]?)(\d+)x(\d+)\2\](.*?)\[\/img\1?\]/im,
|
159
|
+
'<img src="\5" style="width: \3px; height: \4px;" />',
|
160
|
+
'Display an image with a set width and height',
|
161
|
+
'[img size=96x96]http://www.google.com/intl/en_ALL/images/logo.gif[/img]',
|
162
|
+
:image],
|
163
|
+
'Image (Alternative)' => [
|
164
|
+
/\[img=([^\[\]].*?)\.(#{@@imageformats})\]/im,
|
165
|
+
'<img src="\1.\2" alt="" />',
|
166
|
+
'Display an image (alternative format)',
|
167
|
+
'[img=http://myimage.com/logo.gif]',
|
168
|
+
:image],
|
169
|
+
'Image' => [
|
170
|
+
/\[img(:.+)?\]([^\[\]].*?)\.(#{@@imageformats})\[\/img\1?\]/im,
|
171
|
+
'<img src="\2.\3" alt="" />',
|
172
|
+
'Display an image',
|
173
|
+
'Check out this crazy cat: [img]http://catsweekly.com/crazycat.jpg[/img]',
|
174
|
+
:image],
|
175
|
+
'YouTube' => [
|
176
|
+
/\[youtube\](.*?)\?v=([\w\d\-]+).*\[\/youtube\]/im,
|
177
|
+
'<object width="400" height="330"><param name="movie" value="http://www.youtube.com/v/\2"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/\2" type="application/x-shockwave-flash" wmode="transparent" width="400" height="330"></embed></object>',
|
178
|
+
'Display a video from YouTube.com',
|
179
|
+
'[youtube]http://youtube.com/watch?v=E4Fbk52Mk1w[/youtube]',
|
180
|
+
:video],
|
181
|
+
'YouTube (Alternative)' => [
|
182
|
+
/\[youtube\](.*?)\/v\/([\w\d\-]+)\[\/youtube\]/im,
|
183
|
+
'<object width="400" height="330"><param name="movie" value="http://www.youtube.com/v/\2"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/\2" type="application/x-shockwave-flash" wmode="transparent" width="400" height="330"></embed></object>',
|
184
|
+
'Display a video from YouTube.com (alternative format)',
|
185
|
+
'[youtube]http://youtube.com/watch/v/E4Fbk52Mk1w[/youtube]',
|
186
|
+
:video],
|
187
|
+
'Google Video' => [
|
188
|
+
/\[gvideo\](.*?)\?docid=([-]{0,1}\d+).*\[\/gvideo\]/mi,
|
189
|
+
'<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=\2" flashvars=""> </embed>',
|
190
|
+
'Display a video from Google Video',
|
191
|
+
'[gvideo]http://video.google.com/videoplay?docid=-2200109535941088987[/gvideo]',
|
192
|
+
:video],
|
193
|
+
'Email' => [
|
194
|
+
/\[email(:.+)?\](.+)\[\/email\1?\]/i,
|
195
|
+
'<a href="mailto:\2">\2</a>',
|
196
|
+
'Link to email address',
|
197
|
+
'[email]wadus@wadus.com[/email]',
|
198
|
+
:email]
|
199
|
+
}
|
200
|
+
|
201
|
+
def self.to_html(text, tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
|
202
|
+
text = text.clone
|
203
|
+
# escape < and > to remove any html
|
204
|
+
if escape_html
|
205
|
+
text.gsub!( '<', '<' )
|
206
|
+
text.gsub!( '>', '>' )
|
207
|
+
end
|
208
|
+
|
209
|
+
tags_definition = @@tags.merge(tags_alternative_definition)
|
210
|
+
|
211
|
+
# parse bbcode tags
|
212
|
+
case method
|
213
|
+
when :enable
|
214
|
+
tags_definition.each_value { |t| text.gsub!(t[0], t[1]) if tags[0].include?(t[4]) }
|
215
|
+
when :disable
|
216
|
+
# this works nicely because the default is disable and the default set of tags is [] (so none disabled) :)
|
217
|
+
tags_definition.each_value { |t| text.gsub!(t[0], t[1]) unless tags[0].include?(t[4]) }
|
218
|
+
end
|
219
|
+
|
220
|
+
# parse spacing
|
221
|
+
text.gsub!( /\r\n?/, "\n" )
|
222
|
+
text.gsub!( /\n/, "<br />" )
|
223
|
+
|
224
|
+
# return markup
|
225
|
+
text
|
226
|
+
end
|
227
|
+
|
228
|
+
def self.tags
|
229
|
+
@@tags.each { |tn, ti|
|
230
|
+
# yields the tag name, a description of it and example
|
231
|
+
yield tn, ti[2], ti[3] if ti[2]
|
232
|
+
}
|
233
|
+
end
|
234
|
+
|
235
|
+
def self.tag_list
|
236
|
+
@@tags
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
class String
|
241
|
+
def bbcode_to_html(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
|
242
|
+
BBRuby.to_html(self, tags_alternative_definition, escape_html, method, tags)
|
243
|
+
end
|
244
|
+
def bbcode_to_html!(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
|
245
|
+
self.replace(BBRuby.to_html(self, tags_alternative_definition, escape_html, method, tags))
|
246
|
+
end
|
247
|
+
end
|
@@ -0,0 +1,209 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
4
|
+
|
5
|
+
class TestBBRuby < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_strong
|
8
|
+
assert_equal '<strong>simple</strong>', '[b]simple[/b]'.bbcode_to_html
|
9
|
+
assert_equal '<strong>simple</strong>', '[b:7a9ca2c5c3]simple[/b:7a9ca2c5c3]'.bbcode_to_html
|
10
|
+
assert_equal "<strong>line 1<br />line 2</strong>", "[b:7a9ca2c5c3]line 1\nline 2[/b:7a9ca2c5c3]".bbcode_to_html
|
11
|
+
assert_equal '<strong>1. text 1:</strong> text 2<br /><strong>2. text 3</strong>', "[b:post_uid0]1. text 1:[/b:post_uid0] text 2\n[b:post_uid0]2. text 3[/b:post_uid0]".bbcode_to_html
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_em
|
15
|
+
assert_equal '<em>simple</em>', '[i]simple[/i]'.bbcode_to_html
|
16
|
+
assert_equal '<em>simple</em>', '[i:7a9ca2c5c3]simple[/i:7a9ca2c5c3]'.bbcode_to_html
|
17
|
+
assert_equal "<em>line 1<br />line 2</em>", "[i:7a9ca2c5c3]line 1\nline 2[/i:7a9ca2c5c3]".bbcode_to_html
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_u
|
21
|
+
assert_equal '<u>simple</u>', '[u]simple[/u]'.bbcode_to_html
|
22
|
+
assert_equal '<u>simple</u>', '[u:7a9ca2c5c3]simple[/u:7a9ca2c5c3]'.bbcode_to_html
|
23
|
+
assert_equal "<u>line 1<br />line 2</u>", "[u:7a9ca2c5c3]line 1\nline 2[/u:7a9ca2c5c3]".bbcode_to_html
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_del
|
27
|
+
assert_equal '<del>simple</del>', '[del]simple[/del]'.bbcode_to_html
|
28
|
+
assert_equal '<del>simple</del>', '[del:7a9ca2c5c3]simple[/del:7a9ca2c5c3]'.bbcode_to_html
|
29
|
+
assert_equal '<del>simple</del>', '[s]simple[/s]'.bbcode_to_html
|
30
|
+
assert_equal '<del>simple</del>', '[s:7a9ca2c5c3]simple[/s:7a9ca2c5c3]'.bbcode_to_html
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_ins
|
34
|
+
assert_equal '<ins>simple</ins>', '[ins]simple[/ins]'.bbcode_to_html
|
35
|
+
assert_equal '<ins>simple</ins>', '[ins:7a9ca2c5c3]simple[/ins:7a9ca2c5c3]'.bbcode_to_html
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_code
|
39
|
+
assert_equal '<code>simple</code>', '[code]simple[/code]'.bbcode_to_html
|
40
|
+
assert_equal '<code>simple</code>', '[code:7a9ca2c5c3]simple[/code:7a9ca2c5c3]'.bbcode_to_html
|
41
|
+
assert_equal "<code>var bxi = 0;<br />//Holds current speed of scrolling menu</code>", "[code:1:91cbdd72b7]var bxi = 0;\n//Holds current speed of scrolling menu[/code:1:91cbdd72b7]".bbcode_to_html
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_size
|
45
|
+
assert_equal '<span style="font-size: 32px;">12px Text</span>', '[size=32]12px Text[/size]'.bbcode_to_html
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_color
|
49
|
+
assert_equal '<span style="color: red;">Red Text</span>', '[color=red]Red Text[/color]'.bbcode_to_html
|
50
|
+
assert_equal '<span style="color: #ff0023;">Hex Color Text</span>', '[color=#ff0023]Hex Color Text[/color]'.bbcode_to_html
|
51
|
+
assert_equal '<span style="color: #B23803;">text</span>', '[color=#B23803:05d7c56429]text[/color:05d7c56429]'.bbcode_to_html
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_ordered_list
|
55
|
+
assert_equal '<ol><li>item 1</li><li>item 2</li></ol>', '[ol][li]item 1[/li][li]item 2[/li][/ol]'.bbcode_to_html
|
56
|
+
assert_equal '<ol><li>item 1</li><li>item 2</li></ol>', '[ol][*]item 1[*]item 2[/ol]'.bbcode_to_html
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_unordered_list
|
60
|
+
assert_equal '<ul><li>item 1</li><li>item 2</li></ul>', '[ul][li]item 1[/li][li]item 2[/li][/ul]'.bbcode_to_html
|
61
|
+
assert_equal '<ul><li>item 1</li><li>item 2</li></ul>', '[ul][*]item 1[*]item 2[/ul]'.bbcode_to_html
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_list_unordered
|
65
|
+
assert_equal '<ul><li>item 1</li><li>item 2</li></ul>', '[list][li]item 1[/li][li]item 2[/li][/list]'.bbcode_to_html
|
66
|
+
assert_equal '<ul><li>item 1</li><li>item 2</li></ul>', '[list:7a9ca2c5c3][li]item 1[/li][li]item 2[/li][/list:o:7a9ca2c5c3]'.bbcode_to_html
|
67
|
+
assert_equal '<ul><li>item 1</li><li>item 2</li></ul><ul><li>item 3</li><li>item 4</li></ul>',
|
68
|
+
'[list:7a9ca2c5c3][li]item 1[/li][li]item 2[/li][/list:o:7a9ca2c5c3][list:7a9ca2c5c3][li]item 3[/li][li]item 4[/li][/list:o:7a9ca2c5c3]'.bbcode_to_html
|
69
|
+
assert_equal '<ul><li>item 1</li><li>item 2</li></ul><ul><li>item 3</li><li>item 4</li></ul><ul><li>item 5</li><li>item 6</li></ul><ul><li>item 7</li><li>item 8</li></ul>',
|
70
|
+
'[list:7a9ca2c5c3][li]item 1[/li][li]item 2[/li][/list:o:7a9ca2c5c3][list:7a9ca2c5c3][li]item 3[/li][li]item 4[/li][/list:o:7a9ca2c5c3][list:7a9ca2c5c3][li]item 5[/li][li]item 6[/li][/list:o:7a9ca2c5c3][list:7a9ca2c5c3][li]item 7[/li][li]item 8[/li][/list:o:7a9ca2c5c3]'.bbcode_to_html
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_list_unordered_alternative
|
74
|
+
assert_equal '<li>item1</li><li>item2</li>', '[*:asdf]item1[*:asdf]item2'.bbcode_to_html
|
75
|
+
assert_equal '<ul><li>item1</li><li>item2</li></ul>', '[list:5d7cf5560a][*]item1[*]item2[/list:u:5d7cf5560a]'.bbcode_to_html
|
76
|
+
assert_equal '<ul><li>item1</li><li>item2</li></ul>', '[list:5d7cf5560a][*:5d7cf5560a]item1[*:5d7cf5560a]item2[/list:u:5d7cf5560a]'.bbcode_to_html
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_list_ordered_numerically
|
80
|
+
assert_equal '<ol><li>item 1</li><li>item 2</li></ol>', '[list=1][li]item 1[/li][li]item 2[/li][/list]'.bbcode_to_html
|
81
|
+
assert_equal '<ol><li>item 1</li><li>item 2</li></ol>', '[list=1:7a9ca2c5c3][li]item 1[/li][li]item 2[/li][/list:7a9ca2c5c3]'.bbcode_to_html
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_list_ordered_alphabetically
|
85
|
+
assert_equal '<ol sytle="list-style-type: lower-alpha;"><li>item 1</li><li>item 2</li></ol>', '[list=a][li]item 1[/li][li]item 2[/li][/list]'.bbcode_to_html
|
86
|
+
assert_equal '<ol sytle="list-style-type: lower-alpha;"><li>item 1</li><li>item 2</li></ol>', '[list=a:7a9ca2c5c3][li]item 1[/li][li]item 2[/li][/list:o:7a9ca2c5c3]'.bbcode_to_html
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_two_lists
|
90
|
+
assert_equal '<ul><li>item1</li><li>item2</li></ul><ul><li>item1</li><li>item2</li></ul>',
|
91
|
+
'[list:5d7cf5560a][*:5d7cf5560a]item1[*:5d7cf5560a]item2[/list:u:5d7cf5560a][list:5d7cf5560a][*:5d7cf5560a]item1[*:5d7cf5560a]item2[/list:u:5d7cf5560a]'.bbcode_to_html
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_definition_list_term_definition
|
95
|
+
assert_equal '<dl><dt>term 1</dt><dd>definition 1</dd><dt>term 2</dt><dd>definition 2</dd></dl>', '[dl][dt]term 1[/dt][dd]definition 1[/dd][dt]term 2[/dt][dd]definition 2[/dd][/dl]'.bbcode_to_html
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_quote
|
99
|
+
assert_equal '<fieldset><blockquote>quoting</blockquote></fieldset>', '[quote]quoting[/quote]'.bbcode_to_html
|
100
|
+
assert_equal '<fieldset><blockquote>quoting</blockquote></fieldset>', '[quote]quoting[/quote]'.bbcode_to_html.bbcode_to_html({}, false, :disable)
|
101
|
+
assert_equal '<fieldset><legend>black</legend><blockquote>si el niño hubiera sido de "penalty" le hubieran llamado <strong>system Error</strong>!!! :)</blockquote></fieldset>', "[quote:7a9ca2c5c3=\"black\"]si el niño hubiera sido de \"penalty\" le hubieran llamado [b:7a9ca2c5c3]system Error[/b:7a9ca2c5c3]!!! :)[/quote:7a9ca2c5c3]".bbcode_to_html
|
102
|
+
assert_equal '<fieldset><legend>black</legend><blockquote>si el niño hubiera sido de "penalty" le hubieran llamado <strong>system Error</strong>!!! :)</blockquote></fieldset>', "[quote:7a9ca2c5c3=\"black\"]si el niño hubiera sido de \"penalty\" le hubieran llamado [b:7a9ca2c5c3]system Error[/b:7a9ca2c5c3]!!! :)[/quote:7a9ca2c5c3]".bbcode_to_html.bbcode_to_html({}, false, :disable)
|
103
|
+
assert_equal '<fieldset><legend>Who</legend><blockquote>said that</blockquote></fieldset>', '[quote=Who]said that[/quote]'.bbcode_to_html
|
104
|
+
assert_equal '<fieldset><legend>Who</legend><blockquote>said that</blockquote></fieldset>', '[quote=Who]said that[/quote]'.bbcode_to_html.bbcode_to_html({}, false, :disable)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_double_quote
|
108
|
+
assert_equal '<fieldset><legend>Kitten</legend><blockquote><fieldset><legend>creatiu</legend><blockquote>f1</blockquote></fieldset>f2</blockquote></fieldset>',
|
109
|
+
'[quote:26fe26a6a9="Kitten"][quote:26fe26a6a9="creatiu"]f1[/quote:26fe26a6a9]f2[/quote:26fe26a6a9]'.bbcode_to_html.bbcode_to_html({}, false, :disable)
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_link
|
113
|
+
assert_equal '<a href="http://google.com">Google</a>', '[url=http://google.com]Google[/url]'.bbcode_to_html
|
114
|
+
assert_equal '<a href="http://google.com">http://google.com</a>', '[url]http://google.com[/url]'.bbcode_to_html
|
115
|
+
assert_equal '<a href="http://www.altctrlsupr.com/dmstk/kdd070803/00.html"> ABRIR ALBUM </a>','[URL=http://www.altctrlsupr.com/dmstk/kdd070803/00.html] ABRIR ALBUM [/URL]'.bbcode_to_html
|
116
|
+
assert_equal '<a href="http://www.altctrlsupr.com/dmstk/kdd070803/00.html"> ABRIR<br />ALBUM </a>',"[URL=http://www.altctrlsupr.com/dmstk/kdd070803/00.html] ABRIR\nALBUM [/URL]".bbcode_to_html
|
117
|
+
assert_equal '<a href="http://www.urimalet.com/cadaverex.mp3">aha</a>', "[URL=http://www.urimalet.com/cadaverex.mp3]aha[/URL]".bbcode_to_html
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_image
|
121
|
+
assert_equal '<img src="http://zoople/hochzeit.png" alt="" />', '[img]http://zoople/hochzeit.png[/img]'.bbcode_to_html
|
122
|
+
assert_equal '<img src="http://zoople/hochzeit.png" alt="" />', '[img=http://zoople/hochzeit.png]'.bbcode_to_html
|
123
|
+
assert_equal '<img src="http://zoople/hochzeit.png" style="width: 95px; height: 96px;" />', '[img size=95x96]http://zoople/hochzeit.png[/img]'.bbcode_to_html
|
124
|
+
assert_equal '<img src="http://zoople/hochzeit.png" alt="" />', '[img:7a9ca2c5c3]http://zoople/hochzeit.png[/img:7a9ca2c5c3]'.bbcode_to_html
|
125
|
+
assert_equal '<img src="http://zoople/hochzeit.png" style="width: 95px; height: 96px;" />', '[img:7a9ca2c5c3 size=95x96]http://zoople/hochzeit.png[/img:7a9ca2c5c3]'.bbcode_to_html
|
126
|
+
assert_equal '<img src="http://www.marcodigital.com/sitanddie/sitanddiepequeño.jpg" alt="" />', '[img:post_uid0]http://www.marcodigital.com/sitanddie/sitanddiepequeño.jpg[/img:post_uid0]'.bbcode_to_html
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_youtube
|
130
|
+
assert_equal '<object width="400" height="330"><param name="movie" value="http://www.youtube.com/v/E4Fbk52Mk1w"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/E4Fbk52Mk1w" type="application/x-shockwave-flash" wmode="transparent" width="400" height="330"></embed></object>','[youtube]http://youtube.com/watch?v=E4Fbk52Mk1w[/youtube]'.bbcode_to_html
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_google_video
|
134
|
+
assert_equal '<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-2200109535941088987" flashvars=""> </embed>', '[gvideo]http://video.google.com/videoplay?docid=-2200109535941088987[/gvideo]'.bbcode_to_html
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_email
|
138
|
+
assert_equal '<a href="mailto:wadus@wadus.com">wadus@wadus.com</a>', '[email]wadus@wadus.com[/email]'.bbcode_to_html
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_html_escaping
|
142
|
+
assert_equal "<strong><i>foobar</i></strong>", '[b]<i>foobar</i>[/b]'.bbcode_to_html
|
143
|
+
assert_equal "<strong><i>foobar</i></strong>", '[b]<i>foobar</i>[/b]'.bbcode_to_html({}, false)
|
144
|
+
assert_equal "1 is < 2", '1 is < 2'.bbcode_to_html
|
145
|
+
assert_equal "1 is < 2", '1 is < 2'.bbcode_to_html({}, false)
|
146
|
+
assert_equal "2 is > 1", '2 is > 1'.bbcode_to_html
|
147
|
+
assert_equal "2 is > 1", '2 is > 1'.bbcode_to_html({}, false)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_disable_tags
|
151
|
+
assert_equal "[b]foobar[/b]", "[b]foobar[/b]".bbcode_to_html({}, true, :disable, :bold)
|
152
|
+
assert_equal "[b]<em>foobar</em>[/b]", "[b][i]foobar[/i][/b]".bbcode_to_html({}, true, :disable, :bold)
|
153
|
+
assert_equal "[b][i]foobar[/i][/b]", "[b][i]foobar[/i][/b]".bbcode_to_html({}, true, :disable, :bold, :italics)
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_enable_tags
|
157
|
+
assert_equal "<strong>foobar</strong>", "[b]foobar[/b]".bbcode_to_html({}, true, :enable, :bold)
|
158
|
+
assert_equal "<strong>[i]foobar[/i]</strong>", "[b][i]foobar[/i][/b]".bbcode_to_html({}, true, :enable, :bold)
|
159
|
+
assert_equal "<strong><em>foobar</em></strong>", "[b][i]foobar[/i][/b]".bbcode_to_html({}, true, :enable, :bold, :italics)
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_to_html_bang_method
|
163
|
+
foo = "[b]foobar[/b]"
|
164
|
+
assert_equal "<strong>foobar</strong>", foo.bbcode_to_html!
|
165
|
+
assert_equal "<strong>foobar</strong>", foo
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_self_tag_list
|
169
|
+
assert_equal 30, BBRuby.tag_list.size
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_redefinition_of_tag_html
|
173
|
+
mydef = {
|
174
|
+
'Quote' => [
|
175
|
+
/\[quote(:.*)?="?(.*?)"?\](.*?)\[\/quote\1?\]/mi,
|
176
|
+
'<div class="quote"><p><cite>\2</cite></p><blockquote>\3</blockquote></div>',
|
177
|
+
'Quote with citation',
|
178
|
+
nil, nil,
|
179
|
+
:quote],
|
180
|
+
'Image (Resized)' => [
|
181
|
+
/\[img(:.+)? size=(['"]?)(\d+)x(\d+)\2\](.*?)\[\/img\1?\]/im,
|
182
|
+
'<div class="post_image"><img src="\5" style="width: \3px; height: \4px;" /></div>',
|
183
|
+
'Display an image with a set width and height',
|
184
|
+
'[img size=96x96]http://www.google.com/intl/en_ALL/images/logo.gif[/img]',
|
185
|
+
:image],
|
186
|
+
'Image (Alternative)' => [
|
187
|
+
/\[img=([^\[\]].*?)\.(png|bmp|jpg|gif|jpeg)\]/im,
|
188
|
+
'<div class="post_image"><img src="\1.\2" alt="" /></div>',
|
189
|
+
'Display an image (alternative format)',
|
190
|
+
'[img=http://myimage.com/logo.gif]',
|
191
|
+
:image],
|
192
|
+
'Image' => [
|
193
|
+
/\[img(:.+)?\]([^\[\]].*?)\.(png|bmp|jpg|gif|jpeg)\[\/img\1?\]/im,
|
194
|
+
'<div class="post_image"><img src="\2.\3" alt="" /></div>',
|
195
|
+
'Display an image',
|
196
|
+
'Check out this crazy cat: [img]http://catsweekly.com/crazycat.jpg[/img]',
|
197
|
+
:image],
|
198
|
+
}
|
199
|
+
assert_equal '<div class="quote"><p><cite>Who</cite></p><blockquote>said that</blockquote></div>', '[quote=Who]said that[/quote]'.bbcode_to_html(mydef)
|
200
|
+
assert_equal '<div class="quote"><p><cite>flandepan</cite></p><blockquote>hola</blockquote></div>', '[quote:0fc8a224d2="flandepan"]hola[/quote:0fc8a224d2]'.bbcode_to_html(mydef)
|
201
|
+
assert_equal '<div class="post_image"><img src="http://zoople/hochzeit.png" alt="" /></div>', '[img]http://zoople/hochzeit.png[/img]'.bbcode_to_html(mydef)
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_multiple_tag_test
|
205
|
+
assert_equal "<strong>bold</strong><em>italic</em><u>underline</u><fieldset><blockquote>quote</blockquote></fieldset><a href=\"foobar\">link</a>", "[b]bold[/b][i]italic[/i][u]underline[/u][quote]quote[/quote][url=foobar]link[/url]".bbcode_to_html
|
206
|
+
assert_equal "<strong>bold</strong><em>italic</em><u>underline</u><fieldset><blockquote>quote</blockquote></fieldset><a href=\"foobar\">link</a>", "[b]bold[/b][i]italic[/i][u]underline[/u][quote]quote[/quote][url=foobar]link[/url]".bbcode_to_html({}, true, :enable, :bold, :italics, :underline, :link, :quote)
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bb-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Craig P Jolicoeur
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-16 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.0
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.0
|
34
|
+
version:
|
35
|
+
description: bb-ruby is a BBCode (http://www.bbcode.org) implementation for Ruby. It will convert strings with BBCode markup to their HTML equivalent.
|
36
|
+
email:
|
37
|
+
- cpjolicoeur@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- History.txt
|
44
|
+
- Manifest.txt
|
45
|
+
- PostInstall.txt
|
46
|
+
- README.rdoc
|
47
|
+
files:
|
48
|
+
- History.txt
|
49
|
+
- Manifest.txt
|
50
|
+
- PostInstall.txt
|
51
|
+
- README.rdoc
|
52
|
+
- Rakefile
|
53
|
+
- lib/bb-ruby.rb
|
54
|
+
- test/test_bb-ruby.rb
|
55
|
+
- test/test_helper.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://bb-ruby.rubyforge.org
|
58
|
+
post_install_message: PostInstall.txt
|
59
|
+
rdoc_options:
|
60
|
+
- --main
|
61
|
+
- README.rdoc
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project: bb-ruby
|
79
|
+
rubygems_version: 1.3.1
|
80
|
+
signing_key:
|
81
|
+
specification_version: 2
|
82
|
+
summary: bb-ruby is a BBCode (http://www.bbcode.org) implementation for Ruby
|
83
|
+
test_files:
|
84
|
+
- test/test_bb-ruby.rb
|
85
|
+
- test/test_helper.rb
|