bb-ruby 0.9.8 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in bb-ruby.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem 'rake'
8
+ end
data/Gemfile.lock CHANGED
@@ -1,14 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bb-ruby (0.9.8)
4
+ bb-ruby (0.9.9)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ rake (10.0.3)
9
10
 
10
11
  PLATFORMS
11
12
  ruby
12
13
 
13
14
  DEPENDENCIES
14
15
  bb-ruby!
16
+ rake
data/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ == 1.0 2013-05-9
2
+
3
+ * Enable Procs to be used in custom defined replacements
4
+
5
+ == 0.9.9 2013-02-2
6
+
7
+ * Dont insert any extra html tags if just using .to_html method
8
+
1
9
  == 0.9.8 2012-10-03
2
10
 
3
11
  * replace deprecated html <u> tag
@@ -54,4 +62,4 @@
54
62
 
55
63
  * No major enhancements:
56
64
  * Small point release for moving gem from Github to RubyForge
57
- * Gem is no longer served from github via cpjolicoeur-bbruby. Gem is now served directly from Rubyforge via bb-ruby
65
+ * Gem is no longer served from github via cpjolicoeur-bbruby. Gem is now served directly from Rubyforge via bb-ruby
data/README.rdoc CHANGED
@@ -55,6 +55,20 @@ Define your own translation, in order to be more flexible:
55
55
  }
56
56
 
57
57
  text.bbcode_to_html(my_blockquote)
58
+
59
+ Define Proc as replacement:
60
+
61
+ module BBRuby
62
+ @@tags = @@tags.merge({
63
+ 'File' => [
64
+ /\[file(:.*)?=(.*?)\](.*?)\[\/file\1?\]/mi,
65
+ lambda{ |e| "<div class="file"><p><cite>#{e[3]}</cite></p><blockquote>#{file_read_method(e[2])}</blockquote></div>"},
66
+ 'File content with citation',
67
+ '[file=script.rb]Script Caption[/file]',
68
+ :file
69
+ ],
70
+ })
71
+ end
58
72
 
59
73
  You can also use the simple_format method of ActionPack by using the *_with_formatting methods:
60
74
 
data/Rakefile CHANGED
@@ -9,3 +9,4 @@ Rake::TestTask.new(:test) do |t|
9
9
  t.verbose = false
10
10
  end
11
11
 
12
+ task :default => :test
data/lib/bb-ruby.rb CHANGED
@@ -10,7 +10,7 @@ module BBRuby
10
10
  # tag name => [regex, replace, description, example, enable/disable symbol]
11
11
  'Bold' => [
12
12
  /\[b(:.*)?\](.*?)\[\/b\1?\]/mi,
13
- '<strong>\2</strong>',
13
+ '<strong>\2</strong>', #Proc alternative for example: lambda{ |e| "<strong>#{e[2]}</strong>" }
14
14
  'Embolden text',
15
15
  'Look [b]here[/b]',
16
16
  :bold],
@@ -252,10 +252,10 @@ module BBRuby
252
252
 
253
253
  # parse spacing
254
254
  text.gsub!( /\r\n?/, "\n" )
255
- text.gsub!( /\n/, "<br />\n" )
256
- text.gsub!(/\[quote\]/, '<fieldset><legend>Quote:</legend><blockquote>')
257
- text.gsub!(/\[quote(:.*)?="?(.*?)"?\]/, '<fieldset><legend>Quote: \2</legend><blockquote>')
258
- text.gsub!(/\[\/quote\]/, '</blockquote></fieldset>')
255
+
256
+ text.gsub!(/\[quote\]/, '<fieldset><legend>Quote:</legend><blockquote>')
257
+ text.gsub!(/\[quote(:.*)?="?(.*?)"?\]/, '<fieldset><legend>Quote: \2</legend><blockquote>')
258
+ text.gsub!(/\[\/quote\]/, '</blockquote></fieldset>')
259
259
 
260
260
  # return markup
261
261
  text
@@ -298,10 +298,28 @@ module BBRuby
298
298
  # parse bbcode tags
299
299
  case method
300
300
  when :enable
