haml_ejs 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.yardopts +11 -0
- data/CONTRIBUTING +3 -0
- data/MIT-LICENSE +20 -0
- data/README.md +68 -0
- data/Rakefile +419 -0
- data/VERSION +1 -0
- data/VERSION_NAME +1 -0
- data/bin/haml +9 -0
- data/bin/html2haml +7 -0
- data/extra/update_watch.rb +13 -0
- data/init.rb +18 -0
- data/lib/haml.rb +49 -0
- data/lib/haml/buffer.rb +297 -0
- data/lib/haml/compiler.rb +486 -0
- data/lib/haml/engine.rb +312 -0
- data/lib/haml/error.rb +22 -0
- data/lib/haml/exec.rb +362 -0
- data/lib/haml/filters.rb +388 -0
- data/lib/haml/haml_ejs.rb +57 -0
- data/lib/haml/helpers.rb +608 -0
- data/lib/haml/helpers/action_view_extensions.rb +57 -0
- data/lib/haml/helpers/action_view_mods.rb +257 -0
- data/lib/haml/helpers/xss_mods.rb +165 -0
- data/lib/haml/html.rb +412 -0
- data/lib/haml/html/erb.rb +141 -0
- data/lib/haml/parser.rb +760 -0
- data/lib/haml/railtie.rb +19 -0
- data/lib/haml/root.rb +7 -0
- data/lib/haml/shared.rb +78 -0
- data/lib/haml/template.rb +99 -0
- data/lib/haml/template/options.rb +16 -0
- data/lib/haml/template/patch.rb +58 -0
- data/lib/haml/template/plugin.rb +123 -0
- data/lib/haml/util.rb +842 -0
- data/lib/haml/version.rb +109 -0
- data/lib/sass.rb +8 -0
- data/lib/sass/plugin.rb +10 -0
- data/lib/sass/rails2_shim.rb +9 -0
- data/lib/sass/rails3_shim.rb +16 -0
- data/rails/init.rb +1 -0
- data/test/benchmark.rb +91 -0
- data/test/gemfiles/Gemfile.rails-2.0.x +8 -0
- data/test/gemfiles/Gemfile.rails-2.0.x.lock +38 -0
- data/test/gemfiles/Gemfile.rails-2.1.x +8 -0
- data/test/gemfiles/Gemfile.rails-2.1.x.lock +38 -0
- data/test/gemfiles/Gemfile.rails-2.2.x +8 -0
- data/test/gemfiles/Gemfile.rails-2.2.x.lock +38 -0
- data/test/gemfiles/Gemfile.rails-2.3.x +8 -0
- data/test/gemfiles/Gemfile.rails-2.3.x.lock +40 -0
- data/test/gemfiles/Gemfile.rails-3.0.x +8 -0
- data/test/gemfiles/Gemfile.rails-3.0.x.lock +85 -0
- data/test/gemfiles/Gemfile.rails-3.1.x +8 -0
- data/test/gemfiles/Gemfile.rails-3.1.x.lock +98 -0
- data/test/gemfiles/Gemfile.rails-xss-2.3.x +9 -0
- data/test/gemfiles/Gemfile.rails-xss-2.3.x.lock +42 -0
- data/test/haml-ejs/haml-ejs_test.rb +22 -0
- data/test/haml-ejs/templates/conditional.input +3 -0
- data/test/haml-ejs/templates/conditional.output +3 -0
- data/test/haml-ejs/templates/conditional_and_interpolation.input +2 -0
- data/test/haml-ejs/templates/conditional_and_interpolation.output +3 -0
- data/test/haml-ejs/templates/conditional_else.input +5 -0
- data/test/haml-ejs/templates/conditional_else.output +5 -0
- data/test/haml-ejs/templates/conditional_else_elsif.input +7 -0
- data/test/haml-ejs/templates/conditional_else_elsif.output +7 -0
- data/test/haml-ejs/templates/conditional_elsif.input +5 -0
- data/test/haml-ejs/templates/conditional_elsif.output +5 -0
- data/test/haml-ejs/templates/conditional_unless.input +3 -0
- data/test/haml-ejs/templates/conditional_unless.output +3 -0
- data/test/haml-ejs/templates/conditional_unless_else.input +5 -0
- data/test/haml-ejs/templates/conditional_unless_else.output +5 -0
- data/test/haml-ejs/templates/conditional_unless_elsif_else.input +5 -0
- data/test/haml-ejs/templates/conditional_unless_elsif_else.output +5 -0
- data/test/haml-ejs/templates/interpolation.input +2 -0
- data/test/haml-ejs/templates/interpolation.output +1 -0
- data/test/haml-ejs/templates/iteration.input +2 -0
- data/test/haml-ejs/templates/iteration.output +3 -0
- data/test/haml-ejs/templates/suite.input +26 -0
- data/test/haml-ejs/templates/suite.output +27 -0
- data/test/haml/engine_test.rb +1925 -0
- data/test/haml/erb/_av_partial_1.erb +12 -0
- data/test/haml/erb/_av_partial_2.erb +8 -0
- data/test/haml/erb/action_view.erb +62 -0
- data/test/haml/erb/standard.erb +55 -0
- data/test/haml/helper_test.rb +454 -0
- data/test/haml/html2haml/erb_tests.rb +440 -0
- data/test/haml/html2haml_test.rb +336 -0
- data/test/haml/markaby/standard.mab +52 -0
- data/test/haml/mocks/article.rb +6 -0
- data/test/haml/results/content_for_layout.xhtml +12 -0
- data/test/haml/results/eval_suppressed.xhtml +9 -0
- data/test/haml/results/filters.xhtml +62 -0
- data/test/haml/results/helpers.xhtml +70 -0
- data/test/haml/results/helpful.xhtml +10 -0
- data/test/haml/results/just_stuff.xhtml +70 -0
- data/test/haml/results/list.xhtml +12 -0
- data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
- data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
- data/test/haml/results/original_engine.xhtml +20 -0
- data/test/haml/results/partial_layout.xhtml +5 -0
- data/test/haml/results/partials.xhtml +21 -0
- data/test/haml/results/render_layout.xhtml +3 -0
- data/test/haml/results/silent_script.xhtml +74 -0
- data/test/haml/results/standard.xhtml +162 -0
- data/test/haml/results/tag_parsing.xhtml +23 -0
- data/test/haml/results/very_basic.xhtml +5 -0
- data/test/haml/results/whitespace_handling.xhtml +89 -0
- data/test/haml/spec/README.md +97 -0
- data/test/haml/spec/lua_haml_spec.lua +30 -0
- data/test/haml/spec/ruby_haml_test.rb +19 -0
- data/test/haml/spec/tests.json +534 -0
- data/test/haml/spec_test.rb +44 -0
- data/test/haml/template_test.rb +419 -0
- data/test/haml/templates/_av_partial_1.haml +9 -0
- data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
- data/test/haml/templates/_av_partial_2.haml +5 -0
- data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
- data/test/haml/templates/_layout.erb +3 -0
- data/test/haml/templates/_layout_for_partial.haml +3 -0
- data/test/haml/templates/_partial.haml +8 -0
- data/test/haml/templates/_text_area.haml +3 -0
- data/test/haml/templates/action_view.haml +47 -0
- data/test/haml/templates/action_view_ugly.haml +47 -0
- data/test/haml/templates/breakage.haml +8 -0
- data/test/haml/templates/content_for_layout.haml +8 -0
- data/test/haml/templates/eval_suppressed.haml +11 -0
- data/test/haml/templates/filters.haml +66 -0
- data/test/haml/templates/helpers.haml +55 -0
- data/test/haml/templates/helpful.haml +11 -0
- data/test/haml/templates/just_stuff.haml +85 -0
- data/test/haml/templates/list.haml +12 -0
- data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
- data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
- data/test/haml/templates/original_engine.haml +17 -0
- data/test/haml/templates/partial_layout.haml +10 -0
- data/test/haml/templates/partialize.haml +1 -0
- data/test/haml/templates/partials.haml +12 -0
- data/test/haml/templates/render_layout.haml +2 -0
- data/test/haml/templates/silent_script.haml +40 -0
- data/test/haml/templates/standard.haml +43 -0
- data/test/haml/templates/standard_ugly.haml +43 -0
- data/test/haml/templates/tag_parsing.haml +21 -0
- data/test/haml/templates/very_basic.haml +4 -0
- data/test/haml/templates/whitespace_handling.haml +87 -0
- data/test/haml/util_test.rb +300 -0
- data/test/linked_rails.rb +42 -0
- data/test/test_helper.rb +75 -0
- data/vendor/sass/CONTRIBUTING +3 -0
- data/vendor/sass/MIT-LICENSE +20 -0
- data/vendor/sass/README.md +201 -0
- data/vendor/sass/Rakefile +339 -0
- data/vendor/sass/TODO +39 -0
- data/vendor/sass/VERSION +1 -0
- data/vendor/sass/VERSION_NAME +1 -0
- data/vendor/sass/bin/sass +8 -0
- data/vendor/sass/bin/sass-convert +7 -0
- data/vendor/sass/bin/scss +8 -0
- data/vendor/sass/doc-src/FAQ.md +35 -0
- data/vendor/sass/doc-src/INDENTED_SYNTAX.md +210 -0
- data/vendor/sass/doc-src/SASS_CHANGELOG.md +2214 -0
- data/vendor/sass/doc-src/SASS_REFERENCE.md +1957 -0
- data/vendor/sass/doc-src/SCSS_FOR_SASS_USERS.md +155 -0
- data/vendor/sass/ext/extconf.rb +10 -0
- data/vendor/sass/extra/update_watch.rb +13 -0
- data/vendor/sass/init.rb +18 -0
- data/vendor/sass/lib/sass.rb +72 -0
- data/vendor/sass/lib/sass/cache_stores.rb +15 -0
- data/vendor/sass/lib/sass/cache_stores/base.rb +84 -0
- data/vendor/sass/lib/sass/cache_stores/chain.rb +33 -0
- data/vendor/sass/lib/sass/cache_stores/filesystem.rb +58 -0
- data/vendor/sass/lib/sass/cache_stores/memory.rb +47 -0
- data/vendor/sass/lib/sass/cache_stores/null.rb +25 -0
- data/vendor/sass/lib/sass/callbacks.rb +66 -0
- data/vendor/sass/lib/sass/css.rb +294 -0
- data/vendor/sass/lib/sass/engine.rb +862 -0
- data/vendor/sass/lib/sass/environment.rb +155 -0
- data/vendor/sass/lib/sass/error.rb +201 -0
- data/vendor/sass/lib/sass/exec.rb +659 -0
- data/vendor/sass/lib/sass/importers.rb +22 -0
- data/vendor/sass/lib/sass/importers/base.rb +138 -0
- data/vendor/sass/lib/sass/importers/filesystem.rb +144 -0
- data/vendor/sass/lib/sass/less.rb +382 -0
- data/vendor/sass/lib/sass/plugin.rb +136 -0
- data/vendor/sass/lib/sass/plugin/compiler.rb +358 -0
- data/vendor/sass/lib/sass/plugin/configuration.rb +125 -0
- data/vendor/sass/lib/sass/plugin/generic.rb +15 -0
- data/vendor/sass/lib/sass/plugin/merb.rb +48 -0
- data/vendor/sass/lib/sass/plugin/rack.rb +60 -0
- data/vendor/sass/lib/sass/plugin/rails.rb +47 -0
- data/vendor/sass/lib/sass/plugin/staleness_checker.rb +173 -0
- data/vendor/sass/lib/sass/railtie.rb +9 -0
- data/vendor/sass/lib/sass/repl.rb +58 -0
- data/vendor/sass/lib/sass/root.rb +7 -0
- data/vendor/sass/lib/sass/script.rb +40 -0
- data/vendor/sass/lib/sass/script/bool.rb +18 -0
- data/vendor/sass/lib/sass/script/color.rb +480 -0
- data/vendor/sass/lib/sass/script/css_lexer.rb +29 -0
- data/vendor/sass/lib/sass/script/css_parser.rb +31 -0
- data/vendor/sass/lib/sass/script/funcall.rb +162 -0
- data/vendor/sass/lib/sass/script/functions.rb +1343 -0
- data/vendor/sass/lib/sass/script/interpolation.rb +70 -0
- data/vendor/sass/lib/sass/script/lexer.rb +334 -0
- data/vendor/sass/lib/sass/script/list.rb +76 -0
- data/vendor/sass/lib/sass/script/literal.rb +245 -0
- data/vendor/sass/lib/sass/script/node.rb +91 -0
- data/vendor/sass/lib/sass/script/number.rb +429 -0
- data/vendor/sass/lib/sass/script/operation.rb +91 -0
- data/vendor/sass/lib/sass/script/parser.rb +467 -0
- data/vendor/sass/lib/sass/script/string.rb +51 -0
- data/vendor/sass/lib/sass/script/string_interpolation.rb +94 -0
- data/vendor/sass/lib/sass/script/unary_operation.rb +57 -0
- data/vendor/sass/lib/sass/script/variable.rb +54 -0
- data/vendor/sass/lib/sass/scss.rb +17 -0
- data/vendor/sass/lib/sass/scss/css_parser.rb +46 -0
- data/vendor/sass/lib/sass/scss/parser.rb +920 -0
- data/vendor/sass/lib/sass/scss/rx.rb +127 -0
- data/vendor/sass/lib/sass/scss/sass_parser.rb +11 -0
- data/vendor/sass/lib/sass/scss/script_lexer.rb +15 -0
- data/vendor/sass/lib/sass/scss/script_parser.rb +25 -0
- data/vendor/sass/lib/sass/scss/static_parser.rb +40 -0
- data/vendor/sass/lib/sass/selector.rb +361 -0
- data/vendor/sass/lib/sass/selector/abstract_sequence.rb +62 -0
- data/vendor/sass/lib/sass/selector/comma_sequence.rb +81 -0
- data/vendor/sass/lib/sass/selector/sequence.rb +233 -0
- data/vendor/sass/lib/sass/selector/simple.rb +113 -0
- data/vendor/sass/lib/sass/selector/simple_sequence.rb +134 -0
- data/vendor/sass/lib/sass/shared.rb +78 -0
- data/vendor/sass/lib/sass/tree/charset_node.rb +22 -0
- data/vendor/sass/lib/sass/tree/comment_node.rb +77 -0
- data/vendor/sass/lib/sass/tree/debug_node.rb +18 -0
- data/vendor/sass/lib/sass/tree/directive_node.rb +23 -0
- data/vendor/sass/lib/sass/tree/each_node.rb +24 -0
- data/vendor/sass/lib/sass/tree/extend_node.rb +29 -0
- data/vendor/sass/lib/sass/tree/for_node.rb +36 -0
- data/vendor/sass/lib/sass/tree/function_node.rb +27 -0
- data/vendor/sass/lib/sass/tree/if_node.rb +65 -0
- data/vendor/sass/lib/sass/tree/import_node.rb +68 -0
- data/vendor/sass/lib/sass/tree/media_node.rb +32 -0
- data/vendor/sass/lib/sass/tree/mixin_def_node.rb +27 -0
- data/vendor/sass/lib/sass/tree/mixin_node.rb +32 -0
- data/vendor/sass/lib/sass/tree/node.rb +204 -0
- data/vendor/sass/lib/sass/tree/prop_node.rb +155 -0
- data/vendor/sass/lib/sass/tree/return_node.rb +18 -0
- data/vendor/sass/lib/sass/tree/root_node.rb +28 -0
- data/vendor/sass/lib/sass/tree/rule_node.rb +129 -0
- data/vendor/sass/lib/sass/tree/variable_node.rb +30 -0
- data/vendor/sass/lib/sass/tree/visitors/base.rb +75 -0
- data/vendor/sass/lib/sass/tree/visitors/check_nesting.rb +134 -0
- data/vendor/sass/lib/sass/tree/visitors/convert.rb +255 -0
- data/vendor/sass/lib/sass/tree/visitors/cssize.rb +175 -0
- data/vendor/sass/lib/sass/tree/visitors/perform.rb +301 -0
- data/vendor/sass/lib/sass/tree/visitors/to_css.rb +216 -0
- data/vendor/sass/lib/sass/tree/warn_node.rb +18 -0
- data/vendor/sass/lib/sass/tree/while_node.rb +18 -0
- data/vendor/sass/lib/sass/util.rb +669 -0
- data/vendor/sass/lib/sass/util/subset_map.rb +101 -0
- data/vendor/sass/lib/sass/version.rb +112 -0
- data/vendor/sass/rails/init.rb +1 -0
- data/vendor/sass/sass.gemspec +32 -0
- data/vendor/sass/test/sass/cache_test.rb +74 -0
- data/vendor/sass/test/sass/callbacks_test.rb +61 -0
- data/vendor/sass/test/sass/conversion_test.rb +1203 -0
- data/vendor/sass/test/sass/css2sass_test.rb +364 -0
- data/vendor/sass/test/sass/data/hsl-rgb.txt +319 -0
- data/vendor/sass/test/sass/engine_test.rb +2469 -0
- data/vendor/sass/test/sass/extend_test.rb +1348 -0
- data/vendor/sass/test/sass/functions_test.rb +1025 -0
- data/vendor/sass/test/sass/importer_test.rb +82 -0
- data/vendor/sass/test/sass/less_conversion_test.rb +653 -0
- data/vendor/sass/test/sass/mock_importer.rb +49 -0
- data/vendor/sass/test/sass/more_results/more1.css +9 -0
- data/vendor/sass/test/sass/more_results/more1_with_line_comments.css +26 -0
- data/vendor/sass/test/sass/more_results/more_import.css +29 -0
- data/vendor/sass/test/sass/more_templates/_more_partial.sass +2 -0
- data/vendor/sass/test/sass/more_templates/more1.sass +23 -0
- data/vendor/sass/test/sass/more_templates/more_import.sass +11 -0
- data/vendor/sass/test/sass/plugin_test.rb +469 -0
- data/vendor/sass/test/sass/results/alt.css +4 -0
- data/vendor/sass/test/sass/results/basic.css +9 -0
- data/vendor/sass/test/sass/results/compact.css +5 -0
- data/vendor/sass/test/sass/results/complex.css +86 -0
- data/vendor/sass/test/sass/results/compressed.css +1 -0
- data/vendor/sass/test/sass/results/expanded.css +19 -0
- data/vendor/sass/test/sass/results/if.css +3 -0
- data/vendor/sass/test/sass/results/import.css +31 -0
- data/vendor/sass/test/sass/results/import_charset.css +4 -0
- data/vendor/sass/test/sass/results/import_charset_1_8.css +4 -0
- data/vendor/sass/test/sass/results/import_charset_ibm866.css +4 -0
- data/vendor/sass/test/sass/results/line_numbers.css +49 -0
- data/vendor/sass/test/sass/results/mixins.css +95 -0
- data/vendor/sass/test/sass/results/multiline.css +24 -0
- data/vendor/sass/test/sass/results/nested.css +22 -0
- data/vendor/sass/test/sass/results/options.css +1 -0
- data/vendor/sass/test/sass/results/parent_ref.css +13 -0
- data/vendor/sass/test/sass/results/script.css +16 -0
- data/vendor/sass/test/sass/results/scss_import.css +31 -0
- data/vendor/sass/test/sass/results/scss_importee.css +2 -0
- data/vendor/sass/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
- data/vendor/sass/test/sass/results/subdir/subdir.css +3 -0
- data/vendor/sass/test/sass/results/units.css +11 -0
- data/vendor/sass/test/sass/results/warn.css +0 -0
- data/vendor/sass/test/sass/results/warn_imported.css +0 -0
- data/vendor/sass/test/sass/script_conversion_test.rb +283 -0
- data/vendor/sass/test/sass/script_test.rb +496 -0
- data/vendor/sass/test/sass/scss/css_test.rb +916 -0
- data/vendor/sass/test/sass/scss/rx_test.rb +156 -0
- data/vendor/sass/test/sass/scss/scss_test.rb +1249 -0
- data/vendor/sass/test/sass/scss/test_helper.rb +37 -0
- data/vendor/sass/test/sass/templates/_imported_charset_ibm866.sass +4 -0
- data/vendor/sass/test/sass/templates/_imported_charset_utf8.sass +4 -0
- data/vendor/sass/test/sass/templates/_partial.sass +2 -0
- data/vendor/sass/test/sass/templates/alt.sass +16 -0
- data/vendor/sass/test/sass/templates/basic.sass +23 -0
- data/vendor/sass/test/sass/templates/bork1.sass +2 -0
- data/vendor/sass/test/sass/templates/bork2.sass +2 -0
- data/vendor/sass/test/sass/templates/bork3.sass +2 -0
- data/vendor/sass/test/sass/templates/bork4.sass +2 -0
- data/vendor/sass/test/sass/templates/compact.sass +17 -0
- data/vendor/sass/test/sass/templates/complex.sass +305 -0
- data/vendor/sass/test/sass/templates/compressed.sass +15 -0
- data/vendor/sass/test/sass/templates/expanded.sass +17 -0
- data/vendor/sass/test/sass/templates/if.sass +11 -0
- data/vendor/sass/test/sass/templates/import.sass +12 -0
- data/vendor/sass/test/sass/templates/import_charset.sass +7 -0
- data/vendor/sass/test/sass/templates/import_charset_1_8.sass +4 -0
- data/vendor/sass/test/sass/templates/import_charset_ibm866.sass +9 -0
- data/vendor/sass/test/sass/templates/importee.less +2 -0
- data/vendor/sass/test/sass/templates/importee.sass +19 -0
- data/vendor/sass/test/sass/templates/line_numbers.sass +13 -0
- data/vendor/sass/test/sass/templates/mixin_bork.sass +5 -0
- data/vendor/sass/test/sass/templates/mixins.sass +76 -0
- data/vendor/sass/test/sass/templates/multiline.sass +20 -0
- data/vendor/sass/test/sass/templates/nested.sass +25 -0
- data/vendor/sass/test/sass/templates/nested_bork1.sass +2 -0
- data/vendor/sass/test/sass/templates/nested_bork2.sass +2 -0
- data/vendor/sass/test/sass/templates/nested_bork3.sass +2 -0
- data/vendor/sass/test/sass/templates/nested_bork4.sass +2 -0
- data/vendor/sass/test/sass/templates/nested_import.sass +2 -0
- data/vendor/sass/test/sass/templates/nested_mixin_bork.sass +6 -0
- data/vendor/sass/test/sass/templates/options.sass +2 -0
- data/vendor/sass/test/sass/templates/parent_ref.sass +25 -0
- data/vendor/sass/test/sass/templates/script.sass +101 -0
- data/vendor/sass/test/sass/templates/scss_import.scss +11 -0
- data/vendor/sass/test/sass/templates/scss_importee.scss +1 -0
- data/vendor/sass/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
- data/vendor/sass/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
- data/vendor/sass/test/sass/templates/subdir/subdir.sass +6 -0
- data/vendor/sass/test/sass/templates/units.sass +11 -0
- data/vendor/sass/test/sass/templates/warn.sass +3 -0
- data/vendor/sass/test/sass/templates/warn_imported.sass +4 -0
- data/vendor/sass/test/sass/test_helper.rb +8 -0
- data/vendor/sass/test/sass/util/subset_map_test.rb +91 -0
- data/vendor/sass/test/sass/util_test.rb +254 -0
- data/vendor/sass/test/test_helper.rb +69 -0
- data/vendor/sass/yard/callbacks.rb +29 -0
- data/vendor/sass/yard/default/fulldoc/html/css/common.sass +26 -0
- data/vendor/sass/yard/default/layout/html/footer.erb +12 -0
- data/vendor/sass/yard/inherited_hash.rb +41 -0
- metadata +458 -0
data/lib/haml/engine.rb
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
require 'haml/helpers'
|
|
2
|
+
require 'haml/buffer'
|
|
3
|
+
require 'haml/parser'
|
|
4
|
+
require 'haml/compiler'
|
|
5
|
+
require 'haml/filters'
|
|
6
|
+
require 'haml/error'
|
|
7
|
+
|
|
8
|
+
module Haml
|
|
9
|
+
# This is the frontend for using Haml programmatically.
|
|
10
|
+
# It can be directly used by the user by creating a
|
|
11
|
+
# new instance and calling \{#render} to render the template.
|
|
12
|
+
# For example:
|
|
13
|
+
#
|
|
14
|
+
# template = File.read('templates/really_cool_template.haml')
|
|
15
|
+
# haml_engine = Haml::Engine.new(template)
|
|
16
|
+
# output = haml_engine.render
|
|
17
|
+
# puts output
|
|
18
|
+
class Engine
|
|
19
|
+
include Parser
|
|
20
|
+
include Compiler
|
|
21
|
+
|
|
22
|
+
# The options hash.
|
|
23
|
+
# See {file:HAML_REFERENCE.md#haml_options the Haml options documentation}.
|
|
24
|
+
#
|
|
25
|
+
# @return [{Symbol => Object}]
|
|
26
|
+
attr_accessor :options
|
|
27
|
+
|
|
28
|
+
# The indentation used in the Haml document,
|
|
29
|
+
# or `nil` if the indentation is ambiguous
|
|
30
|
+
# (for example, for a single-level document).
|
|
31
|
+
#
|
|
32
|
+
# @return [String]
|
|
33
|
+
attr_accessor :indentation
|
|
34
|
+
|
|
35
|
+
# @return [Boolean] Whether or not the format is XHTML.
|
|
36
|
+
def xhtml?
|
|
37
|
+
not html?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @return [Boolean] Whether or not the format is any flavor of HTML.
|
|
41
|
+
def html?
|
|
42
|
+
html4? or html5?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @return [Boolean] Whether or not the format is HTML4.
|
|
46
|
+
def html4?
|
|
47
|
+
@options[:format] == :html4
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @return [Boolean] Whether or not the format is HTML5.
|
|
51
|
+
def html5?
|
|
52
|
+
@options[:format] == :html5
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# The source code that is evaluated to produce the Haml document.
|
|
56
|
+
#
|
|
57
|
+
# In Ruby 1.9, this is automatically converted to the correct encoding
|
|
58
|
+
# (see {file:HAML_REFERENCE.md#encoding-option the `:encoding` option}).
|
|
59
|
+
#
|
|
60
|
+
# @return [String]
|
|
61
|
+
def precompiled
|
|
62
|
+
return @precompiled if ruby1_8?
|
|
63
|
+
encoding = Encoding.find(@options[:encoding])
|
|
64
|
+
return @precompiled.force_encoding(encoding) if encoding == Encoding::BINARY
|
|
65
|
+
return @precompiled.encode(encoding)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Precompiles the Haml template.
|
|
69
|
+
#
|
|
70
|
+
# @param template [String] The Haml template
|
|
71
|
+
# @param options [{Symbol => Object}] An options hash;
|
|
72
|
+
# see {file:HAML_REFERENCE.md#haml_options the Haml options documentation}
|
|
73
|
+
# @raise [Haml::Error] if there's a Haml syntax error in the template
|
|
74
|
+
def initialize(template, options = {})
|
|
75
|
+
@options = {
|
|
76
|
+
:suppress_eval => false,
|
|
77
|
+
:attr_wrapper => "'",
|
|
78
|
+
|
|
79
|
+
# Don't forget to update the docs in doc-src/HAML_REFERENCE.md
|
|
80
|
+
# if you update these
|
|
81
|
+
:autoclose => %w[meta img link br hr input area param col base],
|
|
82
|
+
:preserve => %w[textarea pre code],
|
|
83
|
+
|
|
84
|
+
:filename => '(haml)',
|
|
85
|
+
:line => 1,
|
|
86
|
+
:ugly => false,
|
|
87
|
+
:format => :xhtml,
|
|
88
|
+
:escape_html => false,
|
|
89
|
+
:escape_attrs => true,
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
template = check_haml_encoding(template) do |msg, line|
|
|
94
|
+
raise Haml::Error.new(msg, line)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
unless ruby1_8?
|
|
98
|
+
@options[:encoding] = Encoding.default_internal || template.encoding
|
|
99
|
+
@options[:encoding] = "utf-8" if @options[:encoding].name == "US-ASCII"
|
|
100
|
+
end
|
|
101
|
+
@options.merge! options.reject {|k, v| v.nil?}
|
|
102
|
+
@index = 0
|
|
103
|
+
|
|
104
|
+
unless [:xhtml, :html4, :html5].include?(@options[:format])
|
|
105
|
+
raise Haml::Error, "Invalid output format #{@options[:format].inspect}"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
if @options[:encoding] && @options[:encoding].is_a?(Encoding)
|
|
109
|
+
@options[:encoding] = @options[:encoding].name
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# :eod is a special end-of-document marker
|
|
113
|
+
@template = (template.rstrip).split(/\r\n|\r|\n/) + [:eod, :eod]
|
|
114
|
+
@template_index = 0
|
|
115
|
+
@to_close_stack = []
|
|
116
|
+
@output_tabs = 0
|
|
117
|
+
@template_tabs = 0
|
|
118
|
+
@flat = false
|
|
119
|
+
@newlines = 0
|
|
120
|
+
@precompiled = ''
|
|
121
|
+
@to_merge = []
|
|
122
|
+
@tab_change = 0
|
|
123
|
+
|
|
124
|
+
compile(parse)
|
|
125
|
+
rescue Haml::Error => e
|
|
126
|
+
if @index || e.line
|
|
127
|
+
e.backtrace.unshift "#{@options[:filename]}:#{(e.line ? e.line + 1 : @index) + @options[:line] - 1}"
|
|
128
|
+
end
|
|
129
|
+
raise
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Processes the template and returns the result as a string.
|
|
133
|
+
#
|
|
134
|
+
# `scope` is the context in which the template is evaluated.
|
|
135
|
+
# If it's a `Binding` or `Proc` object,
|
|
136
|
+
# Haml uses it as the second argument to `Kernel#eval`;
|
|
137
|
+
# otherwise, Haml just uses its `#instance_eval` context.
|
|
138
|
+
#
|
|
139
|
+
# Note that Haml modifies the evaluation context
|
|
140
|
+
# (either the scope object or the `self` object of the scope binding).
|
|
141
|
+
# It extends {Haml::Helpers}, and various instance variables are set
|
|
142
|
+
# (all prefixed with `haml_`).
|
|
143
|
+
# For example:
|
|
144
|
+
#
|
|
145
|
+
# s = "foobar"
|
|
146
|
+
# Haml::Engine.new("%p= upcase").render(s) #=> "<p>FOOBAR</p>"
|
|
147
|
+
#
|
|
148
|
+
# # s now extends Haml::Helpers
|
|
149
|
+
# s.respond_to?(:html_attrs) #=> true
|
|
150
|
+
#
|
|
151
|
+
# `locals` is a hash of local variables to make available to the template.
|
|
152
|
+
# For example:
|
|
153
|
+
#
|
|
154
|
+
# Haml::Engine.new("%p= foo").render(Object.new, :foo => "Hello, world!") #=> "<p>Hello, world!</p>"
|
|
155
|
+
#
|
|
156
|
+
# If a block is passed to render,
|
|
157
|
+
# that block is run when `yield` is called
|
|
158
|
+
# within the template.
|
|
159
|
+
#
|
|
160
|
+
# Due to some Ruby quirks,
|
|
161
|
+
# if `scope` is a `Binding` or `Proc` object and a block is given,
|
|
162
|
+
# the evaluation context may not be quite what the user expects.
|
|
163
|
+
# In particular, it's equivalent to passing `eval("self", scope)` as `scope`.
|
|
164
|
+
# This won't have an effect in most cases,
|
|
165
|
+
# but if you're relying on local variables defined in the context of `scope`,
|
|
166
|
+
# they won't work.
|
|
167
|
+
#
|
|
168
|
+
# @param scope [Binding, Proc, Object] The context in which the template is evaluated
|
|
169
|
+
# @param locals [{Symbol => Object}] Local variables that will be made available
|
|
170
|
+
# to the template
|
|
171
|
+
# @param block [#to_proc] A block that can be yielded to within the template
|
|
172
|
+
# @return [String] The rendered template
|
|
173
|
+
def render(scope = Object.new, locals = {}, &block)
|
|
174
|
+
buffer = Haml::Buffer.new(scope.instance_variable_get('@haml_buffer'), options_for_buffer)
|
|
175
|
+
|
|
176
|
+
if scope.is_a?(Binding) || scope.is_a?(Proc)
|
|
177
|
+
scope_object = eval("self", scope)
|
|
178
|
+
scope = scope_object.instance_eval{binding} if block_given?
|
|
179
|
+
else
|
|
180
|
+
scope_object = scope
|
|
181
|
+
scope = scope_object.instance_eval{binding}
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
set_locals(locals.merge(:_hamlout => buffer, :_erbout => buffer.buffer), scope, scope_object)
|
|
185
|
+
|
|
186
|
+
scope_object.instance_eval do
|
|
187
|
+
extend Haml::Helpers
|
|
188
|
+
@haml_buffer = buffer
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
eval(precompiled + ";" + precompiled_method_return_value,
|
|
192
|
+
scope, @options[:filename], @options[:line])
|
|
193
|
+
ensure
|
|
194
|
+
# Get rid of the current buffer
|
|
195
|
+
scope_object.instance_eval do
|
|
196
|
+
@haml_buffer = buffer.upper if buffer
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
alias_method :to_html, :render
|
|
200
|
+
|
|
201
|
+
# Returns a proc that, when called,
|
|
202
|
+
# renders the template and returns the result as a string.
|
|
203
|
+
#
|
|
204
|
+
# `scope` works the same as it does for render.
|
|
205
|
+
#
|
|
206
|
+
# The first argument of the returned proc is a hash of local variable names to values.
|
|
207
|
+
# However, due to an unfortunate Ruby quirk,
|
|
208
|
+
# the local variables which can be assigned must be pre-declared.
|
|
209
|
+
# This is done with the `local_names` argument.
|
|
210
|
+
# For example:
|
|
211
|
+
#
|
|
212
|
+
# # This works
|
|
213
|
+
# Haml::Engine.new("%p= foo").render_proc(Object.new, :foo).call :foo => "Hello!"
|
|
214
|
+
# #=> "<p>Hello!</p>"
|
|
215
|
+
#
|
|
216
|
+
# # This doesn't
|
|
217
|
+
# Haml::Engine.new("%p= foo").render_proc.call :foo => "Hello!"
|
|
218
|
+
# #=> NameError: undefined local variable or method `foo'
|
|
219
|
+
#
|
|
220
|
+
# The proc doesn't take a block; any yields in the template will fail.
|
|
221
|
+
#
|
|
222
|
+
# @param scope [Binding, Proc, Object] The context in which the template is evaluated
|
|
223
|
+
# @param local_names [Array<Symbol>] The names of the locals that can be passed to the proc
|
|
224
|
+
# @return [Proc] The proc that will run the template
|
|
225
|
+
def render_proc(scope = Object.new, *local_names)
|
|
226
|
+
if scope.is_a?(Binding) || scope.is_a?(Proc)
|
|
227
|
+
scope_object = eval("self", scope)
|
|
228
|
+
else
|
|
229
|
+
scope_object = scope
|
|
230
|
+
scope = scope_object.instance_eval{binding}
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
eval("Proc.new { |*_haml_locals| _haml_locals = _haml_locals[0] || {};" +
|
|
234
|
+
precompiled_with_ambles(local_names) + "}\n", scope, @options[:filename], @options[:line])
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# Defines a method on `object` with the given name
|
|
238
|
+
# that renders the template and returns the result as a string.
|
|
239
|
+
#
|
|
240
|
+
# If `object` is a class or module,
|
|
241
|
+
# the method will instead by defined as an instance method.
|
|
242
|
+
# For example:
|
|
243
|
+
#
|
|
244
|
+
# t = Time.now
|
|
245
|
+
# Haml::Engine.new("%p\n Today's date is\n .date= self.to_s").def_method(t, :render)
|
|
246
|
+
# t.render #=> "<p>\n Today's date is\n <div class='date'>Fri Nov 23 18:28:29 -0800 2007</div>\n</p>\n"
|
|
247
|
+
#
|
|
248
|
+
# Haml::Engine.new(".upcased= upcase").def_method(String, :upcased_div)
|
|
249
|
+
# "foobar".upcased_div #=> "<div class='upcased'>FOOBAR</div>\n"
|
|
250
|
+
#
|
|
251
|
+
# The first argument of the defined method is a hash of local variable names to values.
|
|
252
|
+
# However, due to an unfortunate Ruby quirk,
|
|
253
|
+
# the local variables which can be assigned must be pre-declared.
|
|
254
|
+
# This is done with the `local_names` argument.
|
|
255
|
+
# For example:
|
|
256
|
+
#
|
|
257
|
+
# # This works
|
|
258
|
+
# obj = Object.new
|
|
259
|
+
# Haml::Engine.new("%p= foo").def_method(obj, :render, :foo)
|
|
260
|
+
# obj.render(:foo => "Hello!") #=> "<p>Hello!</p>"
|
|
261
|
+
#
|
|
262
|
+
# # This doesn't
|
|
263
|
+
# obj = Object.new
|
|
264
|
+
# Haml::Engine.new("%p= foo").def_method(obj, :render)
|
|
265
|
+
# obj.render(:foo => "Hello!") #=> NameError: undefined local variable or method `foo'
|
|
266
|
+
#
|
|
267
|
+
# Note that Haml modifies the evaluation context
|
|
268
|
+
# (either the scope object or the `self` object of the scope binding).
|
|
269
|
+
# It extends {Haml::Helpers}, and various instance variables are set
|
|
270
|
+
# (all prefixed with `haml_`).
|
|
271
|
+
#
|
|
272
|
+
# @param object [Object, Module] The object on which to define the method
|
|
273
|
+
# @param name [String, Symbol] The name of the method to define
|
|
274
|
+
# @param local_names [Array<Symbol>] The names of the locals that can be passed to the proc
|
|
275
|
+
def def_method(object, name, *local_names)
|
|
276
|
+
method = object.is_a?(Module) ? :module_eval : :instance_eval
|
|
277
|
+
|
|
278
|
+
object.send(method, "def #{name}(_haml_locals = {}); #{precompiled_with_ambles(local_names)}; end",
|
|
279
|
+
@options[:filename], @options[:line])
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
protected
|
|
283
|
+
|
|
284
|
+
# Returns a subset of \{#options}: those that {Haml::Buffer} cares about.
|
|
285
|
+
# All of the values here are such that when `#inspect` is called on the hash,
|
|
286
|
+
# it can be `Kernel#eval`ed to get the same result back.
|
|
287
|
+
#
|
|
288
|
+
# See {file:HAML_REFERENCE.md#haml_options the Haml options documentation}.
|
|
289
|
+
#
|
|
290
|
+
# @return [{Symbol => Object}] The options hash
|
|
291
|
+
def options_for_buffer
|
|
292
|
+
{
|
|
293
|
+
:autoclose => @options[:autoclose],
|
|
294
|
+
:preserve => @options[:preserve],
|
|
295
|
+
:attr_wrapper => @options[:attr_wrapper],
|
|
296
|
+
:ugly => @options[:ugly],
|
|
297
|
+
:format => @options[:format],
|
|
298
|
+
:encoding => @options[:encoding],
|
|
299
|
+
:escape_html => @options[:escape_html],
|
|
300
|
+
:escape_attrs => @options[:escape_attrs],
|
|
301
|
+
}
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
private
|
|
305
|
+
|
|
306
|
+
def set_locals(locals, scope, scope_object)
|
|
307
|
+
scope_object.send(:instance_variable_set, '@_haml_locals', locals)
|
|
308
|
+
set_locals = locals.keys.map { |k| "#{k} = @_haml_locals[#{k.inspect}]" }.join("\n")
|
|
309
|
+
eval(set_locals, scope)
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
end
|
data/lib/haml/error.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module Haml
|
|
2
|
+
# An exception raised by Haml code.
|
|
3
|
+
class Error < StandardError
|
|
4
|
+
# The line of the template on which the error occurred.
|
|
5
|
+
#
|
|
6
|
+
# @return [Fixnum]
|
|
7
|
+
attr_reader :line
|
|
8
|
+
|
|
9
|
+
# @param message [String] The error message
|
|
10
|
+
# @param line [Fixnum] See \{#line}
|
|
11
|
+
def initialize(message = nil, line = nil)
|
|
12
|
+
super(message)
|
|
13
|
+
@line = line
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# SyntaxError is the type of exception raised when Haml encounters an
|
|
18
|
+
# ill-formatted document.
|
|
19
|
+
# It's not particularly interesting,
|
|
20
|
+
# except in that it's a subclass of {Haml::Error}.
|
|
21
|
+
class SyntaxError < Haml::Error; end
|
|
22
|
+
end
|
data/lib/haml/exec.rb
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
require 'optparse'
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
|
|
4
|
+
module Haml
|
|
5
|
+
# This module handles the various Haml executables (`haml` and `haml-convert`).
|
|
6
|
+
module Exec
|
|
7
|
+
# An abstract class that encapsulates the executable code for all three executables.
|
|
8
|
+
class Generic
|
|
9
|
+
# @param args [Array<String>] The command-line arguments
|
|
10
|
+
def initialize(args)
|
|
11
|
+
@args = args
|
|
12
|
+
@options = {:for_engine => {}}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Parses the command-line arguments and runs the executable.
|
|
16
|
+
# Calls `Kernel#exit` at the end, so it never returns.
|
|
17
|
+
#
|
|
18
|
+
# @see #parse
|
|
19
|
+
def parse!
|
|
20
|
+
begin
|
|
21
|
+
parse
|
|
22
|
+
rescue Exception => e
|
|
23
|
+
raise e if @options[:trace] || e.is_a?(SystemExit)
|
|
24
|
+
|
|
25
|
+
$stderr.print "#{e.class}: " unless e.class == RuntimeError
|
|
26
|
+
$stderr.puts "#{e.message}"
|
|
27
|
+
$stderr.puts " Use --trace for backtrace."
|
|
28
|
+
exit 1
|
|
29
|
+
end
|
|
30
|
+
exit 0
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Parses the command-line arguments and runs the executable.
|
|
34
|
+
# This does not handle exceptions or exit the program.
|
|
35
|
+
#
|
|
36
|
+
# @see #parse!
|
|
37
|
+
def parse
|
|
38
|
+
@opts = OptionParser.new(&method(:set_opts))
|
|
39
|
+
@opts.parse!(@args)
|
|
40
|
+
|
|
41
|
+
process_result
|
|
42
|
+
|
|
43
|
+
@options
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @return [String] A description of the executable
|
|
47
|
+
def to_s
|
|
48
|
+
@opts.to_s
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
protected
|
|
52
|
+
|
|
53
|
+
# Finds the line of the source template
|
|
54
|
+
# on which an exception was raised.
|
|
55
|
+
#
|
|
56
|
+
# @param exception [Exception] The exception
|
|
57
|
+
# @return [String] The line number
|
|
58
|
+
def get_line(exception)
|
|
59
|
+
# SyntaxErrors have weird line reporting
|
|
60
|
+
# when there's trailing whitespace,
|
|
61
|
+
# which there is for Haml documents.
|
|
62
|
+
return (exception.message.scan(/:(\d+)/).first || ["??"]).first if exception.is_a?(::SyntaxError)
|
|
63
|
+
(exception.backtrace[0].scan(/:(\d+)/).first || ["??"]).first
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Tells optparse how to parse the arguments
|
|
67
|
+
# available for all executables.
|
|
68
|
+
#
|
|
69
|
+
# This is meant to be overridden by subclasses
|
|
70
|
+
# so they can add their own options.
|
|
71
|
+
#
|
|
72
|
+
# @param opts [OptionParser]
|
|
73
|
+
def set_opts(opts)
|
|
74
|
+
opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do
|
|
75
|
+
@options[:input] = $stdin
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
opts.on('--trace', :NONE, 'Show a full traceback on error') do
|
|
79
|
+
@options[:trace] = true
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
opts.on('--unix-newlines', 'Use Unix-style newlines in written files.') do
|
|
83
|
+
@options[:unix_newlines] = true if ::Haml::Util.windows?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
opts.on_tail("-?", "-h", "--help", "Show this message") do
|
|
87
|
+
puts opts
|
|
88
|
+
exit
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
opts.on_tail("-v", "--version", "Print version") do
|
|
92
|
+
puts("Haml #{::Haml.version[:string]}")
|
|
93
|
+
exit
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Processes the options set by the command-line arguments.
|
|
98
|
+
# In particular, sets `@options[:input]` and `@options[:output]`
|
|
99
|
+
# to appropriate IO streams.
|
|
100
|
+
#
|
|
101
|
+
# This is meant to be overridden by subclasses
|
|
102
|
+
# so they can run their respective programs.
|
|
103
|
+
def process_result
|
|
104
|
+
input, output = @options[:input], @options[:output]
|
|
105
|
+
args = @args.dup
|
|
106
|
+
input ||=
|
|
107
|
+
begin
|
|
108
|
+
filename = args.shift
|
|
109
|
+
@options[:filename] = filename
|
|
110
|
+
open_file(filename) || $stdin
|
|
111
|
+
end
|
|
112
|
+
output ||= open_file(args.shift, 'w') || $stdout
|
|
113
|
+
|
|
114
|
+
@options[:input], @options[:output] = input, output
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
COLORS = { :red => 31, :green => 32, :yellow => 33 }
|
|
118
|
+
|
|
119
|
+
# Prints a status message about performing the given action,
|
|
120
|
+
# colored using the given color (via terminal escapes) if possible.
|
|
121
|
+
#
|
|
122
|
+
# @param name [#to_s] A short name for the action being performed.
|
|
123
|
+
# Shouldn't be longer than 11 characters.
|
|
124
|
+
# @param color [Symbol] The name of the color to use for this action.
|
|
125
|
+
# Can be `:red`, `:green`, or `:yellow`.
|
|
126
|
+
def puts_action(name, color, arg)
|
|
127
|
+
return if @options[:for_engine][:quiet]
|
|
128
|
+
printf color(color, "%11s %s\n"), name, arg
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Same as \{Kernel.puts}, but doesn't print anything if the `--quiet` option is set.
|
|
132
|
+
#
|
|
133
|
+
# @param args [Array] Passed on to \{Kernel.puts}
|
|
134
|
+
def puts(*args)
|
|
135
|
+
return if @options[:for_engine][:quiet]
|
|
136
|
+
Kernel.puts(*args)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Wraps the given string in terminal escapes
|
|
140
|
+
# causing it to have the given color.
|
|
141
|
+
# If terminal esapes aren't supported on this platform,
|
|
142
|
+
# just returns the string instead.
|
|
143
|
+
#
|
|
144
|
+
# @param color [Symbol] The name of the color to use.
|
|
145
|
+
# Can be `:red`, `:green`, or `:yellow`.
|
|
146
|
+
# @param str [String] The string to wrap in the given color.
|
|
147
|
+
# @return [String] The wrapped string.
|
|
148
|
+
def color(color, str)
|
|
149
|
+
raise "[BUG] Unrecognized color #{color}" unless COLORS[color]
|
|
150
|
+
|
|
151
|
+
# Almost any real Unix terminal will support color,
|
|
152
|
+
# so we just filter for Windows terms (which don't set TERM)
|
|
153
|
+
# and not-real terminals, which aren't ttys.
|
|
154
|
+
return str if ENV["TERM"].nil? || ENV["TERM"].empty? || !STDOUT.tty?
|
|
155
|
+
return "\e[#{COLORS[color]}m#{str}\e[0m"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
def open_file(filename, flag = 'r')
|
|
161
|
+
return if filename.nil?
|
|
162
|
+
flag = 'wb' if @options[:unix_newlines] && flag == 'w'
|
|
163
|
+
File.open(filename, flag)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def handle_load_error(err)
|
|
167
|
+
dep = err.message[/^no such file to load -- (.*)/, 1]
|
|
168
|
+
raise err if @options[:trace] || dep.nil? || dep.empty?
|
|
169
|
+
$stderr.puts <<MESSAGE
|
|
170
|
+
Required dependency #{dep} not found!
|
|
171
|
+
Run "gem install #{dep}" to get it.
|
|
172
|
+
Use --trace for backtrace.
|
|
173
|
+
MESSAGE
|
|
174
|
+
exit 1
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# The `haml` executable.
|
|
179
|
+
class Haml < Generic
|
|
180
|
+
# @param args [Array<String>] The command-line arguments
|
|
181
|
+
def initialize(args)
|
|
182
|
+
super
|
|
183
|
+
@options[:for_engine] = {}
|
|
184
|
+
@options[:requires] = []
|
|
185
|
+
@options[:load_paths] = []
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Tells optparse how to parse the arguments.
|
|
189
|
+
#
|
|
190
|
+
# @param opts [OptionParser]
|
|
191
|
+
def set_opts(opts)
|
|
192
|
+
super
|
|
193
|
+
|
|
194
|
+
opts.banner = <<END
|
|
195
|
+
Usage: haml [options] [INPUT] [OUTPUT]
|
|
196
|
+
|
|
197
|
+
Description:
|
|
198
|
+
Converts Haml files to HTML.
|
|
199
|
+
|
|
200
|
+
Options:
|
|
201
|
+
END
|
|
202
|
+
|
|
203
|
+
opts.on('-c', '--check', "Just check syntax, don't evaluate.") do
|
|
204
|
+
require 'stringio'
|
|
205
|
+
@options[:check_syntax] = true
|
|
206
|
+
@options[:output] = StringIO.new
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
opts.on('-t', '--style NAME',
|
|
210
|
+
'Output style. Can be indented (default) or ugly.') do |name|
|
|
211
|
+
@options[:for_engine][:ugly] = true if name.to_sym == :ugly
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
opts.on('-f', '--format NAME',
|
|
215
|
+
'Output format. Can be xhtml (default), html4, or html5.') do |name|
|
|
216
|
+
@options[:for_engine][:format] = name.to_sym
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
opts.on('-e', '--escape-html',
|
|
220
|
+
'Escape HTML characters (like ampersands and angle brackets) by default.') do
|
|
221
|
+
@options[:for_engine][:escape_html] = true
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
opts.on('--no-escape-attrs',
|
|
225
|
+
"Don't escape HTML characters (like ampersands and angle brackets) in attributes.") do
|
|
226
|
+
@options[:for_engine][:escape_attrs] = false
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
opts.on('-q', '--double-quote-attributes',
|
|
230
|
+
'Set attribute wrapper to double-quotes (default is single).') do
|
|
231
|
+
@options[:for_engine][:attr_wrapper] = '"'
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
|
|
235
|
+
@options[:requires] << file
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
|
|
239
|
+
@options[:load_paths] << path
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
unless ::Haml::Util.ruby1_8?
|
|
243
|
+
opts.on('-E ex[:in]', 'Specify the default external and internal character encodings.') do |encoding|
|
|
244
|
+
external, internal = encoding.split(':')
|
|
245
|
+
Encoding.default_external = external if external && !external.empty?
|
|
246
|
+
Encoding.default_internal = internal if internal && !internal.empty?
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
opts.on('--debug', "Print out the precompiled Ruby source.") do
|
|
251
|
+
@options[:debug] = true
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Processes the options set by the command-line arguments,
|
|
256
|
+
# and runs the Haml compiler appropriately.
|
|
257
|
+
def process_result
|
|
258
|
+
super
|
|
259
|
+
@options[:for_engine][:filename] = @options[:filename]
|
|
260
|
+
input = @options[:input]
|
|
261
|
+
output = @options[:output]
|
|
262
|
+
|
|
263
|
+
template = input.read()
|
|
264
|
+
input.close() if input.is_a? File
|
|
265
|
+
|
|
266
|
+
@options[:load_paths].each {|p| $LOAD_PATH << p}
|
|
267
|
+
@options[:requires].each {|f| require f}
|
|
268
|
+
|
|
269
|
+
begin
|
|
270
|
+
engine = ::Haml::Engine.new(template, @options[:for_engine])
|
|
271
|
+
if @options[:check_syntax]
|
|
272
|
+
puts "Syntax OK"
|
|
273
|
+
return
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
if @options[:debug]
|
|
277
|
+
puts engine.precompiled
|
|
278
|
+
puts '=' * 100
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
result = engine.to_html
|
|
282
|
+
rescue Exception => e
|
|
283
|
+
raise e if @options[:trace]
|
|
284
|
+
|
|
285
|
+
case e
|
|
286
|
+
when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
|
|
287
|
+
when ::Haml::Error; raise "Haml error on line #{get_line e}: #{e.message}"
|
|
288
|
+
else raise "Exception on line #{get_line e}: #{e.message}\n Use --trace for backtrace."
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
output.write(result)
|
|
293
|
+
output.close() if output.is_a? File
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# The `html2haml` executable.
|
|
298
|
+
class HTML2Haml < Generic
|
|
299
|
+
# @param args [Array<String>] The command-line arguments
|
|
300
|
+
def initialize(args)
|
|
301
|
+
super
|
|
302
|
+
@module_opts = {}
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Tells optparse how to parse the arguments.
|
|
306
|
+
#
|
|
307
|
+
# @param opts [OptionParser]
|
|
308
|
+
def set_opts(opts)
|
|
309
|
+
opts.banner = <<END
|
|
310
|
+
Usage: html2haml [options] [INPUT] [OUTPUT]
|
|
311
|
+
|
|
312
|
+
Description: Transforms an HTML file into corresponding Haml code.
|
|
313
|
+
|
|
314
|
+
Options:
|
|
315
|
+
END
|
|
316
|
+
|
|
317
|
+
opts.on('-e', '--erb', 'Parse ERb tags.') do
|
|
318
|
+
@module_opts[:erb] = true
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
opts.on('--no-erb', "Don't parse ERb tags.") do
|
|
322
|
+
@options[:no_erb] = true
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
opts.on('-r', '--rhtml', 'Deprecated; same as --erb.') do
|
|
326
|
+
@module_opts[:erb] = true
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
opts.on('--no-rhtml', "Deprecated; same as --no-erb.") do
|
|
330
|
+
@options[:no_erb] = true
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
opts.on('-x', '--xhtml', 'Parse the input using the more strict XHTML parser.') do
|
|
334
|
+
@module_opts[:xhtml] = true
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
super
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# Processes the options set by the command-line arguments,
|
|
341
|
+
# and runs the HTML compiler appropriately.
|
|
342
|
+
def process_result
|
|
343
|
+
super
|
|
344
|
+
|
|
345
|
+
require 'haml/html'
|
|
346
|
+
|
|
347
|
+
input = @options[:input]
|
|
348
|
+
output = @options[:output]
|
|
349
|
+
|
|
350
|
+
@module_opts[:erb] ||= input.respond_to?(:path) && input.path =~ /\.(rhtml|erb)$/
|
|
351
|
+
@module_opts[:erb] &&= @options[:no_erb] != false
|
|
352
|
+
|
|
353
|
+
output.write(::Haml::HTML.new(input, @module_opts).render)
|
|
354
|
+
rescue ::Haml::Error => e
|
|
355
|
+
raise "#{e.is_a?(::Haml::SyntaxError) ? "Syntax error" : "Error"} on line " +
|
|
356
|
+
"#{get_line e}: #{e.message}"
|
|
357
|
+
rescue LoadError => err
|
|
358
|
+
handle_load_error(err)
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
end
|