hamlit 1.7.2 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -3
- data/.gitmodules +3 -0
- data/.travis.yml +25 -37
- data/CHANGELOG.md +18 -0
- data/Gemfile +16 -0
- data/LICENSE.txt +23 -2
- data/README.md +106 -48
- data/REFERENCE.md +222 -0
- data/Rakefile +77 -19
- data/benchmark/boolean_attribute.haml +6 -0
- data/benchmark/class_attribute.haml +5 -0
- data/benchmark/common_attribute.haml +3 -0
- data/benchmark/data_attribute.haml +4 -0
- data/benchmark/dynamic_attributes/boolean_attribute.haml +4 -0
- data/benchmark/dynamic_attributes/class_attribute.haml +4 -0
- data/benchmark/dynamic_attributes/common_attribute.haml +2 -0
- data/benchmark/dynamic_attributes/data_attribute.haml +2 -0
- data/benchmark/dynamic_attributes/id_attribute.haml +2 -0
- data/benchmark/etc/attribute_builder.haml +5 -0
- data/benchmark/etc/real_sample.haml +888 -0
- data/benchmark/etc/real_sample.rb +11 -0
- data/benchmark/etc/static_analyzer.haml +1 -0
- data/benchmark/etc/tags.haml +3 -0
- data/benchmark/ext/build_data.rb +15 -0
- data/benchmark/ext/build_id.rb +13 -0
- data/benchmark/id_attribute.haml +3 -0
- data/benchmark/plain.haml +4 -0
- data/benchmark/script.haml +4 -0
- data/benchmark/slim/LICENSE +21 -0
- data/{benchmarks → benchmark/slim}/context.rb +2 -4
- data/benchmark/slim/run-benchmarks.rb +94 -0
- data/{benchmarks → benchmark/slim}/view.erb +3 -3
- data/{benchmarks → benchmark/slim}/view.haml +0 -0
- data/{benchmarks/view.escaped.slim → benchmark/slim/view.slim} +1 -1
- data/benchmark/string_interpolation.haml +2 -0
- data/benchmark/utils/benchmark_ips_extension.rb +43 -0
- data/bin/bench +85 -0
- data/bin/clone +14 -0
- data/bin/console +11 -0
- data/bin/lineprof +48 -0
- data/bin/ruby +3 -0
- data/bin/setup +7 -0
- data/bin/stackprof +27 -0
- data/{test → bin/test} +6 -10
- data/{bin → exe}/hamlit +0 -0
- data/ext/hamlit/extconf.rb +14 -0
- data/ext/hamlit/hamlit.c +512 -0
- data/ext/hamlit/houdini/.gitignore +3 -0
- data/ext/hamlit/houdini/COPYING +7 -0
- data/ext/hamlit/houdini/Makefile +79 -0
- data/ext/hamlit/houdini/README.md +59 -0
- data/ext/hamlit/houdini/buffer.c +249 -0
- data/ext/hamlit/houdini/buffer.h +113 -0
- data/ext/hamlit/houdini/houdini.h +46 -0
- data/ext/hamlit/houdini/houdini_href_e.c +115 -0
- data/ext/hamlit/houdini/houdini_html_e.c +90 -0
- data/ext/hamlit/houdini/houdini_html_u.c +122 -0
- data/ext/hamlit/houdini/houdini_js_e.c +90 -0
- data/ext/hamlit/houdini/houdini_js_u.c +60 -0
- data/ext/hamlit/houdini/houdini_uri_e.c +107 -0
- data/ext/hamlit/houdini/houdini_uri_u.c +68 -0
- data/ext/hamlit/houdini/houdini_xml_e.c +136 -0
- data/ext/hamlit/houdini/html_unescape.gperf +258 -0
- data/ext/hamlit/houdini/html_unescape.h +754 -0
- data/ext/hamlit/houdini/tools/build_table.py +13 -0
- data/ext/hamlit/houdini/tools/build_tables.c +51 -0
- data/ext/hamlit/houdini/tools/wikipedia_table.txt +2025 -0
- data/hamlit.gemspec +30 -31
- data/lib/hamlit.rb +3 -1
- data/lib/hamlit/attribute_builder.rb +12 -0
- data/lib/hamlit/cli.rb +44 -43
- data/lib/hamlit/compiler.rb +92 -16
- data/lib/hamlit/compiler/attribute_compiler.rb +148 -0
- data/lib/hamlit/compiler/children_compiler.rb +111 -0
- data/lib/hamlit/compiler/comment_compiler.rb +36 -0
- data/lib/hamlit/compiler/doctype_compiler.rb +45 -0
- data/lib/hamlit/compiler/script_compiler.rb +97 -0
- data/lib/hamlit/compiler/silent_script_compiler.rb +24 -0
- data/lib/hamlit/compiler/tag_compiler.rb +69 -0
- data/lib/hamlit/engine.rb +12 -7
- data/lib/hamlit/error.rb +14 -0
- data/lib/hamlit/escapable.rb +12 -0
- data/lib/hamlit/filters.rb +65 -0
- data/lib/hamlit/filters/base.rb +4 -62
- data/lib/hamlit/filters/coffee.rb +9 -7
- data/lib/hamlit/filters/css.rb +25 -8
- data/lib/hamlit/filters/erb.rb +4 -6
- data/lib/hamlit/filters/escaped.rb +11 -9
- data/lib/hamlit/filters/javascript.rb +25 -8
- data/lib/hamlit/filters/less.rb +9 -7
- data/lib/hamlit/filters/markdown.rb +5 -6
- data/lib/hamlit/filters/plain.rb +11 -15
- data/lib/hamlit/filters/preserve.rb +15 -5
- data/lib/hamlit/filters/ruby.rb +3 -5
- data/lib/hamlit/filters/sass.rb +9 -7
- data/lib/hamlit/filters/scss.rb +9 -7
- data/lib/hamlit/filters/text_base.rb +24 -0
- data/lib/hamlit/filters/tilt_base.rb +47 -0
- data/lib/hamlit/hash_parser.rb +107 -0
- data/lib/hamlit/html.rb +9 -6
- data/lib/hamlit/identity.rb +12 -0
- data/lib/hamlit/object_ref.rb +29 -0
- data/lib/hamlit/parser.rb +25 -142
- data/lib/hamlit/parser/MIT-LICENSE +20 -0
- data/lib/hamlit/parser/README.md +28 -0
- data/lib/hamlit/parser/haml_buffer.rb +348 -0
- data/lib/hamlit/parser/haml_compiler.rb +553 -0
- data/lib/hamlit/parser/haml_error.rb +61 -0
- data/lib/hamlit/parser/haml_helpers.rb +727 -0
- data/lib/hamlit/parser/haml_options.rb +286 -0
- data/lib/hamlit/parser/haml_parser.rb +801 -0
- data/lib/hamlit/parser/haml_util.rb +283 -0
- data/lib/hamlit/parser/haml_xss_mods.rb +109 -0
- data/lib/hamlit/{helpers.rb → rails_helpers.rb} +2 -7
- data/lib/hamlit/rails_template.rb +30 -0
- data/lib/hamlit/railtie.rb +1 -12
- data/lib/hamlit/ruby_expression.rb +31 -0
- data/lib/hamlit/static_analyzer.rb +49 -0
- data/lib/hamlit/string_interpolation.rb +69 -0
- data/lib/hamlit/template.rb +8 -0
- data/lib/hamlit/utils.rb +9 -0
- data/lib/hamlit/version.rb +1 -1
- metadata +116 -324
- data/.rspec +0 -2
- data/benchmarks/benchmark.rb +0 -110
- data/benchmarks/view.slim +0 -17
- data/doc/README.md +0 -19
- data/doc/engine/indent.md +0 -48
- data/doc/engine/new_attribute.md +0 -77
- data/doc/engine/old_attributes.md +0 -198
- data/doc/engine/silent_script.md +0 -97
- data/doc/engine/tag.md +0 -48
- data/doc/engine/text.md +0 -64
- data/doc/faml/README.md +0 -16
- data/doc/faml/engine/indent.md +0 -48
- data/doc/faml/engine/old_attributes.md +0 -111
- data/doc/faml/engine/silent_script.md +0 -97
- data/doc/faml/engine/text.md +0 -59
- data/doc/faml/filters/erb.md +0 -24
- data/doc/faml/filters/javascript.md +0 -27
- data/doc/faml/filters/less.md +0 -57
- data/doc/faml/filters/plain.md +0 -25
- data/doc/filters/erb.md +0 -31
- data/doc/filters/javascript.md +0 -83
- data/doc/filters/less.md +0 -57
- data/doc/filters/markdown.md +0 -31
- data/doc/filters/plain.md +0 -25
- data/doc/haml/README.md +0 -15
- data/doc/haml/engine/new_attribute.md +0 -77
- data/doc/haml/engine/old_attributes.md +0 -142
- data/doc/haml/engine/tag.md +0 -48
- data/doc/haml/engine/text.md +0 -29
- data/doc/haml/filters/erb.md +0 -26
- data/doc/haml/filters/javascript.md +0 -76
- data/doc/haml/filters/markdown.md +0 -31
- data/lib/hamlit/attribute.rb +0 -78
- data/lib/hamlit/compilers/attributes.rb +0 -108
- data/lib/hamlit/compilers/comment.rb +0 -13
- data/lib/hamlit/compilers/doctype.rb +0 -39
- data/lib/hamlit/compilers/filter.rb +0 -53
- data/lib/hamlit/compilers/new_attribute.rb +0 -115
- data/lib/hamlit/compilers/old_attribute.rb +0 -241
- data/lib/hamlit/compilers/runtime_attribute.rb +0 -58
- data/lib/hamlit/compilers/script.rb +0 -31
- data/lib/hamlit/compilers/strip.rb +0 -19
- data/lib/hamlit/compilers/text.rb +0 -111
- data/lib/hamlit/concerns/attribute_builder.rb +0 -22
- data/lib/hamlit/concerns/balanceable.rb +0 -68
- data/lib/hamlit/concerns/deprecation.rb +0 -20
- data/lib/hamlit/concerns/error.rb +0 -31
- data/lib/hamlit/concerns/escapable.rb +0 -17
- data/lib/hamlit/concerns/included.rb +0 -28
- data/lib/hamlit/concerns/indentable.rb +0 -117
- data/lib/hamlit/concerns/lexable.rb +0 -32
- data/lib/hamlit/concerns/line_reader.rb +0 -62
- data/lib/hamlit/concerns/registerable.rb +0 -24
- data/lib/hamlit/concerns/string_interpolation.rb +0 -48
- data/lib/hamlit/concerns/whitespace.rb +0 -91
- data/lib/hamlit/filters/tilt.rb +0 -41
- data/lib/hamlit/parsers/attribute.rb +0 -71
- data/lib/hamlit/parsers/comment.rb +0 -30
- data/lib/hamlit/parsers/doctype.rb +0 -18
- data/lib/hamlit/parsers/filter.rb +0 -18
- data/lib/hamlit/parsers/multiline.rb +0 -58
- data/lib/hamlit/parsers/script.rb +0 -126
- data/lib/hamlit/parsers/tag.rb +0 -83
- data/lib/hamlit/parsers/text.rb +0 -28
- data/lib/hamlit/temple.rb +0 -9
- data/release +0 -6
- data/spec/Rakefile +0 -72
- data/spec/hamlit/engine/comment_spec.rb +0 -56
- data/spec/hamlit/engine/doctype_spec.rb +0 -19
- data/spec/hamlit/engine/error_spec.rb +0 -135
- data/spec/hamlit/engine/indent_spec.rb +0 -42
- data/spec/hamlit/engine/multiline_spec.rb +0 -44
- data/spec/hamlit/engine/new_attribute_spec.rb +0 -110
- data/spec/hamlit/engine/old_attributes_spec.rb +0 -404
- data/spec/hamlit/engine/script_spec.rb +0 -116
- data/spec/hamlit/engine/silent_script_spec.rb +0 -213
- data/spec/hamlit/engine/tag_spec.rb +0 -295
- data/spec/hamlit/engine/text_spec.rb +0 -239
- data/spec/hamlit/engine_spec.rb +0 -58
- data/spec/hamlit/filters/coffee_spec.rb +0 -60
- data/spec/hamlit/filters/css_spec.rb +0 -33
- data/spec/hamlit/filters/erb_spec.rb +0 -16
- data/spec/hamlit/filters/javascript_spec.rb +0 -82
- data/spec/hamlit/filters/less_spec.rb +0 -37
- data/spec/hamlit/filters/markdown_spec.rb +0 -30
- data/spec/hamlit/filters/plain_spec.rb +0 -15
- data/spec/hamlit/filters/ruby_spec.rb +0 -24
- data/spec/hamlit/filters/sass_spec.rb +0 -33
- data/spec/hamlit/filters/scss_spec.rb +0 -37
- data/spec/hamlit/haml_spec.rb +0 -910
- data/spec/rails/.gitignore +0 -18
- data/spec/rails/.rspec +0 -2
- data/spec/rails/Gemfile +0 -19
- data/spec/rails/README.rdoc +0 -28
- data/spec/rails/Rakefile +0 -6
- data/spec/rails/app/assets/images/.keep +0 -0
- data/spec/rails/app/assets/javascripts/application.js +0 -15
- data/spec/rails/app/assets/stylesheets/application.css +0 -15
- data/spec/rails/app/controllers/application_controller.rb +0 -8
- data/spec/rails/app/controllers/concerns/.keep +0 -0
- data/spec/rails/app/controllers/users_controller.rb +0 -23
- data/spec/rails/app/helpers/application_helper.rb +0 -2
- data/spec/rails/app/mailers/.keep +0 -0
- data/spec/rails/app/models/.keep +0 -0
- data/spec/rails/app/models/concerns/.keep +0 -0
- data/spec/rails/app/views/application/index.html.haml +0 -18
- data/spec/rails/app/views/layouts/application.html.haml +0 -12
- data/spec/rails/app/views/users/capture.html.haml +0 -5
- data/spec/rails/app/views/users/capture_haml.html.haml +0 -5
- data/spec/rails/app/views/users/form.html.haml +0 -2
- data/spec/rails/app/views/users/helpers.html.haml +0 -10
- data/spec/rails/app/views/users/index.html.haml +0 -9
- data/spec/rails/app/views/users/inline.html.haml +0 -6
- data/spec/rails/app/views/users/old_attributes.html.haml +0 -5
- data/spec/rails/app/views/users/safe_buffer.html.haml +0 -4
- data/spec/rails/app/views/users/whitespace.html.haml +0 -4
- data/spec/rails/bin/bundle +0 -3
- data/spec/rails/bin/rails +0 -8
- data/spec/rails/bin/rake +0 -8
- data/spec/rails/bin/setup +0 -29
- data/spec/rails/bin/spring +0 -15
- data/spec/rails/config.ru +0 -4
- data/spec/rails/config/application.rb +0 -34
- data/spec/rails/config/boot.rb +0 -3
- data/spec/rails/config/database.yml +0 -25
- data/spec/rails/config/environment.rb +0 -5
- data/spec/rails/config/environments/development.rb +0 -41
- data/spec/rails/config/environments/production.rb +0 -79
- data/spec/rails/config/environments/test.rb +0 -42
- data/spec/rails/config/initializers/assets.rb +0 -11
- data/spec/rails/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/rails/config/initializers/cookies_serializer.rb +0 -3
- data/spec/rails/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/rails/config/initializers/inflections.rb +0 -16
- data/spec/rails/config/initializers/mime_types.rb +0 -4
- data/spec/rails/config/initializers/session_store.rb +0 -3
- data/spec/rails/config/initializers/wrap_parameters.rb +0 -14
- data/spec/rails/config/locales/en.yml +0 -24
- data/spec/rails/config/routes.rb +0 -16
- data/spec/rails/config/secrets.yml +0 -22
- data/spec/rails/db/schema.rb +0 -16
- data/spec/rails/db/seeds.rb +0 -7
- data/spec/rails/lib/assets/.keep +0 -0
- data/spec/rails/lib/tasks/.keep +0 -0
- data/spec/rails/log/.keep +0 -0
- data/spec/rails/public/404.html +0 -67
- data/spec/rails/public/422.html +0 -67
- data/spec/rails/public/500.html +0 -66
- data/spec/rails/public/favicon.ico +0 -0
- data/spec/rails/public/robots.txt +0 -5
- data/spec/rails/spec/hamlit_spec.rb +0 -123
- data/spec/rails/spec/rails_helper.rb +0 -56
- data/spec/rails/spec/spec_helper.rb +0 -91
- data/spec/rails/vendor/assets/javascripts/.keep +0 -0
- data/spec/rails/vendor/assets/stylesheets/.keep +0 -0
- data/spec/spec_helper.rb +0 -36
- data/spec/spec_helper/document_generator.rb +0 -93
- data/spec/spec_helper/render_helper.rb +0 -120
- data/spec/spec_helper/test_case.rb +0 -55
@@ -0,0 +1,283 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'erubis/tiny'
|
5
|
+
rescue LoadError
|
6
|
+
require 'erb'
|
7
|
+
end
|
8
|
+
require 'set'
|
9
|
+
require 'stringio'
|
10
|
+
require 'strscan'
|
11
|
+
|
12
|
+
module Hamlit
|
13
|
+
# A module containing various useful functions.
|
14
|
+
module HamlUtil
|
15
|
+
extend self
|
16
|
+
|
17
|
+
# Silence all output to STDERR within a block.
|
18
|
+
#
|
19
|
+
# @yield A block in which no output will be printed to STDERR
|
20
|
+
def silence_warnings
|
21
|
+
the_real_stderr, $stderr = $stderr, StringIO.new
|
22
|
+
yield
|
23
|
+
ensure
|
24
|
+
$stderr = the_real_stderr
|
25
|
+
end
|
26
|
+
|
27
|
+
## Rails XSS Safety
|
28
|
+
|
29
|
+
# Whether or not ActionView's XSS protection is available and enabled,
|
30
|
+
# as is the default for Rails 3.0+, and optional for version 2.3.5+.
|
31
|
+
# Overridden in haml/template.rb if this is the case.
|
32
|
+
#
|
33
|
+
# @return [Boolean]
|
34
|
+
def rails_xss_safe?
|
35
|
+
false
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the given text, marked as being HTML-safe.
|
39
|
+
# With older versions of the Rails XSS-safety mechanism,
|
40
|
+
# this destructively modifies the HTML-safety of `text`.
|
41
|
+
#
|
42
|
+
# It only works if you are using ActiveSupport or the parameter `text`
|
43
|
+
# implements the #html_safe method.
|
44
|
+
#
|
45
|
+
# @param text [String, nil]
|
46
|
+
# @return [String, nil] `text`, marked as HTML-safe
|
47
|
+
def html_safe(text)
|
48
|
+
return unless text
|
49
|
+
text.html_safe
|
50
|
+
end
|
51
|
+
|
52
|
+
# Checks that the encoding of a string is valid
|
53
|
+
# and cleans up potential encoding gotchas like the UTF-8 BOM.
|
54
|
+
# If it's not, yields an error string describing the invalid character
|
55
|
+
# and the line on which it occurs.
|
56
|
+
#
|
57
|
+
# @param str [String] The string of which to check the encoding
|
58
|
+
# @yield [msg] A block in which an encoding error can be raised.
|
59
|
+
# Only yields if there is an encoding error
|
60
|
+
# @yieldparam msg [String] The error message to be raised
|
61
|
+
# @return [String] `str`, potentially with encoding gotchas like BOMs removed
|
62
|
+
def check_encoding(str)
|
63
|
+
if str.valid_encoding?
|
64
|
+
# Get rid of the Unicode BOM if possible
|
65
|
+
# Shortcut for UTF-8 which might be the majority case
|
66
|
+
if str.encoding == Encoding::UTF_8
|
67
|
+
return str.gsub(/\A\uFEFF/, '')
|
68
|
+
elsif str.encoding.name =~ /^UTF-(16|32)(BE|LE)?$/
|
69
|
+
return str.gsub(Regexp.new("\\A\uFEFF".encode(str.encoding)), '')
|
70
|
+
else
|
71
|
+
return str
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
encoding = str.encoding
|
76
|
+
newlines = Regexp.new("\r\n|\r|\n".encode(encoding).force_encoding(Encoding::ASCII_8BIT))
|
77
|
+
str.force_encoding(Encoding::ASCII_8BIT).split(newlines).each_with_index do |line, i|
|
78
|
+
begin
|
79
|
+
line.encode(encoding)
|
80
|
+
rescue Encoding::UndefinedConversionError => e
|
81
|
+
yield <<MSG.rstrip, i + 1
|
82
|
+
Invalid #{encoding.name} character #{e.error_char.dump}
|
83
|
+
MSG
|
84
|
+
end
|
85
|
+
end
|
86
|
+
return str
|
87
|
+
end
|
88
|
+
|
89
|
+
# Like {\#check\_encoding}, but also checks for a Ruby-style `-# coding:` comment
|
90
|
+
# at the beginning of the template and uses that encoding if it exists.
|
91
|
+
#
|
92
|
+
# The Haml encoding rules are simple.
|
93
|
+
# If a `-# coding:` comment exists,
|
94
|
+
# we assume that that's the original encoding of the document.
|
95
|
+
# Otherwise, we use whatever encoding Ruby has.
|
96
|
+
#
|
97
|
+
# Haml uses the same rules for parsing coding comments as Ruby.
|
98
|
+
# This means that it can understand Emacs-style comments
|
99
|
+
# (e.g. `-*- encoding: "utf-8" -*-`),
|
100
|
+
# and also that it cannot understand non-ASCII-compatible encodings
|
101
|
+
# such as `UTF-16` and `UTF-32`.
|
102
|
+
#
|
103
|
+
# @param str [String] The Haml template of which to check the encoding
|
104
|
+
# @yield [msg] A block in which an encoding error can be raised.
|
105
|
+
# Only yields if there is an encoding error
|
106
|
+
# @yieldparam msg [String] The error message to be raised
|
107
|
+
# @return [String] The original string encoded properly
|
108
|
+
# @raise [ArgumentError] if the document declares an unknown encoding
|
109
|
+
def check_haml_encoding(str, &block)
|
110
|
+
str = str.dup if str.frozen?
|
111
|
+
|
112
|
+
bom, encoding = parse_haml_magic_comment(str)
|
113
|
+
if encoding; str.force_encoding(encoding)
|
114
|
+
elsif bom; str.force_encoding(Encoding::UTF_8)
|
115
|
+
end
|
116
|
+
|
117
|
+
return check_encoding(str, &block)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Like `Object#inspect`, but preserves non-ASCII characters rather than escaping them.
|
121
|
+
# This is necessary so that the precompiled Haml template can be `#encode`d into `@options[:encoding]`
|
122
|
+
# before being evaluated.
|
123
|
+
#
|
124
|
+
# @param obj {Object}
|
125
|
+
# @return {String}
|
126
|
+
def inspect_obj(obj)
|
127
|
+
case obj
|
128
|
+
when String
|
129
|
+
%Q!"#{obj.gsub(/[\x00-\x7F]+/) {|s| s.inspect[1...-1]}}"!
|
130
|
+
when Symbol
|
131
|
+
":#{inspect_obj(obj.to_s)}"
|
132
|
+
else
|
133
|
+
obj.inspect
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Scans through a string looking for the interoplation-opening `#{`
|
138
|
+
# and, when it's found, yields the scanner to the calling code
|
139
|
+
# so it can handle it properly.
|
140
|
+
#
|
141
|
+
# The scanner will have any backslashes immediately in front of the `#{`
|
142
|
+
# as the second capture group (`scan[2]`),
|
143
|
+
# and the text prior to that as the first (`scan[1]`).
|
144
|
+
#
|
145
|
+
# @yieldparam scan [StringScanner] The scanner scanning through the string
|
146
|
+
# @return [String] The text remaining in the scanner after all `#{`s have been processed
|
147
|
+
def handle_interpolation(str)
|
148
|
+
scan = StringScanner.new(str)
|
149
|
+
yield scan while scan.scan(/(.*?)(\\*)#([\{@$])/)
|
150
|
+
scan.rest
|
151
|
+
end
|
152
|
+
|
153
|
+
# Moves a scanner through a balanced pair of characters.
|
154
|
+
# For example:
|
155
|
+
#
|
156
|
+
# Foo (Bar (Baz bang) bop) (Bang (bop bip))
|
157
|
+
# ^ ^
|
158
|
+
# from to
|
159
|
+
#
|
160
|
+
# @param scanner [StringScanner] The string scanner to move
|
161
|
+
# @param start [String] The character opening the balanced pair.
|
162
|
+
# @param finish [String] The character closing the balanced pair.
|
163
|
+
# @param count [Fixnum] The number of opening characters matched
|
164
|
+
# before calling this method
|
165
|
+
# @return [(String, String)] The string matched within the balanced pair
|
166
|
+
# and the rest of the string.
|
167
|
+
# `["Foo (Bar (Baz bang) bop)", " (Bang (bop bip))"]` in the example above.
|
168
|
+
def balance(scanner, start, finish, count = 0)
|
169
|
+
str = ''
|
170
|
+
scanner = StringScanner.new(scanner) unless scanner.is_a? StringScanner
|
171
|
+
regexp = Regexp.new("(.*?)[\\#{start.chr}\\#{finish.chr}]", Regexp::MULTILINE)
|
172
|
+
while scanner.scan(regexp)
|
173
|
+
str << scanner.matched
|
174
|
+
count += 1 if scanner.matched[-1] == start
|
175
|
+
count -= 1 if scanner.matched[-1] == finish
|
176
|
+
return [str.strip, scanner.rest] if count == 0
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# Formats a string for use in error messages about indentation.
|
181
|
+
#
|
182
|
+
# @param indentation [String] The string used for indentation
|
183
|
+
# @return [String] The name of the indentation (e.g. `"12 spaces"`, `"1 tab"`)
|
184
|
+
def human_indentation(indentation)
|
185
|
+
if !indentation.include?(?\t)
|
186
|
+
noun = 'space'
|
187
|
+
elsif !indentation.include?(?\s)
|
188
|
+
noun = 'tab'
|
189
|
+
else
|
190
|
+
return indentation.inspect
|
191
|
+
end
|
192
|
+
|
193
|
+
singular = indentation.length == 1
|
194
|
+
"#{indentation.length} #{noun}#{'s' unless singular}"
|
195
|
+
end
|
196
|
+
|
197
|
+
def contains_interpolation?(str)
|
198
|
+
/#[\{$@]/ === str
|
199
|
+
end
|
200
|
+
|
201
|
+
# Original Haml::Util.unescape_interpolation
|
202
|
+
def slow_unescape_interpolation(str, escape_html = nil)
|
203
|
+
res = ''
|
204
|
+
rest = ::Hamlit::HamlUtil.handle_interpolation str.dump do |scan|
|
205
|
+
escapes = (scan[2].size - 1) / 2
|
206
|
+
char = scan[3] # '{', '@' or '$'
|
207
|
+
res << scan.matched[0...-3 - escapes]
|
208
|
+
if escapes % 2 == 1
|
209
|
+
res << "\##{char}"
|
210
|
+
else
|
211
|
+
interpolated = if char == '{'
|
212
|
+
balance(scan, ?{, ?}, 1)[0][0...-1]
|
213
|
+
else
|
214
|
+
scan.scan(/\w+/)
|
215
|
+
end
|
216
|
+
content = eval('"' + interpolated + '"')
|
217
|
+
content.prepend(char) if char == '@' || char == '$'
|
218
|
+
content = "::Hamlit::HamlHelpers.html_escape((#{content}))" if escape_html
|
219
|
+
|
220
|
+
res << "\#{#{content}}"
|
221
|
+
end
|
222
|
+
end
|
223
|
+
res + rest
|
224
|
+
end
|
225
|
+
|
226
|
+
# Customized Haml::Util.unescape_interpolation to handle escape by Hamlit
|
227
|
+
def unescape_interpolation(str)
|
228
|
+
res = ''
|
229
|
+
rest = ::Hamlit::HamlUtil.handle_interpolation str.dump do |scan|
|
230
|
+
escapes = (scan[2].size - 1) / 2
|
231
|
+
char = scan[3] # '{', '@' or '$'
|
232
|
+
res << scan.matched[0...-3 - escapes]
|
233
|
+
if escapes % 2 == 1
|
234
|
+
res << "\##{char}"
|
235
|
+
else
|
236
|
+
interpolated = if char == '{'
|
237
|
+
balance(scan, ?{, ?}, 1)[0][0...-1]
|
238
|
+
else
|
239
|
+
scan.scan(/\w+/)
|
240
|
+
end
|
241
|
+
content = eval('"' + interpolated + '"')
|
242
|
+
content.prepend(char) if char == '@' || char == '$'
|
243
|
+
|
244
|
+
res << "\#{#{content}}"
|
245
|
+
end
|
246
|
+
end
|
247
|
+
res + rest
|
248
|
+
end
|
249
|
+
|
250
|
+
private
|
251
|
+
|
252
|
+
# Parses a magic comment at the beginning of a Haml file.
|
253
|
+
# The parsing rules are basically the same as Ruby's.
|
254
|
+
#
|
255
|
+
# @return [(Boolean, String or nil)]
|
256
|
+
# Whether the document begins with a UTF-8 BOM,
|
257
|
+
# and the declared encoding of the document (or nil if none is declared)
|
258
|
+
def parse_haml_magic_comment(str)
|
259
|
+
scanner = StringScanner.new(str.dup.force_encoding(Encoding::ASCII_8BIT))
|
260
|
+
bom = scanner.scan(/\xEF\xBB\xBF/n)
|
261
|
+
return bom unless scanner.scan(/-\s*#\s*/n)
|
262
|
+
if coding = try_parse_haml_emacs_magic_comment(scanner)
|
263
|
+
return bom, coding
|
264
|
+
end
|
265
|
+
|
266
|
+
return bom unless scanner.scan(/.*?coding[=:]\s*([\w-]+)/in)
|
267
|
+
return bom, scanner[1]
|
268
|
+
end
|
269
|
+
|
270
|
+
def try_parse_haml_emacs_magic_comment(scanner)
|
271
|
+
pos = scanner.pos
|
272
|
+
return unless scanner.scan(/.*?-\*-\s*/n)
|
273
|
+
# From Ruby's parse.y
|
274
|
+
return unless scanner.scan(/([^\s'":;]+)\s*:\s*("(?:\\.|[^"])*"|[^"\s;]+?)[\s;]*-\*-/n)
|
275
|
+
name, val = scanner[1], scanner[2]
|
276
|
+
return unless name =~ /(en)?coding/in
|
277
|
+
val = $1 if val =~ /^"(.*)"$/n
|
278
|
+
return val
|
279
|
+
ensure
|
280
|
+
scanner.pos = pos
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module Hamlit
|
2
|
+
module HamlHelpers
|
3
|
+
# This module overrides Haml helpers to work properly
|
4
|
+
# in the context of ActionView.
|
5
|
+
# Currently it's only used for modifying the helpers
|
6
|
+
# to work with Rails' XSS protection methods.
|
7
|
+
module XssMods
|
8
|
+
def self.included(base)
|
9
|
+
%w[html_escape find_and_preserve preserve list_of surround
|
10
|
+
precede succeed capture_haml haml_concat haml_internal_concat haml_indent
|
11
|
+
escape_once].each do |name|
|
12
|
+
base.send(:alias_method, "#{name}_without_haml_xss", name)
|
13
|
+
base.send(:alias_method, name, "#{name}_with_haml_xss")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Don't escape text that's already safe,
|
18
|
+
# output is always HTML safe
|
19
|
+
def html_escape_with_haml_xss(text)
|
20
|
+
str = text.to_s
|
21
|
+
return text if str.html_safe?
|
22
|
+
::Hamlit::HamlUtil.html_safe(html_escape_without_haml_xss(str))
|
23
|
+
end
|
24
|
+
|
25
|
+
# Output is always HTML safe
|
26
|
+
def find_and_preserve_with_haml_xss(*args, &block)
|
27
|
+
::Hamlit::HamlUtil.html_safe(find_and_preserve_without_haml_xss(*args, &block))
|
28
|
+
end
|
29
|
+
|
30
|
+
# Output is always HTML safe
|
31
|
+
def preserve_with_haml_xss(*args, &block)
|
32
|
+
::Hamlit::HamlUtil.html_safe(preserve_without_haml_xss(*args, &block))
|
33
|
+
end
|
34
|
+
|
35
|
+
# Output is always HTML safe
|
36
|
+
def list_of_with_haml_xss(*args, &block)
|
37
|
+
::Hamlit::HamlUtil.html_safe(list_of_without_haml_xss(*args, &block))
|
38
|
+
end
|
39
|
+
|
40
|
+
# Input is escaped, output is always HTML safe
|
41
|
+
def surround_with_haml_xss(front, back = front, &block)
|
42
|
+
::Hamlit::HamlUtil.html_safe(
|
43
|
+
surround_without_haml_xss(
|
44
|
+
haml_xss_html_escape(front),
|
45
|
+
haml_xss_html_escape(back),
|
46
|
+
&block))
|
47
|
+
end
|
48
|
+
|
49
|
+
# Input is escaped, output is always HTML safe
|
50
|
+
def precede_with_haml_xss(str, &block)
|
51
|
+
::Hamlit::HamlUtil.html_safe(precede_without_haml_xss(haml_xss_html_escape(str), &block))
|
52
|
+
end
|
53
|
+
|
54
|
+
# Input is escaped, output is always HTML safe
|
55
|
+
def succeed_with_haml_xss(str, &block)
|
56
|
+
::Hamlit::HamlUtil.html_safe(succeed_without_haml_xss(haml_xss_html_escape(str), &block))
|
57
|
+
end
|
58
|
+
|
59
|
+
# Output is always HTML safe
|
60
|
+
def capture_haml_with_haml_xss(*args, &block)
|
61
|
+
::Hamlit::HamlUtil.html_safe(capture_haml_without_haml_xss(*args, &block))
|
62
|
+
end
|
63
|
+
|
64
|
+
# Input will be escaped unless this is in a `with_raw_haml_concat`
|
65
|
+
# block. See #Haml::Helpers::ActionViewExtensions#with_raw_haml_concat.
|
66
|
+
def haml_concat_with_haml_xss(text = "")
|
67
|
+
raw = instance_variable_defined?(:@_haml_concat_raw) ? @_haml_concat_raw : false
|
68
|
+
if raw
|
69
|
+
haml_internal_concat_raw text
|
70
|
+
else
|
71
|
+
haml_internal_concat text
|
72
|
+
end
|
73
|
+
ErrorReturn.new("haml_concat")
|
74
|
+
end
|
75
|
+
|
76
|
+
# Input is escaped
|
77
|
+
def haml_internal_concat_with_haml_xss(text="", newline=true, indent=true)
|
78
|
+
haml_internal_concat_without_haml_xss(haml_xss_html_escape(text), newline, indent)
|
79
|
+
end
|
80
|
+
private :haml_internal_concat_with_haml_xss
|
81
|
+
|
82
|
+
# Output is always HTML safe
|
83
|
+
def haml_indent_with_haml_xss
|
84
|
+
::Hamlit::HamlUtil.html_safe(haml_indent_without_haml_xss)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Output is always HTML safe
|
88
|
+
def escape_once_with_haml_xss(*args)
|
89
|
+
::Hamlit::HamlUtil.html_safe(escape_once_without_haml_xss(*args))
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
# Escapes the HTML in the text if and only if
|
95
|
+
# Rails XSS protection is enabled *and* the `:escape_html` option is set.
|
96
|
+
def haml_xss_html_escape(text)
|
97
|
+
return text unless ::Hamlit::HamlUtil.rails_xss_safe? && haml_buffer.options[:escape_html]
|
98
|
+
html_escape(text)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class ErrorReturn
|
103
|
+
# Any attempt to treat ErrorReturn as a string should cause it to blow up.
|
104
|
+
alias_method :html_safe, :to_s
|
105
|
+
alias_method :html_safe?, :to_s
|
106
|
+
alias_method :html_safe!, :to_s
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -1,12 +1,7 @@
|
|
1
|
-
#
|
2
|
-
# It is included by ActionView in initializer.
|
3
|
-
#
|
4
|
-
# NOTE: currently Hamlit::Helpers is enabled by default
|
5
|
-
# on only Rails environment.
|
6
|
-
# And currently this Hamlit::Helpers depends on
|
1
|
+
# Currently this Hamlit::Helpers depends on
|
7
2
|
# ActionView internal implementation. (not desired)
|
8
3
|
module Hamlit
|
9
|
-
module
|
4
|
+
module RailsHelpers
|
10
5
|
extend self
|
11
6
|
|
12
7
|
DEFAULT_PRESERVE_TAGS = %w[textarea pre code].freeze
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'temple'
|
2
|
+
require 'hamlit/engine'
|
3
|
+
require 'hamlit/rails_helpers'
|
4
|
+
require 'hamlit/parser/haml_helpers'
|
5
|
+
require 'hamlit/parser/haml_util'
|
6
|
+
|
7
|
+
module Hamlit
|
8
|
+
RailsTemplate = Temple::Templates::Rails.create(
|
9
|
+
Hamlit::Engine,
|
10
|
+
generator: Temple::Generators::RailsOutputBuffer,
|
11
|
+
register_as: :haml,
|
12
|
+
use_html_safe: true,
|
13
|
+
streaming: true,
|
14
|
+
)
|
15
|
+
|
16
|
+
# https://github.com/haml/haml/blob/4.0.7/lib/haml/template.rb
|
17
|
+
module HamlHelpers
|
18
|
+
require 'hamlit/parser/haml_xss_mods'
|
19
|
+
include Hamlit::HamlHelpers::XssMods
|
20
|
+
end
|
21
|
+
|
22
|
+
module HamlUtil
|
23
|
+
undef :rails_xss_safe? if defined? rails_xss_safe?
|
24
|
+
def rails_xss_safe?; true; end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Haml extends Haml::Helpers in ActionView each time.
|
29
|
+
# It costs much, so Hamlit includes a compatible module at first.
|
30
|
+
ActionView::Base.send :include, Hamlit::RailsHelpers
|
data/lib/hamlit/railtie.rb
CHANGED
@@ -1,20 +1,9 @@
|
|
1
1
|
require 'rails'
|
2
|
-
require 'temple'
|
3
2
|
|
4
3
|
module Hamlit
|
5
4
|
class Railtie < ::Rails::Railtie
|
6
5
|
initializer :hamlit do |app|
|
7
|
-
|
8
|
-
Hamlit::Engine,
|
9
|
-
generator: Temple::Generators::RailsOutputBuffer,
|
10
|
-
register_as: :haml,
|
11
|
-
escape_html: true,
|
12
|
-
streaming: true,
|
13
|
-
)
|
14
|
-
|
15
|
-
# Haml extends Haml::Helpers in ActionView each time.
|
16
|
-
# It costs much, so Hamlit includes a compatible module at first.
|
17
|
-
ActionView::Base.send :include, Hamlit::Helpers
|
6
|
+
require 'hamlit/rails_template'
|
18
7
|
end
|
19
8
|
end
|
20
9
|
end
|