haml_ejs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (358) hide show
  1. data/.yardopts +11 -0
  2. data/CONTRIBUTING +3 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +68 -0
  5. data/Rakefile +419 -0
  6. data/VERSION +1 -0
  7. data/VERSION_NAME +1 -0
  8. data/bin/haml +9 -0
  9. data/bin/html2haml +7 -0
  10. data/extra/update_watch.rb +13 -0
  11. data/init.rb +18 -0
  12. data/lib/haml.rb +49 -0
  13. data/lib/haml/buffer.rb +297 -0
  14. data/lib/haml/compiler.rb +486 -0
  15. data/lib/haml/engine.rb +312 -0
  16. data/lib/haml/error.rb +22 -0
  17. data/lib/haml/exec.rb +362 -0
  18. data/lib/haml/filters.rb +388 -0
  19. data/lib/haml/haml_ejs.rb +57 -0
  20. data/lib/haml/helpers.rb +608 -0
  21. data/lib/haml/helpers/action_view_extensions.rb +57 -0
  22. data/lib/haml/helpers/action_view_mods.rb +257 -0
  23. data/lib/haml/helpers/xss_mods.rb +165 -0
  24. data/lib/haml/html.rb +412 -0
  25. data/lib/haml/html/erb.rb +141 -0
  26. data/lib/haml/parser.rb +760 -0
  27. data/lib/haml/railtie.rb +19 -0
  28. data/lib/haml/root.rb +7 -0
  29. data/lib/haml/shared.rb +78 -0
  30. data/lib/haml/template.rb +99 -0
  31. data/lib/haml/template/options.rb +16 -0
  32. data/lib/haml/template/patch.rb +58 -0
  33. data/lib/haml/template/plugin.rb +123 -0
  34. data/lib/haml/util.rb +842 -0
  35. data/lib/haml/version.rb +109 -0
  36. data/lib/sass.rb +8 -0
  37. data/lib/sass/plugin.rb +10 -0
  38. data/lib/sass/rails2_shim.rb +9 -0
  39. data/lib/sass/rails3_shim.rb +16 -0
  40. data/rails/init.rb +1 -0
  41. data/test/benchmark.rb +91 -0
  42. data/test/gemfiles/Gemfile.rails-2.0.x +8 -0
  43. data/test/gemfiles/Gemfile.rails-2.0.x.lock +38 -0
  44. data/test/gemfiles/Gemfile.rails-2.1.x +8 -0
  45. data/test/gemfiles/Gemfile.rails-2.1.x.lock +38 -0
  46. data/test/gemfiles/Gemfile.rails-2.2.x +8 -0
  47. data/test/gemfiles/Gemfile.rails-2.2.x.lock +38 -0
  48. data/test/gemfiles/Gemfile.rails-2.3.x +8 -0
  49. data/test/gemfiles/Gemfile.rails-2.3.x.lock +40 -0
  50. data/test/gemfiles/Gemfile.rails-3.0.x +8 -0
  51. data/test/gemfiles/Gemfile.rails-3.0.x.lock +85 -0
  52. data/test/gemfiles/Gemfile.rails-3.1.x +8 -0
  53. data/test/gemfiles/Gemfile.rails-3.1.x.lock +98 -0
  54. data/test/gemfiles/Gemfile.rails-xss-2.3.x +9 -0
  55. data/test/gemfiles/Gemfile.rails-xss-2.3.x.lock +42 -0
  56. data/test/haml-ejs/haml-ejs_test.rb +22 -0
  57. data/test/haml-ejs/templates/conditional.input +3 -0
  58. data/test/haml-ejs/templates/conditional.output +3 -0
  59. data/test/haml-ejs/templates/conditional_and_interpolation.input +2 -0
  60. data/test/haml-ejs/templates/conditional_and_interpolation.output +3 -0
  61. data/test/haml-ejs/templates/conditional_else.input +5 -0
  62. data/test/haml-ejs/templates/conditional_else.output +5 -0
  63. data/test/haml-ejs/templates/conditional_else_elsif.input +7 -0
  64. data/test/haml-ejs/templates/conditional_else_elsif.output +7 -0
  65. data/test/haml-ejs/templates/conditional_elsif.input +5 -0
  66. data/test/haml-ejs/templates/conditional_elsif.output +5 -0
  67. data/test/haml-ejs/templates/conditional_unless.input +3 -0
  68. data/test/haml-ejs/templates/conditional_unless.output +3 -0
  69. data/test/haml-ejs/templates/conditional_unless_else.input +5 -0
  70. data/test/haml-ejs/templates/conditional_unless_else.output +5 -0
  71. data/test/haml-ejs/templates/conditional_unless_elsif_else.input +5 -0
  72. data/test/haml-ejs/templates/conditional_unless_elsif_else.output +5 -0
  73. data/test/haml-ejs/templates/interpolation.input +2 -0
  74. data/test/haml-ejs/templates/interpolation.output +1 -0
  75. data/test/haml-ejs/templates/iteration.input +2 -0
  76. data/test/haml-ejs/templates/iteration.output +3 -0
  77. data/test/haml-ejs/templates/suite.input +26 -0
  78. data/test/haml-ejs/templates/suite.output +27 -0
  79. data/test/haml/engine_test.rb +1925 -0
  80. data/test/haml/erb/_av_partial_1.erb +12 -0
  81. data/test/haml/erb/_av_partial_2.erb +8 -0
  82. data/test/haml/erb/action_view.erb +62 -0
  83. data/test/haml/erb/standard.erb +55 -0
  84. data/test/haml/helper_test.rb +454 -0
  85. data/test/haml/html2haml/erb_tests.rb +440 -0
  86. data/test/haml/html2haml_test.rb +336 -0
  87. data/test/haml/markaby/standard.mab +52 -0
  88. data/test/haml/mocks/article.rb +6 -0
  89. data/test/haml/results/content_for_layout.xhtml +12 -0
  90. data/test/haml/results/eval_suppressed.xhtml +9 -0
  91. data/test/haml/results/filters.xhtml +62 -0
  92. data/test/haml/results/helpers.xhtml +70 -0
  93. data/test/haml/results/helpful.xhtml +10 -0
  94. data/test/haml/results/just_stuff.xhtml +70 -0
  95. data/test/haml/results/list.xhtml +12 -0
  96. data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
  97. data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
  98. data/test/haml/results/original_engine.xhtml +20 -0
  99. data/test/haml/results/partial_layout.xhtml +5 -0
  100. data/test/haml/results/partials.xhtml +21 -0
  101. data/test/haml/results/render_layout.xhtml +3 -0
  102. data/test/haml/results/silent_script.xhtml +74 -0
  103. data/test/haml/results/standard.xhtml +162 -0
  104. data/test/haml/results/tag_parsing.xhtml +23 -0
  105. data/test/haml/results/very_basic.xhtml +5 -0
  106. data/test/haml/results/whitespace_handling.xhtml +89 -0
  107. data/test/haml/spec/README.md +97 -0
  108. data/test/haml/spec/lua_haml_spec.lua +30 -0
  109. data/test/haml/spec/ruby_haml_test.rb +19 -0
  110. data/test/haml/spec/tests.json +534 -0
  111. data/test/haml/spec_test.rb +44 -0
  112. data/test/haml/template_test.rb +419 -0
  113. data/test/haml/templates/_av_partial_1.haml +9 -0
  114. data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
  115. data/test/haml/templates/_av_partial_2.haml +5 -0
  116. data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
  117. data/test/haml/templates/_layout.erb +3 -0
  118. data/test/haml/templates/_layout_for_partial.haml +3 -0
  119. data/test/haml/templates/_partial.haml +8 -0
  120. data/test/haml/templates/_text_area.haml +3 -0
  121. data/test/haml/templates/action_view.haml +47 -0
  122. data/test/haml/templates/action_view_ugly.haml +47 -0
  123. data/test/haml/templates/breakage.haml +8 -0
  124. data/test/haml/templates/content_for_layout.haml +8 -0
  125. data/test/haml/templates/eval_suppressed.haml +11 -0
  126. data/test/haml/templates/filters.haml +66 -0
  127. data/test/haml/templates/helpers.haml +55 -0
  128. data/test/haml/templates/helpful.haml +11 -0
  129. data/test/haml/templates/just_stuff.haml +85 -0
  130. data/test/haml/templates/list.haml +12 -0
  131. data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
  132. data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
  133. data/test/haml/templates/original_engine.haml +17 -0
  134. data/test/haml/templates/partial_layout.haml +10 -0
  135. data/test/haml/templates/partialize.haml +1 -0
  136. data/test/haml/templates/partials.haml +12 -0
  137. data/test/haml/templates/render_layout.haml +2 -0
  138. data/test/haml/templates/silent_script.haml +40 -0
  139. data/test/haml/templates/standard.haml +43 -0
  140. data/test/haml/templates/standard_ugly.haml +43 -0
  141. data/test/haml/templates/tag_parsing.haml +21 -0
  142. data/test/haml/templates/very_basic.haml +4 -0
  143. data/test/haml/templates/whitespace_handling.haml +87 -0
  144. data/test/haml/util_test.rb +300 -0
  145. data/test/linked_rails.rb +42 -0
  146. data/test/test_helper.rb +75 -0
  147. data/vendor/sass/CONTRIBUTING +3 -0
  148. data/vendor/sass/MIT-LICENSE +20 -0
  149. data/vendor/sass/README.md +201 -0
  150. data/vendor/sass/Rakefile +339 -0
  151. data/vendor/sass/TODO +39 -0
  152. data/vendor/sass/VERSION +1 -0
  153. data/vendor/sass/VERSION_NAME +1 -0
  154. data/vendor/sass/bin/sass +8 -0
  155. data/vendor/sass/bin/sass-convert +7 -0
  156. data/vendor/sass/bin/scss +8 -0
  157. data/vendor/sass/doc-src/FAQ.md +35 -0
  158. data/vendor/sass/doc-src/INDENTED_SYNTAX.md +210 -0
  159. data/vendor/sass/doc-src/SASS_CHANGELOG.md +2214 -0
  160. data/vendor/sass/doc-src/SASS_REFERENCE.md +1957 -0
  161. data/vendor/sass/doc-src/SCSS_FOR_SASS_USERS.md +155 -0
  162. data/vendor/sass/ext/extconf.rb +10 -0
  163. data/vendor/sass/extra/update_watch.rb +13 -0
  164. data/vendor/sass/init.rb +18 -0
  165. data/vendor/sass/lib/sass.rb +72 -0
  166. data/vendor/sass/lib/sass/cache_stores.rb +15 -0
  167. data/vendor/sass/lib/sass/cache_stores/base.rb +84 -0
  168. data/vendor/sass/lib/sass/cache_stores/chain.rb +33 -0
  169. data/vendor/sass/lib/sass/cache_stores/filesystem.rb +58 -0
  170. data/vendor/sass/lib/sass/cache_stores/memory.rb +47 -0
  171. data/vendor/sass/lib/sass/cache_stores/null.rb +25 -0
  172. data/vendor/sass/lib/sass/callbacks.rb +66 -0
  173. data/vendor/sass/lib/sass/css.rb +294 -0
  174. data/vendor/sass/lib/sass/engine.rb +862 -0
  175. data/vendor/sass/lib/sass/environment.rb +155 -0
  176. data/vendor/sass/lib/sass/error.rb +201 -0
  177. data/vendor/sass/lib/sass/exec.rb +659 -0
  178. data/vendor/sass/lib/sass/importers.rb +22 -0
  179. data/vendor/sass/lib/sass/importers/base.rb +138 -0
  180. data/vendor/sass/lib/sass/importers/filesystem.rb +144 -0
  181. data/vendor/sass/lib/sass/less.rb +382 -0
  182. data/vendor/sass/lib/sass/plugin.rb +136 -0
  183. data/vendor/sass/lib/sass/plugin/compiler.rb +358 -0
  184. data/vendor/sass/lib/sass/plugin/configuration.rb +125 -0
  185. data/vendor/sass/lib/sass/plugin/generic.rb +15 -0
  186. data/vendor/sass/lib/sass/plugin/merb.rb +48 -0
  187. data/vendor/sass/lib/sass/plugin/rack.rb +60 -0
  188. data/vendor/sass/lib/sass/plugin/rails.rb +47 -0
  189. data/vendor/sass/lib/sass/plugin/staleness_checker.rb +173 -0
  190. data/vendor/sass/lib/sass/railtie.rb +9 -0
  191. data/vendor/sass/lib/sass/repl.rb +58 -0
  192. data/vendor/sass/lib/sass/root.rb +7 -0
  193. data/vendor/sass/lib/sass/script.rb +40 -0
  194. data/vendor/sass/lib/sass/script/bool.rb +18 -0
  195. data/vendor/sass/lib/sass/script/color.rb +480 -0
  196. data/vendor/sass/lib/sass/script/css_lexer.rb +29 -0
  197. data/vendor/sass/lib/sass/script/css_parser.rb +31 -0
  198. data/vendor/sass/lib/sass/script/funcall.rb +162 -0
  199. data/vendor/sass/lib/sass/script/functions.rb +1343 -0
  200. data/vendor/sass/lib/sass/script/interpolation.rb +70 -0
  201. data/vendor/sass/lib/sass/script/lexer.rb +334 -0
  202. data/vendor/sass/lib/sass/script/list.rb +76 -0
  203. data/vendor/sass/lib/sass/script/literal.rb +245 -0
  204. data/vendor/sass/lib/sass/script/node.rb +91 -0
  205. data/vendor/sass/lib/sass/script/number.rb +429 -0
  206. data/vendor/sass/lib/sass/script/operation.rb +91 -0
  207. data/vendor/sass/lib/sass/script/parser.rb +467 -0
  208. data/vendor/sass/lib/sass/script/string.rb +51 -0
  209. data/vendor/sass/lib/sass/script/string_interpolation.rb +94 -0
  210. data/vendor/sass/lib/sass/script/unary_operation.rb +57 -0
  211. data/vendor/sass/lib/sass/script/variable.rb +54 -0
  212. data/vendor/sass/lib/sass/scss.rb +17 -0
  213. data/vendor/sass/lib/sass/scss/css_parser.rb +46 -0
  214. data/vendor/sass/lib/sass/scss/parser.rb +920 -0
  215. data/vendor/sass/lib/sass/scss/rx.rb +127 -0
  216. data/vendor/sass/lib/sass/scss/sass_parser.rb +11 -0
  217. data/vendor/sass/lib/sass/scss/script_lexer.rb +15 -0
  218. data/vendor/sass/lib/sass/scss/script_parser.rb +25 -0
  219. data/vendor/sass/lib/sass/scss/static_parser.rb +40 -0
  220. data/vendor/sass/lib/sass/selector.rb +361 -0
  221. data/vendor/sass/lib/sass/selector/abstract_sequence.rb +62 -0
  222. data/vendor/sass/lib/sass/selector/comma_sequence.rb +81 -0
  223. data/vendor/sass/lib/sass/selector/sequence.rb +233 -0
  224. data/vendor/sass/lib/sass/selector/simple.rb +113 -0
  225. data/vendor/sass/lib/sass/selector/simple_sequence.rb +134 -0
  226. data/vendor/sass/lib/sass/shared.rb +78 -0
  227. data/vendor/sass/lib/sass/tree/charset_node.rb +22 -0
  228. data/vendor/sass/lib/sass/tree/comment_node.rb +77 -0
  229. data/vendor/sass/lib/sass/tree/debug_node.rb +18 -0
  230. data/vendor/sass/lib/sass/tree/directive_node.rb +23 -0
  231. data/vendor/sass/lib/sass/tree/each_node.rb +24 -0
  232. data/vendor/sass/lib/sass/tree/extend_node.rb +29 -0
  233. data/vendor/sass/lib/sass/tree/for_node.rb +36 -0
  234. data/vendor/sass/lib/sass/tree/function_node.rb +27 -0
  235. data/vendor/sass/lib/sass/tree/if_node.rb +65 -0
  236. data/vendor/sass/lib/sass/tree/import_node.rb +68 -0
  237. data/vendor/sass/lib/sass/tree/media_node.rb +32 -0
  238. data/vendor/sass/lib/sass/tree/mixin_def_node.rb +27 -0
  239. data/vendor/sass/lib/sass/tree/mixin_node.rb +32 -0
  240. data/vendor/sass/lib/sass/tree/node.rb +204 -0
  241. data/vendor/sass/lib/sass/tree/prop_node.rb +155 -0
  242. data/vendor/sass/lib/sass/tree/return_node.rb +18 -0
  243. data/vendor/sass/lib/sass/tree/root_node.rb +28 -0
  244. data/vendor/sass/lib/sass/tree/rule_node.rb +129 -0
  245. data/vendor/sass/lib/sass/tree/variable_node.rb +30 -0
  246. data/vendor/sass/lib/sass/tree/visitors/base.rb +75 -0
  247. data/vendor/sass/lib/sass/tree/visitors/check_nesting.rb +134 -0
  248. data/vendor/sass/lib/sass/tree/visitors/convert.rb +255 -0
  249. data/vendor/sass/lib/sass/tree/visitors/cssize.rb +175 -0
  250. data/vendor/sass/lib/sass/tree/visitors/perform.rb +301 -0
  251. data/vendor/sass/lib/sass/tree/visitors/to_css.rb +216 -0
  252. data/vendor/sass/lib/sass/tree/warn_node.rb +18 -0
  253. data/vendor/sass/lib/sass/tree/while_node.rb +18 -0
  254. data/vendor/sass/lib/sass/util.rb +669 -0
  255. data/vendor/sass/lib/sass/util/subset_map.rb +101 -0
  256. data/vendor/sass/lib/sass/version.rb +112 -0
  257. data/vendor/sass/rails/init.rb +1 -0
  258. data/vendor/sass/sass.gemspec +32 -0
  259. data/vendor/sass/test/sass/cache_test.rb +74 -0
  260. data/vendor/sass/test/sass/callbacks_test.rb +61 -0
  261. data/vendor/sass/test/sass/conversion_test.rb +1203 -0
  262. data/vendor/sass/test/sass/css2sass_test.rb +364 -0
  263. data/vendor/sass/test/sass/data/hsl-rgb.txt +319 -0
  264. data/vendor/sass/test/sass/engine_test.rb +2469 -0
  265. data/vendor/sass/test/sass/extend_test.rb +1348 -0
  266. data/vendor/sass/test/sass/functions_test.rb +1025 -0
  267. data/vendor/sass/test/sass/importer_test.rb +82 -0
  268. data/vendor/sass/test/sass/less_conversion_test.rb +653 -0
  269. data/vendor/sass/test/sass/mock_importer.rb +49 -0
  270. data/vendor/sass/test/sass/more_results/more1.css +9 -0
  271. data/vendor/sass/test/sass/more_results/more1_with_line_comments.css +26 -0
  272. data/vendor/sass/test/sass/more_results/more_import.css +29 -0
  273. data/vendor/sass/test/sass/more_templates/_more_partial.sass +2 -0
  274. data/vendor/sass/test/sass/more_templates/more1.sass +23 -0
  275. data/vendor/sass/test/sass/more_templates/more_import.sass +11 -0
  276. data/vendor/sass/test/sass/plugin_test.rb +469 -0
  277. data/vendor/sass/test/sass/results/alt.css +4 -0
  278. data/vendor/sass/test/sass/results/basic.css +9 -0
  279. data/vendor/sass/test/sass/results/compact.css +5 -0
  280. data/vendor/sass/test/sass/results/complex.css +86 -0
  281. data/vendor/sass/test/sass/results/compressed.css +1 -0
  282. data/vendor/sass/test/sass/results/expanded.css +19 -0
  283. data/vendor/sass/test/sass/results/if.css +3 -0
  284. data/vendor/sass/test/sass/results/import.css +31 -0
  285. data/vendor/sass/test/sass/results/import_charset.css +4 -0
  286. data/vendor/sass/test/sass/results/import_charset_1_8.css +4 -0
  287. data/vendor/sass/test/sass/results/import_charset_ibm866.css +4 -0
  288. data/vendor/sass/test/sass/results/line_numbers.css +49 -0
  289. data/vendor/sass/test/sass/results/mixins.css +95 -0
  290. data/vendor/sass/test/sass/results/multiline.css +24 -0
  291. data/vendor/sass/test/sass/results/nested.css +22 -0
  292. data/vendor/sass/test/sass/results/options.css +1 -0
  293. data/vendor/sass/test/sass/results/parent_ref.css +13 -0
  294. data/vendor/sass/test/sass/results/script.css +16 -0
  295. data/vendor/sass/test/sass/results/scss_import.css +31 -0
  296. data/vendor/sass/test/sass/results/scss_importee.css +2 -0
  297. data/vendor/sass/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  298. data/vendor/sass/test/sass/results/subdir/subdir.css +3 -0
  299. data/vendor/sass/test/sass/results/units.css +11 -0
  300. data/vendor/sass/test/sass/results/warn.css +0 -0
  301. data/vendor/sass/test/sass/results/warn_imported.css +0 -0
  302. data/vendor/sass/test/sass/script_conversion_test.rb +283 -0
  303. data/vendor/sass/test/sass/script_test.rb +496 -0
  304. data/vendor/sass/test/sass/scss/css_test.rb +916 -0
  305. data/vendor/sass/test/sass/scss/rx_test.rb +156 -0
  306. data/vendor/sass/test/sass/scss/scss_test.rb +1249 -0
  307. data/vendor/sass/test/sass/scss/test_helper.rb +37 -0
  308. data/vendor/sass/test/sass/templates/_imported_charset_ibm866.sass +4 -0
  309. data/vendor/sass/test/sass/templates/_imported_charset_utf8.sass +4 -0
  310. data/vendor/sass/test/sass/templates/_partial.sass +2 -0
  311. data/vendor/sass/test/sass/templates/alt.sass +16 -0
  312. data/vendor/sass/test/sass/templates/basic.sass +23 -0
  313. data/vendor/sass/test/sass/templates/bork1.sass +2 -0
  314. data/vendor/sass/test/sass/templates/bork2.sass +2 -0
  315. data/vendor/sass/test/sass/templates/bork3.sass +2 -0
  316. data/vendor/sass/test/sass/templates/bork4.sass +2 -0
  317. data/vendor/sass/test/sass/templates/compact.sass +17 -0
  318. data/vendor/sass/test/sass/templates/complex.sass +305 -0
  319. data/vendor/sass/test/sass/templates/compressed.sass +15 -0
  320. data/vendor/sass/test/sass/templates/expanded.sass +17 -0
  321. data/vendor/sass/test/sass/templates/if.sass +11 -0
  322. data/vendor/sass/test/sass/templates/import.sass +12 -0
  323. data/vendor/sass/test/sass/templates/import_charset.sass +7 -0
  324. data/vendor/sass/test/sass/templates/import_charset_1_8.sass +4 -0
  325. data/vendor/sass/test/sass/templates/import_charset_ibm866.sass +9 -0
  326. data/vendor/sass/test/sass/templates/importee.less +2 -0
  327. data/vendor/sass/test/sass/templates/importee.sass +19 -0
  328. data/vendor/sass/test/sass/templates/line_numbers.sass +13 -0
  329. data/vendor/sass/test/sass/templates/mixin_bork.sass +5 -0
  330. data/vendor/sass/test/sass/templates/mixins.sass +76 -0
  331. data/vendor/sass/test/sass/templates/multiline.sass +20 -0
  332. data/vendor/sass/test/sass/templates/nested.sass +25 -0
  333. data/vendor/sass/test/sass/templates/nested_bork1.sass +2 -0
  334. data/vendor/sass/test/sass/templates/nested_bork2.sass +2 -0
  335. data/vendor/sass/test/sass/templates/nested_bork3.sass +2 -0
  336. data/vendor/sass/test/sass/templates/nested_bork4.sass +2 -0
  337. data/vendor/sass/test/sass/templates/nested_import.sass +2 -0
  338. data/vendor/sass/test/sass/templates/nested_mixin_bork.sass +6 -0
  339. data/vendor/sass/test/sass/templates/options.sass +2 -0
  340. data/vendor/sass/test/sass/templates/parent_ref.sass +25 -0
  341. data/vendor/sass/test/sass/templates/script.sass +101 -0
  342. data/vendor/sass/test/sass/templates/scss_import.scss +11 -0
  343. data/vendor/sass/test/sass/templates/scss_importee.scss +1 -0
  344. data/vendor/sass/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  345. data/vendor/sass/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  346. data/vendor/sass/test/sass/templates/subdir/subdir.sass +6 -0
  347. data/vendor/sass/test/sass/templates/units.sass +11 -0
  348. data/vendor/sass/test/sass/templates/warn.sass +3 -0
  349. data/vendor/sass/test/sass/templates/warn_imported.sass +4 -0
  350. data/vendor/sass/test/sass/test_helper.rb +8 -0
  351. data/vendor/sass/test/sass/util/subset_map_test.rb +91 -0
  352. data/vendor/sass/test/sass/util_test.rb +254 -0
  353. data/vendor/sass/test/test_helper.rb +69 -0
  354. data/vendor/sass/yard/callbacks.rb +29 -0
  355. data/vendor/sass/yard/default/fulldoc/html/css/common.sass +26 -0
  356. data/vendor/sass/yard/default/layout/html/footer.erb +12 -0
  357. data/vendor/sass/yard/inherited_hash.rb +41 -0
  358. metadata +458 -0
