1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Pre-render tags with no dynamic content.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@528 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-06-06 08:30:40 +00:00
parent 930ff91b81
commit 78e330d130
2 changed files with 65 additions and 12 deletions

View file

@ -48,7 +48,7 @@ module Haml
# than the number of tabs.
@buffer << "#{' ' * tabulation}#{flatten(text + "\n")}"
@one_liner_pending = true
elsif @one_liner_pending && one_liner?(text)
elsif @one_liner_pending && Buffer.one_liner?(text)
@buffer << text
else
if @one_liner_pending
@ -77,6 +77,11 @@ module Haml
end
nil
end
def open_prerendered_tag(tag, tabulation)
@buffer << "#{tabs(tabulation)}#{tag}"
@real_tabs += 1
end
# Takes the various information about the opening tag for an
# element, formats it, and adds it to the buffer.
@ -161,6 +166,12 @@ module Haml
end
result.sort.join
end
# Returns whether or not the given value is short enough to be rendered
# on one line.
def self.one_liner?(value)
value.length <= ONE_LINER_LENGTH && value.scan(/\n/).empty?
end
private
@ -199,12 +210,6 @@ module Haml
{'id' => id, 'class' => class_name}
end
# Returns whether or not the given value is short enough to be rendered
# on one line.
def one_liner?(value)
value.length <= ONE_LINER_LENGTH && value.scan(/\n/).empty?
end
# Changes a word from camel case to underscores.
# Based on the method of the same name in Rails' Inflector,
# but copied here so it'll run properly without Rails.

View file

@ -633,7 +633,7 @@ END
end
attributes
end
def parse_literal_value(text)
text.match(LITERAL_VALUE_REGEX)
@ -665,6 +665,37 @@ END
attributes
end
def build_attributes(attributes = {})
@quote_escape = @options[:attr_wrapper] == '"' ? "&quot;" : "&apos;"
@other_quote_char = @options[:attr_wrapper] == '"' ? "'" : '"'
result = attributes.collect do |a,v|
v = v.to_s
unless v.nil? || v.empty?
attr_wrapper = @options[:attr_wrapper]
if v.include? attr_wrapper
if v.include? @other_quote_char
v = v.gsub(attr_wrapper, @quote_escape)
else
attr_wrapper = @other_quote_char
end
end
" #{a}=#{attr_wrapper}#{v}#{attr_wrapper}"
end
end
result.sort.join
end
def prerender_tag(name, atomic, attributes)
if atomic
str = " />"
else
str = ">"
end
"<#{name}#{build_attributes(attributes)}#{str}"
end
# 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)
@ -713,12 +744,29 @@ END
raise SyntaxError.new("No tag content to parse.")
end
if !@block_opened && !value_exists && !atomic && @options[:autoclose].include?(tag_name)
if !@block_opened && !value_exists && @options[:autoclose].include?(tag_name)
atomic = true
end
push_silent "_hamlout.open_tag(#{tag_name.inspect}, #{@output_tabs}, #{atomic.inspect}, #{value_exists.inspect}, #{attributes.inspect}, #{object_ref}, #{flattened.inspect}, #{attributes_hash[1...-1]})", true
do_one_liner = value_exists && !parse && Buffer.one_liner?(value)
if(object_ref == "nil" && attributes_hash == "{nil}" && !flattened && (do_one_liner || !value_exists))
# This means that we can render the tag directly to text and not process it in the buffer
open_tag = prerender_tag(tag_name, atomic, attributes)
if do_one_liner
open_tag += value
open_tag += "</#{tag_name}>"
end
open_tag += "\n"
push_silent "_hamlout.open_prerendered_tag(#{open_tag.dump}, #{@output_tabs})"
return if do_one_liner
else
push_silent "_hamlout.open_tag(#{tag_name.inspect}, #{@output_tabs}, #{atomic.inspect}, #{value_exists.inspect}, #{attributes.inspect}, #{object_ref}, #{flattened.inspect}, #{attributes_hash[1...-1]})", true
end
unless atomic
push_and_tabulate([:element, tag_name])
@output_tabs += 1