0
@@ -3,53 +3,34 @@ require File.dirname(__FILE__) + '/helpers'
0
# Set the maximum length for a line to be considered a one-liner
0
# Lines <= the maximum will be rendered on one line,
0
# i.e. <tt><p>Hello world</p></tt>
0
MULTILINE_CHAR_VALUE = '|'[0]
0
+ def initialize(template, action_view=nil)
0
+ @template = template #String
0
- def render(template, local_assigns={})
0
- assigns = @view.assigns.dup
0
- # Do content for layout on its own to keep things working in partials
0
- if content_for_layout = @view.instance_variable_get("@content_for_layout")
0
- assigns['content_for_layout'] = content_for_layout
0
- # Get inside the view object's world
0
- @view.instance_eval do
0
- # Set all the instance variables
0
- assigns.each do |key,val|
0
- instance_variable_set "@#{key}", val
0
- # Set all the local assigns
0
- local_assigns.each do |key,val|
0
- class << self; self; end.send(:define_method, key) { val }
0
- # Process each line of the template returning the resulting string
0
- template.each_with_index do |line, index|
0
+ # Process each line of the template
0
+ @template.each_with_index do |line, index|
0
count, line = count_soft_tabs(line)
0
surpress_render, line, count = handle_multiline(count, line)
0
if !surpress_render && count && line
0
count, line = process_line(count, line)
0
# Close all the open tags
0
@to_close_queue.length.times { close_tag }
0
# Return the result string
0
@@ -61,7 +42,7 @@ module Haml #:nodoc:
0
if count <= @to_close_queue.size && @to_close_queue.size > 0
0
(@to_close_queue.size - count).times { close_tag }
0
@@ -70,9 +51,9 @@ module Haml #:nodoc:
0
- add template_eval(line[1, line.length]).to_s
0
+ add template_eval(line[1, line.length]).to_s
if @view0
- add find_and_flatten(template_eval(line[1, line.length])).to_s
0
+ add find_and_flatten(template_eval(line[1, line.length])).to_s
if @view0
@@ -81,22 +62,28 @@ module Haml #:nodoc:
0
def handle_multiline(count, line)
0
- # The code to handle how a multi-line object should work.
0
- if @multiline_buffer && line[-1] == MULTILINE_CHAR_VALUE # '|' is 124
0
+ # Multilines are denoting by ending with a `|` (124)
0
+ if @multiline_buffer && line[-1] == MULTILINE_CHAR_VALUE
0
# A multiline string is active, and is being continued
0
@multiline_buffer += line[0...-1]
0
elsif line[-1] == MULTILINE_CHAR_VALUE
0
# A multiline string has just been activated, start adding the lines
0
@multiline_buffer = line[0...-1]
0
@multiline_count = count
0
elsif @multiline_buffer
0
# A multiline string has just ended, make line into the result
0
process_line(@multiline_count, @multiline_buffer)
0
@multiline_buffer = nil
0
return supress_render, line, count
0
@@ -107,15 +94,27 @@ module Haml #:nodoc:
0
+ def build_attributes(attributes = {})
0
+ attributes.empty? ? String.new : String.new(' ') << (attributes.collect {|a,v| "#{a.to_s}='#{v.to_s}'" unless v.nil? }).compact.join(' ')
0
def open_tag(name, attributes = {})
0
add "<#{name.to_s}#{build_attributes(attributes)}>"
0
@to_close_queue.push name
0
+ add "</#{@to_close_queue.pop}>"
0
def one_line_tag(name, value, attributes = {})
0
add "<#{name.to_s}#{build_attributes(attributes)}>#{value}</#{name.to_s}>"
0
+ value.length <= ONE_LINER_LENGTH && value.scan(/\n/).empty?
0
def print_tag(name, value, attributes = {})
0
@@ -136,37 +135,43 @@ module Haml #:nodoc:
0
add "<#{name.to_s}#{build_attributes(attributes)} />"
0
- def build_attributes(attributes = {})
0
- attributes.empty? ? String.new : String.new(' ') << (attributes.collect {|a,v| "#{a.to_s}='#{v.to_s}'" unless v.nil? }).compact.join(' ')
0
- add "</#{@to_close_queue.pop}>"
0
- render_tag('%div' + line)
0
- def render_comment(line)
0
- add "<!-- #{line[1..line.length].strip} -->"
0
+ def parse_class_and_id(list)
0
+ list.scan(/([#.])([-a-zA-Z_()]+)/).each do |type, property|
0
+ attributes[:class] = property
0
+ attributes[:id] = property
0
line.scan(/[%]([-_a-z1-9]+)([-_a-z\.\#]*)(\{.*\})?(\[.*\])?([=\/\~]?)?(.*)?/).each do |tag_name, attributes, attributes_hash, object_ref, action, value|
0
attributes = parse_class_and_id(attributes.to_s)
0
- attributes.merge!(template_eval(attributes_hash)) unless (attributes_hash.nil? || attributes_hash.empty?)
0
- if object_ref && (object_ref = template_eval(object_ref).first)
0
- class_name = object_ref.class.to_s.underscore
0
- attributes.merge!(:id => "#{class_name}_#{object_ref.id}", :class => class_name)
0
+ unless (attributes_hash.nil? || attributes_hash.empty?)
0
+ # Determine whether to eval the attributes hash in the context of a template
0
+ add_attributes = @view ? template_eval(attributes_hash) : eval(attributes_hash)
0
+ attributes.merge!(add_attributes)
0
+ if object_ref && (object_ref = template_eval(object_ref).first)
0
+ class_name = object_ref.class.to_s.underscore
0
+ attributes.merge!(:id => "#{class_name}_#{object_ref.id}", :class => class_name)
0
atomic_tag(tag_name, attributes)
0
- elsif action == '=' || action == '~'
0
- value = template_eval(value)
0
- value = find_and_flatten(value) if action == '~'
0
+ value = template_eval(value) if @view
0
+ value = find_and_flatten(value) if action == '~' and @view
0
print_tag(tag_name, value.to_s, attributes) if value
0
print_tag(tag_name, value.to_s.strip, attributes)
0
@@ -174,25 +179,14 @@ module Haml #:nodoc:
0
- # Searches for `#` and `.` characters indicating id and class attributes
0
- def parse_class_and_id(list)
0
- list.scan(/([#.])([-a-zA-Z_()]+)/).each do |type, property|
0
- attributes[:class] = property
0
- attributes[:id] = property
0
+ render_tag('%div' + line)
0
- value.length <= ONE_LINER_LENGTH && value.scan(/\n/).empty?
0
+ def render_comment(line)
0
+ add "<!-- #{line[1..line.length].strip} -->"
0
- # Evaluates input in the context of the current ActionView instance
0
def template_eval(args)
0
@view.instance_eval(args)
Comments
No one has commented yet.