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,24 @@
|
|
1
|
+
module Hamlit
|
2
|
+
class Filters
|
3
|
+
class TextBase < Base
|
4
|
+
def compile_text!(temple, node, prefix)
|
5
|
+
text = node.value[:text].rstrip.gsub(/^/, prefix)
|
6
|
+
if ::Hamlit::HamlUtil.contains_interpolation?(node.value[:text])
|
7
|
+
# original: Haml::Filters#compile
|
8
|
+
text = ::Hamlit::HamlUtil.slow_unescape_interpolation(text).gsub(/(\\+)n/) do |s|
|
9
|
+
escapes = $1.size
|
10
|
+
next s if escapes % 2 == 0
|
11
|
+
"#{'\\' * (escapes - 1)}\n"
|
12
|
+
end
|
13
|
+
text.prepend("\n")
|
14
|
+
temple << [:dynamic, text]
|
15
|
+
else
|
16
|
+
node.value[:text].split("\n").size.times do
|
17
|
+
temple << [:newline]
|
18
|
+
end
|
19
|
+
temple << [:static, text]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
|
3
|
+
module Hamlit
|
4
|
+
class Filters
|
5
|
+
class TiltBase < Base
|
6
|
+
def self.render(name, source, indent_width: 0)
|
7
|
+
text = ::Tilt["t.#{name}"].new { source }.render
|
8
|
+
return text if indent_width == 0
|
9
|
+
text.gsub!(/^/, ' '.freeze * indent_width)
|
10
|
+
end
|
11
|
+
|
12
|
+
def explicit_require?
|
13
|
+
Gem::Version.new(Tilt::VERSION) >= Gem::Version.new('2.0.0')
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def compile_with_tilt(node, name, indent_width: 0)
|
19
|
+
if ::Hamlit::HamlUtil.contains_interpolation?(node.value[:text])
|
20
|
+
dynamic_compile(node, name, indent_width: indent_width)
|
21
|
+
else
|
22
|
+
static_compile(node, name, indent_width: indent_width)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def static_compile(node, name, indent_width: 0)
|
27
|
+
temple = [:multi, [:static, TiltBase.render(name, node.value[:text], indent_width: indent_width)]]
|
28
|
+
node.value[:text].split("\n").size.times do
|
29
|
+
temple << [:newline]
|
30
|
+
end
|
31
|
+
temple
|
32
|
+
end
|
33
|
+
|
34
|
+
def dynamic_compile(node, name, indent_width: 0)
|
35
|
+
# original: Haml::Filters#compile
|
36
|
+
text = ::Hamlit::HamlUtil.slow_unescape_interpolation(node.value[:text]).gsub(/(\\+)n/) do |s|
|
37
|
+
escapes = $1.size
|
38
|
+
next s if escapes % 2 == 0
|
39
|
+
"#{'\\' * (escapes - 1)}\n"
|
40
|
+
end
|
41
|
+
text.prepend("\n").sub!(/\n"\z/, '"')
|
42
|
+
|
43
|
+
[:dynamic, "::Hamlit::Filters::TiltBase.render('#{name}', #{text}, indent_width: #{indent_width})"]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'hamlit/ruby_expression'
|
2
|
+
|
3
|
+
module Hamlit
|
4
|
+
class HashParser
|
5
|
+
class ParseSkip < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.parse(text)
|
9
|
+
self.new.parse(text)
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse(text)
|
13
|
+
exp = wrap_bracket(text)
|
14
|
+
return if RubyExpression.syntax_error?(exp)
|
15
|
+
|
16
|
+
hash = {}
|
17
|
+
tokens = Ripper.lex(exp)[1..-2] || []
|
18
|
+
each_attr(tokens) do |attr_tokens|
|
19
|
+
key = parse_key!(attr_tokens)
|
20
|
+
hash[key] = attr_tokens.map(&:last).join.strip
|
21
|
+
end
|
22
|
+
hash
|
23
|
+
rescue ParseSkip
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def wrap_bracket(text)
|
30
|
+
text = text.strip
|
31
|
+
return text if text[0] == '{'.freeze
|
32
|
+
'{' << text << '}'.freeze
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_key!(tokens)
|
36
|
+
_, type, str = tokens.shift
|
37
|
+
case type
|
38
|
+
when :on_sp
|
39
|
+
parse_key!(tokens)
|
40
|
+
when :on_label
|
41
|
+
str.tr(':'.freeze, ''.freeze)
|
42
|
+
when :on_symbeg
|
43
|
+
_, _, key = tokens.shift
|
44
|
+
assert_type!(tokens.shift, :on_tstring_end) if str != ':'.freeze
|
45
|
+
skip_until_hash_rocket!(tokens)
|
46
|
+
key
|
47
|
+
when :on_tstring_beg
|
48
|
+
_, _, key = tokens.shift
|
49
|
+
next_token = tokens.shift
|
50
|
+
unless next_token[1] == :on_label_end
|
51
|
+
assert_type!(next_token, :on_tstring_end)
|
52
|
+
skip_until_hash_rocket!(tokens)
|
53
|
+
end
|
54
|
+
key
|
55
|
+
else
|
56
|
+
raise ParseSkip
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def assert_type!(token, type)
|
61
|
+
raise ParseSkip if token[1] != type
|
62
|
+
end
|
63
|
+
|
64
|
+
def skip_until_hash_rocket!(tokens)
|
65
|
+
until tokens.empty?
|
66
|
+
_, type, str = tokens.shift
|
67
|
+
break if type == :on_op && str == '=>'.freeze
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def each_attr(tokens)
|
72
|
+
attr_tokens = []
|
73
|
+
array_open = 0
|
74
|
+
brace_open = 0
|
75
|
+
paren_open = 0
|
76
|
+
|
77
|
+
tokens.each do |token|
|
78
|
+
(row, col), type, str = token
|
79
|
+
case type
|
80
|
+
when :on_comma
|
81
|
+
if array_open == 0 && brace_open == 0 && paren_open == 0
|
82
|
+
yield(attr_tokens)
|
83
|
+
attr_tokens = []
|
84
|
+
next
|
85
|
+
end
|
86
|
+
when :on_lbracket
|
87
|
+
array_open += 1
|
88
|
+
when :on_rbracket
|
89
|
+
array_open -= 1
|
90
|
+
when :on_lbrace
|
91
|
+
brace_open += 1
|
92
|
+
when :on_rbrace
|
93
|
+
brace_open -= 1
|
94
|
+
when :on_lparen
|
95
|
+
paren_open += 1
|
96
|
+
when :on_rparen
|
97
|
+
paren_open -= 1
|
98
|
+
when :on_sp
|
99
|
+
next if attr_tokens.empty?
|
100
|
+
end
|
101
|
+
|
102
|
+
attr_tokens << token
|
103
|
+
end
|
104
|
+
yield(attr_tokens) unless attr_tokens.empty?
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/lib/hamlit/html.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
-
require 'hamlit/concerns/deprecation'
|
2
|
-
require 'temple/html/fast'
|
3
|
-
|
4
|
-
# This is created to be compatible with Haml's ugly mode.
|
5
|
-
# Currently pretty mode is not supported.
|
6
1
|
module Hamlit
|
7
2
|
class HTML < Temple::HTML::Fast
|
8
|
-
|
3
|
+
DEPRECATED_FORMATS = %i[html4 html5].freeze
|
4
|
+
|
5
|
+
def initialize(opts = {})
|
6
|
+
if DEPRECATED_FORMATS.include?(opts[:format])
|
7
|
+
opts = opts.dup
|
8
|
+
opts[:format] = :html
|
9
|
+
end
|
10
|
+
super(opts)
|
11
|
+
end
|
9
12
|
end
|
10
13
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Hamlit
|
2
|
+
module ObjectRef
|
3
|
+
class << self
|
4
|
+
def parse(args)
|
5
|
+
object, prefix = args
|
6
|
+
return {} unless object
|
7
|
+
|
8
|
+
suffix = underscore(object.class)
|
9
|
+
{
|
10
|
+
'class' => [prefix, suffix].compact.join('_'),
|
11
|
+
'id' => [prefix, suffix, object.id || 'new'].compact.join('_'),
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# Haml::Buffer.underscore
|
18
|
+
def underscore(camel_cased_word)
|
19
|
+
word = camel_cased_word.to_s.dup
|
20
|
+
word.gsub!(/::/, '_')
|
21
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
22
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
23
|
+
word.tr!('-', '_')
|
24
|
+
word.downcase!
|
25
|
+
word
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/hamlit/parser.rb
CHANGED
@@ -1,150 +1,33 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'hamlit/parsers/attribute'
|
4
|
-
require 'hamlit/parsers/comment'
|
5
|
-
require 'hamlit/parsers/doctype'
|
6
|
-
require 'hamlit/parsers/filter'
|
7
|
-
require 'hamlit/parsers/multiline'
|
8
|
-
require 'hamlit/parsers/script'
|
9
|
-
require 'hamlit/parsers/tag'
|
10
|
-
require 'hamlit/parsers/text'
|
1
|
+
# Hamlit::Parser uses original Haml::Parser to generate Haml AST.
|
2
|
+
# hamlit/parser/haml_* are modules originally in haml gem.
|
11
3
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
include Parsers::Script
|
20
|
-
include Parsers::Tag
|
21
|
-
include Parsers::Text
|
22
|
-
|
23
|
-
SKIP_NEWLINE_EXPS = %i[newline code multi].freeze
|
24
|
-
SKIP_NEWLINE_FILTERS = %w[ruby markdown erb].freeze
|
25
|
-
|
26
|
-
define_options :format
|
27
|
-
|
28
|
-
def call(template)
|
29
|
-
reset(template)
|
30
|
-
|
31
|
-
ast = [:multi]
|
32
|
-
ast += parse_lines
|
33
|
-
ast
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
# Reset the parser state.
|
39
|
-
def reset(template)
|
40
|
-
validate_indentation_consistency!(template)
|
41
|
-
template = preprocess_multilines(template)
|
42
|
-
|
43
|
-
reset_lines(template.split("\n"))
|
44
|
-
reset_indent
|
45
|
-
reset_outer_removal
|
46
|
-
end
|
47
|
-
|
48
|
-
# Parse lines in current_indent and return ASTs.
|
49
|
-
def parse_lines
|
50
|
-
ast = []
|
51
|
-
loop do
|
52
|
-
break if validate_indentation!(ast)
|
53
|
-
|
54
|
-
@current_lineno += 1
|
55
|
-
node = parse_line(current_line)
|
56
|
-
remove_last_space!(ast) if outer_remove?
|
57
|
-
|
58
|
-
ast << [:newline] if @current_indent > 0
|
59
|
-
ast << node
|
60
|
-
ast << [:newline] if @current_indent == 0
|
61
|
-
ast << [:static, "\n"] unless skip_newline?(node)
|
62
|
-
end
|
63
|
-
ast
|
64
|
-
rescue => e
|
65
|
-
ast << [:newline]
|
66
|
-
ast << syntax_error(e.message)
|
67
|
-
ast
|
68
|
-
end
|
69
|
-
|
70
|
-
# Parse current line and return AST.
|
71
|
-
def parse_line(line, inline: false)
|
72
|
-
return [:multi] if empty_line?(line)
|
73
|
-
|
74
|
-
scanner = wrap_scanner(line)
|
75
|
-
unless inline
|
76
|
-
scanner.scan(/[ \t]+/)
|
77
|
-
ast = parse_outer_line(scanner)
|
78
|
-
return ast if ast
|
79
|
-
end
|
4
|
+
require 'hamlit/parser/haml_error'
|
5
|
+
require 'hamlit/parser/haml_util'
|
6
|
+
require 'hamlit/parser/haml_buffer'
|
7
|
+
require 'hamlit/parser/haml_compiler'
|
8
|
+
require 'hamlit/parser/haml_parser'
|
9
|
+
require 'hamlit/parser/haml_helpers'
|
10
|
+
require 'hamlit/parser/haml_options'
|
80
11
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
when '\\'
|
94
|
-
parse_text(scanner, scan: /\\/)
|
95
|
-
when '/'
|
96
|
-
parse_comment(scanner)
|
97
|
-
when '-'
|
98
|
-
parse_silent_script(scanner)
|
99
|
-
when ':'
|
100
|
-
parse_filter(scanner)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Parse a line and return ast which is acceptable inside an inline tag
|
105
|
-
def parse_inner_line(scanner, inline: false)
|
106
|
-
return parse_text(scanner, lstrip: true) if scanner.scan(/==/)
|
107
|
-
return parse_text(scanner, lstrip: true, escape: false) if scanner.scan(/! |!==/)
|
108
|
-
return parse_text(scanner, lstrip: true, escape: true) if scanner.scan(/& |&==/)
|
109
|
-
return parse_script(scanner, disable_escape: true) if scanner.match?(/!=/)
|
110
|
-
return parse_script(scanner, force_escape: true) if scanner.match?(/&=/)
|
111
|
-
|
112
|
-
if inline
|
113
|
-
return parse_text(scanner, lstrip: true, escape: false) if scanner.scan(/!/)
|
114
|
-
return parse_text(scanner, lstrip: true, escape: true) if scanner.scan(/&/)
|
115
|
-
end
|
116
|
-
|
117
|
-
case scanner.peek(1)
|
118
|
-
when '=', '~'
|
119
|
-
parse_script(scanner)
|
120
|
-
else
|
121
|
-
parse_text(scanner, lstrip: true, inline: inline)
|
12
|
+
module Hamlit
|
13
|
+
class Parser
|
14
|
+
AVAILABLE_OPTIONS = %i[
|
15
|
+
autoclose
|
16
|
+
escape_html
|
17
|
+
escape_attrs
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
def initialize(options = {})
|
21
|
+
@options = HamlOptions.defaults.dup
|
22
|
+
AVAILABLE_OPTIONS.each do |key|
|
23
|
+
@options[key] = options[key]
|
122
24
|
end
|
123
25
|
end
|
124
26
|
|
125
|
-
def
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
block_script?(ast) ||
|
130
|
-
outer_remove?
|
131
|
-
end
|
132
|
-
|
133
|
-
def newline_skip_filter?(ast)
|
134
|
-
ast[0..1] == [:haml, :filter] && SKIP_NEWLINE_FILTERS.include?(ast[2])
|
135
|
-
end
|
136
|
-
|
137
|
-
def wrap_scanner(str)
|
138
|
-
return str if str.is_a?(StringScanner)
|
139
|
-
StringScanner.new(str)
|
140
|
-
end
|
141
|
-
|
142
|
-
def doctype?(ast)
|
143
|
-
ast[0..1] == [:haml, :doctype]
|
144
|
-
end
|
145
|
-
|
146
|
-
def block_script?(ast)
|
147
|
-
ast[0..1] == [:haml, :script]
|
27
|
+
def call(template)
|
28
|
+
HamlParser.new(template, HamlOptions.new(@options)).parse
|
29
|
+
rescue ::Hamlit::HamlError => e
|
30
|
+
e
|
148
31
|
end
|
149
32
|
end
|
150
33
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006-2009 Hampton Catlin and Natalie Weizenbaum
|
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.
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# lib/hamlit/parser/haml\_\*.rb
|
2
|
+
|
3
|
+
Hamlit::HamlFoo is originally Haml::Foo in haml gem.
|
4
|
+
|
5
|
+
## License
|
6
|
+
|
7
|
+
lib/hamlit/parser/haml\_\*.rb is:
|
8
|
+
|
9
|
+
Copyright (c) 2006-2009 Hampton Catlin and Natalie Weizenbaum
|
10
|
+
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
12
|
+
a copy of this software and associated documentation files (the
|
13
|
+
"Software"), to deal in the Software without restriction, including
|
14
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
15
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
16
|
+
permit persons to whom the Software is furnished to do so, subject to
|
17
|
+
the following conditions:
|
18
|
+
|
19
|
+
The above copyright notice and this permission notice shall be
|
20
|
+
included in all copies or substantial portions of the Software.
|
21
|
+
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
23
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
24
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
25
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
26
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
27
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
28
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|