require 'parser/current' require 'temple' require 'faml/ast' require 'faml/filter_compilers' require 'faml/helpers' require 'faml/rails_helpers' require 'faml/static_hash_parser' require 'faml/text_compiler' module Faml class Compiler < Temple::Parser class UnparsableRubyCode < StandardError end DEFAULT_AUTO_CLOSE_TAGS = %w[ area base basefont br col command embed frame hr img input isindex keygen link menuitem meta param source track wbr ] DEFAULT_PRESERVE_TAGS = %w[pre textarea code] define_options( autoclose: DEFAULT_AUTO_CLOSE_TAGS, format: :html, preserve: DEFAULT_PRESERVE_TAGS, use_html_safe: false, ) def initialize(*) super @text_compiler = TextCompiler.new end def call(ast) compile(ast) end def self.find_and_preserve(input) # Taken from the original haml code re = /<(#{options[:preserve].map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im input.to_s.gsub(re) do |s| s =~ re # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible "<#{$1}#{$2}>#{Helpers.preserve($3)}" end end private def compile(ast) case ast when Ast::Root compile_root(ast) when Ast::Doctype compile_doctype(ast) when Ast::HtmlComment compile_html_comment(ast) when Ast::HamlComment compile_haml_comment(ast) when Ast::Empty [:multi] when Ast::Element compile_element(ast) when Ast::Script compile_script(ast) when Ast::SilentScript compile_silent_script(ast) when Ast::Text compile_text(ast) when Ast::Filter compile_filter(ast) else raise "InternalError: Unknown AST node #{ast.class}: #{ast.inspect}" end end def compile_root(ast) [:multi, [:code, "extend ::#{helper_module.name}"]].tap do |temple| compile_children(ast, temple) end end def helper_module if options[:use_html_safe] RailsHelpers else Helpers end end def compile_children(ast, temple) ast.children.each do |c| temple << compile(c) if need_newline?(c) temple << [:mknl] end unless suppress_code_newline?(c) temple << [:newline] end end end def need_newline?(child) case child when Ast::Script child.children.empty? when Ast::SilentScript, Ast::HamlComment, Ast::Empty false when Ast::Element !child.nuke_outer_whitespace when Ast::Filter FilterCompilers.find(child.name).need_newline? else true end end def suppress_code_newline?(ast) ast.is_a?(Ast::Script) || ast.is_a?(Ast::SilentScript) || (ast.is_a?(Ast::Element) && suppress_code_newline?(ast.oneline_child)) || (ast.is_a?(Ast::Element) && !ast.children.empty?) || (ast.is_a?(Ast::HtmlComment) && !ast.conditional.empty?) end def compile_text(ast) @text_compiler.compile(ast.text, escape_html: ast.escape_html) end # html5 and html4 is deprecated in temple. DEFAULT_DOCTYPE = { html: 'html', html5: 'html', html4: 'transitional', xhtml: 'transitional', }.freeze def compile_doctype(ast) doctype = ast.doctype.downcase if doctype.empty? doctype = DEFAULT_DOCTYPE[options[:format]] end [:haml, :doctype, doctype] end def compile_html_comment(ast) if ast.children.empty? if ast.conditional.empty? [:html, :comment, [:static, " #{ast.comment} "]] else [:html, :comment, [:static, "[#{ast.conditional}]> #{ast.comment} "] << [:mknl] << [:newline] end compile_children(ast, temple) unless ast.conditional.empty? temple << [:static, "