lib/trainbbcode.rb in trainbbcode-0.2.1 vs lib/trainbbcode.rb in trainbbcode-0.2.2
- old
+ new
@@ -1,31 +1,49 @@
class TBBC
def initialize
self.conf(:configed_by => "system")
end
def conf(config)
+ require 'rubygems'
+ begin
+ require 'uv'
+ #UV Settings
+ config[:syntax_output] ||= "html"
+ config[:syntax_line_numbers] ||= true
+ config[:syntax_theme] ||= "twilight"
+ rescue LoadError
+ config[:syntax_highlighting]=false
+ end
config[:configed_by] ||= "user"
config[:image_alt] ||= "Posted Image"
config[:url_target] ||= "_BLANK"
config[:allow_defaults] ||= true
#Instanize the config variable
@config=config
end
def parse(s)
- #First off remove the < and > which will disable all HTML
- s=s.gsub("<","<").gsub(">",">")
+ #Run the UV Parser (if enabled) to sort out any code
+ unless @config[:syntax_highlighting] == false
+ s=uv s
+ end
+ #Remove the < and > which will disable all HTML
+ s=s.gsub("<","<").gsub(">",">") unless @config[:disable_html] == false
#Convert new lines to <br />'s
- s=s.gsub(/\n/,'<br />')
+ s=s.gsub(/\n/,'<br />') unless @config[:newline_enabled] == false
#[nobbc] tags
unless @config[:nobbc_enabled] == false
s=nobbc s
end
#Loading Custom Tags
- if @config[:custom_tags] then
- @config[:custom_tags].each do |tag|
- s=s.gsub(tag[0],tag[1]) unless tag[2] == false
+ begin
+ if @config[:custom_tags] then
+ @config[:custom_tags].each do |tag|
+ s=s.gsub(tag[0],tag[1]) unless tag[2] == false
+ end
end
+ rescue
+ s+="<br />Custom Tags failed to run"
end
#Loading Default Tags and applying them
if @config[:allow_defaults] then
tags=load_default_tags
tags.each do |tag|
@@ -51,19 +69,41 @@
#Un-Ordered List
[/\[ul\](.*?)\[\/ul\]/,'<ul>\1</ul>',@config[:list_enabled]],
#Ordered List
[/\[ol\](.*?)\[\/ol\]/,'<ol>\1</ol>',@config[:list_enabled]],
#List Item
- [/\[li\](.*?)\[\/li\]/,'<li>\1</li>',@config[:list_enabled]]
+ [/\[li\](.*?)\[\/li\]/,'<li>\1</li>',@config[:list_enabled]],
+ #Quote
+ [/\[quote\](.*?)\[\/quote\]/,'<blockquote>\1</blockquote>',@config[:quote_enabled]],
+ [/\[quote=(.*?)\](.*?)\[\/quote\]/,'<blockquote><i>Posted by <b>\1</b></i><br />\2</blockquote>',@config[:quote_enabled]],
+ #Color
+ [/\[color=(.*?)\](.*?)\[\/color\]/,'<span style="color:\1;">\2</span>',@config[:color_enabled]]
]
end
def nobbc(s)
find=s.scan(/\[nobbc\](.*?)\[\/nobbc\]/)
find.each do |f|
replace=f[0].gsub("[","[").gsub("]","]")
s=s.gsub(f[0],replace)
end
s=s.gsub("[nobbc]","").gsub("[/nobbc]","")
+ return s
+ end
+ def css
+ output="<style type=\"text/css\">
+ blockquote{
+
+ }
+ </style>"
+ output+=Uv.themes.map{|theme| %Q(<link rel="stylesheet" type="text/css" href="css/#{theme}.css" />\n)}
+ return output
+ end
+ def uv(s)
+ find=s.scan(/\[code lang=(.*?)\](.*?)\[\/code\]/)
+ finde.each do |f|
+ r=Uv.parse(f[1], @config[:syntax_output], f[0], @config[:syntax_line_numbers], @config[:syntax_theme])
+ end
+ s=s.gsub(/\[code lang=(.*?)\]/,'').gsub("[/code]",'')
return s
end
end
#Add .tbbc to Strings