hamlit 2.10.0 → 2.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.github/workflows/bench.yml +46 -0
  4. data/.github/workflows/test.yml +40 -0
  5. data/.gitignore +2 -1
  6. data/CHANGELOG.md +140 -1
  7. data/Gemfile +1 -7
  8. data/LICENSE.txt +26 -23
  9. data/README.md +9 -9
  10. data/REFERENCE.md +13 -4
  11. data/bin/bench +2 -2
  12. data/bin/update-haml +125 -0
  13. data/ext/hamlit/hamlit.c +28 -46
  14. data/hamlit.gemspec +2 -2
  15. data/lib/hamlit/ambles.rb +20 -0
  16. data/lib/hamlit/attribute_builder.rb +5 -5
  17. data/lib/hamlit/attribute_compiler.rb +7 -4
  18. data/lib/hamlit/cli.rb +35 -10
  19. data/lib/hamlit/compiler/children_compiler.rb +18 -4
  20. data/lib/hamlit/compiler/comment_compiler.rb +8 -5
  21. data/lib/hamlit/engine.rb +3 -0
  22. data/lib/hamlit/filters/escaped.rb +1 -1
  23. data/lib/hamlit/filters/markdown.rb +1 -0
  24. data/lib/hamlit/filters/preserve.rb +1 -1
  25. data/lib/hamlit/filters/text_base.rb +1 -1
  26. data/lib/hamlit/filters/tilt_base.rb +1 -1
  27. data/lib/hamlit/html.rb +8 -0
  28. data/lib/hamlit/parser/haml_attribute_builder.rb +164 -0
  29. data/lib/hamlit/parser/haml_buffer.rb +20 -130
  30. data/lib/hamlit/parser/haml_compiler.rb +1 -553
  31. data/lib/hamlit/parser/haml_error.rb +29 -25
  32. data/lib/hamlit/parser/haml_escapable.rb +1 -0
  33. data/lib/hamlit/parser/haml_generator.rb +1 -0
  34. data/lib/hamlit/parser/{haml_xss_mods.rb → haml_helpers/xss_mods.rb} +20 -15
  35. data/lib/hamlit/parser/haml_helpers.rb +41 -59
  36. data/lib/hamlit/parser/haml_options.rb +53 -66
  37. data/lib/hamlit/parser/haml_parser.rb +133 -73
  38. data/lib/hamlit/parser/haml_temple_engine.rb +123 -0
  39. data/lib/hamlit/parser/haml_util.rb +10 -40
  40. data/lib/hamlit/parser.rb +8 -2
  41. data/lib/hamlit/rails_template.rb +6 -1
  42. data/lib/hamlit/railtie.rb +6 -1
  43. data/lib/hamlit/string_splitter.rb +1 -0
  44. data/lib/hamlit/template.rb +1 -1
  45. data/lib/hamlit/temple_line_counter.rb +31 -0
  46. data/lib/hamlit/version.rb +1 -1
  47. data/lib/hamlit.rb +6 -4
  48. metadata +19 -44
  49. data/.travis.yml +0 -47
  50. data/benchmark/boolean_attribute.haml +0 -6
  51. data/benchmark/class_attribute.haml +0 -5
  52. data/benchmark/common_attribute.haml +0 -3
  53. data/benchmark/data_attribute.haml +0 -4
  54. data/benchmark/dynamic_attributes/boolean_attribute.haml +0 -4
  55. data/benchmark/dynamic_attributes/class_attribute.haml +0 -4
  56. data/benchmark/dynamic_attributes/common_attribute.haml +0 -2
  57. data/benchmark/dynamic_attributes/data_attribute.haml +0 -2
  58. data/benchmark/dynamic_attributes/id_attribute.haml +0 -2
  59. data/benchmark/dynamic_boolean_attribute.haml +0 -4
  60. data/benchmark/dynamic_merger/benchmark.rb +0 -25
  61. data/benchmark/dynamic_merger/hello.haml +0 -50
  62. data/benchmark/dynamic_merger/hello.string +0 -50
  63. data/benchmark/etc/attribute_builder.haml +0 -5
  64. data/benchmark/etc/real_sample.haml +0 -888
  65. data/benchmark/etc/real_sample.rb +0 -11
  66. data/benchmark/etc/static_analyzer.haml +0 -1
  67. data/benchmark/etc/string_interpolation.haml +0 -2
  68. data/benchmark/etc/tags.haml +0 -3
  69. data/benchmark/etc/tags_loop.haml +0 -2
  70. data/benchmark/ext/build_data.rb +0 -17
  71. data/benchmark/ext/build_id.rb +0 -13
  72. data/benchmark/id_attribute.haml +0 -3
  73. data/benchmark/plain.haml +0 -4
  74. data/benchmark/script.haml +0 -4
  75. data/benchmark/slim/LICENSE +0 -21
  76. data/benchmark/slim/context.rb +0 -11
  77. data/benchmark/slim/run-benchmarks.rb +0 -94
  78. data/benchmark/slim/view.erb +0 -23
  79. data/benchmark/slim/view.haml +0 -18
  80. data/benchmark/slim/view.slim +0 -17
  81. data/benchmark/utils/benchmark_ips_extension.rb +0 -43
  82. data/lib/hamlit/parser/MIT-LICENSE +0 -20
  83. data/lib/hamlit/parser/README.md +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 430b8a37db72797f5e809b12d83872c4d224fa61fea3c761b0f8862118b000c6
