bbcodeizer 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,5 @@
1
1
  Copyright (c) 2006 Jonathan Dance / Agora Games
2
+ Copyright (c) 2006 Trevor Turk
2
3
  Copyright (c) 2011 Luke Curley
3
4
 
4
5
  Permission is hereby granted, free of charge, to any person obtaining a
@@ -1,3 +1,3 @@
1
1
  module Bbcodeizer
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/bbcodeizer.rb CHANGED
@@ -3,29 +3,64 @@ module BBCodeizer
3
3
 
4
4
  #:nodoc:
5
5
  Tags = {
6
- :start_code => [ /\[code\]/i, '<pre>' ],
7
- :end_code => [ /\[\/code\]/i, '</pre>' ],
8
- :start_quote => [ /\[quote(?:=".*?")?\]/i, nil ],
9
- :start_quote_with_cite => [ /\[quote="(.*?)"\]/i, '<blockquote><cite>\1 wrote:</cite><br />' ],
10
- :start_quote_sans_cite => [ /\[quote\]/i, '<blockquote>' ],
11
- :end_quote => [ /\[\/quote\]/i, '</blockquote>' ],
12
- :bold => [ /\[b\](.+?)\[\/b\]/i, '<strong>\1</strong>' ],
13
- :italic => [ /\[i\](.+?)\[\/i\]/i, '<em>\1</em>' ],
14
- :underline => [ /\[u\](.+?)\[\/u\]/i, '<u>\1</u>' ],
15
- :email_with_name => [ /\[email=(.+?)\](.+?)\[\/email\]/i, '<a href="mailto:\1">\2</a>' ],
16
- :email_sans_name => [ /\[email\](.+?)\[\/email\]/i, '<a href="mailto:\1">\1</a>' ],
17
- :url_with_title => [ /\[url=(.+?)\](.+?)\[\/url\]/i, '<a href="\1">\2</a>' ],
18
- :url_sans_title => [ /\[url\](.+?)\[\/url\]/i, '<a href="\1">\1</a>' ],
19
- :image => [ /\[img\](.+?)\[\/img\]/i, '<img src="\1" />' ],
20
- :size => [ /\[size=(\d{1,2})\](.+?)\[\/size\]/i, '<span style="font-size: \1px">\2</span>' ],
21
- :color => [ /\[color=([^;]+?)\](.+?)\[\/color\]/i, '<span style="color: \1">\2</span>' ]
6
+ :literal => [ /\[\[(.+?)\]\]/im, '&#91\1&#93' ],
7
+ :start_code => [ /\[code\](\r\n?)?/i, '<pre><code>' ],
8
+ :end_code => [ /(\r\n?)?\[\/code\]/i, '</code></pre>' ],
9
+ :start_list => [ /\[list\](\r\n?)?/i, '<ul>' ],
10
+ :start_li => [ /\[li\](\r\n?)?/i, '<li>' ],
11
+ :end_li => [ /(\r\n?)?\[\/li\](\r\n?)?/i, '</li>' ],
12
+ :end_list => [ /(\r\n?)?\[\/list\]/i, '</ul>' ],
13
+ :start_quote => [ /\[quote(?:=.*?)?\](\r\n?)?/i, '<blockquote>' ],
14
+ :start_quote_with_cite => [ /\[quote=(.*?)\](\r\n?)?/i, '<blockquote><p><cite>\1 wrote:</cite></p>' ],
15
+ :start_quote_sans_cite => [ /\[quote\](\r\n?)?/i, '<blockquote>' ],
16
+ :end_quote => [ /(\r\n?)?\[\/quote\]/i, '</blockquote>' ],
17
+ :bold => [ /\[b\](.+?)\[\/b\]/im, '<strong>\1</strong>' ],
18
+ :italic => [ /\[i\](.+?)\[\/i\]/im, '<em>\1</em>' ],
19
+ :underline => [ /\[u\](.+?)\[\/u\]/im, '<u>\1</u>' ],
20
+ :del => [ /\[del\](.+?)\[\/del\]/im, '<del>\1</del>' ],
21
+ :strike => [ /\[strike\](.+?)\[\/strike\]/im, '<del>\1</del>' ],
22
+ :email_with_name => [ /\[email=(.+?)\](.+?)\[\/email\]/i, '<a href="mailto:\1">\2</a>' ],
23
+ :email_sans_name => [ /\[email\](.+?)\[\/email\]/i, '<a href="mailto:\1">\1</a>' ],
24
+ :url_with_title => [ /\[url=(.+?)\](.+?)\[\/url\]/i, '<a href="\1">\2</a>' ],
25
+ :url_sans_title => [ /\[url\](.+?)\[\/url\]/i, '<a href="\1">\1</a>' ],
26
+ :image => [ /\[img\](.+?)\[\/img\]/i, '<img src="\1" alt="\1" />' ],
27
+ :size => [ /\[size=(\d{1,2})\](.+?)\[\/size\]/im, '<span style="font-size: \1px">\2</span>' ],
28
+ :color => [ /\[color=([^;]+?)\](.+?)\[\/color\]/im, '<span style="color: \1">\2</span>' ],
29
+ :youtube => [ /\[youtube\](.+?)youtube.com\/watch\?v=(.+?)\[\/youtube\]/i, '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/\2?fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/\2?fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="350"></embed></object>' ],
30
+ :googlevid => [ /\[googlevid\](.+?)video.google.com\/videoplay\?docid=(.*?)\[\/googlevid\]/i, '<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=\2&amp;hl=en"></embed>' ],
31
+ :flash => [ /\[flash\](.+?)\[\/flash\]/i, '<object width="100%" height="100%"><param name="movie" value="\1"></param><embed src="\1" type="application/x-shockwave-flash" width="100%" height="100%"></embed></object>' ],
32
+ :spoiler => [ /\[spoiler\](.+?)\[\/spoiler\]/im, '<a href="#" class="spoiler-link" onclick="$(\'_RANDOM_ID_\').toggle(); return false;">SPOILER</a><div id="_RANDOM_ID_" class="spoiler" style="display:none;">\1</div>' ],
33
+ :nsfw => [ /\[nsfw\](.+?)\[\/nsfw\]/im, '<a href="#" class="nsfw-link" onclick="$(\'_RANDOM_ID_\').toggle(); return false;">NSFW</a><div id="_RANDOM_ID_" class="nsfw" style="display:none;">\1</div>' ],
34
+ :hide => [ /\[hide=(.+?)\](.+?)\[\/hide\]/im, '<a href="#" class="hide-link" onclick="$(\'_RANDOM_ID_\').toggle(); return false;">\1</a><div id="_RANDOM_ID_" class="hide" style="display:none;">\2</div>' ],
35
+ :mp3 => [ /\[mp3\](.+?)\[\/mp3\]/i, '<script language="JavaScript" src="/javascripts/audio-player.js"></script><object type="application/x-shockwave-flash" data="/flash/player.swf" id="_RANDOM_ID_" height="24" width="290"><param name="movie" value="/flash/player.swf"><param name="FlashVars" value="playerID=_RANDOM_ID_&amp;soundFile=\1"><param name="quality" value="high"><param name="menu" value="false"><param name="wmode" value="transparent"></object>' ],
36
+ :superdeluxe => [ /\[superdeluxe\](.+?)superdeluxe.com\/sd\/contentDetail.do\?id=(.+?)\[\/superdeluxe\]/i, '<object width="400" height="350"><param name="allowFullScreen" value="true" /><param name="movie" value="http://www.superdeluxe.com/static/swf/share_vidplayer.swf" /><param name="FlashVars" value="id=\2" /><embed src="http://www.superdeluxe.com/static/swf/share_vidplayer.swf" FlashVars="id=\2" type="application/x-shockwave-flash" width="400" height="350" allowFullScreen="true" ></embed></object>' ],
37
+ :comedycentral => [ /\[comedycentral\](.+?)comedycentral.com(.+?)?(.+?)=(.+?)\[\/comedycentral\]/i, '<embed FlashVars="config=http://www.comedycentral.com/motherload/xml/data_synd.jhtml?vid=\4%26myspace=false" src="http://www.comedycentral.com/motherload/syndicated_player/index.jhtml" quality="high" bgcolor="#006699" width="340" height="325" name="comedy_player" align="middle" allowScriptAccess="always" allownetworking="external" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>' ],
38
+ :revver => [ /\[revver\](.+?)revver.com\/video\/(.+?)\/(.+?)\[\/revver\]/i, '<embed type="application/x-shockwave-flash" src="http://flash.revver.com/player/1.0/player.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" scale="noScale" salign="TL" bgcolor="#000000" flashvars="mediaId=\2&affiliateId=0&allowFullScreen=true" allowfullscreen="true" height="392" width="480"></embed>' ],
39
+ :myspacetv => [ /\[myspacetv\](.+?)myspace(.+?)videoid=(.+?)\[\/myspacetv\]/i, '<embed src="http://lads.myspace.com/videos/vplayer.swf" flashvars="m=\3&v=2&type=video" type="application/x-shockwave-flash" width="430" height="346"></embed>' ],
40
+ :collegehumor => [ /\[collegehumor\](.+?)collegehumor.com\/video:(.+?)\[\/collegehumor\]/i, '<embed src="http://www.collegehumor.com/moogaloop/moogaloop.swf?clip_id=\2" quality="best" width="400" height="300" type="application/x-shockwave-flash"></embed>' ],
41
+ :hulu => [ /\[hulu\](.+?)\[\/hulu\]/i, '<object width="512" height="296"><param name="movie" value="http://www.hulu.com/embed/\1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.hulu.com/embed/\1" type="application/x-shockwave-flash" allowFullScreen="true" width="512" height="296"></embed></object>' ],
42
+ :metacafe => [ /\[metacafe\](.+?)metacafe.com\/watch\/(.+?)\/(.+?)\/\[\/metacafe\]/i, '<embed src="http://www.metacafe.com/fplayer/\2/\3.swf" width="400" height="345" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>' ],
43
+ :yahoovid => [ /\[yahoovid\](.+?)video.yahoo.com\/watch\/(.+?)\/(.+?)\[\/yahoovid\]/i, '<object width="512" height="323"><param name="movie" value="http://d.yimg.com/static.video.yahoo.com/yep/YV_YEP.swf?ver=2.2.2" /><param name="allowFullScreen" value="true" /><param name="flashVars" value="id=\3&vid=\2&lang=en-us&intl=us&embed=1" /><embed src="http://d.yimg.com/static.video.yahoo.com/yep/YV_YEP.swf?ver=2.2.2" type="application/x-shockwave-flash" width="512" height="323" allowFullScreen="true" flashVars="id=\3&vid=\2&lang=en-us&intl=us&embed=1" ></embed></object>' ],
44
+ :flickr => [ /\[flickr\](.+?)\/photos\/(.+?)\/(.+?)\[\/flickr\]/i, '<object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=1.162"><param name="flashvars" value="intl_lang=en-us&amp;photo_id=\3&amp;show_info_box=true"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=1.162"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=1.162" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&amp;photo_id=\3&amp;flickr_show_info_box=true" height="300" width="400"></embed></object>' ],
45
+ :gametrailers => [ /\[gametrailers\](.+?)gametrailers.com\/player\/(.+?)\.(.+?)\[\/gametrailers\]/i, '<object codebase="http://download.macromedia.com/pub/shockwave/ca...=8,0,0,0" id="gtembed" width="480" height="392"> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <param name="movie" value="http://www.gametrailers.com/remote_wrap.php?mid=\2"/> <param name="quality" value="high" /> <embed src="http://www.gametrailers.com/remote_wrap.php?mid=\2" swLiveConnect="true" name="gtembed" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="480" height="392"></embed> </object>' ], :slideshare => [ /\[slideshare id=(.+?)&(.+?)=(.+?)&(.+?)=(.+?)\]/i, '<object type="application/x-shockwave-flash" wmode="transparent" data="https://s3.amazonaws.com:443/slideshare/ssplayer.swf?id=\1&#038;doc=\3" width="\5" height="348"><param name="movie" value="https://s3.amazonaws.com:443/slideshare/ssplayer.swf?id=\1&#038;doc=\3" /></object>' ],
46
+ :funnyordie => [ /\[funnyordie\](.+?)funnyordie.com\/videos\/([^\/]+)(.*)\[\/funnyordie\]/i, '<object width="464" height="388"><param name="movie" value="http://www2.funnyordie.com/public/flash/fodplayer.swf?6045" /><param name="flashvars" value="key=\2" /><param name="allowfullscreen" value="true" /><embed width="464" height="388" flashvars="key=\2" allowfullscreen="true" quality="high" src="http://www2.funnyordie.com/public/flash/fodplayer.swf?6045" type="application/x-shockwave-flash"></embed></object>' ],
47
+ :atomfilms => [ /\[atomfilms\](.+?)atomfilms.com\/film\/(.+?)\[\/atomfilms\]/i, '<embed src="http://www.atomfilms.com:80/a/autoplayer/shareEmbed.swf?keyword=\2" width="426" height="350"></embed>' ],
48
+ :current => [ /\[current\](.+?)current.com\/items\/(.+?)\/(.+?)\[\/current\]/i, '<object width="400" height="400"><param name="movie" value="http://current.com/e/\2/en_US"></param><param name="wmode" value="transparent"></param><param name="allowfullscreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://current.com/e/\2/en_US" type="application/x-shockwave-flash" width="400" height="400" wmode="transparent" allowfullscreen="true" allowscriptaccess="always"></embed></object>' ],
49
+ :vimeo => [ /\[vimeo\](.+?)vimeo.com\/(.+?)\[\/vimeo\]/i, '<object type="application/x-shockwave-flash" width="400" height="300" data="http://www.vimeo.com/moogaloop.swf?clip_id=\2&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color="> <param name="quality" value="best" /> <param name="allowfullscreen" value="true" /> <param name="scale" value="showAll" /> <param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=\2&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=" /></object>' ],
50
+ :grooveshark => [ /\[grooveshark\](.+?)\[\/grooveshark\]/i, '<object width="250" height="40"> <param name="movie" value="http://listen.grooveshark.com/songWidget.swf"></param> <param name="wmode" value="window"></param> <param name="allowScriptAccess" value="always"></param> <param name="flashvars" value="hostname=cowbell.grooveshark.com&widgetID=\1&style=grass&p=0"></param> <embed src="http://listen.grooveshark.com/songWidget.swf" type="application/x-shockwave-flash" width="250" height="40" flashvars="hostname=cowbell.grooveshark.com&widgetID=\1&style=grass&p=0" allowScriptAccess="always" wmode="window"></embed></object>'],
51
+ :sup => [ /\[sup\](.+?)\[\/sup\]/im, '<sup>\1</sup>' ],
52
+ :sub => [ /\[sub\](.+?)\[\/sub\]/im, '<sub>\1</sub>' ],
22
53
  }