301
- tags_definition.each_value { |t| text.gsub!(t[0], t[1]) if tags.include?(t[4]) }
301
+ tags_definition.each_value do |t|
302
+ if tags.include?( t[4] )
303
+ if t[1].class == String
304
+ text.gsub!( t[0], t[1] ) # just replace if replacement is String
305
+ else
306
+ text.gsub!( t[0] ){ t[1].call($~) } # call replacement
307
+ # It may be Proc or lambda with one argument
308
+ # Argument is MatchData. See 'Bold' tag name for example.
309
+ end
310
+ end
311
+ end
302
312
  when :disable
303
313
  # this works nicely because the default is disable and the default set of tags is [] (so none disabled) :)
304
- tags_definition.each_value { |t| text.gsub!(t[0], t[1]) unless tags.include?(t[4]) }
314
+ tags_definition.each_value do |t|
315
+ unless tags.include?( t[4] )
316
+ if t[1].class == String
317
+ text.gsub!( t[0], t[1] )
318
+ else
319
+ text.gsub!( t[0] ){ t[1].call($~) }
320
+ end
321
+ end
322
+ end
305
323
  end
306
324
 
307
325
  text
@@ -1,3 +1,3 @@
1
1
  module BBRuby
2
- VERSION = "0.9.8"
2
+ VERSION = "1.0"
3
3
  end
data/test/bb-ruby_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #! /usr/bin/env ruby
2
2
  #coding: utf-8
3
3
 
4
- require File.dirname(__FILE__) + '/test_helper.rb'
4
+ require 'test_helper.rb'
5
5
 
6
6
  class TestBBRuby < Test::Unit::TestCase
7
7
 
@@ -9,20 +9,20 @@ class TestBBRuby < Test::Unit::TestCase
9
9
  assert_equal '<strong>simple</strong>', '[b]simple[/b]'.bbcode_to_html
10
10
  assert_equal '<strong>simple</strong>', '[b:7a9ca2c5c3]simple[/b:7a9ca2c5c3]'.bbcode_to_html
11
11
  assert_equal %Q(<strong>simple</strong>), BBRuby.to_html( %Q([b:7a9ca2c5c3]simple[/b:7a9ca2c5c3]) )
12
- assert_equal "<strong>line 1<br />\nline 2</strong>", "[b:7a9ca2c5c3]line 1\nline 2[/b:7a9ca2c5c3]".bbcode_to_html
13
- assert_equal "<strong>1. text 1:</strong> text 2<br />\n<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
+ assert_equal "<strong>line 1\nline 2</strong>", "[b:7a9ca2c5c3]line 1\nline 2[/b:7a9ca2c5c3]".bbcode_to_html
13
+ assert_equal "<strong>1. text 1:</strong> text 2\n<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
14
14
  end
15
15
 
16
16
  def test_em
17
17
  assert_equal '<em>simple</em>', '[i]simple[/i]'.bbcode_to_html
18
18
  assert_equal '<em>simple</em>', '[i:7a9ca2c5c3]simple[/i:7a9ca2c5c3]'.bbcode_to_html
19
- assert_equal "<em>line 1<br />\nline 2</em>", "[i:7a9ca2c5c3]line 1\nline 2[/i:7a9ca2c5c3]".bbcode_to_html
19
+ assert_equal "<em>line 1\nline 2</em>", "[i:7a9ca2c5c3]line 1\nline 2[/i:7a9ca2c5c3]".bbcode_to_html
20
20
  end
21
21
 
22
22
  def test_u
23
23
  assert_equal '<span style="text-decoration:underline;">simple</span>', '[u]simple[/u]'.bbcode_to_html
24
24
  assert_equal '<span style="text-decoration:underline;">simple</span>', '[u:7a9ca2c5c3]simple[/u:7a9ca2c5c3]'.bbcode_to_html
25
- assert_equal '<span style="text-decoration:underline;">line 1<br />\nline 2</span>', "[u:7a9ca2c5c3]line 1\nline 2[/u:7a9ca2c5c3]".bbcode_to_html
25
+ assert_equal %Q(<span style="text-decoration:underline;">line 1\nline 2</span>), "[u:7a9ca2c5c3]line 1\nline 2[/u:7a9ca2c5c3]".bbcode_to_html
26
26
  end
