html_helpers 1.1.3

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.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
data/CHANGELOG ADDED
@@ -0,0 +1,14 @@
1
+ *1.1.2* 28 May 2008
2
+
3
+ * Me <Thomas Maurer> pushed the plugin to GitHub (mainly for using as git submodule)
4
+ * module Leftbee::HTMLHelpers renamed to generic and consitent way of classes in
5
+ Rails (like HTML::Sanitizer): HTML::EntityCoder
6
+
7
+ *1.1.1* 22 August 2006
8
+
9
+ * module Leftbee::HTMLHelper renamed to Leftbee::HTMLHelpers to be consistent with the name of the plugin and the documentation!
10
+
11
+
12
+ *1.1* 21 August 2006
13
+
14
+ * Slight refactoring to allow the inclusion of the helpers in any class more easily.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ group :dev do
6
+ gem 'rake'
7
+ gem 'actionpack', '~>3'
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionpack (3.0.5)
6
+ activemodel (= 3.0.5)
7
+ activesupport (= 3.0.5)
8
+ builder (~> 2.1.2)
9
+ erubis (~> 2.6.6)
10
+ i18n (~> 0.4)
11
+ rack (~> 1.2.1)
12
+ rack-mount (~> 0.6.13)
13
+ rack-test (~> 0.5.7)
14
+ tzinfo (~> 0.3.23)
15
+ activemodel (3.0.5)
16
+ activesupport (= 3.0.5)
17
+ builder (~> 2.1.2)
18
+ i18n (~> 0.4)
19
+ activesupport (3.0.5)
20
+ builder (2.1.2)
21
+ erubis (2.6.6)
22
+ abstract (>= 1.0.0)
23
+ i18n (0.5.0)
24
+ rack (1.2.1)
25
+ rack-mount (0.6.13)
26
+ rack (>= 1.0.0)
27
+ rack-test (0.5.7)
28
+ rack (>= 1.0)
29
+ rake (0.8.7)
30
+ tzinfo (0.3.24)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ actionpack (~> 3)
37
+ rake
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2006 Ruben Nine
2
+ HTMLEntities is copyright (c) 2005-2006 Paul Battley
3
+
4
+ The MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,44 @@
1
+ HTML::EntityCoder
2
+ =================
3
+
4
+ This plugin lets you encode and decode (UTF-8) strings using HTML entities.
5
+ Two new methods will be automatically available in your views:
6
+
7
+ * encode_entities: Encodes a string into a string with HTML entities:
8
+
9
+ >> <%= encode_entities("Über geek") %>
10
+ => "&Uuml;ber geek"
11
+
12
+ * decode_entities: Decodes a string with HTML entities into a string:
13
+
14
+ >> <%= decode_entities("&Uuml;ber geek") %>
15
+ => "Über geek"
16
+
17
+
18
+ Using Anywhere
19
+ ==============
20
+
21
+ Now it's possible to include the helpers in other classes easily by just including HTML::EntityCoder in your class.
22
+
23
+ If you *really* need to to use the helpers in a model you can do it like this:
24
+
25
+ class Person < ActiveRecord::Base
26
+ include HTML::EntityCoder
27
+ end
28
+
29
+ or a controller:
30
+
31
+ class PersonController < ApplicationController
32
+ include HTML::EntityCoder
33
+ end
34
+
35
+ etc.
36
+
37
+
38
+ Note: Don't feed the helpers with anything else but UTF-8. If you really need to, convert your string to UTF-8 first using Iconv (http://www.ruby-doc.org/stdlib/libdoc/iconv/rdoc/index.html)
39
+
40
+ A basic test unit is included.
41
+
42
+
43
+ HTMLHelpers is copyright (c) 2006 Ruben Nine (ruben.nine@gmail.com), released under the MIT license
44
+ HTMLEntities is copyright (c) 2005-2006 Paul Battley, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ require 'bundler'
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test the html_helpers plugin.'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = true
16
+ end
17
+
18
+ desc 'Generate documentation for the html_helpers plugin.'
19
+ Rake::RDocTask.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'HtmlHelpers'
22
+ rdoc.options << '--line-numbers' << '--inline-source'
23
+ rdoc.rdoc_files.include('README')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
data/about.yml ADDED
@@ -0,0 +1,7 @@
1
+ author: Ruben Nine
2
+ summary: Encode and decode HTML entities in your views and other classes.
3
+ homepage: http://www.leftbee.net
4
+ plugin: git://github.com/tma/html_helpers.git
5
+ license: MIT
6
+ version: 1.1.1
7
+ rails_version: 1.0+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "html_helpers"
6
+ s.version = '1.1.3'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Ruben Nine", "Thomas Maurer"]
9
+ s.email = ["tma@freshbit.ch"]
10
+ s.homepage = "https://github.com/tma/html_helpers"
11
+ s.summary = %q{Rails Plugin with Helpers to en- and decode HTML Entities}
12
+ s.description = %q{Encode and decode HTML entities in your views and other classes}
13
+ s.license = 'MIT'
14
+
15
+ s.rubyforge_project = "html_helpers"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "htmlentities"
2
+ require "html_helpers"
@@ -0,0 +1,34 @@
1
+ # Copyright (c) 2006 Ruben Nine
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ module HTML
23
+ module EntityCoder
24
+ def encode_entities(string)
25
+ HTMLEntities.encode_entities(string, :basic, :named)
26
+ end
27
+
28
+ def decode_entities(string)
29
+ HTMLEntities.decode_entities(string)
30
+ end
31
+ end
32
+ end
33
+
34
+ ActionView::Base.send(:include, HTML::EntityCoder) if defined?(ActionView)
@@ -0,0 +1,165 @@
1
+ #
2
+ # HTML entity encoding and decoding for Ruby
3
+ #
4
+
5
+ module HTMLEntities
6
+
7
+ class InstructionError < RuntimeError
8
+ end
9
+
10
+ module Data #:nodoc:
11
+
12
+ #
13
+ # MAP is a hash of all the HTML entities I could discover, as taken
14
+ # from the w3schools page on the subject:
15
+ # http://www.w3schools.com/html/html_entitiesref.asp
16
+ # The format is 'entity name' => codepoint where entity name is given
17
+ # without the surrounding ampersand and semicolon.
18
+ #
19
+ MAP = {
20
+ 'quot' => 34, 'apos' => 39, 'amp' => 38,
21
+ 'lt' => 60, 'gt' => 62, 'nbsp' => 160,
22
+ 'iexcl' => 161, 'curren' => 164, 'cent' => 162,
23
+ 'pound' => 163, 'yen' => 165, 'brvbar' => 166,
24
+ 'sect' => 167, 'uml' => 168, 'copy' => 169,
25
+ 'ordf' => 170, 'laquo' => 171, 'not' => 172,
26
+ 'shy' => 173, 'reg' => 174, 'trade' => 8482,
27
+ 'macr' => 175, 'deg' => 176, 'plusmn' => 177,
28
+ 'sup2' => 178, 'sup3' => 179, 'acute' => 180,
29
+ 'micro' => 181, 'para' => 182, 'middot' => 183,
30
+ 'cedil' => 184, 'sup1' => 185, 'ordm' => 186,
31
+ 'raquo' => 187, 'frac14' => 188, 'frac12' => 189,
32
+ 'frac34' => 190, 'iquest' => 191, 'times' => 215,
33
+ 'divide' => 247, 'Agrave' => 192, 'Aacute' => 193,
34
+ 'Acirc' => 194, 'Atilde' => 195, 'Auml' => 196,
35
+ 'Aring' => 197, 'AElig' => 198, 'Ccedil' => 199,
36
+ 'Egrave' => 200, 'Eacute' => 201, 'Ecirc' => 202,
37
+ 'Euml' => 203, 'Igrave' => 204, 'Iacute' => 205,
38
+ 'Icirc' => 206, 'Iuml' => 207, 'ETH' => 208,
39
+ 'Ntilde' => 209, 'Ograve' => 210, 'Oacute' => 211,
40
+ 'Ocirc' => 212, 'Otilde' => 213, 'Ouml' => 214,
41
+ 'Oslash' => 216, 'Ugrave' => 217, 'Uacute' => 218,
42
+ 'Ucirc' => 219, 'Uuml' => 220, 'Yacute' => 221,
43
+ 'THORN' => 222, 'szlig' => 223, 'agrave' => 224,
44
+ 'aacute' => 225, 'acirc' => 226, 'atilde' => 227,
45
+ 'auml' => 228, 'aring' => 229, 'aelig' => 230,
46
+ 'ccedil' => 231, 'egrave' => 232, 'eacute' => 233,
47
+ 'ecirc' => 234, 'euml' => 235, 'igrave' => 236,
48
+ 'iacute' => 237, 'icirc' => 238, 'iuml' => 239,
49
+ 'eth' => 240, 'ntilde' => 241, 'ograve' => 242,
50
+ 'oacute' => 243, 'ocirc' => 244, 'otilde' => 245,
51
+ 'ouml' => 246, 'oslash' => 248, 'ugrave' => 249,
52
+ 'uacute' => 250, 'ucirc' => 251, 'uuml' => 252,
53
+ 'yacute' => 253, 'thorn' => 254, 'yuml' => 255,
54
+ 'OElig' => 338, 'oelig' => 339, 'Scaron' => 352,
55
+ 'scaron' => 353, 'Yuml' => 376, 'circ' => 710,
56
+ 'tilde' => 732, 'ensp' => 8194, 'emsp' => 8195,
57
+ 'thinsp' => 8201, 'zwnj' => 8204, 'zwj' => 8205,
58
+ 'lrm' => 8206, 'rlm' => 8207, 'ndash' => 8211,
59
+ 'mdash' => 8212, 'lsquo' => 8216, 'rsquo' => 8217,
60
+ 'sbquo' => 8218, 'ldquo' => 8220, 'rdquo' => 8221,
61
+ 'bdquo' => 8222, 'dagger' => 8224, 'Dagger' => 8225,
62
+ 'hellip' => 8230, 'permil' => 8240, 'lsaquo' => 8249,
63
+ 'rsaquo' => 8250, 'euro' => 8364
64
+ }
65
+
66
+ MIN_LENGTH = MAP.keys.map{ |a| a.length }.min
67
+ MAX_LENGTH = MAP.keys.map{ |a| a.length }.max
68
+ NAMED_ENTITY_REGEXP = /&([a-z]{#{MIN_LENGTH},#{MAX_LENGTH}});/i
69
+ REVERSE_MAP = MAP.invert
70
+
71
+ BASIC_ENTITY_REGEXP = /[<>'"&]/
72
+
73
+ UTF8_NON_ASCII_REGEXP = /[\x00-\x1f]|[\xc0-\xfd][\x80-\xbf]+/
74
+
75
+ ENCODE_ENTITIES_COMMAND_ORDER = {
76
+ :basic => 0,
77
+ :named => 1,
78
+ :decimal => 2,
79
+ :hexadecimal => 3
80
+ }
81
+
82
+ end
83
+
84
+ #
85
+ # Decode XML and HTML 4.01 entities in a string into their UTF-8
86
+ # equivalents. Obviously, if your string is not already in UTF-8, you'd
87
+ # better convert it before using this method, or the output will be mixed
88
+ # up.
89
+ #
90
+ # Unknown named entities are not converted
91
+ #
92
+ def decode_entities(string)
93
+ return string.gsub(Data::NAMED_ENTITY_REGEXP) {
94
+ (cp = Data::MAP[$1]) ? [cp].pack('U') : $&
95
+ }.gsub(/&#([0-9]{1,7});|&#x([0-9a-f]{1,6});/i) {
96
+ $1 ? [$1.to_i].pack('U') : [$2.to_i(16)].pack('U')
97
+ }
98
+ end
99
+
100
+ #
101
+ # Encode codepoints into their corresponding entities. Various operations
102
+ # are possible, and may be specified in order:
103
+ #
104
+ # :basic :: Convert the five XML entities ('"<>&)
105
+ # :named :: Convert non-ASCII characters to their named HTML 4.01 equivalent
106
+ # :decimal :: Convert non-ASCII characters to decimal entities (e.g. &#1234;)
107
+ # :hexadecimal :: Convert non-ASCII characters to hexadecimal entities (e.g. # &#x12ab;)
108
+ #
109
+ # You can specify the commands in any order, but they will be executed in
110
+ # the order listed above to ensure that entity ampersands are not
111
+ # clobbered and that named entities are replaced before numeric ones.
112
+ #
113
+ # If no instructions are specified, :basic will be used.
114
+ #
115
+ # Examples:
116
+ # encode_entities(str) - XML-safe
117
+ # encode_entities(str, :basic, :decimal) - XML-safe and 7-bit clean
118
+ # encode_entities(str, :basic, :named, :decimal) - 7-bit clean, with all
119
+ # non-ASCII characters replaced with their named entity where possible, and
120
+ # decimal equivalents otherwise.
121
+ #
122
+ # Note: It is the program's responsibility to ensure that the string
123
+ # contains valid UTF-8 before calling this method.
124
+ #
125
+ def encode_entities(string, *instructions)
126
+ output = nil
127
+ if (instructions.empty?)
128
+ instructions = [:basic]
129
+ else
130
+ instructions = instructions.sort_by { |instruction|
131
+ Data::ENCODE_ENTITIES_COMMAND_ORDER[instruction] ||
132
+ (raise InstructionError, "unknown encode_entities command `#{instruction.inspect}'")
133
+ }
134
+ end
135
+ instructions.each do |instruction|
136
+ case instruction
137
+ when :basic
138
+ # Handled as basic ASCII
139
+ output = (output || string).gsub(Data::BASIC_ENTITY_REGEXP) {
140
+ # It's safe to use the simpler [0] here because we know
141
+ # that the basic entities are ASCII.
142
+ '&' << Data::REVERSE_MAP[$&[0]] << ';'
143
+ }
144
+ when :named
145
+ # Test everything except printable ASCII
146
+ output = (output || string).gsub(Data::UTF8_NON_ASCII_REGEXP) {
147
+ cp = $&.unpack('U')[0]
148
+ (e = Data::REVERSE_MAP[cp]) ? "&#{e};" : $&
149
+ }
150
+ when :decimal
151
+ output = (output || string).gsub(Data::UTF8_NON_ASCII_REGEXP) {
152
+ "&##{$&.unpack('U')[0]};"
153
+ }
154
+ when :hexadecimal
155
+ output = (output || string).gsub(Data::UTF8_NON_ASCII_REGEXP) {
156
+ "&#x#{$&.unpack('U')[0].to_s(16)};"
157
+ }
158
+ end
159
+ end
160
+ return output
161
+ end
162
+
163
+ extend self
164
+
165
+ end
@@ -0,0 +1,42 @@
1
+ require "test/unit"
2
+ require "action_view"
3
+ require "init"
4
+
5
+ class HtmlEntityCoderTest < Test::Unit::TestCase
6
+ include HTML::EntityCoder
7
+
8
+ def test_basic_encoding
9
+ assert_equal encode_entities("This is <em>emphasized</em>!"),
10
+ "This is &lt;em&gt;emphasized&lt;/em&gt;!"
11
+ end
12
+
13
+ def test_basic_decoding
14
+ assert_equal decode_entities("This is &lt;em&gt;emphasized&lt;/em&gt;!"),
15
+ "This is <em>emphasized</em>!"
16
+ end
17
+
18
+ def test_decoding_numeric_entities
19
+ assert_equal decode_entities("This is &#60;em&#62;emphasized&#60;/em&#62;!"),
20
+ "This is <em>emphasized</em>!"
21
+ end
22
+
23
+ def test_decoding_hex_entities
24
+ assert_equal decode_entities("This is &#x3C;em&#x3E;emphasized&#x3C;/em&#x3E;!"),
25
+ "This is <em>emphasized</em>!"
26
+ end
27
+
28
+ def test_decoding_mixed_entities
29
+ assert_equal decode_entities("This is &lt;em&#x3E;emphasized&lt;/em&#62;!"),
30
+ "This is <em>emphasized</em>!"
31
+ end
32
+
33
+ def test_text_encoding
34
+ assert_equal encode_entities("Ursache sind die hohen Zuflüsse des Regen, der Teile des Bayerischen Waldes entwässert.\nDort ist immer noch die Schneeschmelze im Gange, außerdem hat es Freitag dort teils kräftige Schauer gegeben."),
35
+ "Ursache sind die hohen Zufl&uuml;sse des Regen, der Teile des Bayerischen Waldes entw&auml;ssert.\nDort ist immer noch die Schneeschmelze im Gange, au&szlig;erdem hat es Freitag dort teils kr&auml;ftige Schauer gegeben."
36
+ end
37
+
38
+ def test_text_decoding
39
+ assert_equal decode_entities("Ursache sind die hohen Zufl&uuml;sse des Regen, der Teile des Bayerischen Waldes entw&auml;ssert.\nDort ist immer noch die Schneeschmelze im Gange, au&szlig;erdem hat es Freitag dort teils kr&auml;ftige Schauer gegeben."),
40
+ "Ursache sind die hohen Zuflüsse des Regen, der Teile des Bayerischen Waldes entwässert.\nDort ist immer noch die Schneeschmelze im Gange, außerdem hat es Freitag dort teils kräftige Schauer gegeben."
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_helpers
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 1
9
+ - 3
10
+ version: 1.1.3
11
+ platform: ruby
12
+ authors:
13
+ - Ruben Nine
14
+ - Thomas Maurer
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-03-13 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Encode and decode HTML entities in your views and other classes
24
+ email:
25
+ - tma@freshbit.ch
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - .gitignore
34
+ - CHANGELOG
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - MIT-LICENSE
38
+ - README
39
+ - Rakefile
40
+ - about.yml
41
+ - html_helpers.gemspec
42
+ - init.rb
43
+ - lib/html_helpers.rb
44
+ - lib/htmlentities.rb
45
+ - test/html_helpers_test.rb
46
+ has_rdoc: true
47
+ homepage: https://github.com/tma/html_helpers
48
+ licenses:
49
+ - MIT
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project: html_helpers
76
+ rubygems_version: 1.6.2
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Rails Plugin with Helpers to en- and decode HTML Entities
80
+ test_files:
81
+ - test/html_helpers_test.rb