2006-12-03 23:56:47 +00:00
|
|
|
require 'haml/helpers'
|
|
|
|
require 'haml/buffer'
|
2007-11-23 09:15:00 +00:00
|
|
|
require 'haml/precompiler'
|
2007-01-19 17:03:47 +00:00
|
|
|
require 'haml/filters'
|
2007-01-27 10:29:14 +00:00
|
|
|
require 'haml/error'
|
2007-03-25 08:20:33 +00:00
|
|
|
require 'haml/util'
|
2006-08-06 03:18:54 +00:00
|
|
|
|
2006-10-14 22:24:53 +00:00
|
|
|
module Haml
|
2006-12-17 16:45:07 +00:00
|
|
|
# This is the class where all the parsing and processing of the Haml
|
2006-10-14 22:24:53 +00:00
|
|
|
# template is done. It can be directly used by the user by creating a
|
2006-12-17 16:45:07 +00:00
|
|
|
# new instance and calling <tt>to_html</tt> to render the template. For example:
|
2006-11-04 06:36:16 +00:00
|
|
|
#
|
2007-02-27 18:57:17 +00:00
|
|
|
# template = File.read('templates/really_cool_template.haml')
|
2006-10-14 22:24:53 +00:00
|
|
|
# haml_engine = Haml::Engine.new(template)
|
|
|
|
# output = haml_engine.to_html
|
|
|
|
# puts output
|
2006-08-06 03:18:54 +00:00
|
|
|
class Engine
|
2007-11-23 09:15:00 +00:00
|
|
|
include Precompiler
|
|
|
|
|
2006-10-27 21:20:19 +00:00
|
|
|
# Allow reading and writing of the options hash
|
|
|
|
attr :options, true
|
2006-11-04 06:36:16 +00:00
|
|
|
|
2006-11-21 05:54:26 +00:00
|
|
|
# Creates a new instace of Haml::Engine that will compile the given
|
2007-04-01 08:17:28 +00:00
|
|
|
# template string when <tt>render</tt> is called.
|
2006-12-18 01:31:11 +00:00
|
|
|
# See README for available options.
|
2006-10-27 21:20:19 +00:00
|
|
|
#
|
2006-10-30 06:59:57 +00:00
|
|
|
#--
|
|
|
|
# When adding options, remember to add information about them
|
2006-12-18 01:31:11 +00:00
|
|
|
# to README!
|
2006-10-30 06:59:57 +00:00
|
|
|
#++
|
2006-10-30 06:10:26 +00:00
|
|
|
#
|
2007-11-23 16:40:44 +00:00
|
|
|
def initialize(template, options = {})
|
2006-10-27 21:20:19 +00:00
|
|
|
@options = {
|
2006-10-30 06:10:26 +00:00
|
|
|
:suppress_eval => false,
|
2006-11-04 08:35:06 +00:00
|
|
|
:attr_wrapper => "'",
|
2007-11-18 09:57:58 +00:00
|
|
|
:autoclose => ['meta', 'img', 'link', 'br', 'hr', 'input', 'area'],
|
2007-01-19 17:03:47 +00:00
|
|
|
:filters => {
|
|
|
|
'sass' => Sass::Engine,
|
2007-02-06 08:08:32 +00:00
|
|
|
'plain' => Haml::Filters::Plain,
|
2007-08-11 21:04:51 +00:00
|
|
|
'preserve' => Haml::Filters::Preserve,
|
|
|
|
'redcloth' => Haml::Filters::RedCloth,
|
2007-01-19 17:03:47 +00:00
|
|
|
'textile' => Haml::Filters::Textile,
|
2007-08-11 21:04:51 +00:00
|
|
|
'markdown' => Haml::Filters::Markdown }
|
|
|
|
}
|
2007-11-23 16:40:44 +00:00
|
|
|
@options.rec_merge! options
|
2007-01-19 17:03:47 +00:00
|
|
|
|
2007-03-27 20:24:29 +00:00
|
|
|
unless @options[:suppress_eval]
|
|
|
|
@options[:filters].merge!({
|
|
|
|
'erb' => ERB,
|
|
|
|
'ruby' => Haml::Filters::Ruby
|
|
|
|
})
|
|
|
|
end
|
2007-11-23 16:40:44 +00:00
|
|
|
@options[:filters].rec_merge! options[:filters] if options[:filters]
|
2007-03-27 20:24:29 +00:00
|
|
|
|
2007-11-24 08:35:10 +00: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 07:27:11 +00:00
|
|
|
@template = template.strip #String
|
2006-10-14 22:24:53 +00:00
|
|
|
@to_close_stack = []
|
2006-11-18 07:00:10 +00:00
|
|
|
@output_tabs = 0
|
|
|
|
@template_tabs = 0
|
2007-01-27 07:10:49 +00:00
|
|
|
@index = 0
|
2006-11-04 06:36:16 +00:00
|
|
|
@flat_spaces = -1
|
|
|
|
|
2007-11-23 07:42:59 +00:00
|
|
|
precompile
|
2007-11-23 07:51:08 +00:00
|
|
|
rescue Haml::Error => e
|
|
|
|
e.add_backtrace_entry(@index, @options[:filename])
|
|
|
|
raise e
|
2006-09-12 04:14:21 +00:00
|
|
|
end
|
2006-09-29 19:59:21 +00:00
|
|
|
|
2006-11-21 05:54:26 +00:00
|
|
|
# Processes the template and returns the result as a string.
|
2007-11-23 12:14:02 +00: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-24 02:37:52 +00:00
|
|
|
#
|
2007-11-24 08:35:10 +00: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 12:14:02 +00:00
|
|
|
#
|
|
|
|
# If a block is passed to render,
|
|
|
|
# that block is run when +yield+ is called
|
|
|
|
# within the template.
|
|
|
|
#
|
2007-11-24 08:35:10 +00:00
|
|
|
# Due to some Ruby quirks,
|
2007-11-23 12:14:02 +00: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 08:35:10 +00:00
|
|
|
def render(scope = Object.new, locals = {}, &block)
|
|
|
|
locals = (@options[:locals] || {}).merge(locals)
|
2007-11-23 16:55:30 +00:00
|
|
|
buffer = Haml::Buffer.new(options_for_buffer)
|
2006-08-06 03:18:54 +00:00
|
|
|
|
2007-11-23 12:14:02 +00:00
|
|
|
if scope.is_a?(Binding) || scope.is_a?(Proc)
|
2007-11-23 07:02:07 +00:00
|
|
|
scope_object = eval("self", scope)
|
2007-11-23 12:14:02 +00:00
|
|
|
scope = scope_object.instance_eval{binding} if block_given?
|
2007-11-23 07:02:07 +00:00
|
|
|
else
|
|
|
|
scope_object = scope
|
2007-11-23 12:14:02 +00:00
|
|
|
scope = scope_object.instance_eval{binding}
|
2007-11-23 07:02:07 +00:00
|
|
|
end
|
2007-09-19 06:11:47 +00:00
|
|
|
|
2007-11-24 08:35:10 +00:00
|
|
|
set_locals(locals.merge(:_hamlout => buffer, :_erbout => buffer.buffer), scope, scope_object)
|
2007-11-23 12:14:02 +00:00
|
|
|
|
2007-11-23 07:02:07 +00:00
|
|
|
scope_object.instance_eval do
|
2007-11-23 16:32:03 +00:00
|
|
|
extend Haml::Helpers
|
2006-10-14 22:24:53 +00:00
|
|
|
@haml_stack ||= Array.new
|
|
|
|
@haml_stack.push(buffer)
|
2007-11-23 16:32:03 +00:00
|
|
|
@haml_is_haml = true
|
2007-11-23 07:02:07 +00:00
|
|
|
end
|
2006-11-04 06:36:16 +00:00
|
|
|
|
2006-10-14 22:24:53 +00:00
|
|
|
begin
|
2007-11-23 12:14:02 +00:00
|
|
|
eval(@precompiled, scope, '(haml-eval)')
|
2006-10-14 22:24:53 +00:00
|
|
|
rescue Exception => e
|
2007-11-24 08:35:10 +00:00
|
|
|
raise Engine.add_exception_info(e, @precompiled, @options[:filename])
|
2006-10-14 22:24:53 +00:00
|
|
|
end
|
2006-11-04 06:36:16 +00:00
|
|
|
|
2006-10-14 22:24:53 +00:00
|
|
|
# Get rid of the current buffer
|
2007-11-23 07:02:07 +00:00
|
|
|
scope_object.instance_eval do
|
2006-10-14 22:24:53 +00:00
|
|
|
@haml_stack.pop
|
2007-11-23 16:32:03 +00:00
|
|
|
@haml_is_haml = false
|
2006-11-04 06:36:16 +00:00
|
|
|
end
|
2007-11-23 16:55:30 +00:00
|
|
|
|
|
|
|
buffer.buffer
|
2006-07-19 22:23:01 +00:00
|
|
|
end
|
2007-11-23 16:55:30 +00:00
|
|
|
alias_method :to_html, :render
|
|
|
|
|
2007-11-23 17:26:05 +00: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 08:35:10 +00: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 17:26:05 +00:00
|
|
|
# The proc doesn't take a block;
|
|
|
|
# any yields in the template will fail.
|
2007-11-24 08:35:10 +00:00
|
|
|
def render_proc(scope = Object.new, *local_names)
|
2007-11-23 17:26:05 +00: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
|
|
|
|
|
|
|
|
begin
|
2007-11-24 08:35:10 +00:00
|
|
|
eval("Proc.new { |*_haml_locals| _haml_locals = _haml_locals[0] || {};" +
|
|
|
|
precompiled_with_ambles(local_names) + "}\n", scope, '(haml-eval)')
|
2007-11-23 17:26:05 +00:00
|
|
|
rescue Exception => e
|
2007-11-24 08:35:10 +00:00
|
|
|
raise Haml::Engine.add_exception_info(e, @precompiled, @options[:filename])
|
2007-11-23 17:26:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-24 02:32:18 +00: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 08:35:10 +00: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-24 02:32:18 +00:00
|
|
|
method = object.is_a?(Module) ? :module_eval : :instance_eval
|
|
|
|
|
|
|
|
begin
|
2007-11-24 08:35:10 +00:00
|
|
|
object.send(method, "def #{name}(_haml_locals = {}); #{precompiled_with_ambles(local_names)}; end", '(haml-eval)')
|
2007-11-24 02:32:18 +00:00
|
|
|
rescue Exception => e
|
2007-11-24 08:35:10 +00:00
|
|
|
raise Haml::Engine.add_exception_info(e, @precompiled, @options[:filename])
|
2007-11-24 02:32:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-23 16:55:30 +00:00
|
|
|
private
|
2007-11-23 09:43:26 +00:00
|
|
|
|
2007-11-23 16:32:03 +00: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-24 08:35:10 +00:00
|
|
|
def self.add_exception_info(e, precompiled, filename)
|
2007-11-23 09:43:26 +00:00
|
|
|
metaclass = class << e; self; end
|
|
|
|
metaclass.send(:include, Haml::Error)
|
|
|
|
|
|
|
|
eval_line = e.backtrace[0].scan(/:([0-9]*)/)[0][0].to_i
|
2007-11-24 01:29:57 +00:00
|
|
|
line_marker = precompiled.split("\n")[0...eval_line].grep(/#haml_lineno: [0-9]+/)[-1]
|
|
|
|
e.add_backtrace_entry(line_marker ? line_marker.scan(/[0-9]+/)[0].to_i : -1, filename)
|
2007-11-23 09:43:26 +00:00
|
|
|
|
|
|
|
# Format Ruby compiler errors nicely
|
|
|
|
message = e.message.scan(/compile error\n\(haml-eval\):[0-9]*: (.*)/)[0]
|
|
|
|
metaclass.send(:define_method, :message) { "compile error: #{message}" } if message
|
|
|
|
|
|
|
|
return e
|
|
|
|
end
|
2007-11-23 16:55:48 +00:00
|
|
|
|
|
|
|
# Returns a hash of options that Haml::Buffer cares about.
|
|
|
|
# This should remain loadable form #inspect.
|
|
|
|
def options_for_buffer
|
|
|
|
{:attr_wrapper => @options[:attr_wrapper]}
|
|
|
|
end
|
2006-06-30 15:14:44 +00:00
|
|
|
end
|
2006-09-14 19:12:45 +00:00
|
|
|
end
|