Class: Faml::Compiler

Inherits:
Temple::Parser
  • Object
show all
Defined in:
lib/faml/compiler.rb

Constant Summary collapse

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
].freeze
DEFAULT_PRESERVE_TAGS =
%w[pre textarea code].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompiler

Returns a new instance of Compiler.



29
30
31
32
33
# File 'lib/faml/compiler.rb', line 29

def initialize(*)
  super
  @text_compiler = TextCompiler.new
  @filename = options[:filename]
end

Class Method Details

.find_and_preserve(input) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/faml/compiler.rb', line 44

def self.find_and_preserve(input)
  # Taken from the original haml code
  re = %r{<(#{options[:preserve].map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)}im
  input.to_s.gsub(re) do |s|
    m = s.match(re) # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible
    "<#{m[1]}#{m[2]}>#{Helpers.preserve(m[3])}</#{m[1]}>"
  end
end

Instance Method Details

#call(ast) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/faml/compiler.rb', line 35

def call(ast)
  compile(ast)
rescue Error => e
  if @filename && e.lineno
    e.backtrace.unshift "#{@filename}:#{e.lineno}"
  end
  raise e
end