27
27
 
28
28
  def test_del
@@ -40,7 +40,7 @@ class TestBBRuby < Test::Unit::TestCase
40
40
  def test_code
41
41
  assert_equal '<code>simple</code>', '[code]simple[/code]'.bbcode_to_html
42
42
  assert_equal '<code>simple</code>', '[code:7a9ca2c5c3]simple[/code:7a9ca2c5c3]'.bbcode_to_html
43
- assert_equal "<code>var bxi = 0;<br />\n//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
43
+ assert_equal "<code>var bxi = 0;\n//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
44
44
  end
45
45
 
46
46
  def test_size
@@ -113,7 +113,7 @@ class TestBBRuby < Test::Unit::TestCase
113
113
  end
114
114
 
115
115
  def test_double_quote
116
- assert_equal '<fieldset><legend>Kitten</legend><blockquote><fieldset><legend>creatiu</legend><blockquote>f1</blockquote></fieldset>f2</blockquote></fieldset>',
116
+ assert_equal '<fieldset><legend>Kitten</legend><blockquote><fieldset><legend>Quote: &quot;creatiu&quot;</legend><blockquote>f1</blockquote></fieldset>f2</blockquote></fieldset>',
117
117
  '[quote:26fe26a6a9="Kitten"][quote:26fe26a6a93="creatiu"]f1[/quote:26fe26a6a93]f2[/quote:26fe26a6a9]'.bbcode_to_html.bbcode_to_html({}, false, :disable)
118
118
  end
119
119
 
@@ -121,7 +121,7 @@ class TestBBRuby < Test::Unit::TestCase
121
121
  assert_equal '<a href="http://google.com">Google</a>', '[url=http://google.com]Google[/url]'.bbcode_to_html
122
122
  assert_equal '<a href="http://google.com">http://google.com</a>', '[url]http://google.com[/url]'.bbcode_to_html
123
123
  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
124
- assert_equal %Q(<a href="http://www.altctrlsupr.com/dmstk/kdd070803/00.html"> ABRIR<br />\nALBUM </a>),"[URL=http://www.altctrlsupr.com/dmstk/kdd070803/00.html] ABRIR\nALBUM [/URL]".bbcode_to_html
124
+ assert_equal %Q(<a href="http://www.altctrlsupr.com/dmstk/kdd070803/00.html"> ABRIR\nALBUM </a>),"[URL=http://www.altctrlsupr.com/dmstk/kdd070803/00.html] ABRIR\nALBUM [/URL]".bbcode_to_html
125
125
  assert_equal '<a href="http://www.urimalet.com/cadaverex.mp3">aha</a>', "[URL=http://www.urimalet.com/cadaverex.mp3]aha[/URL]".bbcode_to_html
126
126
  # allow quoted urls:
127
127
  assert_equal '<a href="http://www.apple.com">aha</a>', '[URL="http://www.apple.com"]aha[/URL]'.bbcode_to_html
@@ -195,7 +195,7 @@ class TestBBRuby < Test::Unit::TestCase
195
195
 
196
196
  def test_to_html_with_no_markup
197
197
  foo = "first paragraph\n\nsecond paragraph\nwith a linebreak"
198
- assert_equal "first paragraph<br />\n<br />\nsecond paragraph<br />\nwith a linebreak", foo.bbcode_to_html
198
+ assert_equal foo, foo.bbcode_to_html
199
199
  end
200
200
 
201
201
  def test_to_html_with_formatting
@@ -204,7 +204,7 @@ class TestBBRuby < Test::Unit::TestCase
204
204
  end
205
205
 
206
206
  def test_self_tag_list
207
- assert_equal 32, BBRuby.tag_list.size
207
+ assert_equal 33, BBRuby.tag_list.size
208
208
  end
209
209
 
210
210
  def test_redefinition_of_tag_html
@@ -256,4 +256,58 @@ class TestBBRuby < Test::Unit::TestCase
256
256
  assert_equal "this [b]should not do formatting[/i]", "this [b]should not do formatting[/i]".bbcode_to_html
257
257
  end
258
258
 
