Skip to content
Browse files

Swap config system

  • Loading branch information...
1 parent 4b29b6e commit 71dad38b1d8149c52c6975478c604e4d3a0b2ee8 @Arcath committed
View
28 lib/trainbbcode.rb
@@ -1,6 +1,6 @@
require 'trainbbcode/parse.rb'
require 'trainbbcode/tags.rb'
-require 'trainbbcode/configure.rb'
+require 'trainbbcode/config.rb'
require 'trainbbcode/string.rb'
require 'trainbbcode/css.rb'
require 'trainbbcode/swear_filter.rb'
@@ -14,17 +14,23 @@
ActionView::Base.send :include, TBBCHelper
end
-class TBBC
- # Creates a new TBBC class with everything set to default
- #
- # Also sets :configed_by to "system"
- def initialize
- self.conf(:configed_by => "system")
- end
+module TrainBBCode
+ def self.configure
+ @config = Config.new
+ yield(@config)
+ end
+
+ def self.configured?
+ @config ? true : false
+ end
+
+ def self.config
+ @config
+ end
private
- def nobbc(s)
+ def self.nobbc(s)
find=s.scan(/\[nobbc\](.*?)\[\/nobbc\]/)
find.each do |f|
replace=f[0].gsub("[","[").gsub("]","&#93")
@@ -33,13 +39,13 @@ def nobbc(s)
return s
end
- def correctbrs(s)
+ def self.correctbrs(s)
#Corrects the extra brs
s=s.gsub(/<br \/><(ul|li|table|tr|td|th)/,'<\1')
s=s.gsub(/<br \/><\/(ul|li|table|tr|td|th)/,'</\1')
end
- def needs_html_safe?
+ def self.needs_html_safe?
if defined? Rails
return Rails.version =~ /^3\./
else
View
14 lib/trainbbcode/application_helper.rb
@@ -1,7 +1,9 @@
-module TBBCHelper
- def tbbc_css(config = nil)
- bbc=TBBC.new
- bbc.conf(config) if config
- bbc.css
- end
+module TrainBBCode
+ module TBBCHelper
+ def tbbc_css(config = nil)
+ bbc=TBBC.new
+ bbc.conf(config) if config
+ bbc.css
+ end
+ end
end
View
51 lib/trainbbcode/coderay.rb
@@ -1,27 +1,30 @@
-class TBBC
- def coderay(input)
- input=input.gsub("\r","")
- scan=input.scan(/\[code lang=(.+?)\](.+?)\[\/code\]/m)
- scan.each do |splits|
- parse=splits[1].gsub("&lt;","<").gsub("&gt;",">")
- lang=splits[0]
- parsed="[nobbc]" + CodeRay.scan(parse, lang).div(:line_numbers => @config[:syntax_highlighting_line_numbers], :css => :class) + "[/nobbc]"
- input=input.gsub("[code lang=#{lang}]#{splits[1]}[/code]",parsed)
- end
- coderay_styled(input)
- end
+module TrainBBCode
+ module Coderay
+ def self.parse(input)
+ input=input.gsub("\r","")
+ scan=input.scan(/\[code lang=(.+?)\](.+?)\[\/code\]/m)
+ scan.each do |splits|
+ parse=splits[1].gsub("&lt;","<").gsub("&gt;",">")
+ lang=splits[0]
+ parsed="[nobbc]" + CodeRay.scan(parse, lang).div(:line_numbers => @config[:syntax_highlighting_line_numbers], :css => :class) + "[/nobbc]"
+ input=input.gsub("[code lang=#{lang}]#{splits[1]}[/code]",parsed)
+ end
+ out = coderay_styled(input)
+ return out.to_s
+ end
- def coderay_styled(input)
- 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;\"")
- output = output.gsub(/<pre>/,"<pre style=\"margin: 0px; padding: 0px;\">")
- output = output.gsub(/class="an"/,"style=\"#{@config[:syntax_highlighting_html]}\"").gsub(/class="c"/,"style=\"#{@config[:syntax_highlighting_comment]}\"")
- output = output.gsub(/class="ch"/,"style=\"#{@config[:syntax_highlighting_escaped]}\"").gsub(/class="cl"/,"style=\"#{@config[:syntax_highlighting_class]}\"")
- output = output.gsub(/class="co"/,"style=\"#{@config[:syntax_highlighting_constant]}\"").gsub(/class="fl"/,"style=\"#{@config[:syntax_highlighting_float]}\"")
- output = output.gsub(/class="fu"/,"style=\"#{@config[:syntax_highlighting_function]}\"").gsub(/class="gv"/,"style=\"#{@config[:syntax_highlighting_global]}\"")
- output = output.gsub(/class="i"/,"style=\"#{@config[:syntax_highlighting_integer]}\"").gsub(/class="il"/,"style=\"#{@config[:syntax_highlighting_inline]}\"")
- output = output.gsub(/class="iv"/,"style=\"#{@config[:syntax_highlighting_instance]}\"").gsub(/class="pp"/,"style=\"#{@config[:syntax_highlighting_doctype]}\"")
- output = output.gsub(/class="r"/,"style=\"#{@config[:syntax_highlighting_keyword]}\"").gsub(/class="rx"/,"style=\"#{@config[:syntax_highlighting_regex]}\"")
- output = output.gsub(/class="s"/,"style=\"#{@config[:syntax_highlighting_string]}\"").gsub(/class="sy"/,"style=\"#{@config[:syntax_highlighting_symbol]}\"")
- output = output.gsub(/class="ta"/,"style=\"#{@config[:syntax_highlighting_html]}\"").gsub(/class="pc"/,"style=\"#{@config[:syntax_highlighting_boolean]}\"")
+ def self.coderay_styled(input)
+ 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;\"")
+ output = output.gsub(/<pre>/,"<pre style=\"margin: 0px; padding: 0px;\">")
+ output = output.gsub(/class="an"/,"style=\"#{TrainBBCode.config.syntax_highlighting_html}\"").gsub(/class="c"/,"style=\"#{TrainBBCode.config.syntax_highlighting_comment}\"")
+ output = output.gsub(/class="ch"/,"style=\"#{TrainBBCode.config.syntax_highlighting_escaped}\"").gsub(/class="cl"/,"style=\"#{TrainBBCode.config.syntax_highlighting_class}\"")
+ output = output.gsub(/class="co"/,"style=\"#{TrainBBCode.config.syntax_highlighting_constant}\"").gsub(/class="fl"/,"style=\"#{TrainBBCode.config.syntax_highlighting_float}\"")
+ output = output.gsub(/class="fu"/,"style=\"#{TrainBBCode.config.syntax_highlighting_function}\"").gsub(/class="gv"/,"style=\"#{TrainBBCode.config.syntax_highlighting_global}\"")
+ output = output.gsub(/class="i"/,"style=\"#{TrainBBCode.config.syntax_highlighting_integer}\"").gsub(/class="il"/,"style=\"#{TrainBBCode.config.syntax_highlighting_inline}\"")
+ output = output.gsub(/class="iv"/,"style=\"#{TrainBBCode.config.syntax_highlighting_instance}\"").gsub(/class="pp"/,"style=\"#{TrainBBCode.config.syntax_highlighting_doctype}\"")
+ output = output.gsub(/class="r"/,"style=\"#{TrainBBCode.config.syntax_highlighting_keyword}\"").gsub(/class="rx"/,"style=\"#{TrainBBCode.config.syntax_highlighting_regex}\"")
+ output = output.gsub(/class="s"/,"style=\"#{TrainBBCode.config.syntax_highlighting_string}\"").gsub(/class="sy"/,"style=\"#{TrainBBCode.config.syntax_highlighting_symbol}\"")
+ output = output.gsub(/class="ta"/,"style=\"#{TrainBBCode.config.syntax_highlighting_html}\"").gsub(/class="pc"/,"style=\"#{TrainBBCode.config.syntax_highlighting_boolean}\"")
+ end
end
end
View
63 lib/trainbbcode/config.rb
@@ -0,0 +1,63 @@
+module TrainBBCode
+ class Config
+ attr_accessor :configed_by, :image_alt, :url_target, :allow_defaults, :table_width, :syntax_highlighting, :syntax_highlighting_html,
+ :syntax_highlighting_comment, :syntax_highlighting_escaped, :syntax_highlighting_class, :syntax_highlighting_constant,
+ :syntax_highlighting_float, :syntax_highlighting_function, :syntax_highlighting_global, :syntax_highlighting_integer,
+ :syntax_highlighting_inline, :syntax_highlighting_instance, :syntax_highlighting_doctype, :syntax_highlighting_keyword,
+ :syntax_highlighting_regex, :syntax_highlighting_string, :syntax_highlighting_symbol, :syntax_highlighting_html,
+ :syntax_highlighting_boolean, :syntax_highlighting_line_numbers, :swear_words, :custom_tags, :disable_html, :newline_enabled,
+ :nobbc_enabled, :swear_filter_enabled
+
+ def initialize
+ set_defaults
+ end
+
+ def check_enabled(sym)
+ eval "@#{sym.to_s}"
+ end
+
+ def from_sym(sym)
+ eval "@#{sym.to_s}"
+ end
+
+ def set_from_sym(k, v)
+ eval "@#{k.to_s} = #{v.inspect}"
+ end
+
+ private
+
+ def set_defaults
+ @configed_by ||= "user"
+ @image_alt ||= "Posted Image"
+ @url_target ||= "_BLANK"
+ @allow_defaults ||= true
+ @table_width ||= "100%"
+ @syntax_highlighting ||= true
+ @syntax_highlighting_html ||= "color:#E7BE69"
+ @syntax_highlighting_comment ||= "color:#BC9358; font-style: italic;"
+ @syntax_highlighting_escaped ||= "color:#509E4F"
+ @syntax_highlighting_class ||= "color:#FFF"
+ @syntax_highlighting_constant ||= "color:#FFF"
+ @syntax_highlighting_float ||= "color:#A4C260"
+ @syntax_highlighting_function ||= "color:#FFC56D"
+ @syntax_highlighting_global ||= "color:#D0CFFE"
+ @syntax_highlighting_integer ||= "color:#A4C260"
+ @syntax_highlighting_inline ||= "background:#151515"
+ @syntax_highlighting_instance ||= "color:#D0CFFE"
+ @syntax_highlighting_doctype ||= "color:#E7BE69"
+ @syntax_highlighting_keyword ||= "color:#CB7832"
+ @syntax_highlighting_regex ||= "color:#A4C260"
+ @syntax_highlighting_string ||= "color:#A4C260"
+ @syntax_highlighting_symbol ||= "color:#6C9CBD"
+ @syntax_highlighting_html ||= "color:#E7BE69"
+ @syntax_highlighting_boolean ||= "color:#6C9CBD"
+ @syntax_highlighting_line_numbers||= :inline
+ @swear_words ||= []
+ @custom_tags ||= []
+ @disable_html ||= true
+ @newline_enabled ||= true
+ @nobbc_enabled ||= true
+ @swear_filter_enabled ||= true
+ end
+ end
+end
View
2 lib/trainbbcode/configure.rb
@@ -1,4 +1,4 @@
-class TBBC
+module TrainBBCode
# Configures TBBC, loads a config file if present and the defaults
def conf(config)
@config = config || {}
View
2 lib/trainbbcode/css.rb
@@ -1,4 +1,4 @@
-class TBBC
+module TrainBBCode
# Returns the css required for coderay
def css(config = nil)
View
22 lib/trainbbcode/parse.rb
@@ -1,24 +1,24 @@
-class TBBC
+module TrainBBCode
# Parses the input and returns it in tbbc form.
#
# TBBC.new.parse("[b]Bold[/b]")
#
# Would return "<strong>Bold</strong>"
- def parse(input)
+ def self.parse(input)
#Remove the < and > which will disable all HTML
- input=input.gsub("<","&lt;").gsub(">","&gt;") unless @config[:disable_html] == false
+ input=input.gsub("<","&lt;").gsub(">","&gt;") unless @config.disable_html == false
#CodeRay
- input=coderay(input) unless @config[:syntax_highlighting] == false
+ input=Coderay.parse(input) unless @config.syntax_highlighting == false
#Convert new lines to <br />'s
- input=input.gsub(/\n/,'<br />') unless @config[:newline_enabled] == false
+ input=input.gsub(/\n/,'<br />') unless @config.newline_enabled == false
#[nobbc] tags
- input=nobbc input unless @config[:nobbc_enabled] == false
+ input=nobbc input unless @config.nobbc_enabled == false
#Swear Filtering
- input=swear_filter(input) unless @config[:swear_filter_enabled] == false
+ input=SwearFilter.parse(input) unless @config.swear_filter_enabled == false
#Loading Custom Tags
#begin
- if @config[:custom_tags] then
- @config[:custom_tags].each do |tag|
+ if @config.custom_tags then
+ @config.custom_tags.each do |tag|
input=runtag(input,tag)
end
end
@@ -26,8 +26,8 @@ def parse(input)
#input+="<br />Custom Tags failed to run"
#end
#Loading Default Tags and applying them
- if @config[:allow_defaults] then
- TBBC::Tags.each do |tag|
+ if @config.allow_defaults then
+ TrainBBCode::Tags.each do |tag|
input=runtag(input,tag)
end
end
View
13 lib/trainbbcode/string.rb
@@ -7,8 +7,15 @@ class String
#
# Will return "[b]Bold [i]and italic[/i][/b]"
def tbbc(conf = nil)
- bbc=TBBC.new
- bbc.conf(conf) if conf
- bbc.parse(self)
+ if !(TrainBBCode.configured?) || conf != nil
+ TrainBBCode.configure do |c|
+ if conf
+ conf.each do |k, v|
+ c.set_from_sym(k,v)
+ end
+ end
+ end
+ end
+ TrainBBCode.parse(self)
end
end
View
32 lib/trainbbcode/swear_filter.rb
@@ -1,19 +1,17 @@
-class TBBC
-
- SwearChracters = %w[! $ % ^ * ~]
-
- private
+module TrainBBCode
+ module SwearFilter
+ SwearChracters = %w[! $ % ^ * ~]
+ def self.parse(input)
+ TrainBBCode.config.swear_words.each do |swear|
+ input=input.gsub(/#{swear}/,swear_string(swear))
+ end
+ input
+ end
- def swear_filter(input)
- @config[:swear_words].each do |swear|
- input=input.gsub(/#{swear}/,swear_string(swear))
- end
- input
- end
-
- def swear_string(s)
- out=""
- s.split("").map { |split| out+= SwearChracters[rand(SwearChracters.count)] }
- out
- end
+ def self.swear_string(s)
+ out=""
+ s.split("").map { |split| out+= SwearChracters[rand(SwearChracters.count)] }
+ out
+ end
+ end
end
View
16 lib/trainbbcode/tags.rb
@@ -1,4 +1,4 @@
-class TBBC
+module TrainBBCode
Tags = [[/\[b\](.*?)\[\/b\]/,'<strong>\1</strong>',:strong_enabled,"[b]BOLD[/b]","<strong>BOLD</strong>"],
[/\[i\](.*?)\[\/i\]/,'<i>\1</i>',:italic_enabled,"[i]italics[/i]","<i>italics</i>"],
[/\[u\](.*?)\[\/u\]/,'<u>\1</u>',:underline_enabled,"[u]underline[/u]","<u>underline</u>"],
@@ -29,9 +29,9 @@ class TBBC
private
# Runs the given tag array on the given string.
- def runtag(s,tag)
+ def self.runtag(s,tag)
check = tag[2]
- check = @config[tag[2]] if is_symbol? tag[2]
+ check = @config.check_enabled(tag[2]) if is_symbol? tag[2]
if tag[1] =~ /^Callback:/
s = run_callback(s, tag[0], tag[1])
else
@@ -41,20 +41,20 @@ def runtag(s,tag)
s
end
- def is_symbol?(v)
+ def self.is_symbol?(v)
return false if (v == true or v == false)
v == v.to_sym
end
- def replace_config_values(s)
+ def self.replace_config_values(s)
if s.scan(/@config\[:(.*?)\]/) != [] then
- return s.gsub(/@config\[:(.*?)\]/,@config[$1.to_sym])
+ return s.gsub(/@config\[:(.*?)\]/,@config.from_sym($1.to_sym))
else
return s
end
end
- def run_callback(string, regex, callback)
+ def self.run_callback(string, regex, callback)
code = callback.gsub(/^Callback: /, '')
output = string
string.scan(regex).each do |arguments|
@@ -65,7 +65,7 @@ def run_callback(string, regex, callback)
output
end
- def build_pass_string(args)
+ def self.build_pass_string(args)
output = "("
args.map { |arg| output = "#{output}\"#{arg}\", " }
output.gsub(/, $/,')')
View
2 lib/trainbbcode/version.rb
@@ -1,3 +1,3 @@
-class TBBC
+module TrainBBCode
VERSION = "1.2.0"
end
View
4 spec/trainbbcode_spec.rb
@@ -4,8 +4,8 @@ def upcaser(input)
input.upcase
end
-describe TBBC, "#parse" do
- TBBC::Tags.each do |tag|
+describe TrainBBCode, "#parse" do
+ TrainBBCode::Tags.each do |tag|
it "Should return #{tag[4]} for #{tag[3]}" do
tag[3].tbbc.should == tag[4]
end
View
2 trainbbcode.gemspec
@@ -4,7 +4,7 @@ require "trainbbcode/version"
Gem::Specification.new do |s|
s.name = "trainbbcode"
- s.version = TBBC::VERSION
+ s.version = TrainBBCode::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Adam \"Arcath\" Laycock"]
s.email = ["gems@arcath.net"]

0 comments on commit 71dad38

Please sign in to comment.
Something went wrong with that request. Please try again.