hamlit 1.7.2 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (283) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -3
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +25 -37
  5. data/CHANGELOG.md +18 -0
  6. data/Gemfile +16 -0
  7. data/LICENSE.txt +23 -2
  8. data/README.md +106 -48
  9. data/REFERENCE.md +222 -0
  10. data/Rakefile +77 -19
  11. data/benchmark/boolean_attribute.haml +6 -0
  12. data/benchmark/class_attribute.haml +5 -0
  13. data/benchmark/common_attribute.haml +3 -0
  14. data/benchmark/data_attribute.haml +4 -0
  15. data/benchmark/dynamic_attributes/boolean_attribute.haml +4 -0
  16. data/benchmark/dynamic_attributes/class_attribute.haml +4 -0
  17. data/benchmark/dynamic_attributes/common_attribute.haml +2 -0
  18. data/benchmark/dynamic_attributes/data_attribute.haml +2 -0
  19. data/benchmark/dynamic_attributes/id_attribute.haml +2 -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/tags.haml +3 -0
  25. data/benchmark/ext/build_data.rb +15 -0
  26. data/benchmark/ext/build_id.rb +13 -0
  27. data/benchmark/id_attribute.haml +3 -0
  28. data/benchmark/plain.haml +4 -0
  29. data/benchmark/script.haml +4 -0
  30. data/benchmark/slim/LICENSE +21 -0
  31. data/{benchmarks → benchmark/slim}/context.rb +2 -4
  32. data/benchmark/slim/run-benchmarks.rb +94 -0
  33. data/{benchmarks → benchmark/slim}/view.erb +3 -3
  34. data/{benchmarks → benchmark/slim}/view.haml +0 -0
  35. data/{benchmarks/view.escaped.slim → benchmark/slim/view.slim} +1 -1
  36. data/benchmark/string_interpolation.haml +2 -0
  37. data/benchmark/utils/benchmark_ips_extension.rb +43 -0
  38. data/bin/bench +85 -0
  39. data/bin/clone +14 -0
  40. data/bin/console +11 -0
  41. data/bin/lineprof +48 -0
  42. data/bin/ruby +3 -0
  43. data/bin/setup +7 -0
  44. data/bin/stackprof +27 -0
  45. data/{test → bin/test} +6 -10
  46. data/{bin → exe}/hamlit +0 -0
  47. data/ext/hamlit/extconf.rb +14 -0
  48. data/ext/hamlit/hamlit.c +512 -0
  49. data/ext/hamlit/houdini/.gitignore +3 -0
  50. data/ext/hamlit/houdini/COPYING +7 -0
  51. data/ext/hamlit/houdini/Makefile +79 -0
  52. data/ext/hamlit/houdini/README.md +59 -0
  53. data/ext/hamlit/houdini/buffer.c +249 -0
  54. data/ext/hamlit/houdini/buffer.h +113 -0
  55. data/ext/hamlit/houdini/houdini.h +46 -0
  56. data/ext/hamlit/houdini/houdini_href_e.c +115 -0
  57. data/ext/hamlit/houdini/houdini_html_e.c +90 -0
  58. data/ext/hamlit/houdini/houdini_html_u.c +122 -0
  59. data/ext/hamlit/houdini/houdini_js_e.c +90 -0
  60. data/ext/hamlit/houdini/houdini_js_u.c +60 -0
  61. data/ext/hamlit/houdini/houdini_uri_e.c +107 -0
  62. data/ext/hamlit/houdini/houdini_uri_u.c +68 -0
  63. data/ext/hamlit/houdini/houdini_xml_e.c +136 -0
  64. data/ext/hamlit/houdini/html_unescape.gperf +258 -0
  65. data/ext/hamlit/houdini/html_unescape.h +754 -0
  66. data/ext/hamlit/houdini/tools/build_table.py +13 -0
  67. data/ext/hamlit/houdini/tools/build_tables.c +51 -0
  68. data/ext/hamlit/houdini/tools/wikipedia_table.txt +2025 -0
  69. data/hamlit.gemspec +30 -31
  70. data/lib/hamlit.rb +3 -1
  71. data/lib/hamlit/attribute_builder.rb +12 -0
  72. data/lib/hamlit/cli.rb +44 -43
  73. data/lib/hamlit/compiler.rb +92 -16
  74. data/lib/hamlit/compiler/attribute_compiler.rb +148 -0
  75. data/lib/hamlit/compiler/children_compiler.rb +111 -0
  76. data/lib/hamlit/compiler/comment_compiler.rb +36 -0
  77. data/lib/hamlit/compiler/doctype_compiler.rb +45 -0
  78. data/lib/hamlit/compiler/script_compiler.rb +97 -0
  79. data/lib/hamlit/compiler/silent_script_compiler.rb +24 -0
  80. data/lib/hamlit/compiler/tag_compiler.rb +69 -0
  81. data/lib/hamlit/engine.rb +12 -7
  82. data/lib/hamlit/error.rb +14 -0
  83. data/lib/hamlit/escapable.rb +12 -0
  84. data/lib/hamlit/filters.rb +65 -0
  85. data/lib/hamlit/filters/base.rb +4 -62
  86. data/lib/hamlit/filters/coffee.rb +9 -7
  87. data/lib/hamlit/filters/css.rb +25 -8
  88. data/lib/hamlit/filters/erb.rb +4 -6
  89. data/lib/hamlit/filters/escaped.rb +11 -9
  90. data/lib/hamlit/filters/javascript.rb +25 -8
  91. data/lib/hamlit/filters/less.rb +9 -7
  92. data/lib/hamlit/filters/markdown.rb +5 -6
  93. data/lib/hamlit/filters/plain.rb +11 -15
  94. data/lib/hamlit/filters/preserve.rb +15 -5
  95. data/lib/hamlit/filters/ruby.rb +3 -5
  96. data/lib/hamlit/filters/sass.rb +9 -7
  97. data/lib/hamlit/filters/scss.rb +9 -7
  98. data/lib/hamlit/filters/text_base.rb +24 -0
  99. data/lib/hamlit/filters/tilt_base.rb +47 -0
  100. data/lib/hamlit/hash_parser.rb +107 -0
  101. data/lib/hamlit/html.rb +9 -6
  102. data/lib/hamlit/identity.rb +12 -0
  103. data/lib/hamlit/object_ref.rb +29 -0
  104. data/lib/hamlit/parser.rb +25 -142
  105. data/lib/hamlit/parser/MIT-LICENSE +20 -0
  106. data/lib/hamlit/parser/README.md +28 -0
  107. data/lib/hamlit/parser/haml_buffer.rb +348 -0
  108. data/lib/hamlit/parser/haml_compiler.rb +553 -0
  109. data/lib/hamlit/parser/haml_error.rb +61 -0
  110. data/lib/hamlit/parser/haml_helpers.rb +727 -0
  111. data/lib/hamlit/parser/haml_options.rb +286 -0
  112. data/lib/hamlit/parser/haml_parser.rb +801 -0
  113. data/lib/hamlit/parser/haml_util.rb +283 -0
  114. data/lib/hamlit/parser/haml_xss_mods.rb +109 -0
  115. data/lib/hamlit/{helpers.rb → rails_helpers.rb} +2 -7
  116. data/lib/hamlit/rails_template.rb +30 -0
  117. data/lib/hamlit/railtie.rb +1 -12
  118. data/lib/hamlit/ruby_expression.rb +31 -0
  119. data/lib/hamlit/static_analyzer.rb +49 -0
  120. data/lib/hamlit/string_interpolation.rb +69 -0
  121. data/lib/hamlit/template.rb +8 -0
  122. data/lib/hamlit/utils.rb +9 -0
  123. data/lib/hamlit/version.rb +1 -1
  124. metadata +116 -324
  125. data/.rspec +0 -2
  126. data/benchmarks/benchmark.rb +0 -110
  127. data/benchmarks/view.slim +0 -17
  128. data/doc/README.md +0 -19
  129. data/doc/engine/indent.md +0 -48
  130. data/doc/engine/new_attribute.md +0 -77
  131. data/doc/engine/old_attributes.md +0 -198
  132. data/doc/engine/silent_script.md +0 -97
  133. data/doc/engine/tag.md +0 -48
  134. data/doc/engine/text.md +0 -64
  135. data/doc/faml/README.md +0 -16
  136. data/doc/faml/engine/indent.md +0 -48
  137. data/doc/faml/engine/old_attributes.md +0 -111
  138. data/doc/faml/engine/silent_script.md +0 -97
  139. data/doc/faml/engine/text.md +0 -59
  140. data/doc/faml/filters/erb.md +0 -24
  141. data/doc/faml/filters/javascript.md +0 -27
  142. data/doc/faml/filters/less.md +0 -57
  143. data/doc/faml/filters/plain.md +0 -25
  144. data/doc/filters/erb.md +0 -31
  145. data/doc/filters/javascript.md +0 -83
  146. data/doc/filters/less.md +0 -57
  147. data/doc/filters/markdown.md +0 -31
  148. data/doc/filters/plain.md +0 -25
  149. data/doc/haml/README.md +0 -15
  150. data/doc/haml/engine/new_attribute.md +0 -77
  151. data/doc/haml/engine/old_attributes.md +0 -142
  152. data/doc/haml/engine/tag.md +0 -48
  153. data/doc/haml/engine/text.md +0 -29
  154. data/doc/haml/filters/erb.md +0 -26
  155. data/doc/haml/filters/javascript.md +0 -76
  156. data/doc/haml/filters/markdown.md +0 -31
  157. data/lib/hamlit/attribute.rb +0 -78
  158. data/lib/hamlit/compilers/attributes.rb +0 -108
  159. data/lib/hamlit/compilers/comment.rb +0 -13
  160. data/lib/hamlit/compilers/doctype.rb +0 -39
  161. data/lib/hamlit/compilers/filter.rb +0 -53
  162. data/lib/hamlit/compilers/new_attribute.rb +0 -115
  163. data/lib/hamlit/compilers/old_attribute.rb +0 -241
  164. data/lib/hamlit/compilers/runtime_attribute.rb +0 -58
  165. data/lib/hamlit/compilers/script.rb +0 -31
  166. data/lib/hamlit/compilers/strip.rb +0 -19
  167. data/lib/hamlit/compilers/text.rb +0 -111
  168. data/lib/hamlit/concerns/attribute_builder.rb +0 -22
  169. data/lib/hamlit/concerns/balanceable.rb +0 -68
  170. data/lib/hamlit/concerns/deprecation.rb +0 -20
  171. data/lib/hamlit/concerns/error.rb +0 -31
  172. data/lib/hamlit/concerns/escapable.rb +0 -17
  173. data/lib/hamlit/concerns/included.rb +0 -28
  174. data/lib/hamlit/concerns/indentable.rb +0 -117
  175. data/lib/hamlit/concerns/lexable.rb +0 -32
  176. data/lib/hamlit/concerns/line_reader.rb +0 -62
  177. data/lib/hamlit/concerns/registerable.rb +0 -24
  178. data/lib/hamlit/concerns/string_interpolation.rb +0 -48
  179. data/lib/hamlit/concerns/whitespace.rb +0 -91
  180. data/lib/hamlit/filters/tilt.rb +0 -41
  181. data/lib/hamlit/parsers/attribute.rb +0 -71
  182. data/lib/hamlit/parsers/comment.rb +0 -30
  183. data/lib/hamlit/parsers/doctype.rb +0 -18
  184. data/lib/hamlit/parsers/filter.rb +0 -18
  185. data/lib/hamlit/parsers/multiline.rb +0 -58
  186. data/lib/hamlit/parsers/script.rb +0 -126
  187. data/lib/hamlit/parsers/tag.rb +0 -83
  188. data/lib/hamlit/parsers/text.rb +0 -28
  189. data/lib/hamlit/temple.rb +0 -9
  190. data/release +0 -6
  191. data/spec/Rakefile +0 -72
  192. data/spec/hamlit/engine/comment_spec.rb +0 -56
  193. data/spec/hamlit/engine/doctype_spec.rb +0 -19
  194. data/spec/hamlit/engine/error_spec.rb +0 -135
  195. data/spec/hamlit/engine/indent_spec.rb +0 -42
  196. data/spec/hamlit/engine/multiline_spec.rb +0 -44
  197. data/spec/hamlit/engine/new_attribute_spec.rb +0 -110
  198. data/spec/hamlit/engine/old_attributes_spec.rb +0 -404
  199. data/spec/hamlit/engine/script_spec.rb +0 -116
  200. data/spec/hamlit/engine/silent_script_spec.rb +0 -213
  201. data/spec/hamlit/engine/tag_spec.rb +0 -295
  202. data/spec/hamlit/engine/text_spec.rb +0 -239
  203. data/spec/hamlit/engine_spec.rb +0 -58
  204. data/spec/hamlit/filters/coffee_spec.rb +0 -60
  205. data/spec/hamlit/filters/css_spec.rb +0 -33
  206. data/spec/hamlit/filters/erb_spec.rb +0 -16
  207. data/spec/hamlit/filters/javascript_spec.rb +0 -82
  208. data/spec/hamlit/filters/less_spec.rb +0 -37
  209. data/spec/hamlit/filters/markdown_spec.rb +0 -30
  210. data/spec/hamlit/filters/plain_spec.rb +0 -15
  211. data/spec/hamlit/filters/ruby_spec.rb +0 -24
  212. data/spec/hamlit/filters/sass_spec.rb +0 -33
  213. data/spec/hamlit/filters/scss_spec.rb +0 -37
  214. data/spec/hamlit/haml_spec.rb +0 -910
  215. data/spec/rails/.gitignore +0 -18
  216. data/spec/rails/.rspec +0 -2
  217. data/spec/rails/Gemfile +0 -19
  218. data/spec/rails/README.rdoc +0 -28
  219. data/spec/rails/Rakefile +0 -6
  220. data/spec/rails/app/assets/images/.keep +0 -0
  221. data/spec/rails/app/assets/javascripts/application.js +0 -15
  222. data/spec/rails/app/assets/stylesheets/application.css +0 -15
  223. data/spec/rails/app/controllers/application_controller.rb +0 -8
  224. data/spec/rails/app/controllers/concerns/.keep +0 -0
  225. data/spec/rails/app/controllers/users_controller.rb +0 -23
  226. data/spec/rails/app/helpers/application_helper.rb +0 -2
  227. data/spec/rails/app/mailers/.keep +0 -0
  228. data/spec/rails/app/models/.keep +0 -0
  229. data/spec/rails/app/models/concerns/.keep +0 -0
  230. data/spec/rails/app/views/application/index.html.haml +0 -18
  231. data/spec/rails/app/views/layouts/application.html.haml +0 -12
  232. data/spec/rails/app/views/users/capture.html.haml +0 -5
  233. data/spec/rails/app/views/users/capture_haml.html.haml +0 -5
  234. data/spec/rails/app/views/users/form.html.haml +0 -2
  235. data/spec/rails/app/views/users/helpers.html.haml +0 -10
  236. data/spec/rails/app/views/users/index.html.haml +0 -9
  237. data/spec/rails/app/views/users/inline.html.haml +0 -6
  238. data/spec/rails/app/views/users/old_attributes.html.haml +0 -5
  239. data/spec/rails/app/views/users/safe_buffer.html.haml +0 -4
  240. data/spec/rails/app/views/users/whitespace.html.haml +0 -4
  241. data/spec/rails/bin/bundle +0 -3
  242. data/spec/rails/bin/rails +0 -8
  243. data/spec/rails/bin/rake +0 -8
  244. data/spec/rails/bin/setup +0 -29
  245. data/spec/rails/bin/spring +0 -15
  246. data/spec/rails/config.ru +0 -4
  247. data/spec/rails/config/application.rb +0 -34
  248. data/spec/rails/config/boot.rb +0 -3
  249. data/spec/rails/config/database.yml +0 -25
  250. data/spec/rails/config/environment.rb +0 -5
  251. data/spec/rails/config/environments/development.rb +0 -41
  252. data/spec/rails/config/environments/production.rb +0 -79
  253. data/spec/rails/config/environments/test.rb +0 -42
  254. data/spec/rails/config/initializers/assets.rb +0 -11
  255. data/spec/rails/config/initializers/backtrace_silencers.rb +0 -7
  256. data/spec/rails/config/initializers/cookies_serializer.rb +0 -3
  257. data/spec/rails/config/initializers/filter_parameter_logging.rb +0 -4
  258. data/spec/rails/config/initializers/inflections.rb +0 -16
  259. data/spec/rails/config/initializers/mime_types.rb +0 -4
  260. data/spec/rails/config/initializers/session_store.rb +0 -3
  261. data/spec/rails/config/initializers/wrap_parameters.rb +0 -14
  262. data/spec/rails/config/locales/en.yml +0 -24
  263. data/spec/rails/config/routes.rb +0 -16
  264. data/spec/rails/config/secrets.yml +0 -22
  265. data/spec/rails/db/schema.rb +0 -16
  266. data/spec/rails/db/seeds.rb +0 -7
  267. data/spec/rails/lib/assets/.keep +0 -0
  268. data/spec/rails/lib/tasks/.keep +0 -0
  269. data/spec/rails/log/.keep +0 -0
  270. data/spec/rails/public/404.html +0 -67
  271. data/spec/rails/public/422.html +0 -67
  272. data/spec/rails/public/500.html +0 -66
  273. data/spec/rails/public/favicon.ico +0 -0
  274. data/spec/rails/public/robots.txt +0 -5
  275. data/spec/rails/spec/hamlit_spec.rb +0 -123
  276. data/spec/rails/spec/rails_helper.rb +0 -56
  277. data/spec/rails/spec/spec_helper.rb +0 -91
  278. data/spec/rails/vendor/assets/javascripts/.keep +0 -0
  279. data/spec/rails/vendor/assets/stylesheets/.keep +0 -0
  280. data/spec/spec_helper.rb +0 -36
  281. data/spec/spec_helper/document_generator.rb +0 -93
  282. data/spec/spec_helper/render_helper.rb +0 -120
  283. data/spec/spec_helper/test_case.rb +0 -55