23
54
 
24
55
  # Tags in this list are invoked. To deactivate a particular tag, call BBCodeizer.deactivate.
25
56
  # These names correspond to either names above or methods in this module.
26
- TagList = [ :bold, :italic, :underline, :email_with_name, :email_sans_name,
27
- :url_with_title, :url_sans_title, :image, :size, :color,
28
- :code, :quote ]
57
+ # The ':literal' tag MUST be first for it to work correctly
58
+ TagList = [ :literal, :bold, :italic, :underline, :del, :strike, :email_with_name,
59
+ :email_sans_name, :url_with_title, :url_sans_title, :image, :size, :color,
60
+ :code, :quote, :youtube, :googlevid, :flash, :spoiler, :nsfw, :hide, :mp3,
61
+ :superdeluxe, :comedycentral, :revver, :myspacetv, :collegehumor, :hulu,
62
+ :metacafe, :yahoovid, :flickr, :gametrailers, :slideshare, :funnyordie,
63
+ :atomfilms, :vimeo, :li, :list, :current ]
29
64
 
30
65
  # Parses all bbcode in +text+ and returns a new HTML-formatted string.
31
66
  def bbcodeize(text)
@@ -36,6 +71,9 @@ module BBCodeizer
36
71
  else
37
72
  self.send(tag, text)
