hamlit 2.9.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +45 -0
  4. data/CHANGELOG.md +666 -0
  5. data/Gemfile +28 -0
  6. data/LICENSE.txt +44 -0
  7. data/README.md +146 -0
  8. data/REFERENCE.md +266 -0
  9. data/Rakefile +117 -0
  10. data/benchmark/boolean_attribute.haml +6 -0
  11. data/benchmark/class_attribute.haml +5 -0
  12. data/benchmark/common_attribute.haml +3 -0
  13. data/benchmark/data_attribute.haml +4 -0
  14. data/benchmark/dynamic_attributes/boolean_attribute.haml +4 -0
  15. data/benchmark/dynamic_attributes/class_attribute.haml +4 -0
  16. data/benchmark/dynamic_attributes/common_attribute.haml +2 -0
  17. data/benchmark/dynamic_attributes/data_attribute.haml +2 -0
  18. data/benchmark/dynamic_attributes/id_attribute.haml +2 -0
  19. data/benchmark/dynamic_boolean_attribute.haml +4 -0
  20. data/benchmark/etc/attribute_builder.haml +5 -0
  21. data/benchmark/etc/real_sample.haml +888 -0
  22. data/benchmark/etc/real_sample.rb +11 -0
  23. data/benchmark/etc/static_analyzer.haml +1 -0
  24. data/benchmark/etc/string_interpolation.haml +2 -0
  25. data/benchmark/etc/tags.haml +3 -0
  26. data/benchmark/etc/tags_loop.haml +2 -0
  27. data/benchmark/ext/build_data.rb +17 -0
  28. data/benchmark/ext/build_id.rb +13 -0
  29. data/benchmark/id_attribute.haml +3 -0
  30. data/benchmark/plain.haml +4 -0
  31. data/benchmark/script.haml +4 -0
  32. data/benchmark/slim/LICENSE +21 -0
  33. data/benchmark/slim/context.rb +11 -0
  34. data/benchmark/slim/run-benchmarks.rb +94 -0
  35. data/benchmark/slim/view.erb +23 -0
  36. data/benchmark/slim/view.haml +18 -0
  37. data/benchmark/slim/view.slim +17 -0
  38. data/benchmark/utils/benchmark_ips_extension.rb +43 -0
  39. data/bin/bench +77 -0
  40. data/bin/console +11 -0
  41. data/bin/ruby +3 -0
  42. data/bin/setup +7 -0
  43. data/bin/stackprof +27 -0
  44. data/bin/test +24 -0
  45. data/exe/hamlit +6 -0
  46. data/ext/hamlit/extconf.rb +10 -0
  47. data/ext/hamlit/hamlit.c +553 -0
  48. data/ext/hamlit/hescape.c +108 -0
  49. data/ext/hamlit/hescape.h +20 -0
  50. data/hamlit.gemspec +45 -0
  51. data/lib/hamlit.rb +11 -0
  52. data/lib/hamlit/attribute_builder.rb +173 -0
  53. data/lib/hamlit/attribute_compiler.rb +123 -0
  54. data/lib/hamlit/attribute_parser.rb +110 -0
  55. data/lib/hamlit/cli.rb +130 -0
  56. data/lib/hamlit/compiler.rb +97 -0
  57. data/lib/hamlit/compiler/children_compiler.rb +112 -0
  58. data/lib/hamlit/compiler/comment_compiler.rb +36 -0
  59. data/lib/hamlit/compiler/doctype_compiler.rb +46 -0
  60. data/lib/hamlit/compiler/script_compiler.rb +101 -0
  61. data/lib/hamlit/compiler/silent_script_compiler.rb +24 -0
  62. data/lib/hamlit/compiler/tag_compiler.rb +74 -0
  63. data/lib/hamlit/engine.rb +37 -0
  64. data/lib/hamlit/error.rb +15 -0
  65. data/lib/hamlit/escapable.rb +13 -0
  66. data/lib/hamlit/filters.rb +75 -0
  67. data/lib/hamlit/filters/base.rb +12 -0
  68. data/lib/hamlit/filters/cdata.rb +20 -0
  69. data/lib/hamlit/filters/coffee.rb +17 -0
  70. data/lib/hamlit/filters/css.rb +33 -0
  71. data/lib/hamlit/filters/erb.rb +10 -0
  72. data/lib/hamlit/filters/escaped.rb +22 -0
  73. data/lib/hamlit/filters/javascript.rb +33 -0
  74. data/lib/hamlit/filters/less.rb +20 -0
  75. data/lib/hamlit/filters/markdown.rb +10 -0
  76. data/lib/hamlit/filters/plain.rb +29 -0
  77. data/lib/hamlit/filters/preserve.rb +22 -0
  78. data/lib/hamlit/filters/ruby.rb +10 -0
  79. data/lib/hamlit/filters/sass.rb +15 -0
  80. data/lib/hamlit/filters/scss.rb +15 -0
  81. data/lib/hamlit/filters/text_base.rb +25 -0
  82. data/lib/hamlit/filters/tilt_base.rb +49 -0
  83. data/lib/hamlit/force_escapable.rb +29 -0
  84. data/lib/hamlit/helpers.rb +15 -0
  85. data/lib/hamlit/html.rb +14 -0
  86. data/lib/hamlit/identity.rb +13 -0
  87. data/lib/hamlit/object_ref.rb +30 -0
  88. data/lib/hamlit/parser.rb +49 -0
  89. data/lib/hamlit/parser/MIT-LICENSE +20 -0
  90. data/lib/hamlit/parser/README.md +30 -0
  91. data/lib/hamlit/parser/haml_buffer.rb +348 -0
  92. data/lib/hamlit/parser/haml_compiler.rb +553 -0
  93. data/lib/hamlit/parser/haml_error.rb +61 -0
  94. data/lib/hamlit/parser/haml_helpers.rb +727 -0
  95. data/lib/hamlit/parser/haml_options.rb +286 -0
  96. data/lib/hamlit/parser/haml_parser.rb +800 -0
  97. data/lib/hamlit/parser/haml_util.rb +288 -0
  98. data/lib/hamlit/parser/haml_xss_mods.rb +109 -0
  99. data/lib/hamlit/rails_helpers.rb +51 -0
  100. data/lib/hamlit/rails_template.rb +58 -0
  101. data/lib/hamlit/railtie.rb +10 -0
  102. data/lib/hamlit/ruby_expression.rb +32 -0
  103. data/lib/hamlit/string_splitter.rb +88 -0
  104. data/lib/hamlit/template.rb +28 -0
  105. data/lib/hamlit/utils.rb +18 -0
  106. data/lib/hamlit/version.rb +4 -0
  107. metadata +360 -0
