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
@@ -1,108 +0,0 @@
1
- require 'hamlit/compilers/new_attribute'
2
- require 'hamlit/compilers/old_attribute'
3
- require 'hamlit/compilers/runtime_attribute'
4
- require 'hamlit/concerns/escapable'
5
- require 'hamlit/concerns/included'
6
-
7
- module Hamlit
8
- module Compilers
9
- module Attributes
10
- extend Concerns::Included
11
- include Compilers::NewAttribute
12
- include Compilers::OldAttribute
13
- include Compilers::RuntimeAttribute
14
-
15
- included do
16
- include Concerns::Escapable
17
-
18
- define_options :format, :attr_quote
19
- end
20
-
21
- def on_haml_attrs(*attrs)
22
- attrs = compile_attributes(attrs)
23
- attrs = join_ids(attrs)
24
- attrs = combine_classes(attrs)
25
- attrs = sort_attributes(attrs)
26
-
27
- if has_runtime_attribute?(attrs) && has_attr?(attrs, 'class', 'id')
28
- attrs = merge_runtime_attributes(attrs)
29
- return [:html, :attrs, *escape_attribute_values(attrs)]
30
- end
31
- attrs = attrs.map { |a| compile(a) }
32
-
33
- [:html, :attrs, *escape_attribute_values(attrs)]
34
- end
35
-
36
- private
37
-
38
- def escape_attribute_values(attrs)
39
- attrs.map do |attr|
40
- _, _, name, value = attr
41
- type, arg = value
42
- next attr unless name && type && type && arg
43
-
44
- [:html, :attr, name, escape_html(value, true)]
45
- end
46
- end
47
-
48
- def compile_attributes(exps)
49
- attrs = []
50
- exps.each do |exp|
51
- case exp
52
- when /\A\(.+\)\Z/m
53
- attrs += compile_new_attribute(exp)
54
- when /\A{.+}\Z/
55
- attrs += compile_old_attribute(exp)
56
- else
57
- attrs << compile(exp)
58
- end
59
- end
60
- attrs
61
- end
62
-
63
- def combine_classes(attrs)
64
- class_attrs = filter_attrs(attrs, 'class')
65
- return attrs if class_attrs.length <= 1
66
-
67
- values = class_attrs.map(&:last).sort_by(&:last)
68
- [[:html, :attr, 'class', [:multi, *insert_static(values, ' ')]]] + (attrs - class_attrs)
69
- end
70
-
71
- def join_ids(attrs)
72
- id_attrs = filter_attrs(attrs, 'id')
73
- return attrs if id_attrs.length <= 1
74
-
75
- values = attrs.map(&:last)
76
- [[:html, :attr, 'id', [:multi, *insert_static(values, '_')]]] + (attrs - id_attrs)
77
- end
78
-
79
- def has_attr?(attrs, *targets)
80
- filter_attrs(attrs, *targets).any?
81
- end
82
-
83
- def filter_attrs(attrs, *targets)
84
- attrs.select do |html, attr, name, content|
85
- targets.include?(name)
86
- end
87
- end
88
-
89
- def insert_static(array, str)
90
- result = []
91
- array.each_with_index do |value, index|
92
- result << [:static, str] if index > 0
93
- result << value
94
- end
95
- result
96
- end
97
-
98
- # Sort attributes by attribute name.
99
- # Move runtime attributes to last.
100
- def sort_attributes(attrs)
101
- attrs.sort_by do |(sexp, _, attr_name, _)|
102
- next '' if sexp == :runtime
103
- attr_name
104
- end
105
- end
106
- end
107
- end
108
- end
@@ -1,13 +0,0 @@
1
- module Hamlit
2
- module Compilers
3
- module Comment
4
- def on_haml_comment(condition, exps)
5
- content = [:multi]
6
- content << [:static, "#{condition}>\n"]
7
- content += exps.map { |exp| compile(exp) }
8
- content << [:static, "<![endif]"]
9
- [:html, :comment, content]
10
- end
11
- end
12
- end
13
- end
@@ -1,39 +0,0 @@
1
- module Hamlit
2
- module Compilers
3
- module Doctype
4
- def on_haml_doctype(format, type)
5
- if type == 'XML'
6
- return xml_doctype_tag(format)
7
- elsif type
8
- return doctype_tag(type)
9
- end
10
-
11
- case format
12
- when :html4
13
- doctype_tag(:transitional)
14
- when :html5
15
- doctype_tag(:html)
16
- when :xhtml
17
- doctype_tag(:transitional)
18
- else
19
- doctype_tag(format)
20
- end
21
- end
22
-
23
- private
24
-
25
- def xml_doctype_tag(format)
26
- case format
27
- when :html4, :html5
28
- [:multi]
29
- else
30
- [:multi, [:static, "<?xml version='1.0' encoding='utf-8' ?>"], [:static, "\n"]]
31
- end
32
- end
33
-
34
- def doctype_tag(type)
35
- [:multi, [:html, :doctype, type.to_s], [:static, "\n"]]
36
- end
37
- end
38
- end
39
- end
@@ -1,53 +0,0 @@
1
- require 'hamlit/concerns/included'
2
- require 'hamlit/concerns/registerable'
3
- require 'hamlit/filters/coffee'
4
- require 'hamlit/filters/css'
5
- require 'hamlit/filters/erb'
6
- require 'hamlit/filters/escaped'
7
- require 'hamlit/filters/javascript'
8
- require 'hamlit/filters/less'
9
- require 'hamlit/filters/plain'
10
- require 'hamlit/filters/preserve'
11
- require 'hamlit/filters/ruby'
12
- require 'hamlit/filters/sass'
13
- require 'hamlit/filters/markdown'
14
- require 'hamlit/filters/scss'
15
-
16
- module Hamlit
17
- module Compilers
18
- module Filter
19
- extend Concerns::Included
20
-
21
- included do
22
- extend Concerns::Registerable
23
-
24
- define_options :format
25
-
26
- register :coffee, Filters::Coffee
27
- register :coffeescript, Filters::Coffee
28
- register :css, Filters::Css
29
- register :erb, Filters::Erb
30
- register :escaped, Filters::Escaped
31
- register :javascript, Filters::Javascript
32
- register :less, Filters::Less
33
- register :markdown, Filters::Markdown
34
- register :plain, Filters::Plain
35
- register :preserve, Filters::Preserve
36
- register :ruby, Filters::Ruby
37
- register :sass, Filters::Sass
38
- register :scss, Filters::Scss
39
- end
40
-
41
- def on_haml_filter(name, lines)
42
- ast = compile_filter(name, lines)
43
- compile(ast)
44
- end
45
-
46
- private
47
-
48
- def compile_filter(name, exp)
49
- self.class.find(name).new(options).compile(exp)
50
- end
51
- end
52
- end
53
- end
@@ -1,115 +0,0 @@
1
- require 'hamlit/concerns/lexable'
2
-
3
- # This module compiles new-style attributes, which is
4
- # surrounded by parentheses.
5
- module Hamlit
6
- module Compilers
7
- module NewAttribute
8
- include Concerns::Lexable
9
-
10
- def compile_new_attribute(str)
11
- str = str.gsub(/\A\(|\)\Z/, '')
12
- attrs = parse_new_attributes(str)
13
- attrs.map do |key, value|
14
- next true_attribute(key) if value == 'true'
15
- [:html, :attr, key, [:dynamic, value]]
16
- end
17
- end
18
-
19
- private
20
-
21
- def parse_new_attributes(str)
22
- attributes = {}
23
-
24
- while str.length > 0
25
- tokens = Ripper.lex(str)
26
- key = read_key!(tokens)
27
- val = read_value!(tokens)
28
- if key && val
29
- attributes[key] = val
30
- elsif key
31
- attributes[key] = 'true'
32
- end
33
-
34
- token = tokens.first
35
- break unless token
36
-
37
- pos = convert_position(str, *token.first)
38
- str = str[pos..-1]
39
- end
40
-
41
- attributes
42
- end
43
-
44
- def read_key!(tokens)
45
- skip_tokens!(tokens, :on_sp, :on_nl, :on_ignored_nl)
46
- (row, col), type, key = tokens.shift
47
-
48
- while tokens.any? && tokens.first[2] == '-'
49
- tokens.shift
50
-
51
- (row, col), type, part = tokens.shift
52
-
53
- key = "#{key}-#{part}"
54
- end
55
-
56
- key
57
- end
58
-
59
- def read_value!(tokens)
60
- skip_tokens!(tokens, :on_sp)
61
- _, _, str = tokens.shift
62
- return nil if str != '='
63
-
64
- skip_tokens!(tokens, :on_sp)
65
- return if tokens.empty?
66
-
67
- case tokens.first[TYPE_POSITION]
68
- when :on_tstring_beg
69
- val = fetch_balanced_quotes(tokens)
70
- else
71
- val = fetch_until(tokens, :on_sp)
72
- end
73
- val.length.times { tokens.shift }
74
- val.map(&:last).join
75
- end
76
-
77
- def fetch_balanced_quotes(all_tokens)
78
- tokens = []
79
- open_count = 0
80
-
81
- all_tokens.each do |token|
82
- case type_of(token)
83
- when :on_tstring_beg then open_count += 1
84
- when :on_tstring_end then open_count -= 1
85
- end
86
-
87
- tokens << token
88
- break if open_count == 0
89
- end
90
-
91
- tokens
92
- end
93
-
94
- def fetch_until(all_tokens, type)
95
- tokens = []
96
-
97
- all_tokens.each do |token|
98
- break if token[TYPE_POSITION] == type
99
- tokens << token
100
- end
101
-
102
- tokens
103
- end
104
-
105
- def true_attribute(key)
106
- case options[:format]
107
- when :xhtml
108
- [:html, :attr, key, [:static, key]]
109
- else
110
- [:html, :attr, key, [:multi]]
111
- end
112
- end
113
- end
114
- end
115
- end
@@ -1,241 +0,0 @@
1
- require 'hamlit/concerns/attribute_builder'
2
- require 'hamlit/concerns/balanceable'
3
- require 'hamlit/concerns/lexable'
4
-
5
- # This module compiles only old-style attribute, which is
6
- # surrounded by brackets.
7
- module Hamlit
8
- # This error is raised when hamlit copmiler decide to
9
- # copmile the attributes on runtime.
10
- class RuntimeBuild < StandardError; end
11
-
12
- module Compilers
13
- module OldAttribute
14
- include Concerns::AttributeBuilder
15
- include Concerns::Balanceable
16
- include Concerns::Lexable
17
-
18
- # Only data can be nested for performance.
19
- NESTABLE_ATTRIBUTES = %w[data].freeze
20
- IGNORED_EXPRESSIONS = %w[false nil].freeze
21
-
22
- # Class and id are joined if the value is an array.
23
- JOIN_ATTRIBUTES = %w[class id].freeze
24
- JOINABLE_TOKENS = %i[on_ident on_qwords_beg on_lbracket].freeze
25
-
26
- # Only boolean attributes can be deleted for performance.
27
- BOOLEAN_ATTRIBUTES = %w[disabled readonly multiple checked autobuffer
28
- autoplay controls loop selected hidden scoped async
29
- defer reversed ismap seamless muted required
30
- autofocus novalidate formnovalidate open pubdate
31
- itemscope allowfullscreen default inert sortable
32
- truespeed typemustmatch data].freeze
33
-
34
- DEFAULT_COUNTS = {
35
- brace: -1,
36
- emb: 0,
37
- paren: 0,
38
- bracket: 0,
39
- }.freeze
40
-
41
- OPEN_TOKEN_TYPES = {
42
- on_lbrace: :brace,
43
- on_embexpr_beg: :emb,
44
- on_lparen: :paren,
45
- on_lbracket: :bracket,
46
- }.freeze
47
-
48
- CLOSE_TOKEN_TYPES = {
49
- on_rbrace: :brace,
50
- on_embexpr_end: :emb,
51
- on_rparen: :paren,
52
- on_rbracket: :bracket,
53
- }.freeze
54
-
55
- def compile_old_attribute(str)
56
- raise RuntimeBuild unless valid_hash?(str)
57
-
58
- attrs = parse_old_attributes(str)
59
- assert_no_boolean_attributes!(attrs)
60
-
61
- format_attributes(attrs).map do |key, value|
62
- next true_attribute(key) if value == 'true'
63
- assert_static_value!(value) if NESTABLE_ATTRIBUTES.include?(key)
64
- detect_joinable_value!(value) if JOIN_ATTRIBUTES.include?(key)
65
-
66
- [:html, :attr, key, [:dynamic, value]]
67
- end
68
- rescue RuntimeBuild
69
- # Give up static compilation when given string is invalid as ruby hash
70
- [[:runtime, str]]
71
- end
72
-
73
- private
74
-
75
- def valid_hash?(str)
76
- sexp = Ripper.sexp(str)
77
- return false unless sexp
78
-
79
- sexp.flatten[1] == :hash
80
- end
81
-
82
- def format_attributes(attributes)
83
- attributes = flatten_attributes(attributes)
84
- ignore_falsy_values(attributes)
85
- end
86
-
87
- def ignore_falsy_values(attributes)
88
- attributes = attributes.dup
89
- attributes.each do |key, value|
90
- attributes.delete(key) if IGNORED_EXPRESSIONS.include?(value)
91
- end
92
- end
93
-
94
- # Give up static compilation when variables are detected.
95
- def assert_static_value!(value)
96
- tokens = Ripper.lex(value)
97
- tokens.each do |token|
98
- raise RuntimeBuild if type_of(token) == :on_ident
99
- end
100
- end
101
-
102
- # Give up static compilation when the value is a variable or an array.
103
- def detect_joinable_value!(value)
104
- tokens = Ripper.lex(value)
105
- tokens.each do |token|
106
- raise RuntimeBuild if JOINABLE_TOKENS.include?(type_of(token))
107
- end
108
- end
109
-
110
- # Give up static compilation when attributes have deletable
111
- # attributes, such as 'checked'.
112
- def assert_no_boolean_attributes!(attrs)
113
- if BOOLEAN_ATTRIBUTES.any? { |key| attrs.keys.include?(key) }
114
- raise RuntimeBuild
115
- end
116
- end
117
-
118
- # Parse brace-balanced string and return the result as hash
119
- def parse_old_attributes(str)
120
- attributes = {}
121
-
122
- split_hash(str).each do |attr|
123
- tokens = Ripper.lex("{#{attr}").drop(1)
124
- key = read_hash_key!(tokens)
125
- val = read_hash_value!(tokens)
126
-
127
- attributes[key] = val if key
128
- end
129
-
130
- attributes
131
- end
132
-
133
- def read_hash_value!(tokens)
134
- skip_tokens!(tokens, :on_sp)
135
- val = tokens.map(&:last).join.strip
136
-
137
- if type_of(tokens.first) == :on_lbrace
138
- val = parse_old_attributes(val)
139
- end
140
- val
141
- end
142
-
143
- def read_hash_key!(tokens)
144
- skip_tokens!(tokens, :on_sp)
145
-
146
- (row, col), type, str = tokens.shift
147
- case type
148
- when :on_label
149
- str.gsub!(/:\Z/, '')
150
- when :on_symbeg
151
- if %w[:" :'].include?(str)
152
- str = read_string!(tokens)
153
- else
154
- (row, col), type, str = tokens.shift
155
- end
156
- assert_rocket!(tokens)
157
- when :on_tstring_beg
158
- str = read_string!(tokens, assert_rocket: true)
159
- end
160
- str
161
- end
162
-
163
- def read_string!(tokens, assert_rocket: false)
164
- _, type, str = tokens.shift
165
- return '' if type == :on_tstring_end
166
-
167
- next_token = tokens.shift
168
- return str if RUBY_VERSION >= "2.2.0" && type_of(next_token) == :on_label_end
169
-
170
- raise SyntaxError if type_of(next_token) != :on_tstring_end
171
- assert_rocket!(tokens) if assert_rocket
172
-
173
- str
174
- end
175
-
176
- def assert_rocket!(tokens, *types)
177
- skip_tokens!(tokens, :on_sp)
178
- _, type, str = tokens.shift
179
-
180
- raise SyntaxError unless type == :on_op && str == '=>'
181
- end
182
-
183
- def split_hash(str)
184
- columns = HashParser.assoc_columns(str)
185
- columns = reject_nested_columns(str, columns)
186
-
187
- splitted = []
188
- start_pos = 1
189
- columns.each do |end_pos|
190
- splitted << str.byteslice(start_pos...end_pos)
191
- start_pos = end_pos + 1
192
- end
193
-
194
- splitted
195
- end
196
-
197
- def reject_nested_columns(str, columns)
198
- result = []
199
- counts = DEFAULT_COUNTS.dup
200
-
201
- Ripper.lex(str).each do |(_, col), type, _|
202
- if columns.include?(col) && counts.values.all?(&:zero?)
203
- result << col
204
- end
205
- update_open_counts(counts, type)
206
- end
207
- result
208
- end
209
-
210
- def update_open_counts(counts, type)
211
- case
212
- when OPEN_TOKEN_TYPES[type]
213
- counts[OPEN_TOKEN_TYPES[type]] += 1
214
- when CLOSE_TOKEN_TYPES[type]
215
- counts[CLOSE_TOKEN_TYPES[type]] -= 1
216
- end
217
- end
218
-
219
- class HashParser < Ripper
220
- attr_reader :columns
221
-
222
- def self.assoc_columns(src)
223
- parser = new(src)
224
- parser.parse
225
- parser.columns
226
- end
227
-
228
- def initialize(src)
229
- super(src)
230
- @columns = []
231
- end
232
-
233
- private
234
-
235
- def on_assoc_new(*args)
236
- @columns << column - 1
237
- end
238
- end
239
- end
240
- end
241
- end