4
- data.tar.gz: e7a8a2766ae263f8081c2209df44813bf16f69d07edaf02c5dd2826dd6345b46
3
+ metadata.gz: de464e04c0b09e109a52ccae59ad85ee2a07875eaf903d8d0208d8b4d24b3d7d
4
+ data.tar.gz: a495ef4fc7e65d0e51a3538146a099d3c313b987fe11a03ef4da2468e2cabe5c
5
5
  SHA512:
6
- metadata.gz: aa28798199b8c7981fa690420f5a045e8e1619f459e48fead5e85b100697f7a0bbb2dae0dc016c5e847c6a0e41fc10fdcdfc06de6950dfbdc3f5d3dabaf9eb51
7
- data.tar.gz: b98674075aa2922d8bd76cb765a7f0d4df9f3b323387ddb2bb16c9eddbfa7db89ca1256b72c548c191b947669b530224c52cf743a81972677ed7f6fd123ca995
6
+ metadata.gz: 22f7bd667c39909360a6405768b71b30769d5ea3aeff02e067f48a05ee89c07dda6e2d5467eafb4f000233ece666704483e31e325bda0cb16e53cb88beb97668
7
+ data.tar.gz: 83d3434d5953eebc388eeebddbf10f76d07dece448cacb00f96bfe0da80b8488fbc311002f81cc455ccdb5586ad70a21fd76497927fe656b586e2bcfde2b3bc1
@@ -0,0 +1 @@
1
+ github: k0kubun
@@ -0,0 +1,46 @@
1
+ name: bench
2
+ on:
3
+ push:
4
+ branches:
5
+ - master
6
+ pull_request:
7
+ types:
8
+ - opened
9
+ - synchronize
10
+ - reopened
11
+ schedule:
12
+ - cron: "00 15 * * *" # 7:00 PST (-8), 8:00 PDT (-7)
13
+ jobs:
14
+ bench:
15
+ runs-on: ubuntu-latest
16
+ strategy:
17
+ fail-fast: false
18
+ matrix:
19
+ include:
20
+ - slim: 1
21
+ - template: 'benchmark/boolean_attribute.haml,benchmark/class_attribute.haml,benchmark/id_attribute.haml,benchmark/data_attribute.haml,benchmark/common_attribute.haml'
22
+ - template: 'benchmark/dynamic_attributes/boolean_attribute.haml,benchmark/dynamic_attributes/class_attribute.haml,benchmark/dynamic_attributes/id_attribute.haml,benchmark/dynamic_attributes/data_attribute.haml,benchmark/dynamic_attributes/common_attribute.haml'
23
+ - template: 'benchmark/etc/attribute_builder.haml'
24
+ - template: 'benchmark/etc/static_analyzer.haml'
25
+ - template: 'benchmark/etc/string_interpolation.haml'
26
+ - template: 'test/haml/templates/standard.haml'
27
+ compile: 1
28
+ steps:
29
+ - uses: actions/checkout@v2
30
+ - name: Set up Ruby
31
+ uses: ruby/setup-ruby@v1
32
+ with:
33
+ ruby-version: '3.0'
34
+ - uses: actions/cache@v2
35
+ with:
36
+ path: vendor/bundle
37
+ key: ${{ runner.os }}-${{ matrix.ruby }}-gems-${{ hashFiles('**/Gemfile.lock') }}
38
+ restore-keys: ${{ runner.os }}-gems-
39
+ - run: sudo apt-get update && sudo apt-get install -y nodejs libxslt-dev # nodejs for execjs, libxslt for TruffleRuby nokogiri
40
+ - name: bundle install
41
+ run: bundle config path vendor/bundle && bundle install -j$(nproc) --retry 3
42
+ - run: bundle exec rake bench
43
+ env:
44
+ SLIM_BENCH: ${{ matrix.slim }}
45
+ TEMPLATE: ${{ matrix.template }}
46
+ COMPILE: ${{ matrix.compile }}
@@ -0,0 +1,40 @@
1
+ name: test
2
+ on:
3
+ push:
4
+ branches:
5
+ - master
6
+ pull_request:
7
+ types:
8
+ - opened
9
+ - synchronize
10
+ - reopened
11
+ schedule:
12
+ - cron: "00 15 * * *" # 7:00 PST (-8), 8:00 PDT (-7)
13
+ jobs:
14
+ test:
15
+ runs-on: ubuntu-latest
16
+ strategy:
17
+ fail-fast: false
18
+ matrix:
19
+ ruby:
20
+ - '2.5'
21
+ - '2.6'
22
+ - '2.7'
23
+ - '3.0'
24
+ - jruby
25
+ - truffleruby-head
26
+ steps:
27
+ - uses: actions/checkout@v2
28
+ - name: Set up Ruby
29
+ uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby }}
32
+ - uses: actions/cache@v2
33
+ with:
34
+ path: vendor/bundle
35
+ key: ${{ runner.os }}-${{ matrix.ruby }}-gems-${{ hashFiles('**/Gemfile.lock') }}
36
+ restore-keys: ${{ runner.os }}-gems-
37
+ - run: sudo apt-get update && sudo apt-get install -y nodejs libxslt-dev # nodejs for execjs, libxslt for TruffleRuby nokogiri
38
+ - name: bundle install
39
+ run: bundle config path vendor/bundle && bundle install -j$(nproc) --retry 3
40
+ - run: bundle exec rake test
data/.gitignore CHANGED
@@ -7,11 +7,12 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /haml/
10
11
  .sass-cache
11
12
  .ruby-version
12
13
  *.bundle
13
14
  *.so
14
15
  *.su
15
- *.o
16
16
  *.a
17
+ *.o
17
18
  *.swp
data/CHANGELOG.md CHANGED
@@ -4,12 +4,151 @@ All notable changes to this project will be documented in this file. This
4
4
  project adheres to [Semantic Versioning](http://semver.org/). This change log is based upon
5
5
  [keep-a-changelog](https://github.com/olivierlacan/keep-a-changelog).
6
6
 
7
+ ## [2.16.0](https://github.com/k0kubun/hamlit/compare/v2.15.2...v2.16.0) - 2022-02-03
8
+
9
+ ### Added
10
+
11
+ - Raise an exception on a Haml-level syntax error with `hamlit compile -c`
12
+ [#189](https://github.com/k0kubun/hamlit/issues/189)
13
+ *Thanks to @dlwr*
14
+
15
+ ## [2.15.2](https://github.com/k0kubun/hamlit/compare/v2.15.1...v2.15.2) - 2022-01-04
16
+
17
+ ### Fixed
18
+
19
+ - Consider `playsinline` a boolean attribute [#187](https://github.com/k0kubun/hamlit/issues/187)
20
+ *Thanks to @ghiculescu*
21
+
22
+ ## [2.15.1](https://github.com/k0kubun/hamlit/compare/v2.15.0...v2.15.1) - 2021-07-23
23
+
24
+ ### Fixed
25
+
26
+ - Remove `benchmark` from the gem package to reduce its size [#186](https://github.com/k0kubun/hamlit/issues/186)
27
+ *Thanks to @pocke*
28
+
29
+ ## [2.15.0](https://github.com/k0kubun/hamlit/compare/v2.14.6...v2.15.0) - 2021-04-12
30
+
31
+ ### Added
32
+
33
+ - Always use Hamlit when both haml.gem and hamlit.gem are installed in Rails
34
+ *Thanks to @igor-drozdov*
35
+
36
+ ## [2.14.6](https://github.com/k0kubun/hamlit/compare/v2.14.5...v2.14.6) - 2021-03-23
37
+
38
+ ### Fixed
39
+
40
+ - Optimize v2.14.5's implementation a little
41
+
42
+ ## [2.14.5](https://github.com/k0kubun/hamlit/compare/v2.14.4...v2.14.5) - 2021-03-23
43
+
44
+ ### Added
45
+
46
+ - Support `config.action_view.annotate_rendered_view_with_filenames = true` of Rails 6.1
47
+ *Thanks to @kirin121*
48
+
49
+ ## [2.14.4](https://github.com/k0kubun/hamlit/compare/v2.14.3...v2.14.4) - 2021-02-01
50
+
51
+ ### Fixed
52
+
53
+ - Prevent another SEGV in a C extension after `GC.compact` [#177](https://github.com/k0kubun/hamlit/issues/177)
54
+ *Thanks to @stanhu*
55
+
56
+ ## [2.14.3](https://github.com/k0kubun/hamlit/compare/v2.14.2...v2.14.3) - 2021-01-24
57
+
58
+ ### Fixed
59
+
60
+ - Ensure the Rails initializer is called before `:load_config_initializers` [#176](https://github.com/k0kubun/hamlit/issues/176)
61
+ *Thanks to @sunny*
62
+
63
+ ## [2.14.2](https://github.com/k0kubun/hamlit/compare/v2.14.1...v2.14.2) - 2021-01-21
64
+
65
+ ### Fixed
66
+
67
+ - Prevent SEGV in a C extension after `GC.compact` [#171](https://github.com/k0kubun/hamlit/issues/171)
68
+ *Thanks to @stanhu*
69
+
70
+ ## [2.14.1](https://github.com/k0kubun/hamlit/compare/v2.14.0...v2.14.1) - 2021-01-07
71
+
72
+ ### Added
73
+
74
+ - Add `-c` option to `hamlit compile` that works like `haml -c` [#166](https://github.com/k0kubun/hamlit/issues/166)
75
+ *Thanks to @knightq*
76
+
77
+ ## [2.14.0](https://github.com/k0kubun/hamlit/compare/v2.13.2...v2.14.0) - 2021-01-07
78
+
79
+ ### Changed
80
+
81
+ - CLI changes
82
+ - Remove `-c` shorthand of `--color`.
83
+ - Make `--color` default. Please use `--no-color` to disable it.
84
+ - `--color` uses IRB instead of Pry for syntax highlight.
85
+ - Syntax highlight of `hamlit compile` is enabled only with IRB of Ruby 2.7+.
86
+ - Syntax highlight of `hamlit parse` / `hamlit temple` is enabled only with IRB of Ruby 3.1+.
87
+
88
+ ## [2.13.2](https://github.com/k0kubun/hamlit/compare/v2.13.1...v2.13.2) - 2020-12-27
89
+
90
+ ### Added
91
+
92
+ - Speed up `hamlit` commands [#166](https://github.com/k0kubun/hamlit/issues/166)
93
+ *Thanks to @knightq*
94
+
95
+ ## [2.13.1](https://github.com/k0kubun/hamlit/compare/v2.13.0...v2.13.1) - 2020-12-27
96
+
97
+ ### Added
98
+
99
+ - Support [multiline attributes](https://github.com/haml/haml/pull/1043) of Haml 5.2.1.
100
+
101
+ ## [2.13.0](https://github.com/k0kubun/hamlit/compare/v2.12.0...v2.13.0) - 2020-10-02
102
+
103
+ ### Added
104
+
105
+ - Support `--enable-frozen-string-literal` [#162](https://github.com/k0kubun/hamlit/issues/162).
106
+ *Thanks to @aliismayilov*
107
+
108
+ ### Changed
109
+
110
+ - Upgrade the Haml parser from Haml 4.0 to 5.2 [#163](https://github.com/k0kubun/hamlit/issues/163).
111
+ - Allow `@` as tag's class name.
112
+ - Fix NameError on an `InvalidAttributeNameError` reference introduced at Hamlit v2.12.0.
113
+ - You can no longer specify `ugly` option, which has had no effect.
114
+
115
+ ## [2.12.0](https://github.com/k0kubun/hamlit/compare/v2.11.1...v2.12.0) - 2020-09-30
116
+
117
+ ### Changed
118
+
119
+ - Class names are no longer ordered alphabetically.
120
+ *Thanks to @aliismayilov*
121
+ - This is compatible with [Haml 5.2](https://github.com/haml/haml/blob/v5.2.0/CHANGELOG.md#52)
122
+
123
+ ## [2.11.1](https://github.com/k0kubun/hamlit/compare/v2.11.0...v2.11.1) - 2020-08-25
124
+
125
+ ### Fixed
126
+
127
+ - Fix a line number on an error after filters like preserve, plain, and ruby.
128
+ *Thanks to @rgisiger*
129
+
130
+ ## [2.11.0](https://github.com/k0kubun/hamlit/compare/v2.10.1...v2.11.0) - 2019-12-12
131
+
132
+ ### Added
133
+
134
+ - Support Haml's _revealed_ conditional comment feature on `/![if !IE]` [#153](https://github.com/k0kubun/hamlit/issues/153).
135
+ *Thanks to @esb*
136
+
137
+ ## [2.10.1](https://github.com/k0kubun/hamlit/compare/v2.10.0...v2.10.1) - 2019-11-28
138
+
139
+ ### Added
140
+
141
+ - Register `Hamlit::Template` to Tilt as :hamlit as well, in addition to :haml
142
+
7
143
  ## [2.10.0](https://github.com/k0kubun/hamlit/compare/v2.9.5...v2.10.0) - 2019-09-15
8
144
 
9
145
  ### Added
10
146
 
11
- - Optimize template rendering by exploiting string interpolation [#146](https://github.com/k0kubun/hamlit/issues/146)
147
+ - Optimize template rendering by string interpolation [#146](https://github.com/k0kubun/hamlit/issues/146)
12
148
  - Exploiting pre-allocation of string interpolation introduced in Ruby 2.5 [ruby/ruby#1626](https://github.com/ruby/ruby/pull/1626)
149
+
150
+ ### Changed
151
+
13
152
  - Require temple.gem >= 0.8.2
14
153
 
15
154
  ## [2.9.5](https://github.com/k0kubun/hamlit/compare/v2.9.4...v2.9.5) - 2019-09-08
data/Gemfile CHANGED
@@ -8,10 +8,6 @@ end
8
8
  # Specify your gem's dependencies in hamlit.gemspec
9
9
  gemspec
10
10
 
11
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2.2')
12
- gem 'rack', '< 2'
13
- end
14
-
15
11
  gem 'benchmark-ips', '2.3.0'
16
12
  gem 'maxitest'
17
13
  gem 'pry'
@@ -22,9 +18,7 @@ else
22
18
  gem 'redcarpet'
23
19
 
24
20
  if RUBY_PLATFORM !~ /mswin|mingw/ && RUBY_ENGINE != 'truffleruby'
25
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7.0') # Travis cannot compile ruby.h with C++
26
- gem 'faml'
27
- end
21
+ gem 'faml'
28
22
  gem 'stackprof'
29
23
  end
30
24
  end
data/LICENSE.txt CHANGED
@@ -1,3 +1,29 @@
1
+ Copyright (c) 2006-2019 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.
21
+
22
+ ===
23
+
24
+ The above license is applied to lib/hamlit/parser/*.rb and test/haml/*.
25
+ Everything else is licensed under:
26
+
1
27
  The MIT License (MIT)
2
28
 
3
29
  Copyright (c) 2015 Takashi Kokubun
@@ -19,26 +45,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
45
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
46
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
47
  THE SOFTWARE.
22
-
23
- lib/hamlit/parser/*.rb and test/haml/* are:
24
-
25
- Copyright (c) 2006-2009 Hampton Catlin and Natalie Weizenbaum
26
-
27
- Permission is hereby granted, free of charge, to any person obtaining
28
- a copy of this software and associated documentation files (the
29
- "Software"), to deal in the Software without restriction, including
30
- without limitation the rights to use, copy, modify, merge, publish,
31
- distribute, sublicense, and/or sell copies of the Software, and to
32
- permit persons to whom the Software is furnished to do so, subject to
33
- the following conditions:
34
-
35
- The above copyright notice and this permission notice shall be
36
- included in all copies or substantial portions of the Software.
37
-
38
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
42
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
43
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
44
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Hamlit
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/hamlit.svg)](http://badge.fury.io/rb/hamlit)
4
- [![Build Status](https://travis-ci.org/k0kubun/hamlit.svg?branch=master)](https://travis-ci.org/k0kubun/hamlit)
4
+ [![test](https://github.com/k0kubun/hamlit/workflows/test/badge.svg)](https://github.com/k0kubun/hamlit/actions?query=workflow%3Atest)
5
5
 
6
6
  Hamlit is a high performance [Haml](https://github.com/haml/haml) implementation.
7
7
 
@@ -10,17 +10,17 @@ Hamlit is a high performance [Haml](https://github.com/haml/haml) implementation
10
10
  ### What is Hamlit?
11
11
  Hamlit is another implementation of [Haml](https://github.com/haml/haml).
12
12
  With some [limitations](REFERENCE.md#limitations) by design for performance,
13
- Hamlit is **2.39x times faster** than original haml gem in [this benchmark](benchmark/slim/run-benchmarks.rb),
14
- which is an HTML-escaped version of [slim-template/slim's one](https://github.com/slim-template/slim/blob/v3.0.8/benchmarks/run-benchmarks.rb) for fairness. ([Result on Travis](https://travis-ci.org/k0kubun/hamlit/jobs/236567391))
13
+ Hamlit is **1.94x times faster** than original haml gem in [this benchmark](benchmark/slim/run-benchmarks.rb),
14
+ which is an HTML-escaped version of [slim-template/slim's one](https://github.com/slim-template/slim/blob/4.1.0/benchmarks/run-benchmarks.rb) for fairness. ([Result on Travis](https://travis-ci.org/github/k0kubun/hamlit/jobs/732178446))
15
15
 
16
- <img src="https://i.gyazo.com/0f0c0362b6bd69f82715bec1d8caa191.png" width="600px" alt="Hamlit Benchmark"/>
16
+ <img src="https://raw.githubusercontent.com/k0kubun/hamlit/afcc2b36c4861c2f764baa09afd9530ca25eeafa/benchmark/graph/graph.png" width="600x" alt="Hamlit Benchmark" />
17
17
 
18
18
  ```
19
- hamlit v2.8.1: 131048.9 i/s
20
- erubi v1.6.0: 125445.4 i/s - 1.04x slower
21
- slim v3.0.8: 121390.4 i/s - 1.08x slower
22
- faml v0.8.1: 100750.5 i/s - 1.30x slower
23
- haml v5.0.1: 54882.6 i/s - 2.39x slower
19
+ hamlit v2.13.0: 247404.4 i/s
20
+ erubi v1.9.0: 244356.4 i/s - 1.01x slower
21
+ slim v4.1.0: 238254.3 i/s - 1.04x slower
22
+ faml v0.8.1: 197293.2 i/s - 1.25x slower
23
+ haml v5.2.0: 127834.4 i/s - 1.94x slower
24
24
  ```
25
25
 
26
26
  ### Why is Hamlit faster?
data/REFERENCE.md CHANGED
@@ -57,6 +57,7 @@ for full features in original implementation.
57
57
  - [x] :plain
58
58
  - [x] :preserve
59
59
  - [x] :ruby
60
+ - `haml_io` API is not supported. Use [hamlit-haml\_io.gem](https://github.com/hamlit/hamlit-haml_io) if you need.
60
61
  - [x] :sass
61
62
  - [x] :scss
62
63
  - [ ] :textile
@@ -121,10 +122,10 @@ and merging multiple ids results in concatenation by "\_".
121
122
  %div{ id: false }
122
123
 
123
124
  # Output
124
- <div id='foo_bar'></span>
125
- <div id='foo_bar'></span>
126
- <div id='foo_bar'></span>
127
- <div id=''></span>
125
+ <div id='foo_bar'></div>
126
+ <div id='foo_bar'></div>
127
+ <div id='foo_bar'></div>
128
+ <div id=''></div>
128
129
  ```
129
130
 
130
131
  ### class attribute
@@ -224,6 +225,14 @@ Hamlit::RailsTemplate.set_options attr_quote: '"'
224
225
  set :haml, { attr_quote: '"' }
225
226
  ```
226
227
 
228
+ ## Ruby module
229
+
230
+ `Hamlit::Template` is a module registered to `Tilt`. You can use it like:
231
+
232
+ ```rb
233
+ Hamlit::Template.new { "%strong Yay for HAML!" }.render
234
+ ```
235
+
227
236
  ## Creating a custom filter
228
237
 
229
238
  Currently it doesn't have filter registering interface compatible with Haml.
data/bin/bench CHANGED
@@ -43,7 +43,7 @@ class Bench < Thor
43
43
  object.instance_eval(File.read(ruby_file))
44
44
  end
45
45
 
46
- Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).def_method(object, :haml)
46
+ Haml::Engine.new(haml, escape_html: true, escape_attrs: true).def_method(object, :haml)
47
47
  object.instance_eval "def faml; #{Faml::Engine.new.call(haml)}; end"
48
48
  object.instance_eval "def hamlit; #{Hamlit::Engine.new.call(haml)}; end"
49
49
 
@@ -59,7 +59,7 @@ class Bench < Thor
59
59
  def code(file)
60
60
  haml = File.read(file)
61
61
  puts "#{?= * 49}\n Haml Source: #{file}\n#{?= * 49}"
62
- puts Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).precompiled
62
+ puts Haml::Engine.new(haml, escape_html: true, escape_attrs: true).precompiled
63
63
  puts "\n#{?= * 49}\n Faml Source: #{file}\n#{?= * 49}"
64
64
  puts Faml::Engine.new.call(haml)
65
65
  puts "\n#{?= * 49}\n Hamlit Source: #{file}\n#{?= * 49}"
data/bin/update-haml ADDED
@@ -0,0 +1,125 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ require 'tmpdir'
4
+
5
+ HAML_REPO = 'haml/haml'
6
+ HAML_VERSION = '5.2.1'
7
+
8
+ module GitHubFetcher
9
+ def self.fetch(repo, tag:, path:)
10
+ Dir.mktmpdir do |dir|
11
+ Dir.chdir(dir) do
12
+ url = "https://github.com/#{repo}/archive/#{tag}.tar.gz"
13
+ system("curl -L --fail --retry 3 --retry-delay 1 #{url} -o - | tar zxf -")
14
+ FileUtils.mv("#{File.basename(repo)}-#{tag.sub(/\Av/, '')}", path)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ class LicenseBuilder
21
+ DELIMITER = "\n==="
22
+
23
+ def initialize(haml_license:, hamlit_license:)
24
+ @haml_license = haml_license
25
+ @hamlit_license = hamlit_license
26
+ end
27
+
28
+ def build
29
+ license = [
30
+ File.read(@haml_license),
31
+ File.read(@hamlit_license).split(DELIMITER, 2).last,
32
+ ].join(DELIMITER)
33
+ File.write(@hamlit_license, license)
34
+ end
35
+ end
36
+
37
+ # Generate lib/hamlit/parser from haml
38
+ class HamlitParserBuilder
39
+ TARGET_FILES = [
40
+ 'attribute_builder.rb',
41
+ 'buffer.rb',
42
+ 'error.rb',
43
+ 'helpers.rb',
44
+ 'options.rb',
45
+ 'temple_engine.rb',
46
+ # TODO: make the upstream sharable first
47
+ # 'parser.rb',
48
+ 'util.rb',
49
+ 'helpers/xss_mods.rb',
50
+ ]
51
+
52
+ # Classes which are just referenced by Options and not really used by the parser
53
+ DUMMY_CLASSES = {
54
+ 'compiler.rb' => 'Compiler',
55
+ 'escapable.rb' => 'Escapable',
56
+ 'generator.rb' => 'Generator',
57
+ }
58
+
59
+ def initialize(haml:, hamlit_parser:)
60
+ @haml = haml
61
+ @hamlit_parser = hamlit_parser
62
+ end
63
+
64
+ def build
65
+ TARGET_FILES.each do |file|
66
+ src_path = File.join(@haml, file)
67
+ dest_path = File.join(@hamlit_parser, "haml_#{file}")
68
+
69
+ FileUtils.mkdir_p(File.dirname(dest_path))
70
+ FileUtils.cp(src_path, dest_path)
71
+
72
+ src = File.read(dest_path)
73
+ patch_source!(src, file: file)
74
+ File.write(dest_path, src)
75
+ end
76
+
77
+ DUMMY_CLASSES.each do |file, klass|
78
+ dest_path = File.join(@hamlit_parser, "haml_#{file}")
79
+ src = "class Hamlit::Haml#{klass}; end\n"
80
+ File.write(dest_path, src)
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def patch_source!(src, file:)
87
+ # Use Hamlit::HamlFoo instead of Haml::Foo
88
+ src.gsub!(/^module Haml\n((?: #[^\n]+\n)*) (module|class) ([^ ]+)/, "module Hamlit\n\\1 \\2 Haml\\3")
89
+ src.gsub!(/\bHaml::/, 'Hamlit::Haml')
90
+
91
+ # Prefix Haml to references without Haml::
92
+ src.gsub!(/\b(AttributeBuilder|Error|InvalidAttributeNameError|Options|Parser|SyntaxError)\./, 'Haml\0')
93
+ src.gsub!(/\brescue SyntaxError /, 'rescue HamlSyntaxError ')
94
+
95
+ # Hamlit should not rely on Haml
96
+ src.gsub!(/^require 'haml\/([^']+)'/, "require 'hamlit/parser/haml_\\1'")
97
+
98
+ case file
99
+ when 'error.rb'
100
+ src.gsub!(/^ class ([^ ]+) < ([^ ]+);/, ' class Haml\1 < Haml\2;')
101
+ when 'helpers.rb'
102
+ src.gsub!(/^ def is_haml\?\n false\n end/m) { |str| str.gsub(/^ /, ' # ') } # not needed for the parser
103
+ when 'options.rb'
104
+ src.gsub!(/\.is_a\?\(Options\)/, '.is_a?(HamlOptions)')
105
+ when 'temple_engine.rb'
106
+ src.gsub!(/\buse (Generator|Escapable)$/, 'use Haml\1')
107
+ end
108
+ end
109
+ end
110
+
111
+ FileUtils.rm_rf(haml = File.expand_path('../haml', __dir__))
112
+ GitHubFetcher.fetch(HAML_REPO, tag: HAML_VERSION, path: haml)
113
+
114
+ hamlit = File.expand_path('..', __dir__)
115
+ LicenseBuilder.new(
116
+ haml_license: File.join(haml, 'MIT-LICENSE'),
117
+ hamlit_license: File.join(hamlit, 'LICENSE.txt'),
118
+ ).build
119
+
120
+ hamlit_parser = File.join(hamlit, 'lib/hamlit/parser')
121
+ # TODO: FileUtils.rm_rf(hamlit_parser = File.join(hamlit, 'lib/hamlit/parser'))
122
+ HamlitParserBuilder.new(
123
+ haml: File.join(haml, 'lib/haml'),
124
+ hamlit_parser: hamlit_parser,
125
+ ).build