cpjolicoeur-bb-ruby 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown ADDED
@@ -0,0 +1,73 @@
1
+ # bb-ruby
2
+
3
+ bb-ruby is a [BBCode](http://www.bbcode.org) implementation for Ruby
4
+
5
+ ## Installation
6
+
7
+ To install as a plugin:
8
+
9
+ `./script/plugin install git://github.com/cpjolicoeur/bb-ruby.git`
10
+
11
+ To install as a gem:
12
+
13
+ `sudo gem install bb-ruby --source=http://gems.github.com/`
14
+
15
+ ## Usage
16
+
17
+ `require 'bb-ruby'`
18
+
19
+ BBRuby has been included directly into the String class for use on any string object:
20
+
21
+ `text = "[b]Here is some bold text[/b] followed by some [u]underlined text[/u]"`
22
+
23
+ `output = text.bbcode_to_html`
24
+
25
+ `text.bbcode_to_html!`
26
+
27
+ Only allow certain tags:
28
+
29
+ `output = text.bbcode_to_html(:enable, :image, :bold, :quote)`
30
+
31
+ Disable certain tags:
32
+
33
+ `output = text.bbcode_to_html(:disable, :image, :bold, :quote)`
34
+
35
+ Alternative Direct usage:
36
+
37
+ `output = BBRuby.to_html(bbcode_markup)`
38
+
39
+ ## Developers
40
+
41
+ * [Craig P Jolicoeur](http://github.com/cpjolicoeur)
42
+
43
+ ## License
44
+
45
+ Copyright (c) 2008 Craig P Jolicoeur
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining a copy
48
+ of this software and associated documentation files (the "Software"), to deal
49
+ in the Software without restriction, including without limitation the rights
50
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
51
+ copies of the Software, and to permit persons to whom the Software is
52
+ furnished to do so, subject to the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be included in
55
+ all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
60
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
62
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
63
+ THE SOFTWARE.
64
+
65
+ ## Acknowledgements
66
+
67
+ * [ruby-bbcode project](http://code.google.com/p/ruby-bbcode/)
68
+ * [Nazgum's Blog](http://www.nazgum.com/2008/01/08/bbcode-with-ruby-on-rails-part-1/)
69
+
70
+ ## TODO
71
+
72
+ * add support for [email] tag
73
+ * add support for [code] tag
data/bb-ruby.gemspec ADDED
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "bb-ruby"
3
+ s.version = "0.6.1"
4
+ s.date = "2008-06-27"
5
+ s.summary = "BBCode conversion library for Ruby"
6
+ s.email = "cpjolicoeur@gmail.com"
7
+ s.homepage = "http://github.com/cpjolicoeur/bb-ruby"
8
+ s.description = "bb-ruby is a Ruby library to convert BBCode markup to HTML"
9
+ s.has_rdoc = false
10
+ s.authors = ["Craig P Jolicoeur"]
11
+ s.files = ["bb-ruby.gemspec", "init.rb", "README.markdown", "lib/bb-ruby.rb", "test/test-bb-ruby.rb"]
12
+ s.test_files = ["test/test-bb-ruby.rb"]
13
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'bb-ruby'
data/lib/bb-ruby.rb ADDED
@@ -0,0 +1,215 @@
1
+ module BBRuby
2
+ @@imageformats = 'png|bmp|jpg|gif'
3
+ @@tags = {
4
+ # tag name => [regex, replace, description, example, enable/disable symbol]
5
+ 'Bold' => [
6
+ /\[b\](.*?)\[\/b\]/,
7
+ '<strong>\1</strong>',
8
+ 'Embolden text',
9
+ 'Look [b]here[/b]',
10
+ :bold],
11
+ 'Italics' => [
12
+ /\[i\](.*?)\[\/i\]/,
13
+ '<em>\1</em>',
14
+ 'Italicize or emphasize text',
15
+ 'Even my [i]cat[/i] was chasing the mailman!',
16
+ :italics],
17
+ 'Underline' => [
18
+ /\[u\](.*?)\[\/u\]/,
19
+ '<u>\1</u>',
20
+ 'Underline',
21
+ 'Use it for [u]important[/u] things or something',
22
+ :underline],
23
+ 'Strikeout' => [
24
+ /\[s\](.*?)\[\/s\]/,
25
+ '<del>\1</del>',
26
+ 'Strikeout',
27
+ '[s]nevermind[/s]',
28
+ :strikeout],
29
+ 'Delete' => [
30
+ /\[del\](.*?)\[\/del\]/,
31
+ '<del>\1</del>',
32
+ 'Deleted text',
33
+ '[del]deleted text[/del]',
34
+ :delete],
35
+ 'Insert' => [
36
+ /\[ins\](.*?)\[\/ins\]/,
37
+ '<ins>\1</ins>',
38
+ 'Inserted Text',
39
+ '[ins]inserted text[/del]',
40
+ :insert],
41
+
42
+ 'Size' => [
43
+ /\[size=['"]?(.*?)['"]?\](.*?)\[\/size\]/im,
44
+ '<span style="font-size: \1px;">\2</span>',
45
+ 'Change text size',
46
+ '[size=20]Here is some larger text[/size]',
47
+ :size],
48
+ 'Color' => [
49
+ /\[color=['"]?(.*?)['"]?\](.*?)\[\/color\]/im,
50
+ '<span style="color: \1;">\2</span>',
51
+ 'Change text color',
52
+ '[color=red]This is red text[/color]',
53
+ :color],
54
+
55
+ 'Ordered List' => [
56
+ /\[ol\](.*?)\[\/ol\]/m,
57
+ '<ol>\1</ol>',
58
+ 'Ordered list',
59
+ 'My favorite people (alphabetical order): [ol][li]Jenny[/li][li]Alex[/li][li]Beth[/li][/ol]',
60
+ :orderedlist],
61
+ 'Unordered List' => [
62
+ /\[ul\](.*?)\[\/ul\]/m,
63
+ '<ul>\1</ul>',
64
+ 'Unordered list',
65
+ 'My favorite people (order of importance): [ul][li]Jenny[/li][li]Alex[/li][li]Beth[/li][/ul]',
66
+ :unorderedlist],
67
+ 'List Item' => [
68
+ /\[li\](.*?)\[\/li\]/,
69
+ '<li>\1</li>',
70
+ 'List item',
71
+ 'See ol or ul',
72
+ :listitem],
73
+ 'List Item (alternative)' => [
74
+ /\[\*\](.*?)$/,
75
+ '<li>\1</li>',
76
+ 'List item (alternative)',
77
+ nil, nil,
78
+ :listitem],
79
+
80
+ 'Definition List' => [
81
+ /\[dl\](.*?)\[\/dl\]/m,
82
+ '<dl>\1</dl>',
83
+ 'List of terms/items and their definitions',
84
+ '[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]',
85
+ :definelist],
86
+ 'Definition Term' => [
87
+ /\[dt\](.*?)\[\/dt\]/,
88
+ '<dt>\1</dt>',
89
+ 'List of definition terms',
90
+ nil, nil,
91
+ :defineterm],
92
+ 'Definition Definition' => [
93
+ /\[dd\](.*?)\[\/dd\]/,
94
+ '<dd>\1</dd>',
95
+ 'Definition definitions',
96
+ nil, nil,
97
+ :definition],
98
+
99
+ 'Quote' => [
100
+ /\[quote=(.*?)\](.*?)\[\/quote\]/m,
101
+ '<fieldset><legend>\1</legend><blockquote>\2</blockquote></fieldset>',
102
+ 'Quote with citation',
103
+ nil, nil,
104
+ :quote],
105
+ 'Quote (Sourceless)' => [
106
+ /\[quote\](.*?)\[\/quote\]/m,
107
+ '<fieldset><blockquote>\1</blockquote></fieldset>',
108
+ 'Quote (sourceclass)',
109
+ nil, nil,
110
+ :quote],
111
+
112
+ 'Link' => [
113
+ /\[url=(.*?)\](.*?)\[\/url\]/,
114
+ '<a href="\1">\2</a>',
115
+ 'Hyperlink to somewhere else',
116
+ 'Maybe try looking on [url=http://google.com]Google[/url]?',
117
+ nil, nil,
118
+ :link],
119
+ 'Link (Implied)' => [
120
+ /\[url\](.*?)\[\/url\]/,
121
+ '<a href="\1">\1</a>',
122
+ 'Hyperlink (implied)',
123
+ nil, nil,
124
+ :link],
125
+ #
126
+ # TODO: fix automatic links
127
+ #
128
+ # 'Link (Automatic)' => [
129
+ # /http:\/\/(.*?)[^<\/a>]/,
130
+ # '<a href="\1">\1</a>',
131
+ # 'Hyperlink (automatic)',
132
+ # nil, nil,
133
+ # :link],
134
+
135
+ 'Image' => [
136
+ /\[img\]([^\[\]].*?)\.(#{@@imageformats})\[\/img\]/i,
137
+ '<img src="\1.\2" alt="" />',
138
+ 'Display an image',
139
+ 'Check out this crazy cat: [img]http://catsweekly.com/crazycat.jpg[/img]',
140
+ :image],
141
+ 'Image (Alternative)' => [
142
+ /\[img=([^\[\]].*?)\.(#{@@imageformats})\]/i,
143
+ '<img src="\1.\2" alt="" />',
144
+ 'Display an image (alternative format)',
145
+ '[img=http://myimage.com/logo.gif]',
146
+ :image],
147
+ 'Image (Resized)' => [
148
+ /\[img size=['"]?(\d+)x(\d+)['"]?\](.*?)\[\/img\]/i,
149
+ '<img src="\3" style="width: \1px; height: \2px;" />',
150
+ 'Display an image with a set width and height',
151
+ '[img size=96x96]http://www.google.com/intl/en_ALL/images/logo.gif[/img]',
152
+ :image],
153
+
154
+ 'YouTube' => [
155
+ /\[youtube\](.*?)\?v=([\w\d\-]+).*\[\/youtube\]/i,
156
+ '<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>',
157
+ 'Display a video from YouTube.com',
158
+ '[youtube]http://youtube.com/watch?v=E4Fbk52Mk1w[/youtube]',
159
+ :video],
160
+ 'YouTube (Alternative)' => [
161
+ /\[youtube\](.*?)\/v\/([\w\d\-]+)\[\/youtube\]/i,
162
+ '<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>',
163
+ 'Display a video from YouTube.com (alternative format)',
164
+ '[youtube]http://youtube.com/watch/v/E4Fbk52Mk1w[/youtube]',
165
+ :video],
166
+ 'Google Video' => [
167
+ /\[gvideo\](.*?)\?docid=([-]{0,1}\d+).*\[\/gvideo\]/i,
168
+ '<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=\2" flashvars=""> </embed>',
169
+ 'Display a video from Google Video',
170
+ '[gvideo]http://video.google.com/videoplay?docid=-2200109535941088987[/gvideo]',
171
+ :video]
172
+ }
173
+ def self.to_html(text, method = :disable, *tags)
174
+ text = text.clone
175
+ # escape < and > to remove any html
176
+ text.gsub!( '<', '&lt;' )
177
+ text.gsub!( '>', '&gt;' )
178
+
179
+ # parse bbcode tags
180
+ case method
181
+ when :enable
182
+ @@tags.each_value { |t|
183
+ text.gsub!(t[0], t[1]) if tags.include?(t[4])
184
+ }
185
+ when :disable
186
+ # this works nicely because the default is disable and the default set of tags is [] (so none disabled) :)
187
+ @@tags.each_value { |t|
188
+ text.gsub!(t[0], t[1]) unless tags.include?(t[4])
189
+ }
190
+ end
191
+
192
+ # parse spacing
193
+ text.gsub!( /\r\n?/, "\n" )
194
+ text.gsub!( /\n/, "<br />" )
195
+
196
+ # return markup
197
+ text
198
+ end
199
+
200
+ def self.tags
201
+ @@tags.each { |tn, ti|
202
+ # yields the tag name, a description of it and example
203
+ yield tn, ti[2], ti[3] if ti[2]
204
+ }
205
+ end
206
+ end
207
+
208
+ class String
209
+ def bbcode_to_html(method = :disable, *tags)
210
+ BBRuby.to_html(self, method, tags)
211
+ end
212
+ def bbcode_to_html!(method = :disable, *tags)
213
+ self.replace(BBRuby.to_html(self, method, tags))
214
+ end
215
+ end
@@ -0,0 +1,48 @@
1
+ #! /usr/bin/env ruby
2
+ load 'lib/bb-ruby.rb'
3
+
4
+ text = <<eot
5
+ [b]Here is some bold text[/b]
6
+ [i]Here is some italicized text[/i]
7
+ [u]Underlined text[/u]
8
+ [b]Bold, [u]underlined, and [i]italicized[/i][/u][/b]
9
+
10
+ a [del]mistake[/del] [ins]corrected[/ins]
11
+
12
+ List of stuff:
13
+ [ul]
14
+ [*]Lions
15
+ [*]Tigers
16
+ [li]Bears[/li]
17
+ [/ul]
18
+
19
+ [size=32]12px Text[/size]
20
+ [color=red]Red Text[/color]
21
+ [color=#ff0023]Hex Color Text[/color]
22
+
23
+
24
+
25
+
26
+
27
+ Link: [url=http://google.com]Google[/url]
28
+ Link (implied): [url]http://google.com[/url]
29
+ Link (automatic): http://google.com
30
+ Image: [img]http://zoople/hochzeit.png[/img]
31
+ Image: [img=http://zoople/hochzeit.png]
32
+ Image (resized 1): [img size=96x96]http://zoople/hochzeit.png[/img]
33
+ Image (resized 2): [img size="96x96"]http://zoople/hochzeit.png[/img]
34
+ Image (resized 3): [img size='96x96']http://zoople/hochzeit.png[/img]
35
+ Image (invalid): [img]http://google.com/google.img[/img]
36
+
37
+ [quote=Bush]Go kill them terrists[/quote]
38
+ [quote]Something wise.[/quote]
39
+
40
+ [youtube]http://youtube.com/watch?v=E4Fbk52Mk1w[/youtube]
41
+ [gvideo]http://video.google.com/videoplay?docid=-2200109535941088987[/gvideo]
42
+ eot
43
+
44
+ output = text.bbcode_to_html
45
+ puts output
46
+
47
+ # text.bbcode_to_html! #(:enable, :unorderedlist, :quote)
48
+ # puts text
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpjolicoeur-bb-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.1
5
+ platform: ruby
6
+ authors:
7
+ - Craig P Jolicoeur
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: bb-ruby is a Ruby library to convert BBCode markup to HTML
17
+ email: cpjolicoeur@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - bb-ruby.gemspec
26
+ - init.rb
27
+ - README.markdown
28
+ - lib/bb-ruby.rb
29
+ - test/test-bb-ruby.rb
30
+ has_rdoc: false
31
+ homepage: http://github.com/cpjolicoeur/bb-ruby
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: BBCode conversion library for Ruby
56
+ test_files:
57
+ - test/test-bb-ruby.rb