Class: TBBC

Inherits:
Object
  • Object
show all
Defined in:
lib/trainbbcode.rb,
lib/trainbbcode/css.rb,
lib/trainbbcode/tags.rb,
lib/trainbbcode/parse.rb,
lib/trainbbcode/coderay.rb,
lib/trainbbcode/version.rb,
lib/trainbbcode/configure.rb,
lib/trainbbcode/swear_filter.rb

Constant Summary collapse

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>"],
[/\[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>"],
[/\[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>"],
[/\[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>"],
  [/\[img\](.*?)\[\/img\]/,'<img src="\1" alt="@config[:image_alt]" />',:image_enabled,"[img]test.jpg[/img]","<img src=\"test.jpg\" alt=\"Posted Image\" />"],
[/\[img alt=(.*?)\](.*?)\[\/img\]/,'<img src="\2" alt="\1" />',:image_enabled,"[img alt=Posted Image]test.jpg[/img]","<img src=\"test.jpg\" alt=\"Posted Image\" />"],
[/\[ul\](.*?)\[\/ul\]/,'<ul>\1</ul>',:list_enabled,"[ul]list[/ul]","<ul>list</ul>"],
[/\[ol\](.*?)\[\/ol\]/,'<ol>\1</ol>',:list_enabled,"[ol]list[/ol]","<ol>list</ol>"],
[/\[li\](.*?)\[\/li\]/,'<li>\1</li>',:list_enabled,"[li]list[/li]","<li>list</li>"],
[/\[quote\](.*?)\[\/quote\]/,'<blockquote>\1</blockquote>',:quote_enabled,"[quote]quote[/quote]","<blockquote>quote</blockquote>"],
[/\[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>"],
[/\[color=(.*?)\](.*?)\[\/color\]/,'<span style="color:\1;">\2</span>',:color_enabled,"[color=red]red text[/color]","<span style=\"color:red;\">red text</span>"],
[/\[center\](.*?)\[\/center\]/,'<div style="text-align:center">\1</div>',:alignment_enabled,"[center]centered text[/center]","<div style=\"text-align:center\">centered text</div>"],
[/\[left\](.*?)\[\/left\]/,'<div style="text-align:left">\1</div>',:alignment_enabled,"[left]left text[/left]","<div style=\"text-align:left\">left text</div>"],
[/\[right\](.*?)\[\/right\]/,'<div style="text-align:right">\1</div>',:alignment_enabled,"[right]right text[/right]","<div style=\"text-align:right\">right text</div>"],
[/\[acronym=(.*?)\](.*?)\[\/acronym\]/,'<acronym title="\1">\2</acronym>',:acronym_enabled,"[acronym=this]that[/acronym]","<acronym title=\"this\">that</acronym>"],
[/\[table\](.*?)\[\/table\]/,'<table width="@config[:table_width]">\1</table>',:table_enabled,"[table]...[/table]","<table width=\"100%\">...</table>"],
[/\[tr\](.*?)\[\/tr\]/,'<tr>\1</tr>',:table_enabled,"[tr]...[/tr]","<tr>...</tr>"],
[/\[td\](.*?)\[\/td\]/,'<td>\1</td>',:table_enabled,"[td]...[/td]","<td>...</td>"],
[/\[th\](.*?)\[\/th\]/,'<th>\1</th>',:table_enabled,"[th]...[/th]","<th>...</th>"],
[/\[h1\](.*?)\[\/h1\]/,'<h1>\1</h1>',:header_enabled,'[h1]heading[/h1]',"<h1>heading</h1>"],
[/\[h2\](.*?)\[\/h2\]/,'<h2>\1</h2>',:header_enabled,'[h2]heading[/h2]',"<h2>heading</h2>"],
[/\[h3\](.*?)\[\/h3\]/,'<h3>\1</h3>',:header_enabled,'[h3]heading[/h3]',"<h3>heading</h3>"],
[/\[h4\](.*?)\[\/h4\]/,'<h4>\1</h4>',:header_enabled,'[h4]heading[/h4]',"<h4>heading</h4>"]]
VERSION =
"1.2.0"
SwearChracters =
%w[! $ % ^ * ~]

Instance Method Summary collapse

Constructor Details

#initializeTBBC

Creates a new TBBC class with everything set to default

Also sets :configed_by to “system”



21
22
23
# File 'lib/trainbbcode.rb', line 21

def initialize
  self.conf(:configed_by => "system")
end

Instance Method Details

#coderay(input) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/trainbbcode/coderay.rb', line 2

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

#coderay_styled(input) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/trainbbcode/coderay.rb', line 14

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]}\"")
end

#conf(config) ⇒ Object

Configures TBBC, loads a config file if present and the defaults



3
4
5
6
7
8
9
# File 'lib/trainbbcode/configure.rb', line 3

def conf(config)
  @config = config || {}
  load_config_from_defaults
  if File.exist?("config/tbbc.rb")
    load_config_from_file
  end
end

#css(config = nil) ⇒ Object

Returns the css required for coderay



4
5
6
7
# File 'lib/trainbbcode/css.rb', line 4

def css(config = nil)
       warn "[DEPRECATION] `css` is no longer used, styles are applied inline."
       ""
end

#parse(input) ⇒ Object

Parses the input and returns it in tbbc form.

TBBC.new.parse("[b]Bold[/b]")

Would return “<strong>Bold</strong>”



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/trainbbcode/parse.rb', line 7

def parse(input)
  #Remove the < and > which will disable all HTML
  input=input.gsub("<","&lt;").gsub(">","&gt;") unless @config[:disable_html] == false
  #CodeRay
  input=coderay(input) unless @config[:syntax_highlighting] == false
  #Convert new lines to <br />'s
  input=input.gsub(/\n/,'<br />') unless @config[:newline_enabled] == false
  #[nobbc] tags
  input=nobbc input unless @config[:nobbc_enabled] == false
  #Swear Filtering
  input=swear_filter(input) unless @config[:swear_filter_enabled] == false
  #Loading Custom Tags
  #begin
    if @config[:custom_tags] then
      @config[:custom_tags].each do |tag|
        input=runtag(input,tag)
      end
    end
  #rescue
    #input+="<br />Custom Tags failed to run"
  #end
  #Loading Default Tags and applying them
  if @config[:allow_defaults] then
    TBBC::Tags.each do |tag|
      input=runtag(input,tag)
    end 
  end
  input=correctbrs input
  if needs_html_safe? then
    return input.html_safe
  else
    return input
  end
end