Class: BBCode

Inherits:
Object show all
Defined in:
lib/carat/bbcode.rb

Overview

BBCode

This module helps easening the separation of core and frontend with the core (or submodules) being still able to say, what colors shall be used in it’s responses. This is achieved by encoding formatting information using the BBCode tokens. This enables you to “pipe” layout information such as colors, style, fonttype, size and alignment through the core to the frontend.

Additionally it converts markups/codes between ANSI, HTML and BBCode almost freely ;)

Example

# Converting a string to ANSI and XHTML

require 'raspberry/new/bbcode'

str = "this is [COLOR=red]red[/COLOR], this is [B]bold[/B]"
print( BBCode.bbcode_to_ansi(str) )
print( BBCode.bbcode_to_html(str) )

Author

  • Thomas-Ivo Heinen

Copyright (c) 2002 Thomas-Ivo Heinen

History

02-07-16  Bugfix BBCODE=>ANSI, bbcode_to_html, ansi_to_html,
          html_to_bbcode, html_to_ansi

02-11-11  Comment fixes, more case insensitive matching,
          convert deprecated HTML styles (<b>, <i>), more
          space-tolerant matching, img-tags in html_to_bbcode
          works now, some error-preventing stripping and downcasing.

Stamp

$Id: bbcode.rb, v2.2 2002/07/16 transami Exp $

Constant Summary collapse

VERSION =
2.2
ANSINAME2CODE =

ANSIname => ANSIcode LUT

{ "reset"     => "\e[0m",    "bold"       => "\e[1m",
  "underline" => "\e[4m",    "blink"      => "\e[5m",
  "reverse"   => "\e[7m",    "invisible"  => "\e[8m",
  "black"     => "\e[0;30m", "darkgrey"   => "\e[1;30m",
  "red"       => "\e[0;31m", "lightred"   => "\e[1;31m",
  "green"     => "\e[0;32m", "lightgreen" => "\e[1;32m",
  "brown"     => "\e[0;33m", "yellow"     => "\e[1;33m",
  "blue"      => "\e[0;34m", "lightblue"  => "\e[1;34m",
  "purple"    => "\e[0;35m", "magenta"    => "\e[1;35m",
  "cyan"      => "\e[1;36m", "lightcyan"  => "\e[1;36m",
  "grey"      => "\e[0;37m", "white"      => "\e[1;37m",
  "bgblack"   => "\e[40m",   "bgred"      => "\e[41m",
  "bggreen"   => "\e[42m",   "bgyellow"   => "\e[43m",
  "bgblue"    => "\e[44m",   "bgmagenta"  => "\e[45m",
  "bgcyan"    => "\e[46m",   "bgwhite"    => "\e[47m"
}
BBCOLOR2ANSI =

BBColor => ANSIname LUT

{ "skyblue"   => "blue",     "royalblue" => "blue",
  "blue"      => "blue",     "darkblue"  => "blue",
  "orange"    => "red",      "orangered" => "red",
  "crimson"   => "red",      "red"       => "lightred",
  "firebrick" => "red",      "darkred"   => "red",
  "green"     => "green",    "limegreen" => "green",
  "seagreen"  => "green",    "darkgreen" => "green",
  "deeppink"  => "magenta",  "tomato"    => "red",
  "coral"     => "cyan",     "purple"    => "purple", 
  "indigo"    => "blue",     "burlywood" => "red",
  "sandybrown"=> "red",      "sierra"    => "sierra",
  "chocolate" => "brown",    "teal"      => "teal",
  "silver"    => "white",     
  "black"     => "black",    "yellow"    => "yellow",
  "magenta"   => "magenta",  "cyan"      => "cyan",
  "white"     => "white" 
}
ANSINAME2BBCODE =

ANSInames => BBCode LUT

{ "bold" => "B", "underline" => "U", "reverse" => "I",

  "red"    => "COLOR=red",      "blue"   => "COLOR=blue",
  "green"  => "COLOR=green",    "cyan"   => "COLOR=cyan",
  "magenta"=> "COLOR=deeppink", "purple" => "COLOR=purple",
  "black"  => "COLOR=black",    "white"  => "COLOR=white",
  "yellow" => "COLOR=yellow",   "brown"  => "COLOR=chocolate"
}
@@width =

Needed for alignments

80

Class Method Summary collapse

Class Method Details

.ansi(colorname) ⇒ Object

Returns the ANSI sequence for given color, if existant



107
108
109
110
# File 'lib/carat/bbcode.rb', line 107

def BBCode.ansi(colorname)
    colorname.strip!
    return ANSINAME2CODE[ colorname.downcase ]
end

.ansi_to_bbcode(string) ⇒ Object

Returns the string with all ansi escape sequences converted to BBCodes



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/carat/bbcode.rb', line 120