38
73
  end
74
+ # Replace the matching random ids for the following tags: spoiler, nsfw, mp3
75
+ @random_id = random_string
76
+ 2.times { text = text.sub('_RANDOM_ID_', @random_id) }
39
77
  end
40
78
  text
41
79
  end
@@ -57,6 +95,29 @@ module BBCodeizer
57
95
  # code tags must match, else don't do any replacing.
58
96
  if string.scan(Tags[:start_code].first).size == string.scan(Tags[:end_code].first).size
59
97
  apply_tags(string, :start_code, :end_code)
98
+ # strip out newlines from within the tags and replace them with '<br />', otherwise
99
+ # simple_format will simply append a '<br />' to the newlines, creating double spaces
100
+ string.gsub!(/#{Tags[:start_code].last}.+?#{Tags[:end_code].last}/im) { |s| s.gsub(/\r\n?/, '<br />') }
101
+ end
102
+ end
103
+
104
+ def li(string)
105
+ # list tags must match, else don't do any replacing.
106
+ if string.scan(Tags[:start_li].first).size == string.scan(Tags[:end_li].first).size
107
+ apply_tags(string, :start_li, :end_li)
108
+ # strip out newlines from within the tags and replace them with '<br />', otherwise
109
+ # simple_format will simply append a '<br />' to the newlines, creating double spaces
110
+ string.gsub!(/#{Tags[:start_li].last}.+?#{Tags[:end_li].last}/im) { |s| s.gsub(/\r\n?/, '<br />') }
111
+ end
112
+ end
113
+
114
+ def list(string)
115
+ # list tags must match, else don't do any replacing.
116
+ if string.scan(Tags[:start_list].first).size == string.scan(Tags[:end_list].first).size
117
+ apply_tags(string, :start_list, :end_list)
118
+ # strip out newlines from within the tags and replace them with '<br />', otherwise
119
+ # simple_format will simply append a '<br />' to the newlines, creating double spaces
120
+ string.gsub!(/#{Tags[:start_list].last}.+?#{Tags[:end_list].last}/im) { |s| s.gsub(/\r\n?/, '<br />') }
60
121
  end
61
122
  end
62
123
 
@@ -64,6 +125,9 @@ module BBCodeizer
64
125
  # quotes must match, else don't do any replacing
65
126
  if string.scan(Tags[:start_quote].first).size == string.scan(Tags[:end_quote].first).size
66
127
  apply_tags(string, :start_quote_with_cite, :start_quote_sans_cite, :end_quote)
128
+ # strip out newlines from within the tags and replace them with '<br />', otherwise
129
+ # simple_format will simply append a '<br />' to the newlines, creating double spaces
130
+ string.gsub!(/#{Tags[:start_quote].last}.+?#{Tags[:end_quote].last}/im) { |s| s.gsub(/\r\n?/, '<br />') }
67
131
  end
68
132
  end
69
133
 
@@ -73,5 +137,10 @@ module BBCodeizer
73
137
  end
74
138
  end
75
139
  alias_method :apply_tag, :apply_tags
140
+
141
+ # http://stackoverflow.com/questions/88311/how-best-to-generate-a-random-string-in-ruby
142
+ def random_string
143
+ (0...16).map{ ('a'..'z').to_a[rand(26)] }.join
144
+ end
76
145
  end
77
146
  end
File without changes
@@ -1,12 +1,12 @@
1
1
  require 'test/unit'
2
- require 'bbcodeize_helper'
2
+ require 'bbcodeizer_helper'
3
3
 
4
4
  class BbcodeizerTest < Test::Unit::TestCase
5
5
  include BBCodeizeHelper
6
6
 
7
7
  def test_quote_with_cite
8
8
  assert_equal(
9
- "<blockquote><cite>JD wrote:</cite><br />\nBBCode is great!\n</blockquote>",
9
+ "<blockquote><p><cite>\"JD\" wrote:</cite></p>\nBBCode is great!\n</blockquote>",
10
10
  bbcodeize("[quote=\"JD\"]\nBBCode is great!\n[/quote]"))
11
11
  end
12
12
 
@@ -24,7 +24,7 @@ class BbcodeizerTest < Test::Unit::TestCase
24
24
 
25
25
  def test_code
26
26
  assert_equal(
27
- "<pre>\ncode goes here\n</pre>",
27
+ "<pre><code>\ncode goes here\n</code></pre>",
28
28
  bbcodeize("[code]\ncode goes here\n[/code]"))
29
29
  end
30
30
 
@@ -72,7 +72,7 @@ class BbcodeizerTest < Test::Unit::TestCase
72
72
 
73
73
  def test_image
74
74
  assert_equal(
75
- "<img src=\"http://example.com/example.gif\" />",
75
+ "<img src=\"http://example.com/example.gif\" alt=\"http://example.com/example.gif\" />",
76
76
  bbcodeize("[img]http://example.com/example.gif[/img]"))
77
77
  end
78
78
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: bbcodeizer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jonathan Dance
@@ -54,9 +54,9 @@ files:
54
54
  - Rakefile
55
55
  - bbcodeizer.gemspec
56
56
  - init.rb
57
- - lib/bbcodeize_helper.rb
58
57
  - lib/bbcodeizer.rb
59
58
  - lib/bbcodeizer/version.rb
59
+ - lib/bbcodeizer_helper.rb
60
60
  - rdoc/BBCodeizeHelper.html
61
61
  - rdoc/Bbcodeizer.html
62
62
  - rdoc/README.html