@@ -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,3 @@
1
+ %span hello
2
+ %div
3
+ world
@@ -0,0 +1,15 @@
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
+ x.report("Faml::AB.build") { Faml::AttributeBuilder.build("'", true, nil, data: h) }
13
+ x.report("Hamlit.build_data") { Hamlit::AttributeBuilder.build_data(true, "'", h) }
14
+ x.compare!
15
+ 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.
@@ -4,10 +4,8 @@ class Context
4
4
  end
5
5
 
6
6
  def item
7
- [
8
- { name: 'red', current: true, url: '#red' },
7
+ [ { name: 'red', current: true, url: '#red' },
9
8
  { name: 'green', current: false, url: '#green' },
10
- { name: 'blue', current: false, url: '#blue' },
11
- ]
9
+ { name: 'blue', current: false, url: '#blue' } ]
12
10
  end
13
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 'erubis'
44
+ require 'erb'
45
+ require 'haml'
46
+ require 'faml'
47
+ require 'hamlit'
48
+
49
+ class SlimBenchmarks
50
+ def initialize(slow)
51
+ @benches = []
52
+
53
+ @erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
54
+ @haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
55
+ @slim_code = File.read(File.dirname(__FILE__) + '/view.slim')
56
+
57
+ init_compiled_benches
58
+ end
59
+
60
+ def init_compiled_benches
61
+ haml_ugly = Haml::Engine.new(@haml_code, format: :html5, ugly: true, escape_html: true)
62
+
63
+ context = Context.new
64
+
65
+ haml_ugly.def_method(context, :run_haml_ugly)
66
+ context.instance_eval %{
67
+ def run_erubis; #{Erubis::Eruby.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("erubis v#{Erubis::VERSION}") { context.run_erubis }
74
+ bench("slim v#{Slim::VERSION}") { context.run_slim_ugly }
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['slow']).run
@@ -5,14 +5,14 @@
5
5
  <title>Simple Benchmark</title>
