2006-08-05 23:18:54 -04:00
|
|
|
require File.dirname(__FILE__) + '/helpers'
|
2006-10-14 18:24:53 -04:00
|
|
|
require File.dirname(__FILE__) + '/buffer'
|
2006-08-05 23:18:54 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
module Haml
|
|
|
|
# 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:
|
2006-11-04 01:36:16 -05:00
|
|
|
#
|
2006-10-14 18:24:53 -04:00
|
|
|
# template = File.load('templates/really_cool_template.haml')
|
|
|
|
# haml_engine = Haml::Engine.new(template)
|
|
|
|
# output = haml_engine.to_html
|
|
|
|
# puts output
|
2006-08-05 23:18:54 -04:00
|
|
|
class Engine
|
2006-10-21 16:07:01 -04:00
|
|
|
# Allow access to the precompiled template
|
|
|
|
attr_reader :precompiled
|
2006-11-04 01:36:16 -05:00
|
|
|
|
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
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# Designates an XHTML/XML element.
|
|
|
|
ELEMENT = '%'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# Designates a <tt><div></tt> element with the given class.
|
|
|
|
DIV_CLASS = '.'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# Designates a <tt><div></tt> element with the given id.
|
|
|
|
DIV_ID = '#'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# Designates an XHTML/XML comment.
|
|
|
|
COMMENT = '/'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:13:17 -04:00
|
|
|
# Designates an XHTML doctype.
|
|
|
|
DOCTYPE = '!'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# Designates script, the result of which is output.
|
|
|
|
SCRIPT = '='[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# Designates script, the result of which is flattened and output.
|
|
|
|
FLAT_SCRIPT = '~'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# Designates script which is run but not output.
|
|
|
|
SILENT_SCRIPT = '-'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# When following SILENT_SCRIPT, designates a comment that is not output.
|
|
|
|
SILENT_COMMENT = '#'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-11-16 20:12:50 -05:00
|
|
|
# Designates a non-parsed line.
|
|
|
|
ESCAPE = '\\'[0]
|
|
|
|
|
2006-10-22 15:13:17 -04:00
|
|
|
# Designates a non-parsed line. Not actually a character.
|
|
|
|
PLAIN_TEXT = -1
|
2006-11-04 01:36:16 -05:00
|
|
|
|
|
|
|
# Keeps track of the ASCII values of the characters that begin a
|
2006-10-14 18:24:53 -04:00
|
|
|
# specially-interpreted line.
|
2006-10-22 15:09:53 -04:00
|
|
|
SPECIAL_CHARACTERS = [
|
|
|
|
ELEMENT,
|
|
|
|
DIV_CLASS,
|
|
|
|
DIV_ID,
|
|
|
|
COMMENT,
|
2006-10-22 15:13:17 -04:00
|
|
|
DOCTYPE,
|
2006-10-22 15:09:53 -04:00
|
|
|
SCRIPT,
|
|
|
|
FLAT_SCRIPT,
|
2006-11-16 20:12:50 -05:00
|
|
|
SILENT_SCRIPT,
|
|
|
|
ESCAPE
|
2006-10-22 15:09:53 -04:00
|
|
|
]
|
2006-10-14 18:24:53 -04:00
|
|
|
|
|
|
|
# The value of the character that designates that a line is part
|
|
|
|
# of a multiline string.
|
2006-09-21 22:18:43 -04:00
|
|
|
MULTILINE_CHAR_VALUE = '|'[0]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Characters that designate that a multiline string may be about
|
|
|
|
# to begin.
|
2006-09-29 15:20:53 -04:00
|
|
|
MULTILINE_STARTERS = SPECIAL_CHARACTERS - ["/"[0]]
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# 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:
|
2006-11-04 01:36:16 -05:00
|
|
|
#
|
2006-10-14 18:24:53 -04:00
|
|
|
# - if foo
|
|
|
|
# %p yes!
|
|
|
|
# - else
|
|
|
|
# %p no!
|
2006-11-04 01:36:16 -05:00
|
|
|
#
|
2006-10-14 18:24:53 -04:00
|
|
|
# The block is ended after <tt>%p no!</tt>, because <tt>else</tt>
|
|
|
|
# is a member of this array.
|
|
|
|
MID_BLOCK_KEYWORDS = ['else', 'elsif', 'rescue', 'ensure', 'when']
|
2006-09-29 14:39:13 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Creates a new instace of Haml::Engine to compile the given
|
2006-10-30 01:59:57 -05:00
|
|
|
# template string. See REFERENCE 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
|
|
|
|
# to REFERENCE!
|
|
|
|
#++
|
2006-10-30 01:10:26 -05:00
|
|
|
#
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-09-29 15:57:35 -04: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 => "'",
|
|
|
|
:locals => {}
|
2006-10-27 17:20:19 -04:00
|
|
|
}.merge options
|
|
|
|
@precompiled = @options[:precompiled]
|
2006-09-29 15:57:35 -04:00
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
@template = template #String
|
2006-10-14 18:24:53 -04:00
|
|
|
@to_close_stack = []
|
|
|
|
@tabulation = 0
|
2006-10-21 16:07:01 -04:00
|
|
|
|
2006-11-04 01:36:16 -05:00
|
|
|
# This is the base tabulation of the currently active
|
|
|
|
# flattened block. -1 signifies that there is no such block.
|
|
|
|
@flat_spaces = -1
|
|
|
|
|
2006-10-21 16:07:01 -04:00
|
|
|
# Only do the first round of pre-compiling if we really need to.
|
|
|
|
# They might be passing in the precompiled string.
|
|
|
|
do_precompile if @precompiled.nil? && (@precompiled = String.new)
|
2006-09-12 00:14:21 -04:00
|
|
|
end
|
2006-09-29 15:59:21 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Processes the template and returns the resulting (X)HTML code as
|
|
|
|
# a string.
|
2006-10-22 16:21:15 -04:00
|
|
|
def to_html(scope = Object.new, &block)
|
2006-10-21 17:11:55 -04:00
|
|
|
@scope_object = scope
|
2006-10-30 01:10:26 -05:00
|
|
|
@buffer = Haml::Buffer.new(@options)
|
2006-10-21 16:07:01 -04:00
|
|
|
|
2006-11-04 03:35:06 -05:00
|
|
|
local_assigns = @options[:locals]
|
|
|
|
|
|
|
|
# Get inside the view object's world
|
|
|
|
@scope_object.instance_eval do
|
|
|
|
# Set all the local assigns
|
|
|
|
local_assigns.each do |key,val|
|
|
|
|
self.class.send(:define_method, key) { val }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-10-21 16:07:01 -04:00
|
|
|
# Compile the @precompiled buffer
|
2006-10-22 16:21:15 -04:00
|
|
|
compile &block
|
2006-10-21 16:07:01 -04:00
|
|
|
|
|
|
|
# Return the result string
|
|
|
|
@buffer.buffer
|
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-21 16:07:01 -04:00
|
|
|
private
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-21 16:07:01 -04:00
|
|
|
#Precompile each line
|
|
|
|
def do_precompile
|
2006-10-22 16:21:15 -04:00
|
|
|
push_silent <<-END
|
|
|
|
def _haml_render
|
|
|
|
_hamlout = @haml_stack[-1]
|
|
|
|
_erbout = _hamlout.buffer
|
|
|
|
END
|
2006-11-18 01:32:03 -05:00
|
|
|
|
|
|
|
old_line = nil
|
|
|
|
old_index = nil
|
|
|
|
old_spaces = nil
|
|
|
|
old_tabs = nil
|
|
|
|
(@template + "\n\n").each_with_index do |line, index|
|
2006-11-04 01:36:16 -05:00
|
|
|
spaces, tabs = count_soft_tabs(line)
|
|
|
|
line = line.strip
|
2006-11-18 01:32:03 -05:00
|
|
|
|
|
|
|
if old_line
|
|
|
|
block_opened = tabs > old_tabs
|
|
|
|
|
|
|
|
suppress_render = handle_multiline(old_spaces, old_tabs, old_line, old_index, block_opened)
|
2006-11-18 01:55:08 -05:00
|
|
|
|
2006-11-18 01:32:03 -05:00
|
|
|
if !suppress_render && old_spaces
|
2006-11-18 01:55:08 -05:00
|
|
|
line_empty = old_line.empty?
|
|
|
|
process_indent(old_tabs, old_line) unless line_empty
|
|
|
|
flat = @flat_spaces != -1
|
|
|
|
|
|
|
|
if flat
|
|
|
|
push_flat(old_line, old_spaces)
|
|
|
|
elsif !line_empty
|
|
|
|
process_line(old_tabs, old_line, old_index, block_opened)
|
|
|
|
end
|
2006-11-18 01:32:03 -05:00
|
|
|
end
|
2006-07-01 22:09:44 -04:00
|
|
|
end
|
2006-11-18 01:32:03 -05:00
|
|
|
|
|
|
|
old_line = line
|
|
|
|
old_index = index
|
|
|
|
old_spaces = spaces
|
|
|
|
old_tabs = tabs
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-09-12 00:14:21 -04:00
|
|
|
# Close all the open tags
|
2006-10-14 18:24:53 -04:00
|
|
|
@to_close_stack.length.times { close }
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 16:21:15 -04:00
|
|
|
push_silent "end"
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|
2006-11-18 01:55:08 -05:00
|
|
|
|
|
|
|
def process_indent(count, line)
|
|
|
|
if count <= @to_close_stack.size && @to_close_stack.size > 0
|
|
|
|
to_close = @to_close_stack.size - count
|
|
|
|
|
|
|
|
to_close.times do |i|
|
|
|
|
offset = to_close - 1 - i
|
|
|
|
unless offset == 0 && mid_block_keyword?(line)
|
|
|
|
close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Processes a single line of HAML. <tt>count</tt> does *not* represent the
|
|
|
|
# line number; rather, it represents the tabulation count (the number of
|
|
|
|
# spaces before the line divided by two).
|
2006-11-04 01:36:16 -05:00
|
|
|
#
|
2006-10-14 18:24:53 -04:00
|
|
|
# This method doesn't return anything; it simply processes the line and
|
|
|
|
# adds the appropriate code to <tt>@precompiled</tt>.
|
2006-11-18 01:55:08 -05:00
|
|
|
def process_line(count, line, index, block_opened)
|
|
|
|
case line[0]
|
|
|
|
when DIV_CLASS, DIV_ID
|
|
|
|
render_div(line, index)
|
|
|
|
when ELEMENT
|
|
|
|
render_tag(line, index)
|
|
|
|
when COMMENT
|
|
|
|
render_comment(line)
|
|
|
|
when SCRIPT
|
|
|
|
push_script(line[1..-1], false, index)
|
|
|
|
when FLAT_SCRIPT
|
|
|
|
push_script(line[1..-1], true, index)
|
|
|
|
when SILENT_SCRIPT
|
|
|
|
sub_line = line[1..-1]
|
|
|
|
unless sub_line[0] == SILENT_COMMENT
|
|
|
|
push_silent(sub_line, index)
|
|
|
|
if block_opened && !mid_block_keyword?(line)
|
|
|
|
@to_close_stack.push([:script])
|
2006-10-22 15:28:54 -04:00
|
|
|
end
|
|
|
|
end
|
2006-11-18 01:55:08 -05:00
|
|
|
when DOCTYPE
|
|
|
|
if line[0...3] == '!!!'
|
|
|
|
line = line[3..-1].lstrip.downcase
|
|
|
|
if line[0...3] == "xml"
|
|
|
|
encoding = line.split[1] || "utf-8"
|
|
|
|
wrapper = @options[:attr_wrapper]
|
|
|
|
doctype = "<?xml version=#{wrapper}1.0#{wrapper} encoding=#{wrapper}#{encoding}#{wrapper} ?>"
|
|
|
|
else
|
|
|
|
version, type = line.scan(/([0-9]\.[0-9])?[\s]*([a-zA-Z]*)/)[0]
|
|
|
|
if version == "1.1"
|
|
|
|
doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
|
2006-10-27 16:36:34 -04:00
|
|
|
else
|
2006-11-18 01:55:08 -05:00
|
|
|
case type
|
|
|
|
when "strict"
|
|
|
|
doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
|
|
|
|
when "frameset"
|
|
|
|
doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
|
2006-10-27 16:36:34 -04:00
|
|
|
else
|
2006-11-18 01:55:08 -05:00
|
|
|
doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
|
2006-10-27 16:36:34 -04:00
|
|
|
end
|
|
|
|
end
|
2006-10-14 18:24:53 -04:00
|
|
|
end
|
2006-11-18 01:55:08 -05:00
|
|
|
push_text doctype
|
2006-10-22 15:13:17 -04:00
|
|
|
else
|
2006-11-04 01:36:16 -05:00
|
|
|
push_text line
|
2006-09-21 22:18:43 -04:00
|
|
|
end
|
2006-11-18 01:55:08 -05:00
|
|
|
when ESCAPE
|
|
|
|
push_text line[1..-1]
|
|
|
|
else
|
|
|
|
push_text line
|
2006-09-21 22:18:43 -04:00
|
|
|
end
|
|
|
|
end
|
2006-11-18 01:32:03 -05:00
|
|
|
|
|
|
|
# Returns whether or not the line is a silent script line with one
|
|
|
|
# of Ruby's mid-block keywords.
|
|
|
|
def mid_block_keyword?(line)
|
|
|
|
line.length > 2 && line[0] == SILENT_SCRIPT && MID_BLOCK_KEYWORDS.include?(line[1..-1].split[0])
|
|
|
|
end
|
2006-09-21 22:18:43 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Deals with all the logic of figuring out whether a given line is
|
|
|
|
# the beginning, continuation, or end of a multiline sequence. Like
|
|
|
|
# process_line, <tt>count</tt> represents the tabulation, not line
|
|
|
|
# number.
|
2006-11-04 01:36:16 -05:00
|
|
|
#
|
2006-10-14 18:24:53 -04:00
|
|
|
# This returns whether or not the line should be
|
|
|
|
# rendered normally.
|
2006-11-18 01:32:03 -05:00
|
|
|
def handle_multiline(spaces, count, line, index, block_opened)
|
2006-09-29 14:39:13 -04:00
|
|
|
# Multilines are denoting by ending with a `|` (124)
|
2006-10-22 15:09:53 -04:00
|
|
|
if is_multiline?(line) && @multiline_buffer
|
2006-11-04 01:36:16 -05:00
|
|
|
# A multiline string is active, and is being continued
|
2006-09-21 22:18:43 -04:00
|
|
|
@multiline_buffer += line[0...-1]
|
2006-10-14 18:24:53 -04:00
|
|
|
suppress_render = true
|
2006-10-22 15:09:53 -04:00
|
|
|
elsif is_multiline?(line) && (MULTILINE_STARTERS.include? line[0])
|
2006-09-21 22:18:43 -04:00
|
|
|
# A multiline string has just been activated, start adding the lines
|
|
|
|
@multiline_buffer = line[0...-1]
|
|
|
|
@multiline_count = count
|
2006-10-14 18:24:53 -04:00
|
|
|
@multiline_index = index
|
|
|
|
suppress_render = true
|
2006-09-21 22:18:43 -04:00
|
|
|
elsif @multiline_buffer
|
|
|
|
# A multiline string has just ended, make line into the result
|
2006-11-18 01:55:08 -05:00
|
|
|
process_line(@multiline_count, @multiline_buffer, @multiline_index, block_opened)
|
2006-09-21 22:18:43 -04:00
|
|
|
@multiline_buffer = nil
|
2006-10-14 18:24:53 -04:00
|
|
|
suppress_render = false
|
2006-09-21 22:18:43 -04:00
|
|
|
end
|
2006-10-02 19:49:53 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
return suppress_render
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 15:09:53 -04:00
|
|
|
# Checks whether or not +line+ is in a multiline sequence.
|
|
|
|
def is_multiline?(line) # ' '[0] == 32
|
|
|
|
line && line.length > 1 && line[-1] == MULTILINE_CHAR_VALUE && line[-2] == 32
|
|
|
|
end
|
2006-08-05 23:18:54 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Takes <tt>@precompiled</tt>, a string buffer of Ruby code, and
|
|
|
|
# evaluates it in the context of <tt>@scope_object</tt>, after preparing
|
|
|
|
# <tt>@scope_object</tt>. The code in <tt>@precompiled</tt> populates
|
|
|
|
# <tt>@buffer</tt> with the compiled XHTML code.
|
2006-10-22 16:21:15 -04:00
|
|
|
def compile(&block)
|
2006-10-14 18:24:53 -04:00
|
|
|
# Set the local variables pointing to the buffer
|
|
|
|
buffer = @buffer
|
2006-11-14 10:52:54 -05:00
|
|
|
@scope_object.extend Haml::Helpers
|
2006-10-14 18:24:53 -04:00
|
|
|
@scope_object.instance_eval do
|
|
|
|
@haml_stack ||= Array.new
|
|
|
|
@haml_stack.push(buffer)
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
class << self
|
|
|
|
attr :haml_lineno
|
2006-09-29 15:57:35 -04:00
|
|
|
end
|
2006-10-14 18:24:53 -04:00
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
begin
|
2006-10-21 16:16:26 -04:00
|
|
|
# Evaluate the buffer in the context of the scope object
|
2006-10-14 18:24:53 -04:00
|
|
|
@scope_object.instance_eval @precompiled
|
2006-10-22 16:21:15 -04:00
|
|
|
@scope_object._haml_render &block
|
2006-10-14 18:24:53 -04:00
|
|
|
rescue Exception => e
|
2006-10-21 16:16:26 -04:00
|
|
|
# Get information from the exception and format it so that
|
|
|
|
# Rails can understand it.
|
|
|
|
compile_error = e.message.scan(/\(eval\):([0-9]*):in `[-_a-zA-Z]*': compile error/)[0]
|
2006-10-14 18:24:53 -04:00
|
|
|
filename = "(haml)"
|
|
|
|
if @scope_object.methods.include? "haml_filename"
|
|
|
|
# For some reason that I can't figure out,
|
|
|
|
# @scope_object.methods.include? "haml_filename" && @scope_object.haml_filename
|
|
|
|
# is false when it shouldn't be. Nested if statements work, though.
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
if @scope_object.haml_filename
|
|
|
|
filename = "#{@scope_object.haml_filename}.haml"
|
|
|
|
end
|
|
|
|
end
|
2006-10-21 16:16:26 -04:00
|
|
|
lineno = @scope_object.haml_lineno
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-21 16:16:26 -04:00
|
|
|
if compile_error
|
|
|
|
eval_line = compile_error[0].to_i
|
|
|
|
line_marker = @precompiled.split("\n")[0...eval_line].grep(/@haml_lineno = [0-9]*/)[-1]
|
|
|
|
lineno = line_marker.scan(/[0-9]+/)[0].to_i if line_marker
|
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-21 16:16:26 -04:00
|
|
|
e.backtrace.unshift "#{filename}:#{lineno}"
|
2006-10-14 18:24:53 -04:00
|
|
|
raise e
|
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Get rid of the current buffer
|
|
|
|
@scope_object.instance_eval do
|
|
|
|
@haml_stack.pop
|
2006-11-04 01:36:16 -05:00
|
|
|
end
|
2006-07-19 18:23:01 -04:00
|
|
|
end
|
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Evaluates <tt>text</tt> in the context of <tt>@scope_object</tt>, but
|
|
|
|
# does not output the result.
|
|
|
|
def push_silent(text, index = nil)
|
|
|
|
if index
|
|
|
|
@precompiled << "@haml_lineno = #{index + 1}\n#{text}\n"
|
|
|
|
else
|
|
|
|
# Not really DRY, but probably faster
|
|
|
|
@precompiled << "#{text}\n"
|
|
|
|
end
|
2006-09-29 14:39:13 -04:00
|
|
|
end
|
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Adds <tt>text</tt> to <tt>@buffer</tt> with appropriate tabulation
|
|
|
|
# without parsing it.
|
|
|
|
def push_text(text)
|
|
|
|
@precompiled << "_hamlout.push_text(#{text.dump}, #{@tabulation})\n"
|
2006-07-19 18:23:01 -04:00
|
|
|
end
|
2006-08-05 23:18:54 -04:00
|
|
|
|
2006-11-04 01:36:16 -05:00
|
|
|
# Adds +text+ to <tt>@buffer</tt> while flattening text.
|
|
|
|
def push_flat(text, spaces)
|
|
|
|
tabulation = spaces - @flat_spaces
|
|
|
|
@precompiled << "_hamlout.push_text(#{text.dump}, #{tabulation > -1 ? tabulation : 0}, true)\n"
|
|
|
|
end
|
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Causes <tt>text</tt> to be evaluated in the context of
|
|
|
|
# <tt>@scope_object</tt> and the result to be added to <tt>@buffer</tt>.
|
2006-11-04 01:36:16 -05:00
|
|
|
#
|
2006-10-14 18:24:53 -04:00
|
|
|
# If <tt>flattened</tt> is true, Haml::Helpers#find_and_flatten is run on
|
|
|
|
# the result before it is added to <tt>@buffer</tt>
|
|
|
|
def push_script(text, flattened, index)
|
2006-10-27 17:20:19 -04:00
|
|
|
unless options[:suppress_eval]
|
2006-10-14 18:24:53 -04:00
|
|
|
push_silent("haml_temp = #{text}", index)
|
|
|
|
@precompiled << "haml_temp = _hamlout.push_script(haml_temp, #{@tabulation}, #{flattened})\n"
|
|
|
|
end
|
2006-09-29 14:39:13 -04:00
|
|
|
end
|
2006-10-05 11:18:35 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Closes the most recent item in <tt>@to_close_stack</tt>.
|
|
|
|
def close
|
2006-10-22 17:42:45 -04:00
|
|
|
tag, value = @to_close_stack.pop
|
|
|
|
case tag
|
|
|
|
when :script
|
2006-10-14 18:24:53 -04:00
|
|
|
close_block
|
2006-10-22 17:42:45 -04:00
|
|
|
when :comment
|
|
|
|
close_comment value
|
|
|
|
when :element
|
|
|
|
close_tag value
|
2006-07-20 00:01:23 -04:00
|
|
|
end
|
|
|
|
end
|
2006-07-19 18:23:01 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Puts a line in <tt>@precompiled</tt> that will add the closing tag of
|
|
|
|
# the most recently opened tag.
|
|
|
|
def close_tag(tag)
|
|
|
|
@tabulation -= 1
|
2006-11-04 01:36:16 -05:00
|
|
|
@flat_spaces = -1
|
2006-10-14 18:24:53 -04:00
|
|
|
@precompiled << "_hamlout.close_tag(#{tag.dump}, #{@tabulation})\n"
|
2006-07-19 18:23:01 -04:00
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Closes a Ruby block.
|
|
|
|
def close_block
|
|
|
|
push_silent "end"
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-22 17:42:45 -04:00
|
|
|
# Closes a comment.
|
|
|
|
def close_comment(has_conditional)
|
|
|
|
@tabulation -= 1
|
|
|
|
push_silent "_hamlout.close_comment(#{has_conditional}, #{@tabulation})"
|
|
|
|
end
|
2006-08-05 23:18:54 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Parses a line that will render as an XHTML tag, and adds the code that will
|
|
|
|
# render that tag to <tt>@precompiled</tt>.
|
|
|
|
def render_tag(line, index)
|
2006-10-21 23:56:59 -04:00
|
|
|
line.scan(/[%]([-:_a-zA-Z0-9]+)([-_a-zA-Z0-9\.\#]*)(\{.*\})?(\[.*\])?([=\/\~]?)?(.*)?/) do |tag_name, attributes, attributes_hash, object_ref, action, value|
|
2006-10-14 18:24:53 -04:00
|
|
|
value = value.to_s
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-09-29 14:39:13 -04:00
|
|
|
case action
|
|
|
|
when '/'
|
2006-10-14 18:24:53 -04:00
|
|
|
atomic = true
|
2006-09-29 14:39:13 -04:00
|
|
|
when '=', '~'
|
2006-10-14 18:24:53 -04:00
|
|
|
parse = true
|
2006-07-19 18:23:01 -04:00
|
|
|
else
|
2006-10-14 18:24:53 -04:00
|
|
|
value = value.strip
|
|
|
|
end
|
2006-11-04 01:36:16 -05:00
|
|
|
|
|
|
|
flattened = (action == '~')
|
2006-10-14 18:24:53 -04:00
|
|
|
value_exists = !value.empty?
|
|
|
|
attributes_hash = "nil" unless attributes_hash
|
|
|
|
object_ref = "nil" unless object_ref
|
2006-11-04 01:36:16 -05:00
|
|
|
|
|
|
|
push_silent "_hamlout.open_tag(#{tag_name.inspect}, #{@tabulation}, #{atomic.inspect}, #{value_exists.inspect}, #{attributes.inspect}, #{attributes_hash}, #{object_ref}, #{flattened.inspect})"
|
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
unless atomic
|
2006-10-22 17:42:45 -04:00
|
|
|
@to_close_stack.push [:element, tag_name]
|
2006-10-14 18:24:53 -04:00
|
|
|
@tabulation += 1
|
2006-11-04 01:36:16 -05:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
if value_exists
|
|
|
|
if parse
|
|
|
|
push_script(value, flattened, index)
|
|
|
|
else
|
|
|
|
push_text(value)
|
|
|
|
end
|
|
|
|
close
|
2006-11-04 01:36:16 -05:00
|
|
|
elsif flattened
|
|
|
|
# @flat_spaces is the number of indentations in the template
|
|
|
|
# that forms the base of the flattened area
|
|
|
|
@flat_spaces = @to_close_stack.size * 2
|
2006-10-14 18:24:53 -04:00
|
|
|
end
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-07-20 00:01:23 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Renders a line that creates an XHTML tag and has an implicit div because of
|
|
|
|
# <tt>.</tt> or <tt>#</tt>.
|
|
|
|
def render_div(line, index)
|
|
|
|
render_tag('%div' + line, index)
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|
2006-08-09 14:12:54 -04:00
|
|
|
|
2006-10-14 18:24:53 -04:00
|
|
|
# Renders an XHTML comment.
|
2006-09-29 14:39:13 -04:00
|
|
|
def render_comment(line)
|
2006-10-22 17:42:45 -04:00
|
|
|
conditional, content = line.scan(/\/(\[[a-zA-Z0-9 ]*\])?(.*)/)[0]
|
|
|
|
content = content.strip
|
|
|
|
try_one_line = !content.empty?
|
|
|
|
push_silent "_hamlout.open_comment(#{try_one_line}, #{conditional.inspect}, #{@tabulation})"
|
|
|
|
@tabulation += 1
|
|
|
|
@to_close_stack.push [:comment, !conditional.nil?]
|
|
|
|
if try_one_line
|
|
|
|
push_text content
|
|
|
|
close
|
|
|
|
end
|
2006-07-20 00:01:23 -04:00
|
|
|
end
|
2006-11-05 22:01:04 -05:00
|
|
|
|
|
|
|
# Counts the tabulation of a line.
|
|
|
|
def count_soft_tabs(line)
|
|
|
|
spaces = line.index(/[^ ]/)
|
|
|
|
spaces ? [spaces, spaces/2] : []
|
|
|
|
end
|
2006-06-30 11:14:44 -04:00
|
|
|
end
|
2006-09-14 15:12:45 -04:00
|
|
|
end
|