Class | Haml::Engine |
In: |
lib/haml/engine.rb
|
Parent: | Object |
This is the class where all the parsing and processing of the Haml template is done. It can be directly used by the user by creating a new instance and calling to_html to render the template. For example:
template = File.read('templates/really_cool_template.haml') haml_engine = Haml::Engine.new(template) output = haml_engine.to_html puts output
ELEMENT | = | ?% | Designates an XHTML/XML element. | |
DIV_CLASS | = | ?. | Designates a <div> element with the given class. | |
DIV_ID | = | ?# | Designates a <div> element with the given id. | |
COMMENT | = | ?/ | Designates an XHTML/XML comment. | |
DOCTYPE | = | ?! | Designates an XHTML doctype. | |
SCRIPT | = | ?= | Designates script, the result of which is output. | |
FLAT_SCRIPT | = | ?~ | Designates script, the result of which is flattened and output. | |
SILENT_SCRIPT | = | ?- | Designates script which is run but not output. | |
SILENT_COMMENT | = | ?# | When following SILENT_SCRIPT, designates a comment that is not output. | |
ESCAPE | = | ?\\ | Designates a non-parsed line. | |
FILTER | = | ?: | Designates a block of filtered text. | |
PLAIN_TEXT | = | -1 | Designates a non-parsed line. Not actually a character. | |
SPECIAL_CHARACTERS | = | [ ELEMENT, DIV_CLASS, DIV_ID, COMMENT, DOCTYPE, SCRIPT, FLAT_SCRIPT, SILENT_SCRIPT, ESCAPE, FILTER | Keeps track of the ASCII values of the characters that begin a specially-interpreted line. | |
MULTILINE_CHAR_VALUE | = | ?| | The value of the character that designates that a line is part of a multiline string. | |
MULTILINE_STARTERS | = | SPECIAL_CHARACTERS - [?/] | Characters that designate that a multiline string may be about to begin. | |
MID_BLOCK_KEYWORDS | = | ['else', 'elsif', 'rescue', 'ensure', 'when'] |
Keywords that appear in the middle of a Ruby block with lowered
indentation. If a block has been started using indentation, lowering the
indentation with one of these won‘t end the block. For example:
- if foo %p yes! - else %p no! The block is ended after %p no!, because else is a member of this array. |
|
COMMENT_REGEX | = | /\/(\[[\w\s\.]*\])?(.*)/ | The Regex that matches an HTML comment command. | |
DOCTYPE_REGEX | = | /(\d\.\d)?[\s]*([a-z]*)/i | The Regex that matches a Doctype command. | |
TAG_REGEX | = | /[%]([-:\w]+)([-\w\.\#]*)(\{.*\})?(\[.*\])?([=\/\~]?)?(.*)?/ | The Regex that matches an HTML tag command. | |
LITERAL_VALUE_REGEX | = | /^\s*(:(\w*)|(('|")([^\\\#'"]*?)\4))\s*$/ | The Regex that matches a literal string or symbol value | |
FLAT_WARNING | = | <<END Haml deprecation warning: The ~ command is deprecated and will be removed in future Haml versions. Use the :preserve filter, the preserve helper, or the find_and_preserve helper instead. END |
options | [RW] | Allow reading and writing of the options hash |
Creates a new instace of Haml::Engine that will compile the given template string when render is called. See README for available options.
# File lib/haml/engine.rb, line 122 122: def initialize(template, l_options = {}) 123: @options = { 124: :suppress_eval => false, 125: :attr_wrapper => "'", 126: :locals => {}, 127: :autoclose => ['meta', 'img', 'link', 'br', 'hr', 'input', 'area'], 128: :filters => { 129: 'sass' => Sass::Engine, 130: 'plain' => Haml::Filters::Plain, 131: 'preserve' => Haml::Filters::Preserve } 132: } 133: 134: if !NOT_LOADED.include? 'redcloth' 135: @options[:filters].merge!({ 136: 'redcloth' => RedCloth, 137: 'textile' => Haml::Filters::Textile, 138: 'markdown' => Haml::Filters::Markdown 139: }) 140: end 141: if !NOT_LOADED.include? 'bluecloth' 142: @options[:filters]['markdown'] = Haml::Filters::Markdown 143: end 144: 145: @options.rec_merge! l_options 146: 147: unless @options[:suppress_eval] 148: @options[:filters].merge!({ 149: 'erb' => ERB, 150: 'ruby' => Haml::Filters::Ruby 151: }) 152: end 153: @options[:filters].rec_merge! l_options[:filters] if l_options[:filters] 154: 155: @template = template.strip #String 156: @to_close_stack = [] 157: @output_tabs = 0 158: @template_tabs = 0 159: @index = 0 160: 161: # This is the base tabulation of the currently active 162: # flattened block. -1 signifies that there is no such block. 163: @flat_spaces = -1 164: 165: begin 166: # Only do the first round of pre-compiling if we really need to. 167: # They might be passing in the precompiled string. 168: requires_precompile = true 169: if @@method_names[@template] 170: # Check that the compiled method supports a superset of the local assigns we want to do 171: supported_assigns = @@supported_local_assigns[@template] 172: requires_precompile = !@options[:locals].keys.all? {|var| supported_assigns.include? var} 173: end 174: do_precompile if requires_precompile 175: rescue Haml::Error => e 176: e.add_backtrace_entry(@index, @options[:filename]) 177: raise e 178: end 179: end
This method is deprecated and shouldn‘t be used.
# File lib/haml/engine.rb, line 196 196: def precompiled 197: $stderr.puts "The Haml precompiled method and :precompiled option\nare deprecated and will be removed in version 2.0.\nHaml::Engine now automatically handles caching.\n" 198: nil 199: end
Processes the template and returns the result as a string.
# File lib/haml/engine.rb, line 182 182: def render(scope = Object.new, &block) 183: @scope_object = scope 184: @buffer = Haml::Buffer.new(@options) 185: 186: # Run the compiled evaluator function 187: compile &block 188: 189: # Return the result string 190: @buffer.buffer 191: end