campfire-bot 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +11 -0
- data/.gitignore +6 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +57 -0
- data/README.textile +52 -0
- data/TODO +72 -0
- data/bin/bot +13 -0
- data/campfire-bot.gemspec +27 -0
- data/cfbot-stop.sh +8 -0
- data/config.example.yml +31 -0
- data/lib/bot.rb +194 -0
- data/lib/event.rb +114 -0
- data/lib/message.rb +30 -0
- data/lib/plugin.rb +77 -0
- data/lib/version.rb +3 -0
- data/plugins/accountability.rb +45 -0
- data/plugins/austin.rb +29 -0
- data/plugins/basecamp.rb +48 -0
- data/plugins/beer.rb +214 -0
- data/plugins/beijing_tally.rb +30 -0
- data/plugins/boop.rb +127 -0
- data/plugins/bruce.rb +15 -0
- data/plugins/bugzilla.rb +198 -0
- data/plugins/calvin.rb +43 -0
- data/plugins/chuck.rb +23 -0
- data/plugins/dilbert.rb +51 -0
- data/plugins/excuse.rb +478 -0
- data/plugins/fail.rb +16 -0
- data/plugins/figlet.rb +10 -0
- data/plugins/fun.rb +95 -0
- data/plugins/garfield.rb +43 -0
- data/plugins/generic_search.rb +66 -0
- data/plugins/help.rb +13 -0
- data/plugins/infobot.rb +58 -0
- data/plugins/insult.rb +87 -0
- data/plugins/jira.rb +197 -0
- data/plugins/lolcats.rb +17 -0
- data/plugins/our_quotes.rb +195 -0
- data/plugins/quote.rb +31 -0
- data/plugins/schneier.rb +28 -0
- data/plugins/seen.rb +88 -0
- data/plugins/signal_filter.rb +9 -0
- data/plugins/svn.rb +167 -0
- data/plugins/twitter_echo.rb +54 -0
- data/plugins/unfuddle.rb +69 -0
- data/plugins/weather.rb +25 -0
- data/plugins/xkcd.rb +43 -0
- data/spec/beer_spec.rb +224 -0
- data/spec/bugzilla_spec.rb +90 -0
- data/spec/command_spec.rb +96 -0
- data/spec/jira_spec.rb +264 -0
- data/spec/our_quotes_spec.rb +186 -0
- data/spec/plugin_spec.rb +43 -0
- data/spec/spec.opts +1 -0
- data/vendor/escape/ChangeLog +30 -0
- data/vendor/escape/Makefile +5 -0
- data/vendor/escape/README +81 -0
- data/vendor/escape/VERSION +1 -0
- data/vendor/escape/escape.rb +302 -0
- data/vendor/escape/install.rb +109 -0
- data/vendor/escape/misc/README.erb +85 -0
- data/vendor/escape/rdoc/classes/Escape.html +427 -0
- data/vendor/escape/rdoc/classes/Escape.src/M000022.html +19 -0
- data/vendor/escape/rdoc/classes/Escape.src/M000023.html +32 -0
- data/vendor/escape/rdoc/classes/Escape.src/M000024.html +24 -0
- data/vendor/escape/rdoc/classes/Escape.src/M000025.html +19 -0
- data/vendor/escape/rdoc/classes/Escape.src/M000026.html +48 -0
- data/vendor/escape/rdoc/classes/Escape.src/M000027.html +19 -0
- data/vendor/escape/rdoc/classes/Escape.src/M000028.html +19 -0
- data/vendor/escape/rdoc/classes/Escape/HTMLAttrValue.html +113 -0
- data/vendor/escape/rdoc/classes/Escape/HTMLEscaped.html +113 -0
- data/vendor/escape/rdoc/classes/Escape/PercentEncoded.html +113 -0
- data/vendor/escape/rdoc/classes/Escape/ShellEscaped.html +113 -0
- data/vendor/escape/rdoc/classes/Escape/StringWrapper.html +242 -0
- data/vendor/escape/rdoc/classes/Escape/StringWrapper.src/M000029.html +18 -0
- data/vendor/escape/rdoc/classes/Escape/StringWrapper.src/M000030.html +18 -0
- data/vendor/escape/rdoc/classes/Escape/StringWrapper.src/M000031.html +18 -0
- data/vendor/escape/rdoc/classes/Escape/StringWrapper.src/M000032.html +18 -0
- data/vendor/escape/rdoc/classes/Escape/StringWrapper.src/M000033.html +18 -0
- data/vendor/escape/rdoc/classes/Escape/StringWrapper.src/M000035.html +18 -0
- data/vendor/escape/rdoc/classes/TestEscapeHTML.html +182 -0
- data/vendor/escape/rdoc/classes/TestEscapeHTML.src/M000008.html +18 -0
- data/vendor/escape/rdoc/classes/TestEscapeHTML.src/M000009.html +18 -0
- data/vendor/escape/rdoc/classes/TestEscapeHTML.src/M000010.html +18 -0
- data/vendor/escape/rdoc/classes/TestEscapeHTML.src/M000011.html +18 -0
- data/vendor/escape/rdoc/classes/TestEscapePercentEncoded.html +182 -0
- data/vendor/escape/rdoc/classes/TestEscapePercentEncoded.src/M000012.html +18 -0
- data/vendor/escape/rdoc/classes/TestEscapePercentEncoded.src/M000013.html +19 -0
- data/vendor/escape/rdoc/classes/TestEscapePercentEncoded.src/M000014.html +20 -0
- data/vendor/escape/rdoc/classes/TestEscapePercentEncoded.src/M000015.html +22 -0
- data/vendor/escape/rdoc/classes/TestEscapeShellEscaped.html +167 -0
- data/vendor/escape/rdoc/classes/TestEscapeShellEscaped.src/M000016.html +18 -0
- data/vendor/escape/rdoc/classes/TestEscapeShellEscaped.src/M000017.html +20 -0
- data/vendor/escape/rdoc/classes/TestEscapeShellEscaped.src/M000018.html +20 -0
- data/vendor/escape/rdoc/classes/TestEscapeStringWrapper.html +167 -0
- data/vendor/escape/rdoc/classes/TestEscapeStringWrapper.src/M000019.html +20 -0
- data/vendor/escape/rdoc/classes/TestEscapeStringWrapper.src/M000020.html +24 -0
- data/vendor/escape/rdoc/classes/TestEscapeStringWrapper.src/M000021.html +22 -0
- data/vendor/escape/rdoc/files/escape_rb.html +136 -0
- data/vendor/escape/rdoc/files/install_rb.html +250 -0
- data/vendor/escape/rdoc/files/install_rb.src/M000001.html +23 -0
- data/vendor/escape/rdoc/files/install_rb.src/M000002.html +31 -0
- data/vendor/escape/rdoc/files/install_rb.src/M000003.html +27 -0
- data/vendor/escape/rdoc/files/install_rb.src/M000004.html +27 -0
- data/vendor/escape/rdoc/files/install_rb.src/M000005.html +21 -0
- data/vendor/escape/rdoc/files/install_rb.src/M000006.html +23 -0
- data/vendor/escape/rdoc/files/install_rb.src/M000007.html +21 -0
- data/vendor/escape/rdoc/files/test/test-escape_rb.html +109 -0
- data/vendor/escape/rdoc/fr_class_index.html +36 -0
- data/vendor/escape/rdoc/fr_file_index.html +29 -0
- data/vendor/escape/rdoc/fr_method_index.html +61 -0
- data/vendor/escape/rdoc/index.html +24 -0
- data/vendor/escape/rdoc/rdoc-style.css +208 -0
- data/vendor/escape/test/test-escape.rb +90 -0
- metadata +259 -0
data/spec/plugin_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec'
|
2
|
+
BOT_ROOT = File.join(File.dirname(__FILE__), '..')
|
3
|
+
BOT_ENVIRONMENT = 'test'
|
4
|
+
|
5
|
+
require File.join(File.dirname(__FILE__), '../lib/bot.rb')
|
6
|
+
bot = CampfireBot::Bot.instance
|
7
|
+
|
8
|
+
describe "config_var method" do
|
9
|
+
class ExamplePlugin < CampfireBot::Plugin
|
10
|
+
config_var :foo, "default foo"
|
11
|
+
end
|
12
|
+
|
13
|
+
def setup_plugin(klass, config={})
|
14
|
+
@bot = CampfireBot::Bot.instance
|
15
|
+
@bot.stub!(:config).and_return({'nickname' => 'Bot'}.merge(config))
|
16
|
+
@plugin = klass.new
|
17
|
+
CampfireBot::Plugin.registered_plugins[klass.to_s] = @plugin
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should setup default bug_url value" do
|
21
|
+
setup_plugin ExamplePlugin
|
22
|
+
@plugin.foo.should == "default foo"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should allow override of parameter via configuration" do
|
26
|
+
setup_plugin ExamplePlugin, 'example_plugin_foo' => 'bar'
|
27
|
+
@plugin.foo.should == "bar"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should keep defaults for two classes separate" do
|
31
|
+
class ExamplePlugin2 < CampfireBot::Plugin
|
32
|
+
config_var :bar, "default bar"
|
33
|
+
end
|
34
|
+
setup_plugin ExamplePlugin
|
35
|
+
plugin = @plugin
|
36
|
+
setup_plugin ExamplePlugin2
|
37
|
+
plugin.instance_variable_get(:@foo).should == "default foo"
|
38
|
+
@plugin.instance_variable_get(:@bar).should == "default bar"
|
39
|
+
# Used to fail with value "default bar"
|
40
|
+
plugin.instance_variable_get(:@bar).should == nil
|
41
|
+
@plugin.instance_variable_get(:@foo).should == nil
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
@@ -0,0 +1,30 @@
|
|
1
|
+
2007-02-28 Tanaka Akira <akr@fsij.org>
|
2
|
+
|
3
|
+
* escape.rb: Escape::HTMLAttrValue defined.
|
4
|
+
Escape.html_attr_value returns it.
|
5
|
+
|
6
|
+
2007-02-27 Tanaka Akira <akr@fsij.org>
|
7
|
+
|
8
|
+
* escape.rb: make StringWrapper into class.
|
9
|
+
Escape::ShellEscaped, Escape::HTMLEscaped and Escape::PercentEncoded
|
10
|
+
inherit it.
|
11
|
+
|
12
|
+
2007-02-26 Tanaka Akira <akr@fsij.org>
|
13
|
+
|
14
|
+
* escape.rb: Escape::ShellEscaped defined.
|
15
|
+
Escape.shell_command and Escape.shell_single_word returns
|
16
|
+
Escape::ShellEscaped object instead of a string.
|
17
|
+
|
18
|
+
* escape.rb: Escape::HTMLEscaped defined.
|
19
|
+
Escape.html_text and Escape.html_attr_value returns
|
20
|
+
Escape::HTMLEscaped object instead of a string.
|
21
|
+
|
22
|
+
* escape.rb: Escape::PercentEncoded defined.
|
23
|
+
Escape.uri_segment, Escape.uri_path and Escape.html_form returns
|
24
|
+
Escape::PercentEncoded object instead of a string.
|
25
|
+
|
26
|
+
* escape.rb: rename Escape.html_attr to Escape.html_attr_value.
|
27
|
+
|
28
|
+
2007-01-05 Tanaka Akira <akr@fsij.org>
|
29
|
+
|
30
|
+
* version 0.1 released.
|
@@ -0,0 +1,81 @@
|
|
1
|
+
= escape - HTML/URI/shell escaping utilities
|
2
|
+
|
3
|
+
|
4
|
+
escape library provides several HTML/URI/shell escaping functions.
|
5
|
+
|
6
|
+
== Author
|
7
|
+
|
8
|
+
Tanaka Akira <akr@fsij.org>
|
9
|
+
|
10
|
+
== Home Page
|
11
|
+
|
12
|
+
((<URL:http://www.a-k-r.org/escape/>))
|
13
|
+
|
14
|
+
== Feature
|
15
|
+
|
16
|
+
* several escaping/composing functions
|
17
|
+
* HTML text
|
18
|
+
* HTML attribute value
|
19
|
+
* HTML form (x-www-form-urlencoded)
|
20
|
+
* URI path
|
21
|
+
* shell command line
|
22
|
+
* dedicated classes for escaped strings
|
23
|
+
* escape and compose strongly related strings at once
|
24
|
+
|
25
|
+
== Usage
|
26
|
+
|
27
|
+
require 'escape'
|
28
|
+
|
29
|
+
Escape.shell_command(["echo", "*"]) #=> #<Escape::ShellEscaped: echo '*'>
|
30
|
+
Escape.uri_path("a?b/c?d/e?f") #=> #<Escape::PercentEncoded: a%3Fb/c%3Fd/e%3Ff>
|
31
|
+
Escape.html_form([["a","b"], ["c","d"]]) #=> #<Escape::PercentEncoded: a=b&c=d>
|
32
|
+
Escape.html_form({"a"=>"b", "c"=>"d"}) #=> #<Escape::PercentEncoded: a=b&c=d>
|
33
|
+
Escape.html_text("a & b < c > d") #=> #<Escape::HTMLEscaped: a & b < c > d>
|
34
|
+
Escape.html_attr_value("ab&<>\"c") #=> #<Escape::HTMLAttrValue: "ab&<>"c">
|
35
|
+
|
36
|
+
== Requirements
|
37
|
+
|
38
|
+
* ruby : http://www.ruby-lang.org/
|
39
|
+
|
40
|
+
== Download
|
41
|
+
|
42
|
+
* latest release: ((<escape-0.2.tar.gz|URL:escape-0.2.tar.gz>))
|
43
|
+
|
44
|
+
* development version in Subversion repository:
|
45
|
+
|
46
|
+
% svn co svn://svn@svn.a-k-r.org/akr/escape/trunk escape
|
47
|
+
|
48
|
+
== Install
|
49
|
+
|
50
|
+
% ruby install.rb
|
51
|
+
|
52
|
+
== Reference Manual
|
53
|
+
|
54
|
+
See rdoc/classes/Escape.html or
|
55
|
+
((<URL:http://www.a-k-r.org/escape/rdoc/classes/Escape.html>))
|
56
|
+
|
57
|
+
== License
|
58
|
+
|
59
|
+
The modified BSD licence
|
60
|
+
|
61
|
+
Redistribution and use in source and binary forms, with or without
|
62
|
+
modification, are permitted provided that the following conditions are met:
|
63
|
+
|
64
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
65
|
+
list of conditions and the following disclaimer.
|
66
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
67
|
+
this list of conditions and the following disclaimer in the documentation
|
68
|
+
and/or other materials provided with the distribution.
|
69
|
+
3. The name of the author may not be used to endorse or promote products
|
70
|
+
derived from this software without specific prior written permission.
|
71
|
+
|
72
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
73
|
+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
74
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
75
|
+
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
76
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
77
|
+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
78
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
79
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
80
|
+
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
81
|
+
OF SUCH DAMAGE.
|
@@ -0,0 +1 @@
|
|
1
|
+
0.2
|
@@ -0,0 +1,302 @@
|
|
1
|
+
# escape.rb - escape/unescape library for several formats
|
2
|
+
#
|
3
|
+
# Copyright (C) 2006,2007 Tanaka Akira <akr@fsij.org>
|
4
|
+
#
|
5
|
+
# Redistribution and use in source and binary forms, with or without
|
6
|
+
# modification, are permitted provided that the following conditions are met:
|
7
|
+
#
|
8
|
+
# 1. Redistributions of source code must retain the above copyright notice, this
|
9
|
+
# list of conditions and the following disclaimer.
|
10
|
+
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
# this list of conditions and the following disclaimer in the documentation
|
12
|
+
# and/or other materials provided with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote products
|
14
|
+
# derived from this software without specific prior written permission.
|
15
|
+
#
|
16
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
17
|
+
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
18
|
+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
19
|
+
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
20
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
21
|
+
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
24
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
25
|
+
# OF SUCH DAMAGE.
|
26
|
+
|
27
|
+
# Escape module provides several escape functions.
|
28
|
+
# * URI
|
29
|
+
# * HTML
|
30
|
+
# * shell command
|
31
|
+
module Escape
|
32
|
+
module_function
|
33
|
+
|
34
|
+
class StringWrapper
|
35
|
+
class << self
|
36
|
+
alias new_no_dup new
|
37
|
+
def new(str)
|
38
|
+
new_no_dup(str.dup)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize(str)
|
43
|
+
@str = str
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_s
|
47
|
+
@str.dup
|
48
|
+
end
|
49
|
+
|
50
|
+
def inspect
|
51
|
+
"\#<#{self.class}: #{@str}>"
|
52
|
+
end
|
53
|
+
|
54
|
+
def ==(other)
|
55
|
+
other.class == self.class && @str == other.instance_variable_get(:@str)
|
56
|
+
end
|
57
|
+
alias eql? ==
|
58
|
+
|
59
|
+
def hash
|
60
|
+
@str.hash
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class ShellEscaped < StringWrapper
|
65
|
+
end
|
66
|
+
|
67
|
+
# Escape.shell_command composes
|
68
|
+
# a sequence of words to
|
69
|
+
# a single shell command line.
|
70
|
+
# All shell meta characters are quoted and
|
71
|
+
# the words are concatenated with interleaving space.
|
72
|
+
# It returns an instance of ShellEscaped.
|
73
|
+
#
|
74
|
+
# Escape.shell_command(["ls", "/"]) #=> #<Escape::ShellEscaped: ls />
|
75
|
+
# Escape.shell_command(["echo", "*"]) #=> #<Escape::ShellEscaped: echo '*'>
|
76
|
+
#
|
77
|
+
# Note that system(*command) and
|
78
|
+
# system(Escape.shell_command(command)) is roughly same.
|
79
|
+
# There are two exception as follows.
|
80
|
+
# * The first is that the later may invokes /bin/sh.
|
81
|
+
# * The second is an interpretation of an array with only one element:
|
82
|
+
# the element is parsed by the shell with the former but
|
83
|
+
# it is recognized as single word with the later.
|
84
|
+
# For example, system(*["echo foo"]) invokes echo command with an argument "foo".
|
85
|
+
# But system(Escape.shell_command(["echo foo"])) invokes "echo foo" command without arguments (and it probably fails).
|
86
|
+
def shell_command(command)
|
87
|
+
s = command.map {|word| shell_single_word(word) }.join(' ')
|
88
|
+
ShellEscaped.new_no_dup(s)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Escape.shell_single_word quotes shell meta characters.
|
92
|
+
# It returns an instance of ShellEscaped.
|
93
|
+
#
|
94
|
+
# The result string is always single shell word, even if
|
95
|
+
# the argument is "".
|
96
|
+
# Escape.shell_single_word("") returns #<Escape::ShellEscaped: ''>.
|
97
|
+
#
|
98
|
+
# Escape.shell_single_word("") #=> #<Escape::ShellEscaped: ''>
|
99
|
+
# Escape.shell_single_word("foo") #=> #<Escape::ShellEscaped: foo>
|
100
|
+
# Escape.shell_single_word("*") #=> #<Escape::ShellEscaped: '*'>
|
101
|
+
def shell_single_word(str)
|
102
|
+
if str.empty?
|
103
|
+
ShellEscaped.new_no_dup("''")
|
104
|
+
elsif %r{\A[0-9A-Za-z+,./:=@_-]+\z} =~ str
|
105
|
+
ShellEscaped.new(str)
|
106
|
+
else
|
107
|
+
result = ''
|
108
|
+
str.scan(/('+)|[^']+/) {
|
109
|
+
if $1
|
110
|
+
result << %q{\'} * $1.length
|
111
|
+
else
|
112
|
+
result << "'#{$&}'"
|
113
|
+
end
|
114
|
+
}
|
115
|
+
ShellEscaped.new_no_dup(result)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class PercentEncoded < StringWrapper
|
120
|
+
end
|
121
|
+
|
122
|
+
# Escape.uri_segment escapes URI segment using percent-encoding.
|
123
|
+
# It returns an instance of PercentEncoded.
|
124
|
+
#
|
125
|
+
# Escape.uri_segment("a/b") #=> #<Escape::PercentEncoded: a%2Fb>
|
126
|
+
#
|
127
|
+
# The segment is "/"-splitted element after authority before query in URI, as follows.
|
128
|
+
#
|
129
|
+
# scheme://authority/segment1/segment2/.../segmentN?query#fragment
|
130
|
+
#
|
131
|
+
# See RFC 3986 for details of URI.
|
132
|
+
def uri_segment(str)
|
133
|
+
# pchar - pct-encoded = unreserved / sub-delims / ":" / "@"
|
134
|
+
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
135
|
+
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
|
136
|
+
s = str.gsub(%r{[^A-Za-z0-9\-._~!$&'()*+,;=:@]}n) {
|
137
|
+
'%' + $&.unpack("H2")[0].upcase
|
138
|
+
}
|
139
|
+
PercentEncoded.new_no_dup(s)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Escape.uri_path escapes URI path using percent-encoding.
|
143
|
+
# The given path should be a sequence of (non-escaped) segments separated by "/".
|
144
|
+
# The segments cannot contains "/".
|
145
|
+
# It returns an instance of PercentEncoded.
|
146
|
+
#
|
147
|
+
# Escape.uri_path("a/b/c") #=> #<Escape::PercentEncoded: a/b/c>
|
148
|
+
# Escape.uri_path("a?b/c?d/e?f") #=> #<Escape::PercentEncoded: a%3Fb/c%3Fd/e%3Ff>
|
149
|
+
#
|
150
|
+
# The path is the part after authority before query in URI, as follows.
|
151
|
+
#
|
152
|
+
# scheme://authority/path#fragment
|
153
|
+
#
|
154
|
+
# See RFC 3986 for details of URI.
|
155
|
+
#
|
156
|
+
# Note that this function is not appropriate to convert OS path to URI.
|
157
|
+
def uri_path(str)
|
158
|
+
s = str.gsub(%r{[^/]+}n) { uri_segment($&) }
|
159
|
+
PercentEncoded.new_no_dup(s)
|
160
|
+
end
|
161
|
+
|
162
|
+
# :stopdoc:
|
163
|
+
def html_form_fast(pairs, sep='&')
|
164
|
+
s = pairs.map {|k, v|
|
165
|
+
# query-chars - pct-encoded - x-www-form-urlencoded-delimiters =
|
166
|
+
# unreserved / "!" / "$" / "'" / "(" / ")" / "*" / "," / ":" / "@" / "/" / "?"
|
167
|
+
# query-char - pct-encoded = unreserved / sub-delims / ":" / "@" / "/" / "?"
|
168
|
+
# query-char = pchar / "/" / "?" = unreserved / pct-encoded / sub-delims / ":" / "@" / "/" / "?"
|
169
|
+
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
170
|
+
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
|
171
|
+
# x-www-form-urlencoded-delimiters = "&" / "+" / ";" / "="
|
172
|
+
k = k.gsub(%r{[^0-9A-Za-z\-\._~:/?@!\$'()*,]}n) {
|
173
|
+
'%' + $&.unpack("H2")[0].upcase
|
174
|
+
}
|
175
|
+
v = v.gsub(%r{[^0-9A-Za-z\-\._~:/?@!\$'()*,]}n) {
|
176
|
+
'%' + $&.unpack("H2")[0].upcase
|
177
|
+
}
|
178
|
+
"#{k}=#{v}"
|
179
|
+
}.join(sep)
|
180
|
+
PercentEncoded.new_no_dup(s)
|
181
|
+
end
|
182
|
+
# :startdoc:
|
183
|
+
|
184
|
+
# Escape.html_form composes HTML form key-value pairs as a x-www-form-urlencoded encoded string.
|
185
|
+
# It returns an instance of PercentEncoded.
|
186
|
+
#
|
187
|
+
# Escape.html_form takes an array of pair of strings or
|
188
|
+
# an hash from string to string.
|
189
|
+
#
|
190
|
+
# Escape.html_form([["a","b"], ["c","d"]]) #=> #<Escape::PercentEncoded: a=b&c=d>
|
191
|
+
# Escape.html_form({"a"=>"b", "c"=>"d"}) #=> #<Escape::PercentEncoded: a=b&c=d>
|
192
|
+
#
|
193
|
+
# In the array form, it is possible to use same key more than once.
|
194
|
+
# (It is required for a HTML form which contains
|
195
|
+
# checkboxes and select element with multiple attribute.)
|
196
|
+
#
|
197
|
+
# Escape.html_form([["k","1"], ["k","2"]]) #=> #<Escape::PercentEncoded: k=1&k=2>
|
198
|
+
#
|
199
|
+
# If the strings contains characters which must be escaped in x-www-form-urlencoded,
|
200
|
+
# they are escaped using %-encoding.
|
201
|
+
#
|
202
|
+
# Escape.html_form([["k=","&;="]]) #=> #<Escape::PercentEncoded: k%3D=%26%3B%3D>
|
203
|
+
#
|
204
|
+
# The separator can be specified by the optional second argument.
|
205
|
+
#
|
206
|
+
# Escape.html_form([["a","b"], ["c","d"]], ";") #=> #<Escape::PercentEncoded: a=b;c=d>
|
207
|
+
#
|
208
|
+
# See HTML 4.01 for details.
|
209
|
+
def html_form(pairs, sep='&')
|
210
|
+
r = ''
|
211
|
+
first = true
|
212
|
+
pairs.each {|k, v|
|
213
|
+
# query-chars - pct-encoded - x-www-form-urlencoded-delimiters =
|
214
|
+
# unreserved / "!" / "$" / "'" / "(" / ")" / "*" / "," / ":" / "@" / "/" / "?"
|
215
|
+
# query-char - pct-encoded = unreserved / sub-delims / ":" / "@" / "/" / "?"
|
216
|
+
# query-char = pchar / "/" / "?" = unreserved / pct-encoded / sub-delims / ":" / "@" / "/" / "?"
|
217
|
+
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
218
|
+
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
|
219
|
+
# x-www-form-urlencoded-delimiters = "&" / "+" / ";" / "="
|
220
|
+
r << sep if !first
|
221
|
+
first = false
|
222
|
+
k.each_byte {|byte|
|
223
|
+
ch = byte.chr
|
224
|
+
if %r{[^0-9A-Za-z\-\._~:/?@!\$'()*,]}n =~ ch
|
225
|
+
r << "%" << ch.unpack("H2")[0].upcase
|
226
|
+
else
|
227
|
+
r << ch
|
228
|
+
end
|
229
|
+
}
|
230
|
+
r << '='
|
231
|
+
v.each_byte {|byte|
|
232
|
+
ch = byte.chr
|
233
|
+
if %r{[^0-9A-Za-z\-\._~:/?@!\$'()*,]}n =~ ch
|
234
|
+
r << "%" << ch.unpack("H2")[0].upcase
|
235
|
+
else
|
236
|
+
r << ch
|
237
|
+
end
|
238
|
+
}
|
239
|
+
}
|
240
|
+
PercentEncoded.new_no_dup(r)
|
241
|
+
end
|
242
|
+
|
243
|
+
class HTMLEscaped < StringWrapper
|
244
|
+
end
|
245
|
+
|
246
|
+
# :stopdoc:
|
247
|
+
HTML_TEXT_ESCAPE_HASH = {
|
248
|
+
'&' => '&',
|
249
|
+
'<' => '<',
|
250
|
+
'>' => '>',
|
251
|
+
}
|
252
|
+
# :startdoc:
|
253
|
+
|
254
|
+
# Escape.html_text escapes a string appropriate for HTML text using character references.
|
255
|
+
# It returns an instance of HTMLEscaped.
|
256
|
+
#
|
257
|
+
# It escapes 3 characters:
|
258
|
+
# * '&' to '&'
|
259
|
+
# * '<' to '<'
|
260
|
+
# * '>' to '>'
|
261
|
+
#
|
262
|
+
# Escape.html_text("abc") #=> #<Escape::HTMLEscaped: abc>
|
263
|
+
# Escape.html_text("a & b < c > d") #=> #<Escape::HTMLEscaped: a & b < c > d>
|
264
|
+
#
|
265
|
+
# This function is not appropriate for escaping HTML element attribute
|
266
|
+
# because quotes are not escaped.
|
267
|
+
def html_text(str)
|
268
|
+
s = str.gsub(/[&<>]/) {|ch| HTML_TEXT_ESCAPE_HASH[ch] }
|
269
|
+
HTMLEscaped.new_no_dup(s)
|
270
|
+
end
|
271
|
+
|
272
|
+
# :stopdoc:
|
273
|
+
HTML_ATTR_ESCAPE_HASH = {
|
274
|
+
'&' => '&',
|
275
|
+
'<' => '<',
|
276
|
+
'>' => '>',
|
277
|
+
'"' => '"',
|
278
|
+
}
|
279
|
+
# :startdoc:
|
280
|
+
|
281
|
+
class HTMLAttrValue < StringWrapper
|
282
|
+
end
|
283
|
+
|
284
|
+
# Escape.html_attr_value encodes a string as a double-quoted HTML attribute using character references.
|
285
|
+
# It returns an instance of HTMLAttrValue.
|
286
|
+
#
|
287
|
+
# Escape.html_attr_value("abc") #=> #<Escape::HTMLAttrValue: "abc">
|
288
|
+
# Escape.html_attr_value("a&b") #=> #<Escape::HTMLAttrValue: "a&b">
|
289
|
+
# Escape.html_attr_value("ab&<>\"c") #=> #<Escape::HTMLAttrValue: "ab&<>"c">
|
290
|
+
# Escape.html_attr_value("a'c") #=> #<Escape::HTMLAttrValue: "a'c">
|
291
|
+
#
|
292
|
+
# It escapes 4 characters:
|
293
|
+
# * '&' to '&'
|
294
|
+
# * '<' to '<'
|
295
|
+
# * '>' to '>'
|
296
|
+
# * '"' to '"'
|
297
|
+
#
|
298
|
+
def html_attr_value(str)
|
299
|
+
s = '"' + str.gsub(/[&<>"]/) {|ch| HTML_ATTR_ESCAPE_HASH[ch] } + '"'
|
300
|
+
HTMLAttrValue.new_no_dup(s)
|
301
|
+
end
|
302
|
+
end
|