bb-ruby 0.9.4 → 0.9.5

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/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.9.5 2010-02-26
2
+
3
+ * added to_html_with_formatting(!) methods
4
+
5
+ == 0.9.4 2009-11-20
6
+
7
+ * added Vimeo video embed tag support
8
+
1
9
  == 0.9.3 2009-06-11
2
10
 
3
11
  * better auto link support
data/README.rdoc CHANGED
@@ -55,6 +55,12 @@ Define your own translation, in order to be more flexible:
55
55
  }
56
56
 
57
57
  text.bbcode_to_html(my_blockquote)
58
+
59
+ You can also use the simple_format method of ActionPack by using the *_with_formatting methods:
60
+
61
+ output = text.bbcode_to_html_with_formatting
62
+ output = text.bbcode_to_html_with_formatting!
63
+
58
64
 
59
65
  == TAGS PROCESSED:
60
66
 
data/Rakefile CHANGED
@@ -1,6 +1,9 @@
1
1
  %w[rubygems rake rake/clean fileutils newgem rubigen hoe].each { |f| require f }
2
2
  require File.dirname(__FILE__) + '/lib/bb-ruby'
3
3
 
4
+ Hoe.plugin :newgem
5
+ Hoe.plugin :website
6
+
4
7
  # Generate all the Rake tasks
5
8
  # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
9
  $hoe = Hoe.spec('bb-ruby') do |p|
data/lib/bb-ruby.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module BBRuby
5
- VERSION = '0.9.4'
5
+ VERSION = '0.9.5'
6
6
 
7
7
  # allowable image formats
8
8
  @@imageformats = 'png|bmp|jpg|gif|jpeg'
@@ -244,7 +244,38 @@ module BBRuby
244
244
  # BBRuby.to_html(text, {}, true, :disable, :image, :video, :color)
245
245
  #
246
246
  def to_html(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags)
247
- text = text.clone
247
+ text = process_tags(text, tags_alternative_definition, escape_html, method, *tags)
248
+
249
+ # parse spacing
250
+ text.gsub!( /\r\n?/, "\n" )
251
+ text.gsub!( /\n/, "<br />\n" )
252
+
253
+ # return markup
254
+ text
255
+ end
256
+
257
+ # The same as BBRuby.to_html except the output is passed through simple_format first
258
+ #
259
+ # Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n)
260
+ # are considered as a paragraph and wrapped in <p> tags. One newline (\n) is considered as a linebreak and
261
+ # a <br /> tag is appended. This method does not remove the newlines from the text.
262
+ #
263
+ def to_html_with_formatting(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags)
264
+ text = process_tags(text, tags_alternative_definition, escape_html, method, *tags)
265
+
266
+ # parse spacing
267
+ simple_format( text )
268
+ end
269
+
270
+ # Returns the list of tags processed by BBRuby in a Hash object
271
+ def tag_list
272
+ @@tags
273
+ end
274
+
275
+ private
276
+
277
+ def process_tags(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags)
278
+ text = text.dup
248
279
 
249
280
  # escape "<, >, &" to remove any html
250
281
  if escape_html
@@ -263,18 +294,19 @@ module BBRuby
263
294
  # this works nicely because the default is disable and the default set of tags is [] (so none disabled) :)
264
295
  tags_definition.each_value { |t| text.gsub!(t[0], t[1]) unless tags.include?(t[4]) }
265
296
  end
266
-
267
- # parse spacing
268
- text.gsub!( /\r\n?/, "\n" )
269
- text.gsub!( /\n/, "<br />\n" )
270
-
271
- # return markup
297
+
272
298
  text
273
299
  end
274
-
275
- # Returns the list of tags processed by BBRuby in a Hash object
276
- def tag_list
277
- @@tags
300
+
301
+ # extracted from Rails ActionPack
302
+ def simple_format( text )
303
+ start_tag = '<p>'
304
+ text = text.to_s.dup
305
+ text.gsub!(/\r\n?/, "\n") # \r\n and \r => \n
306
+ text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline => paragraph
307
+ text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline => br
308
+ text.insert 0, start_tag
309
+ text << "</p>"
278
310
  end
279
311
  end # class << self
280
312
 
@@ -332,4 +364,13 @@ class String
332
364
  def bbcode_to_html!(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
333
365
  self.replace(BBRuby.to_html(self, tags_alternative_definition, escape_html, method, *tags))
334
366
  end
367
+
368
+ def bbcode_to_html_with_formatting(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
369
+ BBRuby.to_html_with_formatting(self, tags_alternative_definition, escape_html, method, *tags)
370
+ end
371
+
372
+ # Replace the string contents with the HTML-converted markup using simple_format
373
+ def bbcode_to_html_with_formatting!(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
374
+ self.replace(BBRuby.to_html_with_formatting(self, tags_alternative_definition, escape_html, method, *tags))
375
+ end
335
376
  end
data/test/test_bb-ruby.rb CHANGED
@@ -179,6 +179,16 @@ class TestBBRuby < Test::Unit::TestCase
179
179
  assert_equal "<strong>foobar</strong>", foo.bbcode_to_html!
180
180
  assert_equal "<strong>foobar</strong>", foo
181
181
  end
182
+
183
+ def test_to_html_with_no_markup
184
+ foo = "first paragraph\n\nsecond paragraph\nwith a linebreak"
185
+ assert_equal "first paragraph<br />\n<br />\nsecond paragraph<br />\nwith a linebreak", foo.bbcode_to_html
186
+ end
187
+
188
+ def test_to_html_with_formatting
189
+ foo = "first paragraph\n\nsecond paragraph\nwith a linebreak"
190
+ assert_equal %Q(<p>first paragraph</p>\n\n<p>second paragraph\n<br />with a linebreak</p>), foo.bbcode_to_html_with_formatting
191
+ end
182
192
 
183
193
  def test_self_tag_list
184
194
  assert_equal 32, BBRuby.tag_list.size
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bb-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig P Jolicoeur
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-20 00:00:00 -05:00
12
+ date: 2010-02-26 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency