commit 35f7e17de5485e1e21e731297761b670a8fddce2 Author: hcatlin Date: Fri Jun 30 15:14:44 2006 +0000 Initial upload. git-svn-id: svn://hamptoncatlin.com/haml/trunk@1 7063305b-7217-0410-af8c-cdc13e5119b9 diff --git a/README b/README new file mode 100644 index 00000000..d61c006a --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +Haml +==== + +Description goes here \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..7ac8911f --- /dev/null +++ b/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 diff --git a/init.rb b/init.rb new file mode 100644 index 00000000..611304d5 --- /dev/null +++ b/init.rb @@ -0,0 +1,3 @@ +require 'haml' + +ActionView::Base.register_template_handler("haml", HAML::TemplateEngine) \ No newline at end of file diff --git a/install.rb b/install.rb new file mode 100644 index 00000000..83e786be --- /dev/null +++ b/install.rb @@ -0,0 +1 @@ +# No Install needed! diff --git a/lib/haml.rb b/lib/haml.rb new file mode 100644 index 00000000..d9131c5b --- /dev/null +++ b/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 "" + end + + def render_div(line) + open_tag("div", parse_attributes(line)) + end + + def render_comment(line) + add "" + 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 \ No newline at end of file diff --git a/lib/pony.rb b/lib/pony.rb new file mode 100644 index 00000000..37ad8a47 --- /dev/null +++ b/lib/pony.rb @@ -0,0 +1,3 @@ +module HAML + +end diff --git a/tasks/haml_tasks.rake b/tasks/haml_tasks.rake new file mode 100644 index 00000000..250c0e6c --- /dev/null +++ b/tasks/haml_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :haml do +# # Task goes here +# end \ No newline at end of file diff --git a/test/haml_test.rb b/test/haml_test.rb new file mode 100644 index 00000000..1e3cd672 --- /dev/null +++ b/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