rbbcode 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +25 -23
- data/lib/rbbcode/html_maker.rb +3 -14
- data/spec/html_maker_spec.rb +10 -10
- metadata +23 -4
data/README.markdown
CHANGED
@@ -12,13 +12,13 @@ RbbCode validates and cleans input. It supports customizable schemas so you can
|
|
12
12
|
|
13
13
|
Example usage:
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
require 'rubygems'
|
16
|
+
require 'rbbcode'
|
17
|
+
|
18
|
+
bb_code = 'This is [b]bold[/b] text'
|
19
|
+
parser = RbbCode::Parser.new
|
20
|
+
html = parser.parse(bb_code)
|
21
|
+
# => '<p>This is <strong>bold</strong> text</p>'
|
22
22
|
|
23
23
|
Customizing
|
24
24
|
===========
|
@@ -27,26 +27,26 @@ You can customize RbbCode by subclassing HtmlMaker and/or by passing configurati
|
|
27
27
|
|
28
28
|
HtmlMaker can be extended by adding methods like this:
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
class MyHtmlMaker < RbbCode::HtmlMaker
|
31
|
+
def html_from_TAGNAME_tag(node)
|
32
|
+
# ...
|
33
|
+
end
|
34
|
+
end
|
35
35
|
|
36
36
|
...where TAGNAME should be replaced with the name of the tag. The method should accept an RbbCode::TagNode and return HTML as a string. (See tree_maker.rb for the definition of RbbCode::TagNode.) Anytime the parser encounters the specified tag, it will call your method and insert the returned HTML into the output.
|
37
37
|
|
38
38
|
Now you just have to tell the Parser object to use an instance of your custom subclass instead of the default HtmlMaker:
|
39
39
|
|
40
|
-
|
41
|
-
|
40
|
+
my_html_maker = MyHtmlMaker.new
|
41
|
+
parser = RbbCode::Parser.new(:html_maker => my_html_maker)
|
42
42
|
|
43
43
|
RbbCode removes invalid markup by comparing the input against a Schema object. The Schema is much like a DTD in XML. You can set your own rules and change the default ones by calling configuration methods on a Schema instance. Look at Schema#use_defaults in schema.rb for examples.
|
44
44
|
|
45
45
|
Normally, RbbCode instantiates Schema behind the scenes, but if you want to customize it, you'll have to instantiate it yourself and pass the instance to the Parser object:
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
schema = RbbCode::Schema.new
|
48
|
+
schema.tag('quote').may_not_be_nested # Or whatever other configuration methods you want to call
|
49
|
+
parser = RbbCode::Parser.new(:schema => schema)
|
50
50
|
|
51
51
|
Unicode Support
|
52
52
|
===============
|
@@ -64,12 +64,12 @@ http://en.wikipedia.org/wiki/BBCode
|
|
64
64
|
|
65
65
|
From that, I extracted some rules for "common" BBCode syntax. Here are the rules.
|
66
66
|
|
67
|
-
Text gets wrapped in
|
67
|
+
Text gets wrapped in `<p>` tags unless it's marked up as some other block-level element such as a list. A single line break becomes a `<br/>`. Two line breaks mark the end of a paragraph, thus a closing `</p>` and possibly an opening `<p>`.
|
68
68
|
|
69
69
|
Tags must be in one of the following forms:
|
70
70
|
|
71
|
-
|
72
|
-
|
71
|
+
[tagname]Text[/tagname]
|
72
|
+
[tagname=value]Text[/tagname]
|
73
73
|
|
74
74
|
As you can infer from the second example, RbbCode does not support attributes like in HTML and XML. Rather, a tag can have a single "value," which is similar to an anonymous attribute. This is how [url] and [img] tags work, for example.
|
75
75
|
|
@@ -97,6 +97,7 @@ Feature Requests vs Bugs
|
|
97
97
|
========================
|
98
98
|
|
99
99
|
Examples of bugs:
|
100
|
+
|
100
101
|
- Executable JavaScript appears in the output
|
101
102
|
- The output is not a valid XHTML fragment
|
102
103
|
- RbbCode fails to support common BBCode syntax, as exemplified in http://en.wikipedia.org/wiki/BBCode
|
@@ -104,6 +105,7 @@ Examples of bugs:
|
|
104
105
|
- Any of the specs fail
|
105
106
|
|
106
107
|
Example of feature requests:
|
108
|
+
|
107
109
|
- You want support for more tags. RbbCode lets you define your own tags. So the absence of, say, the "color" tag in the default parser is not a bug
|
108
110
|
- You want to support uncommon BBCode syntax, i.e. something you wouldn't see on http://en.wikipedia.org/wiki/BBCode
|
109
111
|
|
@@ -112,9 +114,9 @@ Do not open an issue for a feature request. Just send a message on Github.
|
|
112
114
|
Installation
|
113
115
|
============
|
114
116
|
|
115
|
-
|
117
|
+
gem install rbbcode
|
116
118
|
|
117
119
|
If that doesn't work, it's probably because RbbCode is hosted on Gemcutter, and your computer doesn't know about Gemcutter yet. To fix that:
|
118
120
|
|
119
|
-
|
120
|
-
|
121
|
+
gem install gemcutter
|
122
|
+
gem tumble
|
data/lib/rbbcode/html_maker.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
# TODO: Lists must be surrounded by </p> and <p>
|
2
|
-
|
3
1
|
require 'cgi'
|
2
|
+
require 'sanitize-url'
|
4
3
|
|
5
4
|
module RbbCode
|
6
5
|
DEFAULT_TAG_MAPPINGS = {
|
@@ -16,6 +15,8 @@ module RbbCode
|
|
16
15
|
}
|
17
16
|
|
18
17
|
class HtmlMaker
|
18
|
+
include SanitizeUrl
|
19
|
+
|
19
20
|
def make_html(node)
|
20
21
|
output = ''
|
21
22
|
case node.class.to_s
|
@@ -80,17 +81,5 @@ module RbbCode
|
|
80
81
|
end
|
81
82
|
DEFAULT_TAG_MAPPINGS[tag_name]
|
82
83
|
end
|
83
|
-
|
84
|
-
def sanitize_url(url)
|
85
|
-
# Prepend a protocol if there isn't one
|
86
|
-
unless url.match(/^[a-zA-Z]+:\/\//)
|
87
|
-
url = 'http://' + url
|
88
|
-
end
|
89
|
-
# Replace all functional permutations of "javascript:" with a hex-encoded version of the same
|
90
|
-
url.gsub!(/(\s*j\s*\s*a\s*v\s*a\s*s\s*c\s*r\s*i\s*p\s*t\s*):/i) do |match_str|
|
91
|
-
'%' + $1.unpack('H2' * $1.length).join('%').upcase + '%3A'
|
92
|
-
end
|
93
|
-
url.gsub('"', '%22')
|
94
|
-
end
|
95
84
|
end
|
96
85
|
end
|
data/spec/html_maker_spec.rb
CHANGED
@@ -35,18 +35,18 @@ describe RbbCode::HtmlMaker do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'should not allow JavaScript in URLs' do
|
38
|
-
urls =
|
39
|
-
'javascript:alert("1");'
|
40
|
-
'j a v a script:alert("2");'
|
41
|
-
' javascript:alert("3");'
|
42
|
-
'JavaScript:alert("4");'
|
43
|
-
"java\nscript:alert(\"5\");"
|
44
|
-
"java\rscript:alert(\"6\");"
|
45
|
-
|
38
|
+
urls = [
|
39
|
+
'javascript:alert("1");',
|
40
|
+
'j a v a script:alert("2");',
|
41
|
+
' javascript:alert("3");',
|
42
|
+
'JavaScript:alert("4");',
|
43
|
+
"java\nscript:alert(\"5\");",
|
44
|
+
"java\rscript:alert(\"6\");"
|
45
|
+
]
|
46
46
|
|
47
47
|
# url tag
|
48
|
-
urls.each do |evil_url
|
49
|
-
expect_html(
|
48
|
+
urls.each do |evil_url|
|
49
|
+
expect_html('<p><a href="">foo</a></p>') do
|
50
50
|
tag('p') do
|
51
51
|
tag('url', evil_url) do
|
52
52
|
text 'foo'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbbcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarrett Colby
|
@@ -9,10 +9,29 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-26 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.3.0
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sanitize-url
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.1.3
|
34
|
+
version:
|
16
35
|
description: RbbCode is a customizable Ruby library for parsing BB Code. RbbCode validates and cleans input. It supports customizable schemas so you can set rules about what tags are allowed where. The default rules are designed to ensure valid HTML output.
|
17
36
|
email: jarrett@jarrettcolby.com
|
18
37
|
executables: []
|