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

Initial upload.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@1 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
hcatlin 2006-06-30 15:14:44 +00:00
commit 35f7e17de5
8 changed files with 175 additions and 0 deletions

4
README Normal file
View file

@ -0,0 +1,4 @@
Haml
====
Description goes here

22
Rakefile Normal file
View file

@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
desc 'Test the haml plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
desc 'Generate documentation for the haml plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Haml'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

3
init.rb Normal file
View file

@ -0,0 +1,3 @@
require 'haml'
ActionView::Base.register_template_handler("haml", HAML::TemplateEngine)

1
install.rb Normal file
View file

@ -0,0 +1 @@
# No Install needed!

130
lib/haml.rb Normal file
View file

@ -0,0 +1,130 @@
module HAML
class TemplateEngine
def initialize(base)
@base = base
@tab_index = ["", " "]
#pre-build the tab index up to 9
10.times do |num|
@tab_index << @tab_index.last + " "
end
end
def render(template = "", locals = {})
@pony_land = HappyMagicPonyLand.new(@base, locals)
@result = ""
@to_close_queue = []
#main loop handling line reading
#and interpretation
template.each_line do |line|
count, line = count_levels(line)
if count <= @to_close_queue.size
close_tag
end
case line.first
when '.', '#'
render_div(line)
when '<'
render_tag_or_line(line)
when '/'
render_comment(line)
when '='
add template_eval(line[1, line.length])
else
add line
end
end
puts "closing... " + @to_close_queue.inspect
@to_close_queue.each { close_tag }
@result
end
def add(line)
@result << tabify(@to_close_queue.size) + line + "\n"
end
def tabify(times)
@tab_index[times]
end
def open_tag(name, attributes = {})
attribute_array = []
attributes.each do |attr_name, val|
attribute_array << attr_name.to_s + "='" + val + "'"
end
add "<#{name.to_s}#{attribute_array.empty? ? "" : " "}#{attribute_array.join(" ")}>"
@to_close_queue.push(name)
end
def close_tag
add "</#{@to_close_queue.pop}>"
end
def render_div(line)
open_tag("div", parse_attributes(line))
end
def render_comment(line)
add "<!-- #{line} -->"
end
def render_tag_or_line(line)
line.scan(/[<]([a-zA-Z]+)([a-zA-Z.\#]*)([=]?)([^\n]*)/).each do |tag_name, attributes, action, value|
puts tag_name
open_tag(tag_name, parse_attributes(attributes))
unless value.empty?
if(action == '=')
add(value)
else
add(template_eval(value))
end
close_tag
end
end
end
def template_eval(code)
@pony_land.instance_eval(code) || ""
end
def parse_attributes(list)
attributes = {}
list.scan(/([#.])([-a-zA-Z_()]+)/).each do |type, property|
case type
when "."
attributes[:class] = property
when "#"
attributes[:id] = property
end
end
attributes
end
def count_levels(line)
[line.index(/[^ ]/), line.strip]
end
end
class HappyMagicPonyLand
def initialize(base, attributes = {})
@_action_view = base
@table = attributes
end
def method_missing(name, *args, &block)
name_as_string = name.to_s
if name_as_string.last == "="
@table[name_as_string.scan(/[^=]+/).first.to_sym] = args.first
elsif @table.has_key? name.to_sym
@table[name.to_sym]
elsif @_action_view.respond_to? name
@_action_view.send(name, *args, &block)
else
nil
end
end
end
end

3
lib/pony.rb Normal file
View file

@ -0,0 +1,3 @@
module HAML
end

4
tasks/haml_tasks.rake Normal file
View file

@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :haml do
# # Task goes here
# end

8
test/haml_test.rb Normal file
View file

@ -0,0 +1,8 @@
require 'test/unit'
class HamlTest < Test::Unit::TestCase
# Replace this with your real tests.
def test_this_plugin
flunk
end
end