def BBCode.ansi_to_bbcode(string)
    return "" if string.nil? || string.to_s.strip.empty?
    result = ""
    tagstack = []

    ## Iterate over input lines
    string.split("\n").each do |line|
        ansi = line.scan(/\e\[[0-9;]+m/)
        continue if ansi.nil? || ansi.empty?

        ## Iterate over found ansi sequences
        ansi.each do |seq|  
            ansiname = ANSINAME2CODE.invert["#{seq}"]

            ## Pop last tag and form closing tag
            if ansiname == "reset"
                lasttag = tagstack.pop
                bbname = "/" + String.new( lasttag.split("=")[0] )

            ## Get corresponding BBCode tag + Push to stack
            else
                bbname   = ANSINAME2BBCODE[ansiname]
                tagstack.push(bbname) 
            end

            ## Replace ansi sequence by BBCode tag
            replace = sprintf("[%s]", bbname)
            line.sub!("#{Regexp.quote(seq)}", replace)
        end

        ## Append converted line
        result << sprintf("%s\n", line)
    end


    ## Some tags are unclosed
    while !tagstack.empty?
        result << sprintf("[/%s]", String.new(tagstack.pop.split("=")[0])  )
    end

    return result
end

.ansi_to_html(string) ⇒ Object

Converts a ANSI string to one with HTML markup. Returns the string with ANSI code sequences converted to XHTML markup.



338
339
340
341
342
343
# File 'lib/carat/bbcode.rb', line 338

def BBCode.ansi_to_html(string)
    bbcoded = BBCode.ansi_to_bbcode(string )
    htmled  = BBCode.bbcode_to_html(bbcoded)

    return htmled
end

.bbcode_to_ansi(string, usecolors = true) ⇒ Object

Converts a BBCode string to one with ANSI sequences. Returns the string with all formatting instructions in BBCodes converted to ANSI code sequences / aligned with spaces to specified width.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/carat/bbcode.rb', line 166

def BBCode.bbcode_to_ansi(string, usecolors = true)
    return "" if string.nil? || string.to_s.strip.empty?
    result = ""

    return BBCode.strip_bbcode(string) if !usecolors

    ## Iterate over lines
    string.split("\n").each do |line|

        ## TODO: stacking? other styles!
        ANSINAME2BBCODE.each do |key,val|
            line.gsub!(/\[#{val}\]/, ANSINAME2CODE[key])
            line.gsub!(/\[\/#{val}\]/, ANSINAME2CODE["reset"])
        end

        ## Fonttypes and sizes not available
        line.gsub!(/\[SIZE=\d\]/, "")
        line.gsub!(/\[\/SIZE\]/, "")
        line.gsub!(/\[FONT=[^\]]*\]/, "")
        line.gsub!(/\[\/FONT\]/, "")

        ## Color-mapping
        colors = line.scan(/\[COLOR=(.*?)\]/i)
        colors = colors.collect{|s|  s[0].to_s} if !colors.nil?
        colors.each do |col|  
            name = BBCOLOR2ANSI[col.downcase]
            name = BBCOLOR2ANSI["white"] if name.nil?
            code = ANSINAME2CODE[name]

            line.gsub!(/\[COLOR=#{col}\]/i, code)
        end
        line.gsub!(/\[\/COLOR\]/, ANSINAME2CODE["reset"])

        ## TODO: Alignment
        ## TODO: IMGs
        ## TODO: EMAILs
        ## TODO: URLs
        ## TODO: QUOTEs
        ## TODO: LISTs

        result << sprintf("%s\n", line)
    end

    return result
end

.bbcode_to_html(string) ⇒ Object

Converts a BBCode string to one with HTML markup. Returns the string with all formatting instructions in BBCodes converted to XHTML markups.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/carat/bbcode.rb', line 273

def BBCode.bbcode_to_html(string)
    return "" if string.nil? || string.to_s.strip.empty?
    result = ""
    quote = 0

    ## Iterate over lines
    string.split("\n").each do |line|
        styles = { "b" => "strong", "i" => "em", "u" => "u" }

        ## preserve B, I, U
        styles.each do |code,html|
            line.gsub!(/\[#{code}\]/i, "<#{html}>")
            line.gsub!(/\[\/#{code}\]/i, "</#{html}>")
        end
        
        ## COLOR => font color=... (TODO: should be numeric!)
        line.gsub!(/\[COLOR=(.*?)\]/i, "<font color=\"\\1\">")
        line.gsub!(/\[\/COLOR\]/i,     "</font>")

        ## SIZE => font size=...
        line.gsub!(/\[SIZE=(.*?)\]/i, "<font size=\"\\1\">")
        line.gsub!(/\[\/SIZE\]/i,     "</font>")

        ## URL   
        line.gsub!(/\[URL\]([^\[]+?)\[\/URL\]/i, "<a href=\"\\1\">\\1</a>")
        line.gsub!(/\[URL=(.*?)\](.+?)\[\/URL\]/i, "<a href=\"\\1\">\\2</a>")

        ## IMG
        line.gsub!(/\[IMG=(.*?)\]/i, "<img src=\"\\1\" />")

        ## ALIGN=center (TODO: right, left)
        line.gsub!(/\[ALIGN=center\]/i, "<center>")
        line.gsub!(/\[ALIGN=right\]/i,  "<center>")
        line.gsub!(/\[ALIGN=left\]/i,   "<center>")
        line.gsub!(/\[\/ALIGN\]/i,      "</center>")

        ## QUOTE
        quote+=1 if line =~ /\[QUOTE\]/i
        quote-=1 if (line =~ /\[\/QUOTE\]/i) && (quote > -1)
        line.gsub!(/\[QUOTE\]/i,   "<pre>\n")
        line.gsub!(/\[\/QUOTE\]/i, "</pre>\n")
        line.gsub!(/^/, "&#62;"*quote) if quote > 0

        ## EMAIL
        line.gsub!(/\[EMAIL\](.*?)\[\/EMAIL\]/i, "<a href=\"mailto:\\1\">\\1</a>")

        ## LIST (TODO: LIST=1, LIST=A)
        line.gsub!(/\[LIST(?:=(.*?))?\]/i, "\n<ul>\n")
        line.gsub!(/\[\/LIST\]/i,          "\n</ul>\n")
        line.gsub!(/\[\*\]/i,              "\n<li />")

        ## FONT => font ??????
        ## ?BLUR?, FADE?

        result << sprintf("%s<br />\n", line)
    end

    return result
end

.html_to_ansi(string) ⇒ Object

Returns the (X)HTML markup code as ANSI sequences



346
347
348
349
350
351
# File 'lib/carat/bbcode.rb', line 346

def BBCode.html_to_ansi(string)
    bbcoded = BBCode.html_to_bbcode(string )
    ansied  = BBCode.bbcode_to_ansi(bbcoded)

    return ansied
end

.html_to_bbcode(string) ⇒ Object

Converts a HTML string into one with BBCode markup (TODO) Returns the (X)HTML markup string as BBCode



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/carat/bbcode.rb', line 215

def BBCode.html_to_bbcode(string)
    return "" if string.nil? || string.to_s.strip.empty?
    result = ""

    ## Iterate over lines
    string.split(/<br *\/?>/i).each do |line|
        styles = { "strong" => "b", "b" => "b",
                   "em"     => "i", "i" => "i",
                   "u"      => "u" }

        ## preserve B, I, U
        styles.each do |html,code|
            line.gsub!(/<#{html}>/i, "[#{code.upcase}]")
            line.gsub!(/<\/#{html}>/i, "[/#{code.upcase}]")
        end
        
        ## TODO: COLORs
        ## TODO: SIZEs
        ## TODO: FONTs

        ## EMAIL
        line.gsub!(/<a +href *= *\"mailto:(.*?)\".*?>.*?<\/a>/i, "[EMAIL]\\1[/EMAIL]")
        
        ## URL
        line.gsub!(/<a +href *= *\"((?:https?|ftp):\/\/.*?)\".*?>(.*?)<\/a>/i, "[URL=\\1]\\2[/URL]")
        
        ## Other refs + closing tags => throw away
        line.gsub!(/<a +href *= *\".*?\".*?>/i, "")
        line.gsub!(/<\/a>/i,            "")

        ## IMG
        line.gsub!(/<img +src *= *\"(.*?)\".*?\/?>/i, "[IMG=\\1]")

        ## CENTER (right/left??)
        line.gsub!(/<center>/i,   "[ALIGN=center]")
        line.gsub!(/<\/center>/i, "[/ALIGN]")

        ## QUOTE
        line.gsub!(/<(?:xmp|pre)>/i,   "[QUOTE]")
        line.gsub!(/<\/(?:xmp|pre)>/i, "[/QUOTE]")

        ## LIST
        line.gsub!(/<ul>/i,      "\n[LIST]\n")
        line.gsub!(/<\/ul>/i,    "\n[/LIST]\n")
        line.gsub!(/<li *\/?> */i, "\n[*] ")

        ## Unkown tags => throw away
        line.gsub!(/<.*? *\/?>/, "")

        result << sprintf("%s<br />\n", line)
    end

    return result.gsub!(/<br *\/?>/i, "\n")
end

.strip_bbcode(string) ⇒ Object

— strip_bbcode( string ) Will strip any BBCode tags from the given string.



114
115
116
117
# File 'lib/carat/bbcode.rb', line 114

def BBCode.strip_bbcode(string)
    string.strip!
    return string.gsub(/\[[A-Za-z0-9\/=]+\]/, "") 
end