259
+ ## proc tests below
260
+ def test_redefinition_replacement_to_proc # contrived example
261
+ mydef = {
262
+ 'Quote' => [
263
+ /\[quote(:.*)?=(?:&quot;)?(.*?)(?:&quot;)?\](.*?)\[\/quote\1?\]/mi,
264
+ lambda { |e| "<div class=\"quote\"><p><cite>#{e[2]}</cite></p><blockquote>#{e[3]}</blockquote></div>"},
265
+ 'Quote with citation (lambda)',
266
+ nil, nil,
267
+ :quote]
268
+ }
269
+ assert_equal '<div class="quote"><p><cite>Who</cite></p><blockquote>said that</blockquote></div>', '[quote=Who]said that[/quote]'.bbcode_to_html(mydef)
270
+ end
271
+
272
+ def test_proc_modifer # sum as example
273
+ mydef = {
274
+ 'Sum (lambda)' => [
275
+ /\[sum(:.*)?=(?:&quot;)?(.*?)(?:&quot;)?\](\d+?)\+(\d+?)\[\/sum\1?\]/mi,
276
+ lambda { |e| "<span class=\"#{e[2]}\">#{e[3].to_i + e[4].to_i}</span>"},
277
+ 'Sum (lambda)',
278
+ nil, nil,
279
+ :sum]
280
+ }
281
+ assert_equal '<span class="sum">4</span>', '[sum=sum]2+2[/sum]'.bbcode_to_html(mydef)
282
+ end
283
+
284
+ # for next test
285
+ def sum a, b; a + b end
286
+
287
+ def test_proc_include_method # sum as example
288
+ mydef = {
289
+ 'Sum (lambda)' => [
290
+ /\[sum(:.*)?=(?:&quot;)?(.*?)(?:&quot;)?\](\d+?)\+(\d+?)\[\/sum\1?\]/mi,
291
+ lambda { |e| "<span class=\"#{e[2]}\">#{sum(e[3].to_i, e[4].to_i)}</span>"},
292
+ 'Sum (lambda)',
293
+ nil, nil,
294
+ :sum]
295
+ }
296
+ assert_equal '<span class="sum">4</span>', '[sum=sum]2+2[/sum]'.bbcode_to_html(mydef)
297
+ end
298
+
299
+ # Proc.new{} as opposed to lambda{} may have not params
300
+ def test_proc_instead_of_lambda # copyright
301
+ copyright = "2913-3013 The Company, Ltd."
302
+ mydef = {
303
+ 'copy' => [
304
+ /\[copy\/\]/mi,
305
+ Proc.new{"<span class=\"copy\">&copy; #{copyright}</span>"},
306
+ 'Copy (Proc)',
307
+ nil, nil,
308
+ :copy]
309
+ }
310
+ assert_equal '<span class="copy">&copy; 2913-3013 The Company, Ltd.</span>', '[copy/]'.bbcode_to_html(mydef)
311
+ end
312
+
259
313
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bb-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
5
4
  prerelease:
5
+ version: '1.0'
6
6
  platform: ruby
7
7
  authors:
8
8
  - Craig P. Jolicoeur
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-03 00:00:00.000000000 Z
12
+ date: 2013-05-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: BBCode for Ruby
15
15
  email:
@@ -36,20 +36,20 @@ rdoc_options: []
36
36
  require_paths:
37
37
  - lib
38
38
  required_ruby_version: !ruby/object:Gem::Requirement
39
- none: false
40
39
  requirements:
41
40
  - - ! '>='
42
41
  - !ruby/object:Gem::Version
43
42
  version: '0'
44
- required_rubygems_version: !ruby/object:Gem::Requirement
45
43
  none: false
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
45
  requirements:
47
46
  - - ! '>='
48
47
  - !ruby/object:Gem::Version
49
48
  version: '0'
49
+ none: false
50
50
  requirements: []
51
51
  rubyforge_project:
52
- rubygems_version: 1.8.21
52
+ rubygems_version: 1.8.23
53
53
  signing_key:
54
54
  specification_version: 3
55
55
  summary: BBRuby is a BBCode implementation for Ruby. It will convert strings with