mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
LOTS of changes.
Fixed it so that instance variables now work with edge rails! git-svn-id: svn://hamptoncatlin.com/haml/trunk@7 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
60b2298346
commit
d4c7c8c431
1 changed files with 79 additions and 26 deletions
101
lib/haml.rb
101
lib/haml.rb
|
@ -5,6 +5,7 @@ module HAML
|
||||||
def initialize(base)
|
def initialize(base)
|
||||||
@base = base
|
@base = base
|
||||||
@tab_index = ["", " "]
|
@tab_index = ["", " "]
|
||||||
|
@happy_land = HappyLand.new(@base, @base.assigns)
|
||||||
#pre-build the tab index up to 9
|
#pre-build the tab index up to 9
|
||||||
10.times do |num|
|
10.times do |num|
|
||||||
@tab_index << @tab_index.last + " "
|
@tab_index << @tab_index.last + " "
|
||||||
|
@ -15,9 +16,10 @@ module HAML
|
||||||
@result = ""
|
@result = ""
|
||||||
@to_close_queue = []
|
@to_close_queue = []
|
||||||
|
|
||||||
locals.each do |variable_name, value|
|
|
||||||
@base.instance_eval "@" + variable_name.to_s + " = value"
|
@happy_land.set_locals(locals)
|
||||||
end
|
|
||||||
|
#breakpoint
|
||||||
|
|
||||||
#main loop handling line reading
|
#main loop handling line reading
|
||||||
#and interpretation
|
#and interpretation
|
||||||
|
@ -68,6 +70,21 @@ module HAML
|
||||||
add "<#{name.to_s}#{build_attributes(attributes)}>#{value}</#{name.to_s}>"
|
add "<#{name.to_s}#{build_attributes(attributes)}>#{value}</#{name.to_s}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def print_tag(name, value, attributes = {})
|
||||||
|
unless value.empty?
|
||||||
|
if one_liner?(value)
|
||||||
|
one_line_tag(name, value, attributes)
|
||||||
|
else
|
||||||
|
open_tag(name, attributes)
|
||||||
|
add(value)
|
||||||
|
close_tag
|
||||||
|
end
|
||||||
|
else
|
||||||
|
open_tag(name, attributes)
|
||||||
|
add(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
#used to create single line tags... aka <hello />
|
#used to create single line tags... aka <hello />
|
||||||
def atomic_tag(name, attributes = {})
|
def atomic_tag(name, attributes = {})
|
||||||
add "<#{name.to_s}#{build_attributes(attributes)} />"
|
add "<#{name.to_s}#{build_attributes(attributes)} />"
|
||||||
|
@ -91,36 +108,26 @@ module HAML
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_tag(line)
|
def render_tag(line)
|
||||||
line.scan(/[%]([-_a-z1-9]+)([-_a-z\.\#]*)([=\/]?)([^\n]*)/).each do |tag_name, attributes, action, value|
|
broken_up = line.scan(/[%]([-_a-z1-9]+)([-_a-z\.\#]*)(\{.*\})?([=\/]?)?([^\n]*)?/)
|
||||||
val = template_eval(value)
|
broken_up.each do |tag_name, attributes, attributes_hash, action, value|
|
||||||
attribute_hash = parse_attributes(attributes)
|
attributes = parse_attributes(attributes.to_s)
|
||||||
if val.class == Hash
|
|
||||||
attribute_hash.merge!(val) && val = nil
|
unless(attributes_hash.nil? || attributes_hash.empty?)
|
||||||
elsif !val.nil?
|
attributes_hash = template_eval(attributes_hash)
|
||||||
val = val.to_s
|
attributes = attributes.merge(attributes_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
#check to see if we're a one liner
|
#check to see if we're a one liner
|
||||||
if(action == "\/")
|
if(action == "\/")
|
||||||
atomic_tag(tag_name, attribute_hash)
|
atomic_tag(tag_name, attributes)
|
||||||
|
elsif(action == "=")
|
||||||
|
print_tag(tag_name, template_eval(value).to_s, attributes)
|
||||||
else
|
else
|
||||||
if(!val.nil? && ((val.length < 50) || val.scan(/\n/).empty?))
|
print_tag(tag_name, value, attributes)
|
||||||
one_line_tag(tag_name, val, attribute_hash)
|
|
||||||
else
|
|
||||||
open_tag(tag_name, attribute_hash)
|
|
||||||
|
|
||||||
if(action == '=')
|
|
||||||
add(val)
|
|
||||||
close_tag
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def template_eval(code)
|
|
||||||
@base.instance_eval(code)
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_attributes(list)
|
def parse_attributes(list)
|
||||||
attributes = {}
|
attributes = {}
|
||||||
|
@ -138,6 +145,52 @@ module HAML
|
||||||
def count_levels(line)
|
def count_levels(line)
|
||||||
[line.index(/[^ ]/)/2, line.strip]
|
[line.index(/[^ ]/)/2, line.strip]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def one_liner?(value)
|
||||||
|
((value.length < 50) && value.scan(/\n/).empty?)
|
||||||
|
end
|
||||||
|
|
||||||
|
def template_eval(code)
|
||||||
|
#@base.instance_eval(code)
|
||||||
|
#render :inline => "<%=#{code}%>"
|
||||||
|
@happy_land.instance_eval(code)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class HappyLand #:nodoc
|
||||||
|
def initialize(base, hash_of_assigns, hash_of_locals = {})
|
||||||
|
base.instance_variables.each do |key|
|
||||||
|
value = base.instance_eval(key)
|
||||||
|
eval("#{key} = value")
|
||||||
|
end
|
||||||
|
hash_of_assigns.each do |key, value|
|
||||||
|
eval("@#{key} = value")
|
||||||
|
end
|
||||||
|
@__locals = hash_of_locals
|
||||||
|
@__base = base
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_locals(hash_of_locals)
|
||||||
|
@__locals.merge!(hash_of_locals)
|
||||||
|
end
|
||||||
|
|
||||||
|
def instance_eval(code)
|
||||||
|
eval(code)
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing(action, *args, &block)
|
||||||
|
if action.to_s.first == "@"
|
||||||
|
result = @__base.instance_eval(action)
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
result = @__base.send(action, *args, &block)
|
||||||
|
rescue
|
||||||
|
result = @__locals[action.to_s] || @__locals[action.to_sym]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
Loading…
Add table
Reference in a new issue