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/version.rb,
lib/trainbbcode/configure.rb,
lib/trainbbcode/swear_filter.rb

Constant Summary collapse

Tags =
[[/\[b\](.*?)\[\/b\]/,'<strong>\1</strong>',:strong_enabled],
[/\[i\](.*?)\[\/i\]/,'<i>\1</i>',:italic_enabled],
[/\[u\](.*?)\[\/u\]/,'<u>\1</u>',:underline_enabled],
[/\[url\](.*?)\[\/url\]/,'<a href="\1" target="@config[:url_target]">\1</a>',:url_enabled],
[/\[url=(.*?)\](.*?)\[\/url\]/,'<a href="\1" target="@config[:url_target]">\2</a>',:url_enabled],
[/\[url=(.*?) target=(.*?)\](.*?)\[\/url\]/,'<a href="\1" target="\2">\3</a>',:url_enabled],
[/\[img\](.*?)\[\/img\]/,'<img src="\1" alt="@config[:image_alt]" />',:image_enabled],
[/\[img alt=(.*?)\](.*?)\[\/img\]/,'<img src="\2" alt="\1" />',:image_enabled],
[/\[ul\](.*?)\[\/ul\]/,'<ul>\1</ul>',:list_enabled],
[/\[ol\](.*?)\[\/ol\]/,'<ol>\1</ol>',:list_enabled],
[/\[li\](.*?)\[\/li\]/,'<li>\1</li>',:list_enabled],
[/\[quote\](.*?)\[\/quote\]/,'<blockquote>\1</blockquote>',:quote_enabled],
[/\[quote=(.*?)\](.*?)\[\/quote\]/,'<blockquote><i>Posted by <b>\1</b></i><br />\2</blockquote>',:quote_enabled],
[/\[color=(.*?)\](.*?)\[\/color\]/,'<span style="color:\1;">\2</span>',:color_enabled],
[/\[center\](.*?)\[\/center\]/,'<div style="text-align:center">\1</div>',:alignment_enabled],
[/\[left\](.*?)\[\/left\]/,'<div style="text-align:left">\1</div>',:alignment_enabled],
[/\[right\](.*?)\[\/right\]/,'<div style="text-align:right">\1</div>',:alignment_enabled],
[/\[acronym=(.*?)\](.*?)\[\/acronym\]/,'<acronym title="\1">\2</acronym>',:acronym_enabled],
[/\[table\](.*?)\[\/table\]/,'<table width="@config[:table_width]">\1</table>',:table_enabled],
[/\[tr\](.*?)\[\/tr\]/,'<tr>\1</tr>',:table_enabled],
[/\[td\](.*?)\[\/td\]/,'<td>\1</td>',:table_enabled],
[/\[th\](.*?)\[\/th\]/,'<th>\1</th>',:table_enabled]]
VERSION =
"1.1.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”



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

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

Instance Method Details

#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
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
41
42
43
44
45
46
# File 'lib/trainbbcode/css.rb', line 4

def css(config = nil)
  conf config
  output="   <style type=\"text/css\">
    .CodeRay {
      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;
    }
    .CodeRay pre {
      margin: 0px;
      padding: 0px;
    }

    .CodeRay .an { #{@config[:syntax_highlighting_html]} }    /* html attribute */
    .CodeRay .c  { #{@config[:syntax_highlighting_comment]} } /* comment */
    .CodeRay .ch { #{@config[:syntax_highlighting_escaped]} } /* escaped character */
    .CodeRay .cl { #{@config[:syntax_highlighting_class]} }   /* class */
    .CodeRay .co { #{@config[:syntax_highlighting_constant]} }  /* constant */
    .CodeRay .fl { #{@config[:syntax_highlighting_float]} }   /* float */
    .CodeRay .fu { #{@config[:syntax_highlighting_function]} }  /* function */
    .CodeRay .gv { #{@config[:syntax_highlighting_global]} }  /* global variable */
    .CodeRay .i  { #{@config[:syntax_highlighting_integer]} } /* integer */
    .CodeRay .il { #{@config[:syntax_highlighting_inline]} }  /* inline code */
    .CodeRay .iv { #{@config[:syntax_highlighting_instance]} }  /* instance variable */
    .CodeRay .pp { #{@config[:syntax_highlighting_doctype]} } /* doctype */
    .CodeRay .r  { #{@config[:syntax_highlighting_keyword]} } /* keyword */
    .CodeRay .rx { #{@config[:syntax_highlighting_regex]} }   /* regex */
    .CodeRay .s  { #{@config[:syntax_highlighting_string]} }  /* string */
    .CodeRay .sy { #{@config[:syntax_highlighting_symbol]} }  /* symbol */
    .CodeRay .ta { #{@config[:syntax_highlighting_html]} }    /* html tag */
    .CodeRay .pc { #{@config[:syntax_highlighting_boolean]} } /* boolean */
  </style>" 
  if needs_html_safe? then
    return output.html_safe
  else
    return output
  end
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