Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial upload.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@1 7063305b-7217-0410-af8c-cdc13e5119b9
  • Loading branch information...
commit 35f7e17de5485e1e21e731297761b670a8fddce2 0 parents
hcatlin authored
4 README
@@ -0,0 +1,4 @@
+Haml
+====
+
+Description goes here
22 Rakefile
@@ -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
@@ -0,0 +1,3 @@
+require 'haml'
+
+ActionView::Base.register_template_handler("haml", HAML::TemplateEngine)
1  install.rb
@@ -0,0 +1 @@
+# No Install needed!
130 lib/haml.rb
@@ -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
@@ -0,0 +1,3 @@
+module HAML
+
+end
4 tasks/haml_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :haml do
+# # Task goes here
+# end
8 test/haml_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.