@@ -0,0 +1,19 @@
1
+ if Haml::Util.ap_geq_3? && !Haml::Util.ap_geq?("3.0.0.beta4")
2
+ raise <<ERROR
3
+ Haml no longer supports Rails 3 versions before beta 4.
4
+ Please upgrade to Rails 3.0.0.beta4 or later.
5
+ ERROR
6
+ end
7
+
8
+ # Rails 3.0.0.beta.2+
9
+ if defined?(ActiveSupport) && Haml::Util.has?(:public_method, ActiveSupport, :on_load)
10
+ require 'haml/template/options'
11
+ autoload(:Sass, 'sass/rails3_shim')
12
+ ActiveSupport.on_load(:before_initialize) do
13
+ # resolve autoload if it looks like they're using Sass without options
14
+ Sass if File.exist?(File.join(Rails.root, 'public/stylesheets/sass'))
15
+ ActiveSupport.on_load(:action_view) do
16
+ Haml.init_rails(binding)
17
+ end
18
+ end
19
+ end
data/lib/haml/root.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Haml
2
+ # The root directory of the Haml source tree.
3
+ # This may be overridden by the package manager
4
+ # if the lib directory is separated from the main source tree.
5
+ # @api public
6
+ ROOT_DIR = File.expand_path(File.join(__FILE__, "../../.."))
7
+ end
@@ -0,0 +1,78 @@
1
+ require 'strscan'
2
+
3
+ module Haml
4
+ # This module contains functionality that's shared between Haml and Sass.
5
+ module Shared
6
+ extend self
7
+
8
+ # Scans through a string looking for the interoplation-opening `#{`
9
+ # and, when it's found, yields the scanner to the calling code
10
+ # so it can handle it properly.
11
+ #
12
+ # The scanner will have any backslashes immediately in front of the `#{`
13
+ # as the second capture group (`scan[2]`),
14
+ # and the text prior to that as the first (`scan[1]`).
15
+ #
16
+ # @yieldparam scan [StringScanner] The scanner scanning through the string
17
+ # @return [String] The text remaining in the scanner after all `#{`s have been processed
18
+ def handle_interpolation(str)
19
+ scan = StringScanner.new(str)
20
+ yield scan while scan.scan(/(.*?)(\\*)\#\{/)
21
+ scan.rest
22
+ end
23
+
24
+ # Moves a scanner through a balanced pair of characters.
25
+ # For example:
26
+ #
27
+ # Foo (Bar (Baz bang) bop) (Bang (bop bip))
28
+ # ^ ^
29
+ # from to
30
+ #
31
+ # @param scanner [StringScanner] The string scanner to move
32
+ # @param start [Character] The character opening the balanced pair.
33
+ # A `Fixnum` in 1.8, a `String` in 1.9
34
+ # @param finish [Character] The character closing the balanced pair.
35
+ # A `Fixnum` in 1.8, a `String` in 1.9
36
+ # @param count [Fixnum] The number of opening characters matched
37
+ # before calling this method
38
+ # @return [(String, String)] The string matched within the balanced pair
39
+ # and the rest of the string.
40
+ # `["Foo (Bar (Baz bang) bop)", " (Bang (bop bip))"]` in the example above.
41
+ def balance(scanner, start, finish, count = 0)
42
+ str = ''
43
+ scanner = StringScanner.new(scanner) unless scanner.is_a? StringScanner
44
+ regexp = Regexp.new("(.*?)[\\#{start.chr}\\#{finish.chr}]", Regexp::MULTILINE)
45
+ while scanner.scan(regexp)
46
+ str << scanner.matched
47
+ count += 1 if scanner.matched[-1] == start
48
+ count -= 1 if scanner.matched[-1] == finish
49
+ return [str.strip, scanner.rest] if count == 0
50
+ end
51
+ end
52
+
53
+ # Formats a string for use in error messages about indentation.
54
+ #
55
+ # @param indentation [String] The string used for indentation
56
+ # @param was [Boolean] Whether or not to add `"was"` or `"were"`
57
+ # (depending on how many characters were in `indentation`)
58
+ # @return [String] The name of the indentation (e.g. `"12 spaces"`, `"1 tab"`)
59
+ def human_indentation(indentation, was = false)
60
+ if !indentation.include?(?\t)
61
+ noun = 'space'
62
+ elsif !indentation.include?(?\s)
63
+ noun = 'tab'
64
+ else
65
+ return indentation.inspect + (was ? ' was' : '')
66
+ end
67
+
68
+ singular = indentation.length == 1
69
+ if was
70
+ was = singular ? ' was' : ' were'
71
+ else
72
+ was = ''
73
+ end
74
+
75
+ "#{indentation.length} #{noun}#{'s' unless singular}#{was}"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,99 @@
1
+ require 'haml/template/options'
2
+ require 'haml/engine'
3
+ require 'haml/helpers/action_view_mods'
4
+ require 'haml/helpers/action_view_extensions'
5
+
6
+ if defined?(ActionPack::VERSION::STRING) &&
7
+ ActionPack::VERSION::STRING == "2.3.6"
8
+ raise "Haml does not support Rails 2.3.6. Please upgrade to 2.3.7 or later."
9
+ end
10
+
11
+ module Haml
12
+ # The class that keeps track of the global options for Haml within Rails.
13
+ module Template
14
+ # Enables integration with the Rails 2.2.5+ XSS protection,
15
+ # if it's available and enabled.
16
+ #
17
+ # @return [Boolean] Whether the XSS integration was enabled.
18
+ def try_enabling_xss_integration
19
+ return false unless (ActionView::Base.respond_to?(:xss_safe?) && ActionView::Base.xss_safe?) ||
20
+ # We check for ActiveSupport#on_load here because if we're loading Haml that way, it means:
21
+ # A) we're in Rails 3 so XSS support is always on, and
22
+ # B) we might be in Rails 3 beta 3 where the load order is broken and xss_safe? is undefined
23
+ (defined?(ActiveSupport) && Haml::Util.has?(:public_method, ActiveSupport, :on_load))
24
+
25
+ Haml::Template.options[:escape_html] = true
26
+
27
+ Haml::Util.module_eval {def rails_xss_safe?; true; end}
28
+
29
+ require 'haml/helpers/xss_mods'
30
+ Haml::Helpers.send(:include, Haml::Helpers::XssMods)
31
+
32
+ Haml::Compiler.module_eval do
33
+ def precompiled_method_return_value_with_haml_xss
34
+ "::Haml::Util.html_safe(#{precompiled_method_return_value_without_haml_xss})"
35
+ end
36
+ alias_method :precompiled_method_return_value_without_haml_xss, :precompiled_method_return_value
37
+ alias_method :precompiled_method_return_value, :precompiled_method_return_value_with_haml_xss
38
+ end
39
+
40
+ true
41
+ end
42
+ end
43
+ end
44
+
45
+ unless Haml::Util.rails_env == "development"
46
+ Haml::Template.options[:ugly] ||= true
47
+ end
48
+
49
+ if Haml::Util.ap_geq_3?
50
+ Haml::Template.options[:format] ||= :html5
51
+ end
52
+
53
+ # Decide how we want to load Haml into Rails.
54
+ # Patching was necessary for versions <= 2.0.1,
55
+ # but we can make it a normal handler for higher versions.
56
+ if defined?(ActionView::TemplateHandler) ||
57
+ (defined?(ActionView::Template) && defined?(ActionView::Template::Handler)) ||
58
+ # Rails >= 3.2.0 uses Handlers instead of Handler
59
+ (defined?(ActionView::Template) && defined?(ActionView::Template::Handlers))
60
+ require 'haml/template/plugin'
61
+ else
62
+ require 'haml/template/patch'
63
+ end
64
+
65
+ # Enable XSS integration. Use Rails' after_initialize method
66
+ # so that integration will be checked after the rails_xss plugin is loaded
67
+ # (for Rails 2.3.* where it's not enabled by default).
68
+ #
69
+ # If we're running under Rails 3, though, we don't want to use after_intialize,
70
+ # since Haml loading has already been deferred via ActiveSupport.on_load.
71
+ if defined?(Rails.configuration.after_initialize) &&
72
+ !(defined?(ActiveSupport) && Haml::Util.has?(:public_method, ActiveSupport, :on_load))
73
+ Rails.configuration.after_initialize {Haml::Template.try_enabling_xss_integration}
74
+ else
75
+ Haml::Template.try_enabling_xss_integration
76
+ end
77
+
78
+ if Haml::Util.rails_root
79
+ # Update init.rb to the current version
80
+ # if it's out of date.
81
+ #
82
+ # We can probably remove this as of v1.9,
83
+ # because the new init file is sufficiently flexible
84
+ # to not need updating.
85
+ rails_init_file = File.join(Haml::Util.rails_root, 'vendor', 'plugins', 'haml', 'init.rb')
86
+ haml_init_file = Haml::Util.scope('init.rb')
87
+ begin
88
+ if File.exists?(rails_init_file)
89
+ require 'fileutils'
90
+ FileUtils.cp(haml_init_file, rails_init_file) unless FileUtils.cmp(rails_init_file, haml_init_file)
91
+ end
92
+ rescue SystemCallError
93
+ Haml::Util.haml_warn <<END
94
+ HAML WARNING:
95
+ #{rails_init_file} is out of date and couldn't be automatically updated.
96
+ Please run `haml --rails #{File.expand_path(Haml::Util.rails_root)}' to update it.
97
+ END
98
+ end
99
+ end
@@ -0,0 +1,16 @@
1
+ # We keep options in its own self-contained file
2
+ # so that we can load it independently in Rails 3,
3
+ # where the full template stuff is lazy-loaded.
4
+
5
+ module Haml
6
+ module Template
7
+ extend self
8
+
9
+ @options = {}
10
+ # The options hash for Haml when used within Rails.
11
+ # See {file:HAML_REFERENCE.md#haml_options the Haml options documentation}.
12
+ #
13
+ # @return [{Symbol => Object}]
14
+ attr_accessor :options
15
+ end
16
+ end
@@ -0,0 +1,58 @@
1
+ # This file makes Haml work with Rails
2
+ # by monkeypatching the core template-compilation methods.
3
+ # This is necessary for versions <= 2.0.1 because the template handler API
4
+ # wasn't sufficiently powerful to deal with caching and so forth.
5
+
6
+ # This module refers to the ActionView module that's part of Ruby on Rails.
7
+ # Haml can be used as an alternate templating engine for it,
8
+ # and includes several modifications to make it more Haml-friendly.
9
+ # The documentation can be found
10
+ # here[http://rubyonrails.org/api/classes/ActionView/Base.html].
11
+ module ActionView
12
+ class Base
13
+ def delegate_template_exists_with_haml(template_path)
14
+ template_exists?(template_path, :haml) && [:haml]
15
+ end
16
+ alias_method :delegate_template_exists_without_haml, :delegate_template_exists?
17
+ alias_method :delegate_template_exists?, :delegate_template_exists_with_haml
18
+
19
+ def compile_template_with_haml(extension, template, file_name, local_assigns)
20
+ return compile_haml(template, file_name, local_assigns) if extension.to_s == "haml"
21
+ compile_template_without_haml(extension, template, file_name, local_assigns)
22
+ end
23
+ alias_method :compile_template_without_haml, :compile_template
24
+ alias_method :compile_template, :compile_template_with_haml
25
+
26
+ def compile_haml(template, file_name, local_assigns)
27
+ render_symbol = assign_method_name(:haml, template, file_name)
28
+ locals = local_assigns.keys
29
+
30
+ @@template_args[render_symbol] ||= {}
31
+ locals_keys = @@template_args[render_symbol].keys | locals
32
+ @@template_args[render_symbol] = Haml::Util.to_hash(locals_keys.map {|k| [k, true]})
33
+
34
+ options = Haml::Template.options.dup
35
+ options[:filename] = file_name || 'compiled-template'
36
+
37
+ begin
38
+ Haml::Engine.new(template, options).def_method(CompiledTemplates, render_symbol, *locals_keys)
39
+ rescue Exception => e
40
+ if logger
41
+ logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
42
+ logger.debug "Backtrace: #{e.backtrace.join("\n")}"
43
+ end
44
+
45
+ base_path = if defined?(extract_base_path_from)
46
+ # Rails 2.0.x
47
+ extract_base_path_from(file_name) || view_paths.first
48
+ else
49
+ # Rails <=1.2.6
50
+ @base_path
51
+ end
52
+ raise ActionView::TemplateError.new(base_path, file_name || template, @assigns, template, e)
53
+ end
54
+
55
+ @@compile_time[render_symbol] = Time.now
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,123 @@
1
+ # This file makes Haml work with Rails
2
+ # using the > 2.0.1 template handler API.
3
+
4
+ module Haml
5
+ # In Rails 3.1+, template handlers don't inherit from anything. In <= 3.0, they do.
6
+ # To avoid messy logic figuring this out, we just inherit from whatever the ERB handler does.
7
+ class Plugin < Haml::Util.av_template_class(:Handlers)::ERB.superclass
8
+ if ((defined?(ActionView::TemplateHandlers) &&
9
+ defined?(ActionView::TemplateHandlers::Compilable)) ||
10
+ (defined?(ActionView::Template) &&
11
+ defined?(ActionView::Template::Handlers) &&
12
+ defined?(ActionView::Template::Handlers::Compilable))) &&
13
+ # In Rails 3.1+, we don't need to include Compilable.
14
+ Haml::Util.av_template_class(:Handlers)::ERB.include?(
15
+ Haml::Util.av_template_class(:Handlers)::Compilable)
16
+ include Haml::Util.av_template_class(:Handlers)::Compilable
17
+ end
18
+
19
+ def handles_encoding?; true; end
20
+
21
+ def compile(template)
22
+ options = Haml::Template.options.dup
23
+
24
+ # template is a template object in Rails >=2.1.0,
25
+ # a source string previously
26
+ if template.respond_to? :source
27
+ # Template has a generic identifier in Rails >=3.0.0
28
+ options[:filename] = template.respond_to?(:identifier) ? template.identifier : template.filename
29
+ source = template.source
30
+ else
31
+ source = template
32
+ end
33
+
34
+ Haml::Engine.new(source, options).send(:precompiled_with_ambles, [])
35
+ end
36
+
37
+ # In Rails 3.1+, #call takes the place of #compile
38
+ def self.call(template)
39
+ new.compile(template)
40
+ end
41
+
42
+ def cache_fragment(block, name = {}, options = nil)
43
+ @view.fragment_for(block, name, options) do
44
+ eval("_hamlout.buffer", block.binding)
45
+ end
46
+ end
47
+ end
48
+
49
+ # Rails 3.0 prints a deprecation warning when block helpers
50
+ # return strings that go unused.
51
+ # We want to print the same deprecation warning,
52
+ # so we have to compile in a method call to check for it.
53
+ #
54
+ # I don't like having this in the compilation pipeline,
55
+ # and I'd like to get rid of it once Rails 3.1 is well-established.
56
+ if defined?(ActionView::OutputBuffer) &&
57
+ Haml::Util.has?(:instance_method, ActionView::OutputBuffer, :append_if_string=)
58
+ module Compiler
59
+ def compile_silent_script_with_haml_block_deprecation(&block)
60
+ unless block && !@node.value[:keyword] &&
61
+ @node.value[:text] =~ ActionView::Template::Handlers::Erubis::BLOCK_EXPR
62
+ return compile_silent_script_without_haml_block_deprecation(&block)
63
+ end
64
+
65
+ @node.value[:text] = "_hamlout.append_if_string= #{@node.value[:text]}"
66
+ compile_silent_script_without_haml_block_deprecation(&block)
67
+ end
68
+ alias_method :compile_silent_script_without_haml_block_deprecation, :compile_silent_script
69
+ alias_method :compile_silent_script, :compile_silent_script_with_haml_block_deprecation
70
+ end
71
+
72
+ class Buffer
73
+ def append_if_string=(value)
74
+ if value.is_a?(String) && !value.is_a?(ActionView::NonConcattingString)
75
+ ActiveSupport::Deprecation.warn("- style block helpers are deprecated. Please use =", caller)
76
+ buffer << value
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ if defined? ActionView::Template and ActionView::Template.respond_to? :register_template_handler
84
+ ActionView::Template
85
+ else
86
+ ActionView::Base
87
+ end.register_template_handler(:haml, Haml::Plugin)
88
+
89
+ # In Rails 2.0.2, ActionView::TemplateError took arguments
90
+ # that we can't fill in from the Haml::Plugin context.
91
+ # Thus, we've got to monkeypatch ActionView::Base to catch the error.
92
+ if defined?(ActionView::TemplateError) &&
93
+ ActionView::TemplateError.instance_method(:initialize).arity == 5
94
+ class ActionView::Base
95
+ def compile_template(handler, template, file_name, local_assigns)
96
+ render_symbol = assign_method_name(handler, template, file_name)
97
+
98
+ # Move begin up two lines so it captures compilation exceptions.
99
+ begin
100
+ render_source = create_template_source(handler, template, render_symbol, local_assigns.keys)
101
+ line_offset = @@template_args[render_symbol].size + handler.line_offset
102
+
103
+ file_name = 'compiled-template' if file_name.blank?
104
+ CompiledTemplates.module_eval(render_source, file_name, -line_offset)
105
+ rescue Exception => e # errors from template code
106
+ if logger
107
+ logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
108
+ logger.debug "Function body: #{render_source}"
109
+ logger.debug "Backtrace: #{e.backtrace.join("\n")}"
110
+ end
111
+
112
+ # There's no way to tell Haml about the filename,
113
+ # so we've got to insert it ourselves.
114
+ e.backtrace[0].gsub!('(haml)', file_name) if e.is_a?(Haml::Error)
115
+
116
+ raise ActionView::TemplateError.new(extract_base_path_from(file_name) || view_paths.first, file_name || template, @assigns, template, e)
117
+ end
118
+
119
+ @@compile_time[render_symbol] = Time.now
120
+ # logger.debug "Compiled template #{file_name || template}\n ==> #{render_symbol}" if logger
121
+ end
122
+ end
123
+ end
data/lib/haml/util.rb ADDED
@@ -0,0 +1,842 @@
1
+ require 'erb'
2
+ require 'set'
3
+ require 'enumerator'
4
+ require 'stringio'
5
+ require 'strscan'
6
+ require 'rbconfig'
7
+
8
+ require 'haml/root'
9
+
10
+ module Haml
11
+ # A module containing various useful functions.
12
+ module Util
13
+ extend self
14
+
15
+ # An array of ints representing the Ruby version number.
16
+ # @api public
17
+ RUBY_VERSION = ::RUBY_VERSION.split(".").map {|s| s.to_i}
18
+
19
+ # The Ruby engine we're running under. Defaults to `"ruby"`
20
+ # if the top-level constant is undefined.
21
+ # @api public
22
+ RUBY_ENGINE = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : "ruby"
23
+
24
+ # Returns the path of a file relative to the Haml root directory.
25
+ #
26
+ # @param file [String] The filename relative to the Haml root
27
+ # @return [String] The filename relative to the the working directory
28
+ def scope(file)
29
+ File.join(Haml::ROOT_DIR, file)
30
+ end
31
+
32
+ # Converts an array of `[key, value]` pairs to a hash.
33
+ #
34
+ # @example
35
+ # to_hash([[:foo, "bar"], [:baz, "bang"]])
36
+ # #=> {:foo => "bar", :baz => "bang"}
37
+ # @param arr [Array<(Object, Object)>] An array of pairs
38
+ # @return [Hash] A hash
39
+ def to_hash(arr)
40
+ Hash[arr.compact]
41
+ end
42
+
43
+ # Maps the keys in a hash according to a block.
44
+ #
45
+ # @example
46
+ # map_keys({:foo => "bar", :baz => "bang"}) {|k| k.to_s}
47
+ # #=> {"foo" => "bar", "baz" => "bang"}
48
+ # @param hash [Hash] The hash to map
49
+ # @yield [key] A block in which the keys are transformed
50
+ # @yieldparam key [Object] The key that should be mapped
51
+ # @yieldreturn [Object] The new value for the key
52
+ # @return [Hash] The mapped hash
53
+ # @see #map_vals
54
+ # @see #map_hash
55
+ def map_keys(hash)
56
+ to_hash(hash.map {|k, v| [yield(k), v]})
57
+ end
58
+
59
+ # Maps the values in a hash according to a block.
60
+ #
61
+ # @example
62
+ # map_values({:foo => "bar", :baz => "bang"}) {|v| v.to_sym}
63
+ # #=> {:foo => :bar, :baz => :bang}
64
+ # @param hash [Hash] The hash to map
65
+ # @yield [value] A block in which the values are transformed
66
+ # @yieldparam value [Object] The value that should be mapped
67
+ # @yieldreturn [Object] The new value for the value
68
+ # @return [Hash] The mapped hash
69
+ # @see #map_keys
70
+ # @see #map_hash
71
+ def map_vals(hash)
72
+ to_hash(hash.map {|k, v| [k, yield(v)]})
73
+ end
74
+
75
+ # Maps the key-value pairs of a hash according to a block.
76
+ #
77
+ # @example
78
+ # map_hash({:foo => "bar", :baz => "bang"}) {|k, v| [k.to_s, v.to_sym]}
79
+ # #=> {"foo" => :bar, "baz" => :bang}
80
+ # @param hash [Hash] The hash to map
81
+ # @yield [key, value] A block in which the key-value pairs are transformed
82
+ # @yieldparam [key] The hash key
83
+ # @yieldparam [value] The hash value
84
+ # @yieldreturn [(Object, Object)] The new value for the `[key, value]` pair
85
+ # @return [Hash] The mapped hash
86
+ # @see #map_keys
87
+ # @see #map_vals
88
+ def map_hash(hash, &block)
89
+ to_hash(hash.map(&block))
90
+ end
91
+
92
+ # Computes the powerset of the given array.
93
+ # This is the set of all subsets of the array.
94
+ #
95
+ # @example
96
+ # powerset([1, 2, 3]) #=>
97
+ # Set[Set[], Set[1], Set[2], Set[3], Set[1, 2], Set[2, 3], Set[1, 3], Set[1, 2, 3]]
98
+ # @param arr [Enumerable]
99
+ # @return [Set<Set>] The subsets of `arr`
100
+ def powerset(arr)
101
+ arr.inject([Set.new].to_set) do |powerset, el|
102
+ new_powerset = Set.new
103
+ powerset.each do |subset|
104
+ new_powerset << subset
105
+ new_powerset << subset + [el]
106
+ end
107
+ new_powerset
108
+ end
109
+ end
110
+
111
+ # Restricts a number to falling within a given range.
112
+ # Returns the number if it falls within the range,
113
+ # or the closest value in the range if it doesn't.
114
+ #
115
+ # @param value [Numeric]
116
+ # @param range [Range<Numeric>]
117
+ # @return [Numeric]
118
+ def restrict(value, range)
119
+ [[value, range.first].max, range.last].min
120
+ end
121
+
122
+ # Concatenates all strings that are adjacent in an array,
123
+ # while leaving other elements as they are.
124
+ #
125
+ # @example
126
+ # merge_adjacent_strings([1, "foo", "bar", 2, "baz"])
127
+ # #=> [1, "foobar", 2, "baz"]
128
+ # @param arr [Array]
129
+ # @return [Array] The enumerable with strings merged
130
+ def merge_adjacent_strings(arr)
131
+ # Optimize for the common case of one element
132
+ return arr if arr.size < 2
133
+ arr.inject([]) do |a, e|
134
+ if e.is_a?(String)
135
+ if a.last.is_a?(String)
136
+ a.last << e
137
+ else
138
+ a << e.dup
139
+ end
140
+ else
141
+ a << e
142
+ end
143
+ a
144
+ end
145
+ end
146
+
147
+ # Intersperses a value in an enumerable, as would be done with `Array#join`
148
+ # but without concatenating the array together afterwards.
149
+ #
150
+ # @param enum [Enumerable]
151
+ # @param val
152
+ # @return [Array]
153
+ def intersperse(enum, val)
154
+ enum.inject([]) {|a, e| a << e << val}[0...-1]
155
+ end
156
+
157
+ # Substitutes a sub-array of one array with another sub-array.
158
+ #
159
+ # @param ary [Array] The array in which to make the substitution
160
+ # @param from [Array] The sequence of elements to replace with `to`
161
+ # @param to [Array] The sequence of elements to replace `from` with
162
+ def substitute(ary, from, to)
163
+ res = ary.dup
164
+ i = 0
165
+ while i < res.size
166
+ if res[i...i+from.size] == from
167
+ res[i...i+from.size] = to
168
+ end
169
+ i += 1
170
+ end
171
+ res
172
+ end
173
+
174
+ # Destructively strips whitespace from the beginning and end
175
+ # of the first and last elements, respectively,
176
+ # in the array (if those elements are strings).
177
+ #
178
+ # @param arr [Array]
179
+ # @return [Array] `arr`
180
+ def strip_string_array(arr)
181
+ arr.first.lstrip! if arr.first.is_a?(String)
182
+ arr.last.rstrip! if arr.last.is_a?(String)
183
+ arr
184
+ end
185
+
186
+ # Return an array of all possible paths through the given arrays.
187
+ #
188
+ # @param arrs [Array<Array>]
189
+ # @return [Array<Arrays>]
190
+ #
191
+ # @example
192
+ # paths([[1, 2], [3, 4], [5]]) #=>
193
+ # # [[1, 3, 5],
194
+ # # [2, 3, 5],
195
+ # # [1, 4, 5],
196
+ # # [2, 4, 5]]
197
+ def paths(arrs)
198
+ arrs.inject([[]]) do |paths, arr|
199
+ flatten(arr.map {|e| paths.map {|path| path + [e]}}, 1)
200
+ end
201
+ end
202
+
203
+ # Computes a single longest common subsequence for `x` and `y`.
204
+ # If there are more than one longest common subsequences,
205
+ # the one returned is that which starts first in `x`.
206
+ #
207
+ # @param x [Array]
208
+ # @param y [Array]
209
+ # @yield [a, b] An optional block to use in place of a check for equality
210
+ # between elements of `x` and `y`.
211
+ # @yieldreturn [Object, nil] If the two values register as equal,
212
+ # this will return the value to use in the LCS array.
213
+ # @return [Array] The LCS
214
+ def lcs(x, y, &block)
215
+ x = [nil, *x]
216
+ y = [nil, *y]
217
+ block ||= proc {|a, b| a == b && a}
218
+ lcs_backtrace(lcs_table(x, y, &block), x, y, x.size-1, y.size-1, &block)
219
+ end
220
+
221
+ # Returns information about the caller of the previous method.
222
+ #
223
+ # @param entry [String] An entry in the `#caller` list, or a similarly formatted string
224
+ # @return [[String, Fixnum, (String, nil)]] An array containing the filename, line, and method name of the caller.
225
+ # The method name may be nil
226
+ def caller_info(entry = caller[1])
227
+ info = entry.scan(/^(.*?):(-?.*?)(?::.*`(.+)')?$/).first
228
+ info[1] = info[1].to_i
229
+ # This is added by Rubinius to designate a block, but we don't care about it.
230
+ info[2].sub!(/ \{\}\Z/, '') if info[2]
231
+ info
232
+ end
233
+
234
+ # Returns whether one version string represents a more recent version than another.
235
+ #
236
+ # @param v1 [String] A version string.
237
+ # @param v2 [String] Another version string.
238
+ # @return [Boolean]
239
+ def version_gt(v1, v2)
240
+ # Construct an array to make sure the shorter version is padded with nil
241
+ Array.new([v1.length, v2.length].max).zip(v1.split("."), v2.split(".")) do |_, p1, p2|
242
+ p1 ||= "0"
243
+ p2 ||= "0"
244
+ release1 = p1 =~ /^[0-9]+$/
245
+ release2 = p2 =~ /^[0-9]+$/
246
+ if release1 && release2
247
+ # Integer comparison if both are full releases
248
+ p1, p2 = p1.to_i, p2.to_i
249
+ next if p1 == p2
250
+ return p1 > p2
251
+ elsif !release1 && !release2
252
+ # String comparison if both are prereleases
253
+ next if p1 == p2
254
+ return p1 > p2
255
+ else
256
+ # If only one is a release, that one is newer
257
+ return release1
258
+ end
259
+ end
260
+ end
261
+
262
+ # Returns whether one version string represents the same or a more
263
+ # recent version than another.
264
+ #
265
+ # @param v1 [String] A version string.
266
+ # @param v2 [String] Another version string.
267
+ # @return [Boolean]
268
+ def version_geq(v1, v2)
269
+ version_gt(v1, v2) || !version_gt(v2, v1)
270
+ end
271
+
272
+ # A wrapper for `Marshal.dump` that calls `#_before_dump` on the object
273
+ # before dumping it, `#_after_dump` afterwards.
274
+ # It also calls `#_around_dump` and passes it a block in which the object is dumped.
275
+ #
276
+ # If any of these methods are undefined, they are not called.
277
+ #
278
+ # @param obj [Object] The object to dump.
279
+ # @return [String] The dumped data.
280
+ def dump(obj)
281
+ obj._before_dump if obj.respond_to?(:_before_dump)
282
+ return Marshal.dump(obj) unless obj.respond_to?(:_around_dump)
283
+ res = nil
284
+ obj._around_dump {res = Marshal.dump(obj)}
285
+ res
286
+ ensure
287
+ obj._after_dump if obj.respond_to?(:_after_dump)
288
+ end
289
+
290
+ # A wrapper for `Marshal.load` that calls `#_after_load` on the object
291
+ # after loading it, if it's defined.
292
+ #
293
+ # @param data [String] The data to load.
294
+ # @return [Object] The loaded object.
295
+ def load(data)
296
+ obj = Marshal.load(data)
297
+ obj._after_load if obj.respond_to?(:_after_load)
298
+ obj
299
+ end
300
+
301
+ # Throws a NotImplementedError for an abstract method.
302
+ #
303
+ # @param obj [Object] `self`
304
+ # @raise [NotImplementedError]
305
+ def abstract(obj)
306
+ raise NotImplementedError.new("#{obj.class} must implement ##{caller_info[2]}")
307
+ end
308
+
309
+ # Silence all output to STDERR within a block.
310
+ #
311
+ # @yield A block in which no output will be printed to STDERR
312
+ def silence_warnings
313
+ the_real_stderr, $stderr = $stderr, StringIO.new
314
+ yield
315
+ ensure
316
+ $stderr = the_real_stderr
317
+ end
318
+
319
+ @@silence_warnings = false
320
+ # Silences all Haml warnings within a block.
321
+ #
322
+ # @yield A block in which no Haml warnings will be printed
323
+ def silence_haml_warnings
324
+ old_silence_warnings = @@silence_warnings
325
+ @@silence_warnings = true
326
+ yield
327
+ ensure
328
+ @@silence_warnings = old_silence_warnings
329
+ end
330
+
331
+ # The same as `Kernel#warn`, but is silenced by \{#silence\_haml\_warnings}.
332
+ #
333
+ # @param msg [String]
334
+ def haml_warn(msg)
335
+ return if @@silence_warnings
336
+ warn(msg)
337
+ end
338
+
339
+ # Try loading Sass. If the `sass` gem isn't installed,
340
+ # print a warning and load from the vendored gem.
341
+ #
342
+ # @return [Boolean] True if Sass was successfully loaded from the `sass` gem,
343
+ # false otherwise.
344
+ def try_sass
345
+ return true if defined?(::SASS_BEGUN_TO_LOAD)
346
+ begin
347
+ require 'sass/version'
348
+ loaded = Sass.respond_to?(:version) && Sass.version[:major] &&
349
+ Sass.version[:minor] && ((Sass.version[:major] > 3 && Sass.version[:minor] > 1) ||
350
+ ((Sass.version[:major] == 3 && Sass.version[:minor] == 1) &&
351
+ (Sass.version[:prerelease] || Sass.version[:name] != "Bleeding Edge")))
352
+ rescue LoadError => e
353
+ loaded = false
354
+ end
355
+
356
+ unless loaded
357
+ haml_warn(<<WARNING)
358
+ Sass is in the process of being separated from Haml,
359
+ and will no longer be bundled at all in Haml 3.2.0.
360
+ Please install the 'sass' gem if you want to use Sass.
361
+ WARNING
362
+ $".delete('sass/version')
363
+ $LOAD_PATH.unshift(scope("vendor/sass/lib"))
364
+ end
365
+ loaded
366
+ end
367
+
368
+ ## Cross Rails Version Compatibility
369
+
370
+ # Returns the root of the Rails application,
371
+ # if this is running in a Rails context.
372
+ # Returns `nil` if no such root is defined.
373
+ #
374
+ # @return [String, nil]
375
+ def rails_root
376
+ if defined?(::Rails.root)
377
+ return ::Rails.root.to_s if ::Rails.root
378
+ raise "ERROR: Rails.root is nil!"
379
+ end
380
+ return RAILS_ROOT.to_s if defined?(RAILS_ROOT)
381
+ return nil
382
+ end
383
+
384
+ # Returns the environment of the Rails application,
385
+ # if this is running in a Rails context.
386
+ # Returns `nil` if no such environment is defined.
387
+ #
388
+ # @return [String, nil]
389
+ def rails_env
390
+ return ::Rails.env.to_s if defined?(::Rails.env)
391
+ return RAILS_ENV.to_s if defined?(RAILS_ENV)
392
+ return nil
393
+ end
394
+
395
+ # Returns whether this environment is using ActionPack
396
+ # version 3.0.0 or greater.
397
+ #
398
+ # @return [Boolean]
399
+ def ap_geq_3?
400
+ ap_geq?("3.0.0.beta1")
401
+ end
402
+
403
+ # Returns whether this environment is using ActionPack
404
+ # of a version greater than or equal to that specified.
405
+ #
406
+ # @param version [String] The string version number to check against.
407
+ # Should be greater than or equal to Rails 3,
408
+ # because otherwise ActionPack::VERSION isn't autoloaded
409
+ # @return [Boolean]
410
+ def ap_geq?(version)
411
+ # The ActionPack module is always loaded automatically in Rails >= 3
412
+ return false unless defined?(ActionPack) && defined?(ActionPack::VERSION) &&
413
+ defined?(ActionPack::VERSION::STRING)
414
+
415
+ version_geq(ActionPack::VERSION::STRING, version)
416
+ end
417
+
418
+ # Returns an ActionView::Template* class.
419
+ # In pre-3.0 versions of Rails, most of these classes
420
+ # were of the form `ActionView::TemplateFoo`,
421
+ # while afterwards they were of the form `ActionView;:Template::Foo`.
422
+ #
423
+ # @param name [#to_s] The name of the class to get.
424
+ # For example, `:Error` will return `ActionView::TemplateError`
425
+ # or `ActionView::Template::Error`.
426
+ def av_template_class(name)
427
+ return ActionView.const_get("Template#{name}") if ActionView.const_defined?("Template#{name}")
428
+ return ActionView::Template.const_get(name.to_s)
429
+ end
430
+
431
+ ## Rails XSS Safety
432
+
433
+ # Whether or not ActionView's XSS protection is available and enabled,
434
+ # as is the default for Rails 3.0+, and optional for version 2.3.5+.
435
+ # Overridden in haml/template.rb if this is the case.
436
+ #
437
+ # @return [Boolean]
438
+ def rails_xss_safe?
439
+ false
440
+ end
441
+
442
+ # Returns the given text, marked as being HTML-safe.
443
+ # With older versions of the Rails XSS-safety mechanism,
444
+ # this destructively modifies the HTML-safety of `text`.
445
+ #
446
+ # @param text [String, nil]
447
+ # @return [String, nil] `text`, marked as HTML-safe
448
+ def html_safe(text)
449
+ return unless text
450
+ return text.html_safe if defined?(ActiveSupport::SafeBuffer)
451
+ text.html_safe!
452
+ end
453
+
454
+ # Assert that a given object (usually a String) is HTML safe
455
+ # according to Rails' XSS handling, if it's loaded.
456
+ #
457
+ # @param text [Object]
458
+ def assert_html_safe!(text)
459
+ return unless rails_xss_safe? && text && !text.to_s.html_safe?
460
+ raise Haml::Error.new("Expected #{text.inspect} to be HTML-safe.")
461
+ end
462
+
463
+ # The class for the Rails SafeBuffer XSS protection class.
464
+ # This varies depending on Rails version.
465
+ #
466
+ # @return [Class]
467
+ def rails_safe_buffer_class
468
+ # It's important that we check ActiveSupport first,
469
+ # because in Rails 2.3.6 ActionView::SafeBuffer exists
470
+ # but is a deprecated proxy object.
471
+ return ActiveSupport::SafeBuffer if defined?(ActiveSupport::SafeBuffer)
472
+ return ActionView::SafeBuffer
473
+ end
474
+
475
+ ## Cross-OS Compatibility
476
+
477
+ # Whether or not this is running on Windows.
478
+ #
479
+ # @return [Boolean]
480
+ def windows?
481
+ RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i
482
+ end
483
+
484
+ # Whether or not this is running on IronRuby.
485
+ #
486
+ # @return [Boolean]
487
+ def ironruby?
488
+ RUBY_ENGINE == "ironruby"
489
+ end
490
+
491
+ ## Cross-Ruby-Version Compatibility
492
+
493
+ # Whether or not this is running under Ruby 1.8 or lower.
494
+ #
495
+ # Note that IronRuby counts as Ruby 1.8,
496
+ # because it doesn't support the Ruby 1.9 encoding API.
497
+ #
498
+ # @return [Boolean]
499
+ def ruby1_8?
500
+ # IronRuby says its version is 1.9, but doesn't support any of the encoding APIs.
501
+ # We have to fall back to 1.8 behavior.
502
+ ironruby? || (Haml::Util::RUBY_VERSION[0] == 1 && Haml::Util::RUBY_VERSION[1] < 9)
503
+ end
504
+
505
+ # Whether or not this is running under Ruby 1.8.6 or lower.
506
+ # Note that lower versions are not officially supported.
507
+ #
508
+ # @return [Boolean]
509
+ def ruby1_8_6?
510
+ ruby1_8? && Haml::Util::RUBY_VERSION[2] < 7
511
+ end
512
+
513
+ # Checks that the encoding of a string is valid in Ruby 1.9
514
+ # and cleans up potential encoding gotchas like the UTF-8 BOM.
515
+ # If it's not, yields an error string describing the invalid character
516
+ # and the line on which it occurrs.
517
+ #
518
+ # @param str [String] The string of which to check the encoding
519
+ # @yield [msg] A block in which an encoding error can be raised.
520
+ # Only yields if there is an encoding error
521
+ # @yieldparam msg [String] The error message to be raised
522
+ # @return [String] `str`, potentially with encoding gotchas like BOMs removed
523
+ def check_encoding(str)
524
+ if ruby1_8?
525
+ return str.gsub(/\A\xEF\xBB\xBF/, '') # Get rid of the UTF-8 BOM
526
+ elsif str.valid_encoding?
527
+ # Get rid of the Unicode BOM if possible
528
+ if str.encoding.name =~ /^UTF-(8|16|32)(BE|LE)?$/
529
+ return str.gsub(Regexp.new("\\A\uFEFF".encode(str.encoding.name)), '')
530
+ else
531
+ return str
532
+ end
533
+ end
534
+
535
+ encoding = str.encoding
536
+ newlines = Regexp.new("\r\n|\r|\n".encode(encoding).force_encoding("binary"))
537
+ str.force_encoding("binary").split(newlines).each_with_index do |line, i|
538
+ begin
539
+ line.encode(encoding)
540
+ rescue Encoding::UndefinedConversionError => e
541
+ yield <<MSG.rstrip, i + 1
542
+ Invalid #{encoding.name} character #{e.error_char.dump}
543
+ MSG
544
+ end
545
+ end
546
+ return str
547
+ end
548
+
549
+ # Like {\#check\_encoding}, but also checks for a Ruby-style `-# coding:` comment
550
+ # at the beginning of the template and uses that encoding if it exists.
551
+ #
552
+ # The Haml encoding rules are simple.
553
+ # If a `-# coding:` comment exists,
554
+ # we assume that that's the original encoding of the document.
555
+ # Otherwise, we use whatever encoding Ruby has.
556
+ #
557
+ # Haml uses the same rules for parsing coding comments as Ruby.
558
+ # This means that it can understand Emacs-style comments
559
+ # (e.g. `-*- encoding: "utf-8" -*-`),
560
+ # and also that it cannot understand non-ASCII-compatible encodings
561
+ # such as `UTF-16` and `UTF-32`.
562
+ #
563
+ # @param str [String] The Haml template of which to check the encoding
564
+ # @yield [msg] A block in which an encoding error can be raised.
565
+ # Only yields if there is an encoding error
566
+ # @yieldparam msg [String] The error message to be raised
567
+ # @return [String] The original string encoded properly
568
+ # @raise [ArgumentError] if the document declares an unknown encoding
569
+ def check_haml_encoding(str, &block)
570
+ return check_encoding(str, &block) if ruby1_8?
571
+ str = str.dup if str.frozen?
572
+
573
+ bom, encoding = parse_haml_magic_comment(str)
574
+ if encoding; str.force_encoding(encoding)
575
+ elsif bom; str.force_encoding("UTF-8")
576
+ end
577
+
578
+ return check_encoding(str, &block)
579
+ end
580
+
581
+ unless ruby1_8?
582
+ # @private
583
+ def _enc(string, encoding)
584
+ string.encode(encoding).force_encoding("BINARY")
585
+ end
586
+
587
+ # We could automatically add in any non-ASCII-compatible encodings here,
588
+ # but there's not really a good way to do that
589
+ # without manually checking that each encoding
590
+ # encodes all ASCII characters properly,
591
+ # which takes long enough to affect the startup time of the CLI.
592
+ ENCODINGS_TO_CHECK = %w[UTF-8 UTF-16BE UTF-16LE UTF-32BE UTF-32LE]
593
+
594
+ CHARSET_REGEXPS = Hash.new do |h, e|
595
+ h[e] =
596
+ begin
597
+ # /\A(?:\uFEFF)?@charset "(.*?)"|\A(\uFEFF)/
598
+ Regexp.new(/\A(?:#{_enc("\uFEFF", e)})?#{
599
+ _enc('@charset "', e)}(.*?)#{_enc('"', e)}|\A(#{
600
+ _enc("\uFEFF", e)})/)
601
+ rescue
602
+ # /\A@charset "(.*?)"/
603
+ Regexp.new(/\A#{_enc('@charset "', e)}(.*?)#{_enc('"', e)}/)
604
+ end
605
+ end
606
+ end
607
+
608
+ # Checks to see if a class has a given method.
609
+ # For example:
610
+ #
611
+ # Haml::Util.has?(:public_instance_method, String, :gsub) #=> true
612
+ #
613
+ # Method collections like `Class#instance_methods`
614
+ # return strings in Ruby 1.8 and symbols in Ruby 1.9 and on,
615
+ # so this handles checking for them in a compatible way.
616
+ #
617
+ # @param attr [#to_s] The (singular) name of the method-collection method
618
+ # (e.g. `:instance_methods`, `:private_methods`)
619
+ # @param klass [Module] The class to check the methods of which to check
620
+ # @param method [String, Symbol] The name of the method do check for
621
+ # @return [Boolean] Whether or not the given collection has the given method
622
+ def has?(attr, klass, method)
623
+ klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym)
624
+ end
625
+
626
+ # A version of `Enumerable#enum_with_index` that works in Ruby 1.8 and 1.9.
627
+ #
628
+ # @param enum [Enumerable] The enumerable to get the enumerator for
629
+ # @return [Enumerator] The with-index enumerator
630
+ def enum_with_index(enum)
631
+ ruby1_8? ? enum.enum_with_index : enum.each_with_index
632
+ end
633
+
634
+ # A version of `Enumerable#enum_cons` that works in Ruby 1.8 and 1.9.
635
+ #
636
+ # @param enum [Enumerable] The enumerable to get the enumerator for
637
+ # @param n [Fixnum] The size of each cons
638
+ # @return [Enumerator] The consed enumerator
639
+ def enum_cons(enum, n)
640
+ ruby1_8? ? enum.enum_cons(n) : enum.each_cons(n)
641
+ end
642
+
643
+ # A version of `Enumerable#enum_slice` that works in Ruby 1.8 and 1.9.
644
+ #
645
+ # @param enum [Enumerable] The enumerable to get the enumerator for
646
+ # @param n [Fixnum] The size of each slice
647
+ # @return [Enumerator] The consed enumerator
648
+ def enum_slice(enum, n)
649
+ ruby1_8? ? enum.enum_slice(n) : enum.each_slice(n)
650
+ end
651
+
652
+ # Returns the ASCII code of the given character.
653
+ #
654
+ # @param c [String] All characters but the first are ignored.
655
+ # @return [Fixnum] The ASCII code of `c`.
656
+ def ord(c)
657
+ ruby1_8? ? c[0] : c.ord
658
+ end
659
+
660
+ # Flattens the first `n` nested arrays in a cross-version manner.
661
+ #
662
+ # @param arr [Array] The array to flatten
663
+ # @param n [Fixnum] The number of levels to flatten
664
+ # @return [Array] The flattened array
665
+ def flatten(arr, n)
666
+ return arr.flatten(n) unless ruby1_8_6?
667
+ return arr if n == 0
668
+ arr.inject([]) {|res, e| e.is_a?(Array) ? res.concat(flatten(e, n - 1)) : res << e}
669
+ end
670
+
671
+ # Returns the hash code for a set in a cross-version manner.
672
+ # Aggravatingly, this is order-dependent in Ruby 1.8.6.
673
+ #
674
+ # @param set [Set]
675
+ # @return [Fixnum] The order-independent hashcode of `set`
676
+ def set_hash(set)
677
+ return set.hash unless ruby1_8_6?
678
+ set.map {|e| e.hash}.uniq.sort.hash
679
+ end
680
+
681
+ # Tests the hash-equality of two sets in a cross-version manner.
682
+ # Aggravatingly, this is order-dependent in Ruby 1.8.6.
683
+ #
684
+ # @param set1 [Set]
685
+ # @param set2 [Set]
686
+ # @return [Boolean] Whether or not the sets are hashcode equal
687
+ def set_eql?(set1, set2)
688
+ return set1.eql?(set2) unless ruby1_8_6?
689
+ set1.to_a.uniq.sort_by {|e| e.hash}.eql?(set2.to_a.uniq.sort_by {|e| e.hash})
690
+ end
691
+
692
+ # Like `Object#inspect`, but preserves non-ASCII characters rather than escaping them under Ruby 1.9.2.
693
+ # This is necessary so that the precompiled Haml template can be `#encode`d into `@options[:encoding]`
694
+ # before being evaluated.
695
+ #
696
+ # @param obj {Object}
697
+ # @return {String}
698
+ def inspect_obj(obj)
699
+ return obj.inspect unless version_geq(::RUBY_VERSION, "1.9.2")
700
+ return ':' + inspect_obj(obj.to_s) if obj.is_a?(Symbol)
701
+ return obj.inspect unless obj.is_a?(String)
702
+ '"' + obj.gsub(/[\x00-\x7F]+/) {|s| s.inspect[1...-1]} + '"'
703
+ end
704
+
705
+ ## Static Method Stuff
706
+
707
+ # The context in which the ERB for \{#def\_static\_method} will be run.
708
+ class StaticConditionalContext
709
+ # @param set [#include?] The set of variables that are defined for this context.
710
+ def initialize(set)
711
+ @set = set
712
+ end
713
+
714
+ # Checks whether or not a variable is defined for this context.
715
+ #
716
+ # @param name [Symbol] The name of the variable
717
+ # @return [Boolean]
718
+ def method_missing(name, *args, &block)
719
+ super unless args.empty? && block.nil?
720
+ @set.include?(name)
721
+ end
722
+ end
723
+
724
+ # This is used for methods in {Haml::Buffer} that need to be very fast,
725
+ # and take a lot of boolean parameters
726
+ # that are known at compile-time.
727
+ # Instead of passing the parameters in normally,
728
+ # a separate method is defined for every possible combination of those parameters;
729
+ # these are then called using \{#static\_method\_name}.
730
+ #
731
+ # To define a static method, an ERB template for the method is provided.
732
+ # All conditionals based on the static parameters
733
+ # are done as embedded Ruby within this template.
734
+ # For example:
735
+ #
736
+ # def_static_method(Foo, :my_static_method, [:foo, :bar], :baz, :bang, <<RUBY)
737
+ # <% if baz && bang %>
738
+ # return foo + bar
739
+ # <% elsif baz || bang %>
740
+ # return foo - bar
741
+ # <% else %>
742
+ # return 17
743
+ # <% end %>
744
+ # RUBY
745
+ #
746
+ # \{#static\_method\_name} can be used to call static methods.
747
+ #
748
+ # @overload def_static_method(klass, name, args, *vars, erb)
749
+ # @param klass [Module] The class on which to define the static method
750
+ # @param name [#to_s] The (base) name of the static method
751
+ # @param args [Array<Symbol>] The names of the arguments to the defined methods
752
+ # (**not** to the ERB template)
753
+ # @param vars [Array<Symbol>] The names of the static boolean variables
754
+ # to be made available to the ERB template
755
+ # @param erb [String] The template for the method code
756
+ def def_static_method(klass, name, args, *vars)
757
+ erb = vars.pop
758
+ info = caller_info
759
+ powerset(vars).each do |set|
760
+ context = StaticConditionalContext.new(set).instance_eval {binding}
761
+ klass.class_eval(<<METHOD, info[0], info[1])
762
+ def #{static_method_name(name, *vars.map {|v| set.include?(v)})}(#{args.join(', ')})
763
+ #{ERB.new(erb).result(context)}
764
+ end
765
+ METHOD
766
+ end
767
+ end
768
+
769
+ # Computes the name for a method defined via \{#def\_static\_method}.
770
+ #
771
+ # @param name [String] The base name of the static method
772
+ # @param vars [Array<Boolean>] The static variable assignment
773
+ # @return [String] The real name of the static method
774
+ def static_method_name(name, *vars)
775
+ "#{name}_#{vars.map {|v| !!v}.join('_')}"
776
+ end
777
+
778
+ private
779
+
780
+ # Calculates the memoization table for the Least Common Subsequence algorithm.
781
+ # Algorithm from [Wikipedia](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Computing_the_length_of_the_LCS)
782
+ def lcs_table(x, y)
783
+ c = Array.new(x.size) {[]}
784
+ x.size.times {|i| c[i][0] = 0}
785
+ y.size.times {|j| c[0][j] = 0}
786
+ (1...x.size).each do |i|
787
+ (1...y.size).each do |j|
788
+ c[i][j] =
789
+ if yield x[i], y[j]
790
+ c[i-1][j-1] + 1
791
+ else
792
+ [c[i][j-1], c[i-1][j]].max
793
+ end
794
+ end
795
+ end
796
+ return c
797
+ end
798
+
799
+ # Computes a single longest common subsequence for arrays x and y.
800
+ # Algorithm from [Wikipedia](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Reading_out_an_LCS)
801
+ def lcs_backtrace(c, x, y, i, j, &block)
802
+ return [] if i == 0 || j == 0
803
+ if v = yield(x[i], y[j])
804
+ return lcs_backtrace(c, x, y, i-1, j-1, &block) << v
805
+ end
806
+
807
+ return lcs_backtrace(c, x, y, i, j-1, &block) if c[i][j-1] > c[i-1][j]
808
+ return lcs_backtrace(c, x, y, i-1, j, &block)
809
+ end
810
+
811
+ # Parses a magic comment at the beginning of a Haml file.
812
+ # The parsing rules are basically the same as Ruby's.
813
+ #
814
+ # @return [(Boolean, String or nil)]
815
+ # Whether the document begins with a UTF-8 BOM,
816
+ # and the declared encoding of the document (or nil if none is declared)
817
+ def parse_haml_magic_comment(str)
818
+ scanner = StringScanner.new(str.dup.force_encoding("BINARY"))
819
+ bom = scanner.scan(/\xEF\xBB\xBF/n)
820
+ return bom unless scanner.scan(/-\s*#\s*/n)
821
+ if coding = try_parse_haml_emacs_magic_comment(scanner)
822
+ return bom, coding
823
+ end
824
+
825
+ return bom unless scanner.scan(/.*?coding[=:]\s*([\w-]+)/in)
826
+ return bom, scanner[1]
827
+ end
828
+
829
+ def try_parse_haml_emacs_magic_comment(scanner)
830
+ pos = scanner.pos
831
+ return unless scanner.scan(/.*?-\*-\s*/n)
832
+ # From Ruby's parse.y
833
+ return unless scanner.scan(/([^\s'":;]+)\s*:\s*("(?:\\.|[^"])*"|[^"\s;]+?)[\s;]*-\*-/n)
834
+ name, val = scanner[1], scanner[2]
835
+ return unless name =~ /(en)?coding/in
836
+ val = $1 if val =~ /^"(.*)"$/n
837
+ return val
838
+ ensure
839
+ scanner.pos = pos
840
+ end
841
+ end
842
+ end