2006-12-03 18:56:47 -05:00
|
|
|
require 'haml/helpers'
|
|
|
|
require 'haml/buffer'
|
2007-11-23 04:15:00 -05:00
|
|
|
require 'haml/precompiler'
|
2007-01-19 12:03:47 -05:00
|
|
|
require 'haml/filters'
|
2007-01-27 05:29:14 -05:00
|
|
|
require 'haml/error'
|
2007-03-25 04:20:33 -04:00
|
|
|
require 'haml/util'
|
2006-08-05 23:18:54 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
module Haml
|
2006-12-17 11:45:07 -05:00
|
|
|
# This is the class where all the parsing and processing of the Haml
|
2006-10-14 18:24:53 -04:00
|
|
|
# template is done. It can be directly used by the user by creating a
|
2006-12-17 11:45:07 -05:00
|
|
|
# new instance and calling <tt>to_html</tt> to render the template. For example:
|
2006-11-04 01:36:16 -05:00
|
|
|
#
|
2007-02-27 13:57:17 -05:00
|
|
|
# template = File.read('templates/really_cool_template.haml')
|
2006-10-14 18:24:53 -04:00
|
|
|
# haml_engine = Haml::Engine.new(template)
|
|
|
|
# output = haml_engine.to_html
|
|
|
|
# puts output
|
2006-08-05 23:18:54 -04:00
|
|
|
class Engine
|
2007-11-23 04:15:00 -05:00
|
|
|
include Precompiler
|
|
|
|
|
2006-10-27 17:20:19 -04:00
|
|
|
# Allow reading and writing of the options hash
|
|
|
|
attr :options, true
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2008-01-30 23:10:44 -05:00
|
|
|
# This string contains the source code that is evaluated
|
|
|
|
# to produce the Haml document.
|
|
|
|
attr :precompiled, true
|
|
|
|
|
2008-02-29 14:00:03 -05:00
|
|
|
# True if the format is XHTML
|
2008-02-26 13:57:45 -05:00
|
|
|
def xhtml?
|
2008-02-27 09:20:44 -05:00
|
|
|
not html?
|
2008-02-26 13:57:45 -05:00
|
|
|
end
|
|
|
|
|
2008-02-29 14:00:03 -05:00
|
|
|
# True if the format is any flavor of HTML
|
2008-02-27 09:16:21 -05:00
|
|
|
def html?
|
|
|
|
html4? or html5?
|
|
|
|
end
|
|
|
|
|
2008-02-29 14:00:03 -05:00
|
|
|
# True if the format is HTML4
|
2008-02-26 13:57:45 -05:00
|
|
|
def html4?
|
2008-02-29 14:00:03 -05:00
|
|
|
@options[:format] == :html4
|
2008-02-26 13:57:45 -05:00
|
|
|
end
|
|
|
|
|
2008-02-29 14:00:03 -05:00
|
|
|
# True if the format is HTML5
|
2008-02-27 09:16:21 -05:00
|
|
|
def html5?
|
2008-02-29 14:00:03 -05:00
|
|
|
@options[:format] == :html5
|
2008-02-27 09:16:21 -05:00
|
|
|
end
|
|
|
|
|
2006-11-21 00:54:26 -05:00
|
|
|
# Creates a new instace of Haml::Engine that will compile the given
|
2007-04-01 04:17:28 -04:00
|
|
|
# template string when <tt>render</tt> is called.
|
2008-03-13 04:07:07 -04:00
|
|
|
# See README.rdoc for available options.
|
2006-10-27 17:20:19 -04:00
|
|
|
#
|
2006-10-30 01:59:57 -05:00
|
|
|
#--
|
|
|
|
# When adding options, remember to add information about them
|
2008-03-13 04:07:07 -04:00
|
|
|
# to README.rdoc!
|
2006-10-30 01:59:57 -05:00
|
|
|
#++
|
2006-10-30 01:10:26 -05:00
|
|
|
#
|
2007-11-23 11:40:44 -05:00
|
|
|
def initialize(template, options = {})
|
2006-10-27 17:20:19 -04:00
|
|
|
@options = {
|
2006-10-30 01:10:26 -05:00
|
|
|
:suppress_eval => false,
|
2006-11-04 03:35:06 -05:00
|
|
|
:attr_wrapper => "'",
|
2008-02-26 13:57:45 -05:00
|
|
|
|
|
|
|
# Don't forget to update the docs in lib/haml.rb if you update these
|
2008-03-02 19:24:47 -05:00
|
|
|
:autoclose => %w[meta img link br hr input area param col base],
|
|
|
|
:preserve => %w[textarea pre],
|
|
|
|
|
2007-01-19 12:03:47 -05:00
|
|
|
:filters => {
|
2008-02-23 02:03:25 -05:00
|
|
|
'sass' => Haml::Filters::Sass,
|
2007-02-06 03:08:32 -05:00
|
|
|
'plain' => Haml::Filters::Plain,
|
2008-02-23 04:56:16 -05:00
|
|
|
'javascript' => Haml::Filters::Javascript,
|
2007-08-11 17:04:51 -04:00
|
|
|
'preserve' => Haml::Filters::Preserve,
|
|
|
|
'redcloth' => Haml::Filters::RedCloth,
|
2007-01-19 12:03:47 -05:00
|
|
|
'textile' => Haml::Filters::Textile,
|
2007-11-25 22:26:16 -05:00
|
|
|
'markdown' => Haml::Filters::Markdown },
|
2008-02-13 04:30:21 -05:00
|
|
|
:filename => '(haml)',
|
2008-02-24 17:10:30 -05:00
|
|
|
:ugly => false,
|
2008-02-29 14:00:03 -05:00
|
|
|
:format => :xhtml
|
2007-08-11 17:04:51 -04:00
|
|
|
}
|
2007-11-23 11:40:44 -05:00
|
|
|
@options.rec_merge! options
|
2007-01-19 12:03:47 -05:00
|
|
|
|
2008-02-29 14:00:03 -05:00
|
|
|
unless [:xhtml, :html4, :html5].include?(@options[:format])
|
|
|
|
raise Haml::Error, "Invalid format #{@options[:format].inspect}"
|
2008-02-26 13:57:45 -05:00
|
|
|
end
|
|
|
|
|
2007-03-27 16:24:29 -04:00
|
|
|
unless @options[:suppress_eval]
|
|
|
|
@options[:filters].merge!({
|
2008-02-23 02:03:25 -05:00
|
|
|
'erb' => Haml::Filters::ERB,
|
2007-03-27 16:24:29 -04:00
|
|
|
'ruby' => Haml::Filters::Ruby
|
|
|
|
})
|
|
|
|
end
|
2007-11-23 11:40:44 -05:00
|
|
|
@options[:filters].rec_merge! options[:filters] if options[:filters]
|
2007-03-27 16:24:29 -04:00
|
|
|
|
2007-11-24 03:35:10 -05:00
|
|
|
if @options[:locals]
|
|
|
|
warn <<END
|
|
|
|
DEPRECATION WARNING:
|
|
|
|
The Haml :locals option is deprecated and will be removed in version 2.0.
|
|
|
|
Use the locals option for Haml::Engine#render instead.
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
2007-01-03 02:27:11 -05:00
|
|
|
@template = template.strip #String
|
2006-10-14 18:24:53 -04:00
|
|
|
@to_close_stack = []
|
2006-11-18 02:00:10 -05:00
|
|
|
@output_tabs = 0
|
|
|
|
@template_tabs = 0
|
2007-01-27 02:10:49 -05:00
|
|
|
@index = 0
|
2006-11-04 01:36:16 -05:00
|
|
|
@flat_spaces = -1
|
|
|
|
|
2007-11-23 02:42:59 -05:00
|
|
|
precompile
|
2007-11-25 22:26:16 -05:00
|
|
|
rescue Haml::Error
|
|
|
|
$!.backtrace.unshift "#{@options[:filename]}:#{@index}"
|
|
|
|
raise
|
2006-09-12 00:14:21 -04:00
|
|
|
end
|
2006-09-29 15:59:21 -04:00
|
|
|
|
2006-11-21 00:54:26 -05:00
|
|
|
# Processes the template and returns the result as a string.
|
2007-11-23 07:14:02 -05:00
|
|
|
#
|
|
|
|
# +scope+ is the context in which the template is evaluated.
|
|
|
|
# If it's a Binding or Proc object,
|
|
|
|
# Haml uses it as the second argument to Kernel#eval;
|
|
|
|
# otherwise, Haml just uses its #instance_eval context.
|
2007-11-23 21:37:52 -05:00
|
|
|
#
|
2007-11-24 03:35:10 -05:00
|
|
|
# Note that Haml modifies the evaluation context
|
|
|
|
# (either the scope object or the "self" object of the scope binding).
|
|
|
|
# It extends Haml::Helpers, and various instance variables are set
|
|
|
|
# (all prefixed with "haml").
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# s = "foobar"
|
|
|
|
# Haml::Engine.new("%p= upcase").render(s) #=> "<p>FOOBAR</p>"
|
|
|
|
#
|
|
|
|
# # s now extends Haml::Helpers
|
|
|
|
# s.responds_to?(:html_attrs) #=> true
|
|
|
|
#
|
|
|
|
# +locals+ is a hash of local variables to make available to the template.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# Haml::Engine.new("%p= foo").render(Object.new, :foo => "Hello, world!") #=> "<p>Hello, world!</p>"
|
2007-11-23 07:14:02 -05:00
|
|
|
#
|
|
|
|
# If a block is passed to render,
|
|
|
|
# that block is run when +yield+ is called
|
|
|
|
# within the template.
|
|
|
|
#
|
2007-11-24 03:35:10 -05:00
|
|
|
# Due to some Ruby quirks,
|
2007-11-23 07:14:02 -05:00
|
|
|
# if scope is a Binding or Proc object and a block is given,
|
|
|
|
# the evaluation context may not be quite what the user expects.
|
|
|
|
# In particular, it's equivalent to passing <tt>eval("self", scope)</tt> as scope.
|
|
|
|
# This won't have an effect in most cases,
|
|
|
|
# but if you're relying on local variables defined in the context of scope,
|
|
|
|
# they won't work.
|
2007-11-24 03:35:10 -05:00
|
|
|
def render(scope = Object.new, locals = {}, &block)
|
|
|
|
locals = (@options[:locals] || {}).merge(locals)
|
2007-11-23 11:55:30 -05:00
|
|
|
buffer = Haml::Buffer.new(options_for_buffer)
|
2006-08-05 23:18:54 -04:00
|
|
|
|
2007-11-23 07:14:02 -05:00
|
|
|
if scope.is_a?(Binding) || scope.is_a?(Proc)
|
2007-11-23 02:02:07 -05:00
|
|
|
scope_object = eval("self", scope)
|
2007-11-23 07:14:02 -05:00
|
|
|
scope = scope_object.instance_eval{binding} if block_given?
|
2007-11-23 02:02:07 -05:00
|
|
|
else
|
|
|
|
scope_object = scope
|
2007-11-23 07:14:02 -05:00
|
|
|
scope = scope_object.instance_eval{binding}
|
2007-11-23 02:02:07 -05:00
|
|
|
end
|
2007-09-19 02:11:47 -04:00
|
|
|
|
2007-11-24 03:35:10 -05:00
|
|
|
set_locals(locals.merge(:_hamlout => buffer, :_erbout => buffer.buffer), scope, scope_object)
|
2007-11-23 07:14:02 -05:00
|
|
|
|
2007-11-23 02:02:07 -05:00
|
|
|
scope_object.instance_eval do
|
2007-11-23 11:32:03 -05:00
|
|
|
extend Haml::Helpers
|
2006-10-14 18:24:53 -04:00
|
|
|
@haml_stack ||= Array.new
|
|
|
|
@haml_stack.push(buffer)
|
2007-11-23 11:32:03 -05:00
|
|
|
@haml_is_haml = true
|
2007-11-23 02:02:07 -05:00
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2007-11-25 22:26:16 -05:00
|
|
|
eval(@precompiled, scope, @options[:filename], 0)
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Get rid of the current buffer
|
2007-11-23 02:02:07 -05:00
|
|
|
scope_object.instance_eval do
|
2006-10-14 18:24:53 -04:00
|
|
|
@haml_stack.pop
|
2007-11-23 11:32:03 -05:00
|
|
|
@haml_is_haml = false
|
2006-11-04 01:36:16 -05:00
|
|
|
end
|
2007-11-23 11:55:30 -05:00
|
|
|
|
|
|
|
buffer.buffer
|
2006-07-19 18:23:01 -04:00
|
|
|
end
|
2007-11-23 11:55:30 -05:00
|
|
|
alias_method :to_html, :render
|
|
|
|
|
2007-11-23 12:26:05 -05:00
|
|
|
# Returns a proc that, when called,
|
|
|
|
# renders the template and returns the result as a string.
|
|
|
|
#
|
|
|
|
# +scope+ works the same as it does for render.
|
|
|
|
#
|
2007-11-24 03:35:10 -05:00
|
|
|
# The first argument of the returned proc is a hash of local variable names to values.
|
|
|
|
# However, due to an unfortunate Ruby quirk,
|
|
|
|
# the local variables which can be assigned must be pre-declared.
|
|
|
|
# This is done with the +local_names+ argument.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# # This works
|
|
|
|
# Haml::Engine.new("%p= foo").render_proc(Object.new, :foo).call :foo => "Hello!"
|
|
|
|
# #=> "<p>Hello!</p>"
|
|
|
|
#
|
|
|
|
# # This doesn't
|
|
|
|
# Haml::Engine.new("%p= foo").render_proc.call :foo => "Hello!"
|
|
|
|
# #=> NameError: undefined local variable or method `foo'
|
|
|
|
#
|
2007-11-23 12:26:05 -05:00
|
|
|
# The proc doesn't take a block;
|
|
|
|
# any yields in the template will fail.
|
2007-11-24 03:35:10 -05:00
|
|
|
def render_proc(scope = Object.new, *local_names)
|
2007-11-23 12:26:05 -05:00
|
|
|
if scope.is_a?(Binding) || scope.is_a?(Proc)
|
|
|
|
scope_object = eval("self", scope)
|
|
|
|
else
|
|
|
|
scope_object = scope
|
|
|
|
scope = scope_object.instance_eval{binding}
|
|
|
|
end
|
|
|
|
|
2007-11-25 22:26:16 -05:00
|
|
|
eval("Proc.new { |*_haml_locals| _haml_locals = _haml_locals[0] || {};" +
|
|
|
|
precompiled_with_ambles(local_names) + "}\n", scope, @options[:filename], 0)
|
2007-11-23 12:26:05 -05:00
|
|
|
end
|
|
|
|
|
2007-11-23 21:32:18 -05:00
|
|
|
# Defines a method on +object+
|
|
|
|
# with the given name
|
|
|
|
# that renders the template and returns the result as a string.
|
|
|
|
#
|
|
|
|
# If +object+ is a class or module,
|
|
|
|
# the method will instead by defined as an instance method.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# t = Time.now
|
|
|
|
# Haml::Engine.new("%p\n Today's date is\n .date= self.to_s").def_method(t, :render)
|
|
|
|
# t.render #=> "<p>\n Today's date is\n <div class='date'>Fri Nov 23 18:28:29 -0800 2007</div>\n</p>\n"
|
|
|
|
#
|
|
|
|
# Haml::Engine.new(".upcased= upcase").def_method(String, :upcased_div)
|
|
|
|
# "foobar".upcased_div #=> "<div class='upcased'>FOOBAR</div>\n"
|
|
|
|
#
|
2007-11-24 03:35:10 -05:00
|
|
|
# The first argument of the defined method is a hash of local variable names to values.
|
|
|
|
# However, due to an unfortunate Ruby quirk,
|
|
|
|
# the local variables which can be assigned must be pre-declared.
|
|
|
|
# This is done with the +local_names+ argument.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# # This works
|
|
|
|
# obj = Object.new
|
|
|
|
# Haml::Engine.new("%p= foo").def_method(obj, :render, :foo)
|
|
|
|
# obj.render(:foo => "Hello!") #=> "<p>Hello!</p>"
|
|
|
|
#
|
|
|
|
# # This doesn't
|
|
|
|
# obj = Object.new
|
|
|
|
# Haml::Engine.new("%p= foo").def_method(obj, :render)
|
|
|
|
# obj.render(:foo => "Hello!") #=> NameError: undefined local variable or method `foo'
|
|
|
|
#
|
|
|
|
# Note that Haml modifies the evaluation context
|
|
|
|
# (either the scope object or the "self" object of the scope binding).
|
|
|
|
# It extends Haml::Helpers, and various instance variables are set
|
|
|
|
# (all prefixed with "haml").
|
|
|
|
def def_method(object, name, *local_names)
|
2007-11-23 21:32:18 -05:00
|
|
|
method = object.is_a?(Module) ? :module_eval : :instance_eval
|
|
|
|
|
2007-11-25 22:26:16 -05:00
|
|
|
object.send(method, "def #{name}(_haml_locals = {}); #{precompiled_with_ambles(local_names)}; end",
|
|
|
|
@options[:filename], 0)
|
2007-11-23 21:32:18 -05:00
|
|
|
end
|
|
|
|
|
2007-11-23 11:55:30 -05:00
|
|
|
private
|
2007-11-23 04:43:26 -05:00
|
|
|
|
2007-11-23 11:32:03 -05:00
|
|
|
def set_locals(locals, scope, scope_object)
|
|
|
|
scope_object.send(:instance_variable_set, '@_haml_locals', locals)
|
|
|
|
set_locals = locals.keys.map { |k| "#{k} = @_haml_locals[#{k.inspect}]" }.join("\n")
|
|
|
|
eval(set_locals, scope)
|
|
|
|
end
|
|
|
|
|
2007-11-23 11:55:48 -05:00
|
|
|
# Returns a hash of options that Haml::Buffer cares about.
|
2008-02-29 20:56:38 -05:00
|
|
|
# This should remain loadable from #inspect.
|
2007-11-23 11:55:48 -05:00
|
|
|
def options_for_buffer
|
2008-02-13 04:30:21 -05:00
|
|
|
{
|
2008-03-02 19:24:47 -05:00
|
|
|
:preserve => @options[:preserve],
|
2008-02-13 04:30:21 -05:00
|
|
|
:attr_wrapper => @options[:attr_wrapper],
|
2008-02-29 20:56:38 -05:00
|
|
|
:ugly => @options[:ugly],
|
|
|
|
:format => @options[:format]
|
2008-02-13 04:30:21 -05:00
|
|
|
}
|
2007-11-23 11:55:48 -05:00
|
|
|
end
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|
2006-09-14 15:12:45 -04:00
|
|
|
end
|