6
6
  </head>
7
7
  <body>
8
- <h1><%= header %></h1>
8
+ <h1><%== header %></h1>
9
9
  <% unless item.empty? %>
10
10
  <ul>
11
11
  <% for i in item %>
12
12
  <% if i[:current] %>
13
- <li><strong><%= i[:name] %></strong></li>
13
+ <li><strong><%== i[:name] %></strong></li>
14
14
  <% else %>
15
- <li><a href="<%= i[:url] %>"><%= i[:name] %></a></li>
15
+ <li><a href="<%== i[:url] %>"><%== i[:name] %></a></li>
16
16
  <% end %>
17
17
  <% end %>
18
18
  </ul>
File without changes
@@ -12,6 +12,6 @@ html
12
12
  strong = i[:name]
13
13
  - else
14
14
  li
15
- a href==i[:url] = i[:name]
15
+ a href=i[:url] = i[:name]
16
16
  - else
17
17
  p The list is empty.
@@ -0,0 +1,2 @@
1
+ - id = 12347
2
+ %a{ href: "https://example.com/users/#{id}" }= "id: #{id}"
@@ -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.send(: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
data/bin/bench ADDED
@@ -0,0 +1,85 @@
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 'hamlit1' if File.exist?('hamlit1')
9
+ require_relative '../benchmark/utils/benchmark_ips_extension'
10
+
11
+ class Bench < Thor
12
+ class_option :show_template, type: :boolean, aliases: ['-t']
13
+
14
+ desc 'bench HAML', 'Benchmark haml template'
15
+ option :compile, type: :boolean, aliases: ['-c']
16
+ option :show_code, type: :boolean, aliases: ['-s']
17
+ def bench(*files)
18
+ files.each { |file| render(file) unless options[:compile] }
19
+ files.each { |file| compile(file) if options[:compile] }
20
+ files.each { |file| code(file) if options[:show_code] }
21
+ end
22
+
23
+ desc 'compile HAML', 'Benchmark compilation'
24
+ def compile(file)
25
+ puts "#{?= * 49}\n Compilation: #{file}\n#{?= * 49}"
26
+ haml = File.read(file)
27
+
28
+ Benchmark.ips do |x|
29
+ x.report("haml v#{Haml::VERSION}") { Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).precompiled }
30
+ x.report("faml v#{Faml::VERSION}") { Faml::Engine.new.call(haml) }
31
+ x.report("hamlit#{File.exist?('hamlit1') ? '2' : ''} v#{Hamlit::VERSION}") { Hamlit::Engine.new.call(haml) }
32
+ x.report("hamlit1 v#{Hamlit1::VERSION}") { Hamlit1::Engine.new.call(haml) } if File.exist?('hamlit1')
33
+ x.compare!
34
+ end
35
+ end
36
+
37
+ desc 'render HAML', 'Benchmark rendering'
38
+ def render(file)
39
+ puts "#{?= * 49}\n Rendering: #{file}\n#{?= * 49}"
40
+ haml = File.read(file)
41
+ puts haml + "\n" if options[:show_template]
42
+ object = Object.new
43
+ ruby_file = file.gsub(/\.haml\z/, '.rb')
44
+ if File.exist?(ruby_file)
45
+ object.instance_eval(File.read(ruby_file))
46
+ end
47
+
48
+ Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).def_method(object, :haml)
49
+ object.instance_eval "def faml; #{Faml::Engine.new.call(haml)}; end"
50
+ object.instance_eval "def hamlit; #{Hamlit::Engine.new.call(haml)}; end"
51
+ object.instance_eval "def hamlit1; #{Hamlit1::Engine.new.call(haml)}; end" if File.exist?('hamlit1')
52
+
53
+ Benchmark.ips do |x|
54
+ x.report("haml v#{Haml::VERSION}") { object.haml }
55
+ x.report("faml v#{Faml::VERSION}") { object.faml }
56
+ x.report("hamlit#{File.exist?('hamlit1') ? '2' : ''} v#{Hamlit::VERSION}") { object.hamlit }
57
+ x.report("hamlit1 v#{Hamlit1::VERSION}") { object.hamlit1 } if File.exist?('hamlit1')
58
+ x.compare!
59
+ end
60
+ end
61
+
62
+ desc 'code HAML', 'Show compiled code'
63
+ def code(file)
64
+ haml = File.read(file)
65
+ puts "#{?= * 49}\n Haml Source: #{file}\n#{?= * 49}"
66
+ puts Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).precompiled
67
+ puts "\n#{?= * 49}\n Faml Source: #{file}\n#{?= * 49}"
68
+ puts Faml::Engine.new.call(haml)
69
+ puts "\n#{?= * 49}\n Hamlit Source: #{file}\n#{?= * 49}"
70
+ puts Hamlit::Engine.new.call(haml)
71
+ if File.exist?('hamlit1')
72
+ puts "\n#{?= * 49}\n Hamlit1 Source: #{file}\n#{?= * 49}"
73
+ puts Hamlit1::Engine.new.call(haml)
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def method_missing(*args)
80
+ return super if args.length > 1
81
+ render(args.first.to_s)
82
+ end
83
+ end
84
+
85
+ Bench.start
data/bin/clone ADDED
@@ -0,0 +1,14 @@
1
+ #!/bin/bash
2
+
3
+ if [ -e hamlit1 ]; then
4
+ echo 'With hamlit1'
5
+ exit 0
6
+ fi
7
+
8
+ git clone --depth=1 -b v1.7.2 https://github.com/k0kubun/hamlit hamlit1
9
+ cd hamlit1
10
+ git grep --name-only Hamlit | xargs sed -i "" -e "s/Hamlit/Hamlit1/g" 2> /dev/null
11
+ git grep --name-only hamlit | xargs sed -i "" -e "s/hamlit/hamlit1/g" 2> /dev/null
12
+ git mv hamlit.gemspec hamlit1.gemspec
13
+ git mv lib/hamlit.rb lib/hamlit1.rb
14
+ git mv lib/hamlit lib/hamlit1
data/bin/console ADDED
@@ -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
data/bin/lineprof ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hamlit'
5
+ require 'lineprof'
6
+ require 'tempfile'
7
+ require 'thor'
8
+
9
+ class CLI < Thor
10
+ desc 'render HAML', 'Benchmark render'
11
+ def render(file)
12
+ haml = File.read(file)
13
+ compiled = Hamlit::Engine.new.call(haml)
14
+ code = [
15
+ 'require "lineprof"',
16
+ 'require "hamlit"',
17
+
18
+ 'Lineprof.profile(/./) do',
19
+ '100.times do',
20
+ compiled,
21
+ 'end',
22
+ 'end',
23
+ ].join("\n")
24
+
25
+ file = Tempfile.create('compiled')
26
+ file.write(code)
27
+ file.close
28
+
29
+ system("bundle exec ruby #{file.path}")
30
+ end
31
+
32
+ desc 'compile HAML', 'Benchmark compile'
33
+ def compile(file)
34
+ haml = File.read(file)
35
+ Lineprof.profile(/./) do
36
+ 100.times { Hamlit::Engine.new.call(haml) }
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def method_missing(*args)
43
+ return super if args.length > 1
44
+ render(args.first.to_s)
45
+ end
46
+ end
47
+
48
+ CLI.start