@@ -0,0 +1,11 @@
1
+ def render(*)
2
+ '<div class="render"></div>'
3
+ end
4
+
5
+ def link_to(a, b, *c)
6
+ "<a href='" << b << ">".freeze << a << '</div>'.freeze
7
+ end
8
+
9
+ def image_tag(*)
10
+ '<img src="https://github.com/favicon.ico" />'
11
+ end
@@ -0,0 +1 @@
1
+ #foo.bar{ data: { 'user' => { id: 1234, name: 'k0kubun' }, book_id: 5432 } }
@@ -0,0 +1,2 @@
1
+ - id = 12347
2
+ %a{ href: "https://example.com/users/#{id}" }= "id: #{id}"
@@ -0,0 +1,3 @@
1
+ %span hello
2
+ %div
3
+ world
@@ -0,0 +1,2 @@
1
+ - 100.times do
2
+ %span hello
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hamlit'
5
+ require 'faml'
6
+ require 'benchmark/ips'
7
+ require_relative '../utils/benchmark_ips_extension'
8
+
9
+ h = { 'user' => { id: 1234, name: 'k0kubun' }, book_id: 5432 }
10
+
11
+ Benchmark.ips do |x|
12
+ quote = "'"
13
+ faml_options = { data: h }
14
+ x.report("Faml::AB.build") { Faml::AttributeBuilder.build(quote, true, nil, faml_options) }
15
+ x.report("Hamlit.build_data") { Hamlit::AttributeBuilder.build_data(true, quote, h) }
16
+ x.compare!
17
+ end
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hamlit'
5
+ require 'faml'
6
+ require 'benchmark/ips'
7
+ require_relative '../utils/benchmark_ips_extension'
8
+
9
+ Benchmark.ips do |x|
10
+ x.report("Faml::AB.build") { Faml::AttributeBuilder.build("'", true, nil, {:id=>"book"}, id: %w[content active]) }
11
+ x.report("Hamlit::AB.build_id") { Hamlit::AttributeBuilder.build_id(true, "book", %w[content active]) }
12
+ x.compare!
13
+ end
@@ -0,0 +1,3 @@
1
+ #book{ id: 'content active' }
2
+ - id = %w[content active]
3
+ #book{ id: id }
@@ -0,0 +1,4 @@
1
+ - hello = 'world'
2
+ %span aaa#{hello}bbb
3
+ %span
4
+ aaa#{hello}bbb
@@ -0,0 +1,4 @@
1
+ - dynamic = 'dynamic'
2
+ = "#{ dynamic } script"
3
+ = "#{ 'static'} script"
4
+ = ['&', '"', "'", '<', '>']
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 - 2015 Slim Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,11 @@
1
+ class Context
2
+ def header
3
+ 'Colors'
4
+ end
5
+
6
+ def item
7
+ [ { name: 'red', current: true, url: '#red' },
8
+ { name: 'green', current: false, url: '#green' },
9
+ { name: 'blue', current: false, url: '#blue' } ]
10
+ end
11
+ end
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ =begin
4
+ The MIT License
5
+
6
+ Copyright (c) 2010 - 2015 Slim Team
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
25
+ =end
26
+
27
+ #
28
+ # Original: https://github.com/slim-template/slim/blob/v3.0.6/benchmarks/run-benchmarks.rb
29
+ #
30
+ # SlimBenchmarks with following modifications:
31
+ # 1. Skipping slow engines, tilt and parsing benches.
32
+ # 2. All Ruby script and attributes are escaped for fairness.
33
+ # 3. Faml and Hamlit are added.
34
+ #
35
+
36
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
37
+
38
+ require 'slim'
39
+ require 'context'
40
+
41
+ require 'benchmark/ips'
42
+ require 'tilt'
43
+ require 'erubi'
44
+ require 'erb'
45
+ require 'haml'
46
+ require 'faml'
47
+ require 'hamlit'
48
+
49
+ class SlimBenchmarks
50
+ def initialize(only_haml)
51
+ @only_haml = only_haml
52
+ @benches = []
53
+
54
+ @erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
55
+ @haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
56
+ @slim_code = File.read(File.dirname(__FILE__) + '/view.slim')
57
+
58
+ init_compiled_benches
59
+ end
60
+
61
+ def init_compiled_benches
62
+ context = Context.new
63
+
64
+ haml_ugly = Haml::Engine.new(@haml_code, format: :html5, escape_html: true)
65
+ haml_ugly.def_method(context, :run_haml_ugly)
66
+ context.instance_eval %{
67
+ def run_erubi; #{Erubi::Engine.new(@erb_code).src}; end
68
+ def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end
69
+ def run_faml; #{Faml::Engine.new.call @haml_code}; end
70
+ def run_hamlit; #{Hamlit::Engine.new.call @haml_code}; end
71
+ }
72
+
73
+ bench("erubi v#{Erubi::VERSION}") { context.run_erubi } unless @only_haml
74
+ bench("slim v#{Slim::VERSION}") { context.run_slim_ugly } unless @only_haml
75
+ bench("haml v#{Haml::VERSION}") { context.run_haml_ugly }
76
+ bench("faml v#{Faml::VERSION}") { context.run_faml }
77
+ bench("hamlit v#{Hamlit::VERSION}") { context.run_hamlit }
78
+ end
79
+
80
+ def run
81
+ Benchmark.ips do |x|
82
+ @benches.each do |name, block|
83
+ x.report(name.to_s, &block)
84
+ end
85
+ x.compare!
86
+ end
87
+ end
88
+
89
+ def bench(name, &block)
90
+ @benches.push([name, block])
91
+ end
92
+ end
93
+
94
+ SlimBenchmarks.new(ENV['ONLY_HAML'] == '1').run
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE HTML>
2
+
3
+ <html>
4
+ <head>
5
+ <title>Simple Benchmark</title>
6
+ </head>
7
+ <body>
8
+ <h1><%== header %></h1>
9
+ <% unless item.empty? %>
10
+ <ul>
11
+ <% for i in item %>
12
+ <% if i[:current] %>
13
+ <li><strong><%== i[:name] %></strong></li>
14
+ <% else %>
15
+ <li><a href="<%== i[:url] %>"><%== i[:name] %></a></li>
16
+ <% end %>
17
+ <% end %>
18
+ </ul>
19
+ <% else %>
20
+ <p>The list is empty.</p>
21
+ <% end %>
22
+ </body>
23
+ </html>
@@ -0,0 +1,18 @@
1
+ !!! html
2
+
3
+ %html
4
+ %head
5
+ %title Simple Benchmark
6
+ %body
7
+ %h1= header
8
+ - unless item.empty?
9
+ %ul
10
+ - for i in item
11
+ - if i[:current]
12
+ %li
13
+ %strong= i[:name]
14
+ - else
15
+ %li
16
+ %a{:href => i[:url]}= i[:name]
17
+ - else
18
+ %p The list is empty.
@@ -0,0 +1,17 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title Simple Benchmark
5
+ body
6
+ h1 = header
7
+ - unless item.empty?
8
+ ul
9
+ - for i in item
10
+ - if i[:current]
11
+ li
12
+ strong = i[:name]
13
+ - else
14
+ li
15
+ a href=i[:url] = i[:name]
16
+ - else
17
+ p The list is empty.
@@ -0,0 +1,43 @@
1
+ # Monkey patch to show milliseconds
2
+ module Benchmark
3
+ module IPS
4
+ class Report
5
+ module EntryExtension
6
+ def body
7
+ return super if Benchmark::IPS.options[:format] != :human
8
+
9
+ left = "%s i/s (%1.3fms)" % [Helpers.scale(ips), (1000.0 / ips)]
10
+ iters = Helpers.scale(@iterations)
11
+
12
+ if @show_total_time
13
+ left.ljust(20) + (" - %s in %10.6fs" % [iters, runtime])
14
+ else
15
+ left.ljust(20) + (" - %s" % iters)
16
+ end
17
+ end
18
+ end
19
+ Entry.prepend(EntryExtension)
20
+ end
21
+ end
22
+
23
+ module CompareExtension
24
+ def compare(*reports)
25
+ return if reports.size < 2
26
+
27
+ sorted = reports.sort_by(&:ips).reverse
28
+ best = sorted.shift
29
+ $stdout.puts "\nComparison:"
30
+ $stdout.printf "%20s: %10.1f i/s (%1.3fms)\n", best.label, best.ips, (1000.0 / best.ips)
31
+
32
+ sorted.each do |report|
33
+ name = report.label.to_s
34
+
35
+ x = (best.ips.to_f / report.ips.to_f)
36
+ $stdout.printf "%20s: %10.1f i/s (%1.3fms) - %.2fx slower\n", name, report.ips, (1000.0 / report.ips), x
37
+ end
38
+
39
+ $stdout.puts
40
+ end
41
+ end
42
+ extend CompareExtension
43
+ end
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hamlit'
5
+ require 'faml'
6
+ require 'thor'
7
+ require 'benchmark/ips'
8
+ require_relative '../benchmark/utils/benchmark_ips_extension'
9
+
10
+ class Bench < Thor
11
+ class_option :show_template, type: :boolean, aliases: ['-t']
12
+
13
+ desc 'bench HAML', 'Benchmark haml template'
14
+ option :compile, type: :boolean, aliases: ['-c']
15
+ option :show_code, type: :boolean, aliases: ['-s']
16
+ def bench(*files)
17
+ files.each { |file| render(file) }
18
+ files.each { |file| compile(file) if options[:compile] }
19
+ files.each { |file| code(file) if options[:show_code] }
20
+ end
21
+
22
+ desc 'compile HAML', 'Benchmark compilation'
23
+ def compile(file)
24
+ puts "#{?= * 49}\n Compilation: #{file}\n#{?= * 49}"
25
+ haml = File.read(file)
26
+
27
+ Benchmark.ips do |x|
28
+ x.report("haml v#{Haml::VERSION}") { Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).precompiled }
29
+ x.report("faml v#{Faml::VERSION}") { Faml::Engine.new.call(haml) }
30
+ x.report("hamlit v#{Hamlit::VERSION}") { Hamlit::Engine.new.call(haml) }
31
+ x.compare!
32
+ end
33
+ end
34
+
35
+ desc 'render HAML', 'Benchmark rendering'
36
+ def render(file)
37
+ puts "#{?= * 49}\n Rendering: #{file}\n#{?= * 49}"
38
+ haml = File.read(file)
39
+ puts haml + "\n" if options[:show_template]
40
+ object = Object.new
41
+ ruby_file = file.gsub(/\.haml\z/, '.rb')
42
+ if File.exist?(ruby_file)
43
+ object.instance_eval(File.read(ruby_file))
44
+ end
45
+
46
+ Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).def_method(object, :haml)
47
+ object.instance_eval "def faml; #{Faml::Engine.new.call(haml)}; end"
48
+ object.instance_eval "def hamlit; #{Hamlit::Engine.new.call(haml)}; end"
49
+
50
+ Benchmark.ips do |x|
51
+ x.report("haml v#{Haml::VERSION}") { object.haml }
52
+ x.report("faml v#{Faml::VERSION}") { object.faml }
53
+ x.report("hamlit v#{Hamlit::VERSION}") { object.hamlit }
54
+ x.compare!
55
+ end
56
+ end
57
+
58
+ desc 'code HAML', 'Show compiled code'
59
+ def code(file)
60
+ haml = File.read(file)
61
+ puts "#{?= * 49}\n Haml Source: #{file}\n#{?= * 49}"
62
+ puts Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).precompiled
63
+ puts "\n#{?= * 49}\n Faml Source: #{file}\n#{?= * 49}"
64
+ puts Faml::Engine.new.call(haml)
65
+ puts "\n#{?= * 49}\n Hamlit Source: #{file}\n#{?= * 49}"
66
+ puts Hamlit::Engine.new.call(haml)
67
+ end
68
+
69
+ private
70
+
71
+ def method_missing(*args)
72
+ return super if args.length > 1
73
+ render(args.first.to_s)
74
+ end
75
+ end
76
+
77
+ Bench.start
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hamlit'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ bundle exec ruby -Ilib:test -rtest_helper $@
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hamlit'
5
+ require 'stackprof'
6
+
7
+ def open_flamegraph(report)
8
+ temp = `mktemp /tmp/stackflame-XXXXXXXX`.strip
9
+ data_path = "#{temp}.js"
10
+ system("mv #{temp} #{data_path}")
11
+
12
+ File.open(data_path, 'w') do |f|
13
+ report.print_flamegraph(f)
14
+ end
15
+
16
+ viewer_path = File.join(`bundle show stackprof`.strip, 'lib/stackprof/flamegraph/viewer.html')
17
+ url = "file://#{viewer_path}?data=#{data_path}"
18
+ system(%Q[osascript -e 'open location "#{url}"'])
19
+ end
20
+
21
+ haml = File.read(ARGV.first)
22
+ StackProf.start(mode: :wall, interval: 1, raw: false)
23
+ Hamlit::Engine.new.call(haml)
24
+ StackProf.stop
25
+
26
+ report = StackProf::Report.new(StackProf.results)
27
+ report.print_text(false)