bbgun 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/README.rdoc +43 -0
- data/Rakefile +2 -0
- data/bbgun.gemspec +22 -0
- data/init.rb +1 -0
- data/lib/bbgun/application_helper.rb +9 -0
- data/lib/bbgun/coderay.rb +30 -0
- data/lib/bbgun/config.rb +63 -0
- data/lib/bbgun/css.rb +8 -0
- data/lib/bbgun/parse.rb +42 -0
- data/lib/bbgun/string.rb +21 -0
- data/lib/bbgun/swear_filter.rb +17 -0
- data/lib/bbgun/tags.rb +73 -0
- data/lib/bbgun/version.rb +3 -0
- data/lib/bbgun.rb +61 -0
- data/spec/bbgun_spec.rb +43 -0
- metadata +97 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
bbgun (1.0.0)
|
5
|
+
coderay
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.0.8)
|
11
|
+
diff-lcs (1.1.2)
|
12
|
+
rspec (2.3.0)
|
13
|
+
rspec-core (~> 2.3.0)
|
14
|
+
rspec-expectations (~> 2.3.0)
|
15
|
+
rspec-mocks (~> 2.3.0)
|
16
|
+
rspec-core (2.3.1)
|
17
|
+
rspec-expectations (2.3.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.3.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bbgun!
|
26
|
+
rspec
|
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= BBGun
|
2
|
+
|
3
|
+
{Homepage}[http://www.github.com/PeteMichaud/bbgun]
|
4
|
+
|
5
|
+
Rails Gem for BBCode
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
Add:
|
10
|
+
|
11
|
+
gem "bbgun"
|
12
|
+
|
13
|
+
to your Gemfile and then install the gem using:
|
14
|
+
|
15
|
+
bundle install
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
The easiest way to use tbbc is with the string method provided, for example
|
20
|
+
|
21
|
+
>> "[b]Bold[/b]".tbbc
|
22
|
+
=> "<strong>Bold</strong>"
|
23
|
+
|
24
|
+
== Configuration
|
25
|
+
|
26
|
+
=== On the fly
|
27
|
+
|
28
|
+
If you want to quickly change something, e.g. disable a tag for a specific .tbbc call then you can configure it in the call:
|
29
|
+
|
30
|
+
>> "[b]Bold[/b] [i]Italic[/i]".tbbc(:strong_enabled => false)
|
31
|
+
=> "[b]Bold[/b] <i>Italic</i>"
|
32
|
+
|
33
|
+
Whilst this method works its not the cleanest way of doing it. Its fine for simple things like disabling a single tag (e.g. tables in signatures) but when defining custom tags or the coderay css its better to use the other config method
|
34
|
+
|
35
|
+
=== Globaly
|
36
|
+
|
37
|
+
This is done using config/initializers/tbbc.rb, this file needs to look like this:
|
38
|
+
|
39
|
+
BBGun.configure do |c|
|
40
|
+
c.strong_enabled = false
|
41
|
+
end
|
42
|
+
|
43
|
+
That would produce the same output as the on the fly config but would work when ever you used .tbbc in the project.
|
data/Rakefile
ADDED
data/bbgun.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "bbgun/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "bbgun"
|
7
|
+
s.version = BBGun::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Pete Michaud"]
|
10
|
+
s.email = ["michaudp@gmail.com"]
|
11
|
+
s.homepage = "http://www.github.com/PeteMichaud/bbgun"
|
12
|
+
s.summary = "Provides BBCode for Ruby."
|
13
|
+
s.summary = "Provides BBCode for Ruby."
|
14
|
+
|
15
|
+
s.add_development_dependency "rspec"
|
16
|
+
s.add_dependency "coderay"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bbgun'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module BBGun
|
2
|
+
module Coderay
|
3
|
+
def self.parse(input)
|
4
|
+
input=input.gsub("\r","")
|
5
|
+
scan=input.scan(/\[code lang=(.+?)\](.+?)\[\/code\]/m)
|
6
|
+
scan.each do |splits|
|
7
|
+
parse=splits[1].gsub("<","<").gsub(">",">")
|
8
|
+
lang=splits[0]
|
9
|
+
parsed="[nobbc]" + CodeRay.scan(parse, lang).div(:line_numbers => @config[:syntax_highlighting_line_numbers], :css => :class) + "[/nobbc]"
|
10
|
+
input=input.gsub("[code lang=#{lang}]#{splits[1]}[/code]",parsed)
|
11
|
+
end
|
12
|
+
out = coderay_styled(input)
|
13
|
+
return out.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.coderay_styled(input)
|
17
|
+
output = input.gsub(/class="CodeRay"/,"style=\"background-color: #232323; border: 1px solid black; font-family: 'Courier New', 'Terminal', monospace; color: #E6E0DB; padding: 3px 5px; overflow: auto; font-size: 12px; margin: 12px 0;\"")
|
18
|
+
output = output.gsub(/<pre>/,"<pre style=\"margin: 0px; padding: 0px;\">")
|
19
|
+
output = output.gsub(/class="an"/,"style=\"#{BBGun.config.syntax_highlighting_html}\"").gsub(/class="c"/,"style=\"#{BBGun.config.syntax_highlighting_comment}\"")
|
20
|
+
output = output.gsub(/class="ch"/,"style=\"#{BBGun.config.syntax_highlighting_escaped}\"").gsub(/class="cl"/,"style=\"#{BBGun.config.syntax_highlighting_class}\"")
|
21
|
+
output = output.gsub(/class="co"/,"style=\"#{BBGun.config.syntax_highlighting_constant}\"").gsub(/class="fl"/,"style=\"#{BBGun.config.syntax_highlighting_float}\"")
|
22
|
+
output = output.gsub(/class="fu"/,"style=\"#{BBGun.config.syntax_highlighting_function}\"").gsub(/class="gv"/,"style=\"#{BBGun.config.syntax_highlighting_global}\"")
|
23
|
+
output = output.gsub(/class="i"/,"style=\"#{BBGun.config.syntax_highlighting_integer}\"").gsub(/class="il"/,"style=\"#{BBGun.config.syntax_highlighting_inline}\"")
|
24
|
+
output = output.gsub(/class="iv"/,"style=\"#{BBGun.config.syntax_highlighting_instance}\"").gsub(/class="pp"/,"style=\"#{BBGun.config.syntax_highlighting_doctype}\"")
|
25
|
+
output = output.gsub(/class="r"/,"style=\"#{BBGun.config.syntax_highlighting_keyword}\"").gsub(/class="rx"/,"style=\"#{BBGun.config.syntax_highlighting_regex}\"")
|
26
|
+
output = output.gsub(/class="s"/,"style=\"#{BBGun.config.syntax_highlighting_string}\"").gsub(/class="sy"/,"style=\"#{BBGun.config.syntax_highlighting_symbol}\"")
|
27
|
+
output = output.gsub(/class="ta"/,"style=\"#{BBGun.config.syntax_highlighting_html}\"").gsub(/class="pc"/,"style=\"#{BBGun.config.syntax_highlighting_boolean}\"")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/bbgun/config.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module BBGun
|
2
|
+
class Config
|
3
|
+
attr_accessor :configed_by, :image_alt, :url_target, :allow_defaults, :table_width, :syntax_highlighting, :syntax_highlighting_html,
|
4
|
+
:syntax_highlighting_comment, :syntax_highlighting_escaped, :syntax_highlighting_class, :syntax_highlighting_constant,
|
5
|
+
:syntax_highlighting_float, :syntax_highlighting_function, :syntax_highlighting_global, :syntax_highlighting_integer,
|
6
|
+
:syntax_highlighting_inline, :syntax_highlighting_instance, :syntax_highlighting_doctype, :syntax_highlighting_keyword,
|
7
|
+
:syntax_highlighting_regex, :syntax_highlighting_string, :syntax_highlighting_symbol, :syntax_highlighting_html,
|
8
|
+
:syntax_highlighting_boolean, :syntax_highlighting_line_numbers, :swear_words, :custom_tags, :disable_html, :newline_enabled,
|
9
|
+
:nobbc_enabled, :swear_filter_enabled
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
set_defaults
|
13
|
+
end
|
14
|
+
|
15
|
+
def check_enabled(sym)
|
16
|
+
eval "@#{sym.to_s}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def from_sym(sym)
|
20
|
+
eval "@#{sym.to_s}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def set_from_sym(k, v)
|
24
|
+
eval "@#{k.to_s} = #{v.inspect}"
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def set_defaults
|
30
|
+
@configed_by ||= "user"
|
31
|
+
@image_alt ||= "Posted Image"
|
32
|
+
@url_target ||= "_BLANK"
|
33
|
+
@allow_defaults ||= true
|
34
|
+
@table_width ||= "100%"
|
35
|
+
@syntax_highlighting ||= true
|
36
|
+
@syntax_highlighting_html ||= "color:#E7BE69"
|
37
|
+
@syntax_highlighting_comment ||= "color:#BC9358; font-style: italic;"
|
38
|
+
@syntax_highlighting_escaped ||= "color:#509E4F"
|
39
|
+
@syntax_highlighting_class ||= "color:#FFF"
|
40
|
+
@syntax_highlighting_constant ||= "color:#FFF"
|
41
|
+
@syntax_highlighting_float ||= "color:#A4C260"
|
42
|
+
@syntax_highlighting_function ||= "color:#FFC56D"
|
43
|
+
@syntax_highlighting_global ||= "color:#D0CFFE"
|
44
|
+
@syntax_highlighting_integer ||= "color:#A4C260"
|
45
|
+
@syntax_highlighting_inline ||= "background:#151515"
|
46
|
+
@syntax_highlighting_instance ||= "color:#D0CFFE"
|
47
|
+
@syntax_highlighting_doctype ||= "color:#E7BE69"
|
48
|
+
@syntax_highlighting_keyword ||= "color:#CB7832"
|
49
|
+
@syntax_highlighting_regex ||= "color:#A4C260"
|
50
|
+
@syntax_highlighting_string ||= "color:#A4C260"
|
51
|
+
@syntax_highlighting_symbol ||= "color:#6C9CBD"
|
52
|
+
@syntax_highlighting_html ||= "color:#E7BE69"
|
53
|
+
@syntax_highlighting_boolean ||= "color:#6C9CBD"
|
54
|
+
@syntax_highlighting_line_numbers ||= :inline
|
55
|
+
@swear_words ||= []
|
56
|
+
@custom_tags ||= []
|
57
|
+
@disable_html ||= true
|
58
|
+
@newline_enabled ||= true
|
59
|
+
@nobbc_enabled ||= true
|
60
|
+
@swear_filter_enabled ||= true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/bbgun/css.rb
ADDED
data/lib/bbgun/parse.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module BBGun
|
2
|
+
# Parses the input and returns it in tbbc form.
|
3
|
+
#
|
4
|
+
# TBBC.new.parse("[b]Bold[/b]")
|
5
|
+
#
|
6
|
+
# Would return "<strong>Bold</strong>"
|
7
|
+
def self.parse(input)
|
8
|
+
#Remove the < and > which will disable all HTML
|
9
|
+
input=input.gsub("<","<").gsub(">",">") unless @config.disable_html == false
|
10
|
+
#CodeRay
|
11
|
+
input=Coderay.parse(input) unless @config.syntax_highlighting == false
|
12
|
+
#Convert new lines to <br />'s
|
13
|
+
input=input.gsub(/\n/,'<br />') unless @config.newline_enabled == false
|
14
|
+
#[nobbc] tags
|
15
|
+
input=nobbc input unless @config.nobbc_enabled == false
|
16
|
+
#Swear Filtering
|
17
|
+
input=SwearFilter.parse(input) unless @config.swear_filter_enabled == false
|
18
|
+
#Loading Custom Tags
|
19
|
+
#begin
|
20
|
+
if @config.custom_tags then
|
21
|
+
@config.custom_tags.each do |tag|
|
22
|
+
input=runtag(input,tag)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
#rescue
|
26
|
+
#input+="<br />Custom Tags failed to run"
|
27
|
+
#end
|
28
|
+
#Loading Default Tags and applying them
|
29
|
+
if @config.allow_defaults then
|
30
|
+
BBGun::Tags.each do |tag|
|
31
|
+
input=runtag(input,tag)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
input = correctbrs input
|
35
|
+
input = escape input
|
36
|
+
if needs_html_safe? then
|
37
|
+
return input.html_safe
|
38
|
+
else
|
39
|
+
return input
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/bbgun/string.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class String
|
2
|
+
# Allows for "[b]Bold[/b]".tbbc
|
3
|
+
#
|
4
|
+
# This method is also configurable, taking the normal configuration options.
|
5
|
+
#
|
6
|
+
# "[b]Bold [i]and italic[/i][/b]".tbbc(:strong_enabled => false, :italic_enabled => false)
|
7
|
+
#
|
8
|
+
# Will return "[b]Bold [i]and italic[/i][/b]"
|
9
|
+
def tbbc(conf = nil)
|
10
|
+
if !(BBGun.configured?) || conf != nil
|
11
|
+
BBGun.configure do |c|
|
12
|
+
if conf
|
13
|
+
conf.each do |k, v|
|
14
|
+
c.set_from_sym(k,v)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
BBGun.parse(self)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module BBGun
|
2
|
+
module SwearFilter
|
3
|
+
SwearChracters = %w[! $ % ^ * ~]
|
4
|
+
def self.parse(input)
|
5
|
+
BBGun.config.swear_words.each do |swear|
|
6
|
+
input=input.gsub(/#{swear}/,swear_string(swear))
|
7
|
+
end
|
8
|
+
input
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.swear_string(s)
|
12
|
+
out=""
|
13
|
+
s.split("").map { |split| out+= SwearChracters[rand(SwearChracters.count)] }
|
14
|
+
out
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/bbgun/tags.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
module BBGun
|
2
|
+
Tags = [[/\[b\](.*?)\[\/b\]/,'<strong>\1</strong>',:strong_enabled,"[b]BOLD[/b]","<strong>BOLD</strong>"],
|
3
|
+
[/\[i\](.*?)\[\/i\]/,'<i>\1</i>',:italic_enabled,"[i]italics[/i]","<i>italics</i>"],
|
4
|
+
[/\[u\](.*?)\[\/u\]/,'<u>\1</u>',:underline_enabled,"[u]underline[/u]","<u>underline</u>"],
|
5
|
+
[/\[url\](.*?)\[\/url\]/,'<a href="\1" target="@config[:url_target]">\1</a>',:url_enabled,"[url]http://www.example.com[/url]","<a href=\"http://www.example.com\" target=\"_BLANK\">http://www.example.com</a>"],
|
6
|
+
[/\[url=(.*?)\](.*?)\[\/url\]/,'<a href="\1" target="@config[:url_target]">\2</a>',:url_enabled,"[url=http://www.example.com]Example[/url]","<a href=\"http://www.example.com\" target=\"_BLANK\">Example</a>"],
|
7
|
+
[/\[url=(.*?) target=(.*?)\](.*?)\[\/url\]/,'<a href="\1" target="\2">\3</a>',:url_enabled,"[url=http://www.example.com]Example[/url]","<a href=\"http://www.example.com\" target=\"_BLANK\">Example</a>"],
|
8
|
+
[/\[img\](.*?)\[\/img\]/,'<img src="\1" alt="@config[:image_alt]" />',:image_enabled,"[img]test.jpg[/img]","<img src=\"test.jpg\" alt=\"Posted Image\" />"],
|
9
|
+
[/\[img alt=(.*?)\](.*?)\[\/img\]/,'<img src="\2" alt="\1" />',:image_enabled,"[img alt=Posted Image]test.jpg[/img]","<img src=\"test.jpg\" alt=\"Posted Image\" />"],
|
10
|
+
[/\[ul\](.*?)\[\/ul\]/,'<ul>\1</ul>',:list_enabled,"[ul]list[/ul]","<ul>list</ul>"],
|
11
|
+
[/\[ol\](.*?)\[\/ol\]/,'<ol>\1</ol>',:list_enabled,"[ol]list[/ol]","<ol>list</ol>"],
|
12
|
+
[/\[li\](.*?)\[\/li\]/,'<li>\1</li>',:list_enabled,"[li]list[/li]","<li>list</li>"],
|
13
|
+
[/\[quote\](.*?)\[\/quote\]/,'<blockquote>\1</blockquote>',:quote_enabled,"[quote]quote[/quote]","<blockquote>quote</blockquote>"],
|
14
|
+
[/\[quote=(.*?)\](.*?)\[\/quote\]/,'<blockquote><i>Posted by <b>\1</b></i><br />\2</blockquote>',:quote_enabled,"[quote=user]quote[/quote]","<blockquote><i>Posted by <b>user</b></i><br />quote</blockquote>"],
|
15
|
+
[/\[color=(.*?)\](.*?)\[\/color\]/,'<span style="color:\1;">\2</span>',:color_enabled,"[color=red]red text[/color]","<span style=\"color:red;\">red text</span>"],
|
16
|
+
[/\[center\](.*?)\[\/center\]/,'<div style="text-align:center">\1</div>',:alignment_enabled,"[center]centered text[/center]","<div style=\"text-align:center\">centered text</div>"],
|
17
|
+
[/\[left\](.*?)\[\/left\]/,'<div style="text-align:left">\1</div>',:alignment_enabled,"[left]left text[/left]","<div style=\"text-align:left\">left text</div>"],
|
18
|
+
[/\[right\](.*?)\[\/right\]/,'<div style="text-align:right">\1</div>',:alignment_enabled,"[right]right text[/right]","<div style=\"text-align:right\">right text</div>"],
|
19
|
+
[/\[acronym=(.*?)\](.*?)\[\/acronym\]/,'<acronym title="\1">\2</acronym>',:acronym_enabled,"[acronym=this]that[/acronym]","<acronym title=\"this\">that</acronym>"],
|
20
|
+
[/\[table\](.*?)\[\/table\]/,'<table width="@config[:table_width]">\1</table>',:table_enabled,"[table]...[/table]","<table width=\"100%\">...</table>"],
|
21
|
+
[/\[tr\](.*?)\[\/tr\]/,'<tr>\1</tr>',:table_enabled,"[tr]...[/tr]","<tr>...</tr>"],
|
22
|
+
[/\[td\](.*?)\[\/td\]/,'<td>\1</td>',:table_enabled,"[td]...[/td]","<td>...</td>"],
|
23
|
+
[/\[th\](.*?)\[\/th\]/,'<th>\1</th>',:table_enabled,"[th]...[/th]","<th>...</th>"],
|
24
|
+
[/\[h1\](.*?)\[\/h1\]/,'<h1>\1</h1>',:header_enabled,'[h1]heading[/h1]',"<h1>heading</h1>"],
|
25
|
+
[/\[h2\](.*?)\[\/h2\]/,'<h2>\1</h2>',:header_enabled,'[h2]heading[/h2]',"<h2>heading</h2>"],
|
26
|
+
[/\[h3\](.*?)\[\/h3\]/,'<h3>\1</h3>',:header_enabled,'[h3]heading[/h3]',"<h3>heading</h3>"],
|
27
|
+
[/\[h4\](.*?)\[\/h4\]/,'<h4>\1</h4>',:header_enabled,'[h4]heading[/h4]',"<h4>heading</h4>"]]
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Runs the given tag array on the given string.
|
32
|
+
def self.runtag(s,tag)
|
33
|
+
check = tag[2]
|
34
|
+
check = @config.check_enabled(tag[2]) if is_symbol? tag[2]
|
35
|
+
if tag[1] =~ /^Callback:/
|
36
|
+
s = run_callback(s, tag[0], tag[1])
|
37
|
+
else
|
38
|
+
pattern = tag[1]
|
39
|
+
s=s.gsub(tag[0],replace_config_values(pattern)) unless check == false
|
40
|
+
end
|
41
|
+
s
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.is_symbol?(v)
|
45
|
+
return false if (v == true or v == false)
|
46
|
+
v == v.to_sym
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.replace_config_values(s)
|
50
|
+
if s.scan(/@config\[:(.*?)\]/) != [] then
|
51
|
+
return s.gsub(/@config\[:(.*?)\]/,@config.from_sym($1.to_sym))
|
52
|
+
else
|
53
|
+
return s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.run_callback(string, regex, callback)
|
58
|
+
code = callback.gsub(/^Callback: /, '')
|
59
|
+
output = string
|
60
|
+
string.scan(regex).each do |arguments|
|
61
|
+
arguments_pass = build_pass_string(arguments)
|
62
|
+
replace = eval "#{code}#{arguments_pass}"
|
63
|
+
output = output.sub(regex, replace)
|
64
|
+
end
|
65
|
+
output
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.build_pass_string(args)
|
69
|
+
output = "("
|
70
|
+
args.map { |arg| output = "#{output}\"#{arg}\", " }
|
71
|
+
output.gsub(/, $/,')')
|
72
|
+
end
|
73
|
+
end
|
data/lib/bbgun.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'bbgun/parse.rb'
|
2
|
+
require 'bbgun/tags.rb'
|
3
|
+
require 'bbgun/config.rb'
|
4
|
+
require 'bbgun/string.rb'
|
5
|
+
require 'bbgun/css.rb'
|
6
|
+
require 'bbgun/swear_filter.rb'
|
7
|
+
require 'bbgun/coderay'
|
8
|
+
require 'rubygems'
|
9
|
+
require 'coderay'
|
10
|
+
|
11
|
+
#Helper Method
|
12
|
+
if defined? Rails
|
13
|
+
require 'bbgun/application_helper.rb'
|
14
|
+
ActionView::Base.send :include, TBBCHelper
|
15
|
+
end
|
16
|
+
|
17
|
+
module BBGun
|
18
|
+
def self.configure
|
19
|
+
@config = Config.new
|
20
|
+
yield(@config)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configured?
|
24
|
+
@config ? true : false
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.config
|
28
|
+
@config
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def self.nobbc(s)
|
34
|
+
find=s.scan(/\[nobbc\](.*?)\[\/nobbc\]/)
|
35
|
+
find.each do |f|
|
36
|
+
replace=f[0].gsub("[","[").gsub("]","]")
|
37
|
+
s=s.gsub("[nobbc]#{f[0]}[/nobbc]",replace)
|
38
|
+
end
|
39
|
+
return s
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.correctbrs(s)
|
43
|
+
#Corrects the extra brs
|
44
|
+
s=s.gsub(/<br \/><(ul|li|table|tr|td|th)/,'<\1')
|
45
|
+
s=s.gsub(/<br \/><\/(ul|li|table|tr|td|th)/,'</\1')
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.escape(s)
|
49
|
+
clean = s.scan(/href=\"(.*?)\" target/).join
|
50
|
+
clean = clean.gsub('"', '"')
|
51
|
+
s.gsub(/href=\"(.*?)\" target/, "href=\"#{clean}\" target")
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.needs_html_safe?
|
55
|
+
if defined? Rails
|
56
|
+
return Rails.version =~ /^3\./
|
57
|
+
else
|
58
|
+
return false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/bbgun_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'lib/bbgun.rb'
|
2
|
+
|
3
|
+
def upcaser(input)
|
4
|
+
input.upcase
|
5
|
+
end
|
6
|
+
|
7
|
+
describe BBGun, "#parse" do
|
8
|
+
BBGun::Tags.each do |tag|
|
9
|
+
it "Should return #{tag[4]} for #{tag[3]}" do
|
10
|
+
tag[3].tbbc.should == tag[4]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe String, "#tbbc" do
|
16
|
+
it "Should return <i>italics</i> for [i]italics[/i]" do
|
17
|
+
"[i]italics[/i]".tbbc.should == "<i>italics</i>"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "Should return <strong>BOLD [i]italics[/i]</strong> when italics are disabled" do
|
21
|
+
"[b]BOLD [i]italics[/i][/b]".tbbc(:italic_enabled => false).should == "<strong>BOLD [i]italics[/i]</strong>"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "Should allow custom tags to run and return <strong><i>BOLD italics</i></strong> for [bi]BOLD italics[/bi]" do
|
25
|
+
"[bi]BOLD italics[/bi]".tbbc(:custom_tags => [[/\[bi\](.*?)\[\/bi\]/,'<strong><i>\1</i></strong>',true]]).should == "<strong><i>BOLD italics</i></strong>"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "Should allow custom tags to pass to callbacks" do
|
29
|
+
"[up]HeLlo WorLd[/up]".tbbc(:custom_tags => [[/\[up\](.*?)\[\/up\]/,"Callback: upcaser",true]]).should == "HELLO WORLD"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "Should not fail when a callback is defined but not used" do
|
33
|
+
"[dw]HeLlo WorLd[/dw]".tbbc(:custom_tags => [[/\[up\](.*?)\[\/up\]/,"Callback: upcaser",true]]).should == "[dw]HeLlo WorLd[/dw]"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "Should allow for 2 identical callbacks per string" do
|
37
|
+
"[up]HeLlo WorLd[/up] and [up]Bye bYe WorLd[/up]".tbbc(:custom_tags => [[/\[up\](.*?)\[\/up\]/,"Callback: upcaser",true]]).should == "HELLO WORLD and BYE BYE WORLD"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should stop injection" do
|
41
|
+
'[url=" onclick="alert(\'Hello World!\');]Hello world[/url]'.tbbc.should_not eq "<a href=\"\" onclick=\"alert('Hello World!');\" target=\"_BLANK\">Hello world</a>"
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bbgun
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Pete Michaud
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: coderay
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description:
|
47
|
+
email:
|
48
|
+
- michaudp@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- README.rdoc
|
57
|
+
- Rakefile
|
58
|
+
- bbgun.gemspec
|
59
|
+
- init.rb
|
60
|
+
- lib/bbgun.rb
|
61
|
+
- lib/bbgun/application_helper.rb
|
62
|
+
- lib/bbgun/coderay.rb
|
63
|
+
- lib/bbgun/config.rb
|
64
|
+
- lib/bbgun/css.rb
|
65
|
+
- lib/bbgun/parse.rb
|
66
|
+
- lib/bbgun/string.rb
|
67
|
+
- lib/bbgun/swear_filter.rb
|
68
|
+
- lib/bbgun/tags.rb
|
69
|
+
- lib/bbgun/version.rb
|
70
|
+
- spec/bbgun_spec.rb
|
71
|
+
homepage: http://www.github.com/PeteMichaud/bbgun
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.24
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Provides BBCode for Ruby.
|
95
|
+
test_files:
|
96
|
+
- spec/bbgun_spec.rb
|
97
|
+
has_rdoc:
|