sinatra-helpers-escape 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ Sinatra helpers for HTML escaping.
2
+
3
+ Author: Philipp Kempgen, [http://kempgen.net](http://kempgen.net)
4
+
5
+
6
+ ## Usage
7
+
8
+ Include the module in your Sinatra application:
9
+
10
+ helpers ::Sinatra::Helpers::Escape::HTML
11
+
@@ -0,0 +1,76 @@
1
+ require 'sinatra/base'
2
+ #require 'rack/utils'
3
+
4
+ # see Sinatra
5
+ module Sinatra
6
+ # see Sinatra
7
+ module Helpers
8
+
9
+ # Escaping helpers.
10
+ module Escape
11
+
12
+ # HTML escaping helpers.
13
+ #
14
+ # Usage:
15
+ #
16
+ # Include the module in your Sinatra application:
17
+ #
18
+ # helpers ::Sinatra::Helpers::Escape::HTML
19
+ #
20
+ module HTML
21
+
22
+ # Map special characters to HTML entities.
23
+ ESCAPE_HTML = {
24
+ '&' .freeze => '&',
25
+ '<' .freeze => '&lt;',
26
+ '>' .freeze => '&gt;',
27
+ # '\'' .freeze => '&#x27;',
28
+ '\'' .freeze => '&#39;',
29
+ '"' .freeze => '&quot;',
30
+ }
31
+
32
+ # Regexp search pattern for special characters.
33
+ ESCAPE_HTML_PATTERN = ::Regexp.union( *ESCAPE_HTML.keys )
34
+
35
+ # Escapes the string so it's safe to use in HTML.
36
+ #
37
+ # @param str [String] The string to escape.
38
+ # @return [String] Escaped version of the string.
39
+ #
40
+ def h( str )
41
+ #::Rack::Utils.escape_html( str )
42
+ str.to_s.gsub( ESCAPE_HTML_PATTERN ){ |c| ESCAPE_HTML[c] }
43
+ end
44
+
45
+ extend self # make instance methods available as class methods
46
+ end
47
+
48
+ # XML escaping helpers.
49
+ #
50
+ # Usage:
51
+ #
52
+ # Include the module in your Sinatra application:
53
+ #
54
+ # helpers ::Sinatra::Helpers::Escape::XML
55
+ #
56
+ module XML
57
+
58
+ # Escapes the string so it's safe to use in XML.
59
+ #
60
+ # @param str [String] The string to escape.
61
+ # @return [String] Escaped version of the string.
62
+ #
63
+ def x( str )
64
+ ::Sinatra::Helpers::Escape::HTML.h( str )
65
+ end
66
+
67
+ extend self # make instance methods available as class methods
68
+ end
69
+
70
+ end
71
+ end
72
+
73
+ #helpers Helpers::Escape::HTML
74
+ #::Sinatra::Base.helpers Helpers::Escape::HTML
75
+ end
76
+
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-helpers-escape
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philipp Kempgen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ description: Helpers for HTML escaping in Sinatra.
31
+ email:
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/sinatra-helpers/escape/html.rb
37
+ - README.md
38
+ homepage: https://github.com/philipp-kempgen/sinatra-helpers-escape
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.25
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Sinatra helpers for HTML escaping.
62
+ test_files: []
63
+ has_rdoc: