ubbparser 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ubbparser.rb +39 -19
- metadata +2 -2
data/lib/ubbparser.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
require "strscan"
|
2
2
|
require "securerandom"
|
3
|
+
require "cgi"
|
3
4
|
|
4
5
|
# The UBBParser module converts UBB code into HTML. The parser is flexibel
|
5
6
|
# and adding new ubb codes is as easy as writing methods.
|
6
|
-
# Author:: Taco Jan Osinga, Osinga Software
|
7
7
|
#
|
8
8
|
# You can create your own ubbcodes by opening the module UBBParser and adding a method called render_xxx,
|
9
9
|
# where xxx is your ubb code of choice. The method should have the following arguments:
|
10
|
-
# (inner_text, attributes = {}, parse_options = {})
|
11
|
-
# inner_text
|
12
|
-
# attributes
|
13
|
-
# parse_options
|
10
|
+
# def render_xxx(inner_text, attributes = {}, parse_options = {})
|
11
|
+
# [inner_text] Contains the text that's found between the opening and the closing tag.
|
12
|
+
# [attributes] A hash containing all the key-value attributes that are given.
|
13
|
+
# [parse_options] A hash containing the same parse_options that are used when UBBParser.parse() is called.
|
14
14
|
#
|
15
|
-
#
|
15
|
+
# ===Example
|
16
16
|
# The following example adds the ubb code \[sup\]...\[/sup], which wraps the inner_text with <sup> tags
|
17
17
|
#
|
18
18
|
# module UBBParser
|
@@ -22,7 +22,7 @@ require "securerandom"
|
|
22
22
|
# end
|
23
23
|
#
|
24
24
|
# When defining new ubb codes with a method and the name contains a dash, replace the dash by an underscore.
|
25
|
-
# I.e. the ubb code for img-left uses the method render_img_left
|
25
|
+
# I.e. the ubb code for img-left uses the method render_img_left.
|
26
26
|
|
27
27
|
module UBBParser
|
28
28
|
|
@@ -39,8 +39,10 @@ module UBBParser
|
|
39
39
|
end
|
40
40
|
|
41
41
|
# Converts a hash into a string with key-values. You can use one of the following options:
|
42
|
-
#
|
43
|
-
#
|
42
|
+
# [:allowed_keys] An array of keys that are only allowed
|
43
|
+
# [:denied_keys] An array of keys that are denied
|
44
|
+
# ===Example:
|
45
|
+
# UBBParser.hash_to_attrib_str({}, {:allowed_keys => [:class, :src, :width]})
|
44
46
|
def self.hash_to_attrib_str(hash, options = {})
|
45
47
|
hash.delete_if { | k, v | !options[:allowed_keys].include?(k) } if options[:allowed_keys].is_a?(Array)
|
46
48
|
hash.delete_if { | k, v | options[:denied_keys].include?(k) } if options[:denied_keys].is_a?(Array)
|
@@ -48,10 +50,11 @@ module UBBParser
|
|
48
50
|
end
|
49
51
|
|
50
52
|
# Parses the given text with ubb code into html. Use parse_options to specify a hash of options:
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
53
|
+
# [:convert_newlines] A boolean whether newlines should be convert into <br /> tags (default: true).
|
54
|
+
# [:protect_email] A boolean whether email addresses should be protected from spoofing using embedded JavaScript.
|
55
|
+
# [:class_xxx] A string with css class(es) that is embedded in the html for the tag xxx. Not all tags supports this.
|
56
|
+
# Replace a dash in a tag with underscore (i.e. the class for img-left is defined in :class_img_left).
|
57
|
+
# ===Example:
|
55
58
|
# {:class_code: "prettify linenums"} => <pre class='prettify linenums'>...</pre>
|
56
59
|
#
|
57
60
|
# When developing your own tags, you can also define your own parse_options.
|
@@ -60,7 +63,7 @@ module UBBParser
|
|
60
63
|
scnr = StringScanner.new(text)
|
61
64
|
parse_options.each { | k, v | v.to_s.gsub(/-/, "_").gsub(/[^\w]+/, "") if (k.to_s.start_with?("class_")); v }
|
62
65
|
while (!scnr.eos?)
|
63
|
-
untagged_text = scnr.scan(/[^\[]*/)
|
66
|
+
untagged_text = CGI.escapeHTML(scnr.scan(/[^\[]*/))
|
64
67
|
|
65
68
|
# convert newlines to breaks
|
66
69
|
untagged_text.gsub!(/\n/, "<br />") if (!parse_options.include?(:convert_newlines) || parse_options[:convert_newlines])
|
@@ -187,7 +190,7 @@ module UBBParser
|
|
187
190
|
end
|
188
191
|
|
189
192
|
# Renders csv-data into a html table. You can use the following attributes:
|
190
|
-
# has_header
|
193
|
+
# [:has_header] The first row should be rendered as header cells (using th).
|
191
194
|
# :category: Render methods
|
192
195
|
def self.render_csv(inner_text, attributes = {}, parse_options = {})
|
193
196
|
head_cells = body_cells = ""
|
@@ -366,13 +369,30 @@ module UBBParser
|
|
366
369
|
end
|
367
370
|
|
368
371
|
# Converts the [table] to a <table>. Always use this in combination with [tr] and [td] or [th]. Use the :class_table parse option to define html classes.
|
369
|
-
# [style color: red; border: 1px solid green]...[/style]
|
370
372
|
# :category: Render methods
|
371
373
|
def self.render_table(inner_text, attributes = {}, parse_options = {})
|
372
|
-
|
373
|
-
"<div style='#{styles}' class='#{parse_options[:class_table].to_s.strip}'>#{self.parse(inner_text, parse_options)}</div>"
|
374
|
+
"<table class='#{parse_options[:class_table].to_s.strip}'>#{self.parse(inner_text.gsub(/(^\n+)|(\n+$)/, ""), parse_options)}</table>"
|
374
375
|
end
|
375
|
-
|
376
|
+
|
377
|
+
# Converts the [td] to a <td>. Always use this in combination with [table] and [tr].
|
378
|
+
# :category: Render methods
|
379
|
+
def self.render_td(inner_text, attributes = {}, parse_options = {})
|
380
|
+
"<td>#{self.parse(inner_text, parse_options)}</td>"
|
381
|
+
end
|
382
|
+
|
383
|
+
# Converts the [th] to a <th>. Always use this in combination with [table] and [tr].
|
384
|
+
# :category: Render methods
|
385
|
+
def self.render_th(inner_text, attributes = {}, parse_options = {})
|
386
|
+
|
387
|
+
"<th>#{self.parse(inner_text, parse_options)}</th>"
|
388
|
+
end
|
389
|
+
|
390
|
+
# Converts the [tr] to a <tr>. Always use this in combination with [table] and [td] or [th].
|
391
|
+
# :category: Render methods
|
392
|
+
def self.render_tr(inner_text, attributes = {}, parse_options = {})
|
393
|
+
"<tr>#{self.parse(inner_text.gsub(/(^\n+)|(\n+$)/, ""), parse_options)}</tr>"
|
394
|
+
end
|
395
|
+
|
376
396
|
# Renders the inner_text underline. Use this with caution, since underline text is associated with hyperlinks.
|
377
397
|
# :category: Render methods
|
378
398
|
def self.render_u(inner_text, attributes = {}, parse_options = {})
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ubbparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,7 +18,7 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/ubbparser.rb
|
21
|
-
homepage:
|
21
|
+
homepage: https://github.com/tjosinga/UBBParser
|
22
22
|
licenses: []
|
23
23
|
post_install_message:
|
24
24
|
rdoc_options: []
|