PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/plugins/bb-ruby/lib/bb-ruby.rb

http://iyaoi.googlecode.com/
Ruby | 360 lines | 347 code | 4 blank | 9 comment | 1 complexity | 3a5dbca7da9bd87befe24986d7546231 MD5 | raw file
Possible License(s): LGPL-2.1, MIT
  1. $:.unshift(File.dirname(__FILE__)) unless
  2. $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
  3. module BBRuby
  4. VERSION = '0.9.3'
  5. # allowable image formats
  6. @@imageformats = 'png|bmp|jpg|gif|jpeg'
  7. # built-in BBCode tabs that will be processed
  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. # '<div class=\'codebox\'><dt class=\'show_code\'>&nbsp;<strong>CODE:</strong> <a href="javascript:void(0)" title=\'????\' onclick="selectCode(this)">????</a></dt><pre><code>\2</code></pre></div>',
  49. # 'Code Text',
  50. # '[code]some code[/code]',
  51. # :code],
  52. 'Code' => [
  53. /\[code(:.+)?\](.*?)\[\/code\1?\]/mi,
  54. '<pre><code>\2</code></pre>',
  55. 'Code Text',
  56. '[code]some code[/code]',
  57. :code],
  58. 'Spacer' => [
  59. /\[space(:.+)?\](.*?)\[\/space\1?\]/mi,
  60. '<space>&nbsp;&nbsp;&nbsp;&nbsp;</space>',
  61. 'Code Text',
  62. '[code]some code[/code]',
  63. :code],
  64. 'Size' => [
  65. /\[size=['"]?(.*?)['"]?\](.*?)\[\/size\]/im,
  66. '<span style="font-size: \1px;">\2</span>',
  67. 'Change text size',
  68. '[size=20]Here is some larger text[/size]',
  69. :size],
  70. 'Color' => [
  71. /\[color=['"]?(\w+|\#\w{6})['"]?(:.+)?\](.*?)\[\/color\2?\]/im,
  72. '<span style="color: \1;">\3</span>',
  73. 'Change text color',
  74. '[color=red]This is red text[/color]',
  75. :color],
  76. 'Ordered List' => [
  77. /\[ol\](.*?)\[\/ol\]/mi,
  78. '<ol>\1</ol>',
  79. 'Ordered list',
  80. 'My favorite people (alphabetical order): [ol][li]Jenny[/li][li]Alex[/li][li]Beth[/li][/ol]',
  81. :orderedlist],
  82. 'Unordered List' => [
  83. /\[ul\](.*?)\[\/ul\]/mi,
  84. '<ul>\1</ul>',
  85. 'Unordered list',
  86. 'My favorite people (order of importance): [ul][li]Jenny[/li][li]Alex[/li][li]Beth[/li][/ul]',
  87. :unorderedlist],
  88. 'List Item' => [
  89. /\[li\](.*?)\[\/li\]/mi,
  90. '<li>\1</li>',
  91. 'List item',
  92. 'See ol or ul',
  93. :listitem],
  94. 'List Item (alternative)' => [
  95. /\[\*(:[^\[]+)?\]([^(\[|\<)]+)/mi,
  96. '<li>\2</li>',
  97. 'List item (alternative)',
  98. '[*]list item',
  99. :listitem],
  100. 'Unordered list (alternative)' => [
  101. /\[list(:.*)?\]((?:(?!list).)*)\[\/list(:.)?\1?\]/mi,
  102. '<ul>\2</ul>',
  103. 'Unordered list item',
  104. '[list][*]item 1[*] item2[/list]',
  105. :list],
  106. 'Ordered list (numerical)' => [
  107. /\[list=1(:.*)?\](.+)\[\/list(:.)?\1?\]/mi,
  108. '<ol>\2</ol>',
  109. 'Ordered list numerically',
  110. '[list=1][*]item 1[*] item2[/list]',
  111. :list],
  112. 'Ordered list (alphabetical)' => [
  113. /\[list=a(:.*)?\](.+)\[\/list(:.)?\1?\]/mi,
  114. '<ol sytle="list-style-type: lower-alpha;">\2</ol>',
  115. 'Ordered list alphabetically',
  116. '[list=a][*]item 1[*] item2[/list]',
  117. :list],
  118. 'Definition List' => [
  119. /\[dl\](.*?)\[\/dl\]/im,
  120. '<dl>\1</dl>',
  121. 'List of terms/items and their definitions',
  122. '[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]',
  123. :definelist],
  124. 'Definition Term' => [
  125. /\[dt\](.*?)\[\/dt\]/mi,
  126. '<dt>\1</dt>',
  127. 'List of definition terms',
  128. '[dt]definition term[/dt]',
  129. :defineterm],
  130. 'Definition Definition' => [
  131. /\[dd\](.*?)\[\/dd\]/mi,
  132. '<dd>\1</dd>',
  133. 'Definition definitions',
  134. '[dd]my definition[/dd',
  135. :definition],
  136. 'Quote' => [
  137. /\[quote(:.*)?="?(.*?)"?\](.*?)\[\/quote\1?\]/mi,
  138. '<blockquote><cite>\2</cite>\3</blockquote>',
  139. 'Quote with citation',
  140. "[quote=mike]Now is the time...[/quote]",
  141. :quote],
  142. 'Quote (Sourceless)' => [
  143. /\[quote(:.*)?\](.*?)\[\/quote\1?\]/mi,
  144. '<blockquote><cite>\2</cite></blockquote>',
  145. 'Quote (sourceclass)',
  146. "[quote]Now is the time...[/quote]",
  147. :quote],
  148. 'Link' => [
  149. /\[url=(.*?)\](.*?)\[\/url\]/mi,
  150. '<a href="\1">\2</a>',
  151. 'Hyperlink to somewhere else',
  152. 'Maybe try looking on [url=http://google.com]Google[/url]?',
  153. :link],
  154. 'Link (Implied)' => [
  155. /\[url\](.*?)\[\/url\]/mi,
  156. '<a href="\1">\1</a>',
  157. 'Hyperlink (implied)',
  158. "Maybe try looking on [url]http://google.com[/url]",
  159. :link],
  160. 'Link (Automatic)' => [
  161. /(\A|\s)((https?:\/\/|www\.)[^\s<]+)/,
  162. ' <a href="\2">\2</a>',
  163. 'Hyperlink (automatic)',
  164. 'Maybe try looking on http://www.google.com',
  165. :link],
  166. 'Flash' => [
  167. /\[flash=(.*?)\](.*?)\[\/flash\]/mi,
  168. '<span class="flash_vedio"><a class="flash_vedio" href="\1">\2</a></span>',
  169. 'Display flash',
  170. '[flash]your swf[/flash]',
  171. :flash],
  172. 'Image (Resized)' => [
  173. /\[img(:.+)? size=(['"]?)(\d+)x(\d+)\2\](.*?)\[\/img\1?\]/im,
  174. '<img src="\5" style="width: \3px; height: \4px;" />',
  175. 'Display an image with a set width and height',
  176. '[img size=96x96]http://www.google.com/intl/en_ALL/images/logo.gif[/img]',
  177. :image],
  178. 'Image (Alternative)' => [
  179. /\[img=([^\[\]].*?)\.(#{@@imageformats})\]/im,
  180. '<img src="\1.\2" alt="" />',
  181. 'Display an image (alternative format)',
  182. '[img=http://myimage.com/logo.gif]',
  183. :image],
  184. 'Image' => [
  185. /\[img(:.+)?\]([^\[\]].*?)\.(#{@@imageformats})\[\/img\1?\]/im,
  186. '<img src="\2.\3" alt="" />',
  187. 'Display an image',
  188. 'Check out this crazy cat: [img]http://catsweekly.com/crazycat.jpg[/img]',
  189. :image],
  190. 'YouTube' => [
  191. /\[youtube\](.*?)\?v=([\w\d\-]+).*\[\/youtube\]/im,
  192. # '<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>',
  193. '<object width="320" height="265"><param name="movie" value="http://www.youtube.com/v/\2"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/\2" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265"></embed></object>',
  194. 'Display a video from YouTube.com',
  195. '[youtube]http://youtube.com/watch?v=E4Fbk52Mk1w[/youtube]',
  196. :video],
  197. 'YouTube (Alternative)' => [
  198. /\[youtube\](.*?)\/v\/([\w\d\-]+)\[\/youtube\]/im,
  199. # '<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>',
  200. '<object width="320" height="265"><param name="movie" value="http://www.youtube.com/v/\2"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/\2" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265"></embed></object>',
  201. 'Display a video from YouTube.com (alternative format)',
  202. '[youtube]http://youtube.com/watch/v/E4Fbk52Mk1w[/youtube]',
  203. :video],
  204. 'Google Video' => [
  205. /\[google\](.*?)\?docid=([-]{0,1}\d+).*\[\/google\]/mi,
  206. '<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=\2" flashvars=""> </embed>',
  207. 'Display a video from Google Video',
  208. '[gvideo]http://video.google.com/videoplay?docid=-2200109535941088987[/gvideo]',
  209. :video],
  210. 'Email' => [
  211. /\[email(:.+)?\](.+)\[\/email\1?\]/i,
  212. '<a href="mailto:\2">\2</a>',
  213. 'Link to email address',
  214. '[email]wadus@wadus.com[/email]',
  215. :email],
  216. 'Youku' => [
  217. /\[youku\](.*?)\[\/youku\]/mi,
  218. '<embed src="\1" quality="high" width="480" height="400" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>',
  219. '????',
  220. '[youku]your addr[/youku]'
  221. ],
  222. 'Ku6' => [
  223. /\[ku6\](.*?)\[\/ku6\]/mi,
  224. '<embed src="\1" quality="high" width="480" height="400" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>',
  225. '????',
  226. '[ku6]your addr[/ku6]'
  227. ]
  228. }
  229. class << self
  230. # Convert a string with BBCode markup into its corresponding HTML markup
  231. #
  232. # === Basic Usage
  233. #
  234. # The first parameter is the string off BBCode markup to be processed
  235. #
  236. # text = "[b]some bold text to markup[/b]"
  237. # output = BBRuby.to_html(text)
  238. # # output => "<strong>some bold text to markup</strong>"
  239. #
  240. # === Custom BBCode translations
  241. #
  242. # You can supply your own BBCode markup translations to create your own custom markup
  243. # or override the default BBRuby translations (parameter is a hash of custom translations).
  244. #
  245. # The hash takes the following format: "name" => [regexp, replacement, description, example, enable_symbol]
  246. #
  247. # custom_blockquote = {
  248. # 'Quote' => [
  249. # /\[quote(:.*)?=(.*?)\](.*?)\[\/quote\1?\]/mi,
  250. # '<div class="quote"><p><cite>\2</cite></p><blockquote>\3</blockquote></div>',
  251. # 'Quote with citation',
  252. # '[quote=mike]please quote me[/quote]',
  253. # :quote
  254. # ]
  255. # }
  256. #
  257. # === Enable and Disable specific tags
  258. #
  259. # BBRuby will allow you to only enable certain BBCode tags, or to explicitly disable certain tags.
  260. # Pass in either :disable or :enable to set your method, followed by the comma-separated list of tags
  261. # you wish to disable or enable
  262. #
  263. # BBRuby.to_html(text, {}, true, :enable, :image, :bold, :quote)
  264. # BBRuby.to_html(text, {}, true, :disable, :image, :video, :color)
  265. #
  266. def to_html(text, tags_alternative_definition={}, escape_html=true, method=:disable, *tags)
  267. text = text.clone
  268. # escape "<, >, &" to remove any html
  269. if escape_html
  270. text.gsub!( '&', '&amp;' )
  271. text.gsub!( '<', '&lt;' )
  272. text.gsub!( '>', '&gt;' )
  273. end
  274. tags_definition = @@tags.merge(tags_alternative_definition)
  275. # parse bbcode tags
  276. case method
  277. when :enable
  278. tags_definition.each_value { |t| text.gsub!(t[0], t[1]) if tags.include?(t[4]) }
  279. when :disable
  280. # this works nicely because the default is disable and the default set of tags is [] (so none disabled) :)
  281. tags_definition.each_value { |t| text.gsub!(t[0], t[1]) unless tags.include?(t[4]) }
  282. end
  283. # parse spacing
  284. text.gsub!( /\r\n?/, "\n" )
  285. text.gsub!( /\n/, "<br />\n" )
  286. # return markup
  287. text
  288. end
  289. # Returns the list of tags processed by BBRuby in a Hash object
  290. def tag_list
  291. @@tags
  292. end
  293. end # class << self
  294. end # class BBRuby
  295. class String
  296. # Convert a string with BBCode markup into its corresponding HTML markup
  297. #
  298. # === Basic Usage
  299. #
  300. # text = "[b]some bold text to markup[/b]"
  301. # output = text.bbcode_to_html
  302. # # output => "<strong>some bold text to markup</strong>"
  303. #
  304. # === Custom BBCode translations
  305. #
  306. # You can supply your own BBCode markup translations to create your own custom markup
  307. # or override the default BBRuby translations (parameter is a hash of custom translations).
  308. #
  309. # The hash takes the following format: "name" => [regexp, replacement, description, example, enable_symbol]
  310. #
  311. # custom_blockquote = {
  312. # 'Quote' => [
  313. # /\[quote(:.*)?=(.*?)\](.*?)\[\/quote\1?\]/mi,
  314. # '<div class="quote"><p><cite>\2</cite></p><blockquote>\3</blockquote></div>',
  315. # 'Quote with citation',
  316. # '[quote=mike]please quote me[/quote]',
  317. # :quote
  318. # ]
  319. # }
  320. #
  321. # output = text.bbcode_to_html(custom_blockquote)
  322. #
  323. # === Enable and Disable specific tags
  324. #
  325. # BBRuby will allow you to only enable certain BBCode tags, or to explicitly disable certain tags.
  326. # Pass in either :disable or :enable to set your method, followed by the comma-separated list of tags
  327. # you wish to disable or enable
  328. #
  329. # output = text.bbcode_to_html({}, true, :enable, :image, :bold, :quote)
  330. # output = text.bbcode_to_html({}, true, :disable, :image, :video, :color)
  331. #
  332. # === HTML auto-escaping
  333. #
  334. # By default, BBRuby will auto-escape HTML. You can prevent this by passing in false as the second
  335. # parameter
  336. #
  337. # output = text.bbcode_to_html({}, false)
  338. #
  339. def bbcode_to_html(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
  340. BBRuby.to_html(self, tags_alternative_definition, escape_html, method, *tags)
  341. end
  342. # Replace the string contents with the HTML-converted markup
  343. def bbcode_to_html!(tags_alternative_definition = {}, escape_html=true, method=:disable, *tags)
  344. self.replace(BBRuby.to_html(self, tags_alternative_definition, escape_html, method, *tags))
  345. end
  346. end