th-bbcode 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +37 -3
- data/lib/bbcode/html_handler.rb +1 -3
- data/lib/bbcode/version.rb +1 -1
- data/lib/bbcode.rb +2 -0
- data/spec/spec_helper.rb +0 -2
- metadata +10 -10
data/README.md
CHANGED
@@ -17,8 +17,8 @@ designed to convert bbcode elements to HTML more easily.
|
|
17
17
|
|
18
18
|
Installation:
|
19
19
|
-------------
|
20
|
-
Add the gem to the gemfile of your project
|
21
|
-
|
20
|
+
Add the gem to the gemfile of your project:
|
21
|
+
`gem "th-bbcode", "~> 0.4.0"`
|
22
22
|
|
23
23
|
Usage:
|
24
24
|
------
|
@@ -26,6 +26,8 @@ Create and register a handler. In this example, I'm creating a HtmlHandler and
|
|
26
26
|
I'm going to register it as `:html`.
|
27
27
|
|
28
28
|
```ruby
|
29
|
+
require "bbcode"
|
30
|
+
|
29
31
|
Bbcode::Base.register_handler :html, Bbcode::HtmlHandler.new(
|
30
32
|
:b => :strong,
|
31
33
|
:i => :em,
|
@@ -61,12 +63,44 @@ Features:
|
|
61
63
|
italic[/]`, which might result to `<b>bold<i>and italic</i></b><i>only
|
62
64
|
italic</i>`.
|
63
65
|
|
66
|
+
Using WillScanString:
|
67
|
+
---------------------
|
68
|
+
You might want to convert URLs in the message to be converted to a hyperlink,
|
69
|
+
or you might want smileys in your bbcode message. This can be done by
|
70
|
+
defining a `:"#text"`-handler in your Handler or HtmlHandler.
|
71
|
+
|
72
|
+
I personally used WillScanString's StringScanner class to achieve this:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
# Requires the will_scan_string gem
|
76
|
+
require "will_scan_string"
|
77
|
+
|
78
|
+
string_scanner = WillScanString::StringScanner.new
|
79
|
+
string_scanner.register_replacement "<", "<"
|
80
|
+
string_scanner.register_replacement ">", ">"
|
81
|
+
string_scanner.register_replacement "&", "&"
|
82
|
+
string_scanner.register_replacement "\"", """
|
83
|
+
string_scanner.register_replacement /(?:\r\n|\r|\n)/, "<br>"
|
84
|
+
string_scanner.register_replacement /[^\s@]+@[^\s@]/, ->(email){ %(<a href="mailto:#{CGI.escapeHTML(email)}">#{CGI.escapeHTML(email)}</a>) }
|
85
|
+
|
86
|
+
# +handler+ is your Bbcode::HtmlHandler or Bbcode::Handler instance
|
87
|
+
handler.register_element_handler :"#text", ->(text){ string_scanner.replace(text) }
|
88
|
+
```
|
89
|
+
|
90
|
+
The above example converts newlines to `<br>`-tags, escapes HTML entities
|
91
|
+
and converts e-mail addresses to clickable mailto hyperlinks.
|
92
|
+
|
93
|
+
Note: By overwriting the `:"#text"`-handler in the HtmlHandler, html entities
|
94
|
+
are no longer replaced automatically: You need to escape them in your handler
|
95
|
+
callback yourself. Failing to do so might expose your website to XSS
|
96
|
+
vulnerabilities.
|
97
|
+
|
64
98
|
Todo:
|
65
99
|
-----
|
66
100
|
* An easier way to handle text around bbcode tags to, for example, add smileys
|
67
101
|
and wrap hyperlinks to URLs. Currently, the only way to achieve this is by
|
68
102
|
adding a `:"#text"`-handler to your handler and adding the functionality
|
69
|
-
yourself.
|
103
|
+
yourself. (note: See the above note regarding WillScanString)
|
70
104
|
* An easier way to include the content, source or content-source in the
|
71
105
|
`HtmlHandler`-class.
|
72
106
|
* Review handleability of element interrupts.
|
data/lib/bbcode/html_handler.rb
CHANGED
@@ -9,9 +9,7 @@ module Bbcode
|
|
9
9
|
|
10
10
|
def register_element_handler( name, handler )
|
11
11
|
unless handler.is_a?(Proc)
|
12
|
-
|
13
|
-
target_tagname = args.shift
|
14
|
-
attributes = args.first
|
12
|
+
target_tagname, attributes = handler.is_a?(Array) ? handler : [handler, {}]
|
15
13
|
handler = ->(element){
|
16
14
|
content_tag(target_tagname, element.content, !attributes ? {} : Hash[attributes.map{ |k, v|
|
17
15
|
[k, v.gsub(/%{[^}]+}/) { |m| CGI.escapeHTML element[m[3] == ":" ? m[3...-1].to_sym : m[2...-1].to_i] }]
|
data/lib/bbcode/version.rb
CHANGED
data/lib/bbcode.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: th-bbcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-20 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70135440360960 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2.6'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70135440360960
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activesupport
|
27
|
-
requirement: &
|
27
|
+
requirement: &70135440358260 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 3.0.9
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70135440358260
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: actionpack
|
38
|
-
requirement: &
|
38
|
+
requirement: &70135440352380 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 3.0.9
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70135440352380
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: i18n
|
49
|
-
requirement: &
|
49
|
+
requirement: &70135440349240 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 0.5.0
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70135440349240
|
58
58
|
description: Gem for parsing bbcode-formatted strings to HTML or any other formatting
|
59
59
|
you like (or don't like).
|
60
60
|
email:
|