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

Create Hamlit template

This commit is contained in:
Takashi Kokubun 2015-10-24 16:48:26 +09:00
parent c462bbf6df
commit 4f5da05f22
5 changed files with 66 additions and 9 deletions

View file

@ -2,3 +2,9 @@ require 'hamlit/attribute_builder'
require 'hamlit/engine'
require 'hamlit/error'
require 'hamlit/version'
begin
require 'rails'
require 'hamlit/railtie'
rescue LoadError
end

23
lib/hamlit/railtie.rb Normal file
View file

@ -0,0 +1,23 @@
if defined?(ActiveSupport)
# check for a compatible Rails version when Haml is loaded
if (activesupport_spec = Gem.loaded_specs['activesupport'])
if activesupport_spec.version.to_s < '3.2'
raise Exception.new("\n\n** Haml now requires Rails 3.2 and later. Use Haml version 4.0.4\n\n")
end
end
require 'haml/template/options'
ActiveSupport.on_load(:before_initialize) do
ActiveSupport.on_load(:action_view) do
require "hamlit/template"
end
end
end
module Hamlit
class Railtie < ::Rails::Railtie
initializer :hamlit do |app|
require 'hamlit/template'
end
end
end

28
lib/hamlit/template.rb Normal file
View file

@ -0,0 +1,28 @@
module Hamlit
# This module makes Haml work with Rails using the template handler API.
class Plugin
def handles_encoding?; true; end
def compile(template)
options = {}
options[:filename] = template.identifier
options[:ugly] = defined?(Rails.env) ? !Rails.env.development? : true
options[:escape_html] = true
options[:generator] = Temple::Generators::RailsOutputBuffer
HamlEngine.new(template.source, options).precompiled
end
def self.call(template)
new.compile(template)
end
def cache_fragment(block, name = {}, options = nil)
@view.fragment_for(block, name, options) do
eval("_hamlout.buffer", block.binding)
end
end
end
end
require 'haml/template' # load first to force overwrite
ActionView::Template.register_template_handler(:haml, Hamlit::Plugin)

View file

@ -136,20 +136,20 @@ class TemplateTest < Haml::TestCase
def test_templates_should_render_correctly_with_render_proc; skip
assert_renders_correctly("standard") do |name|
engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), :format => :xhtml)
engine = Hamlit::HamlEngine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), :format => :xhtml)
engine.render_proc(@base).call
end
end
def test_templates_should_render_correctly_with_def_method; skip
assert_renders_correctly("standard") do |name|
engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), :format => :xhtml)
engine = Haml::HamlEngine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), :format => :xhtml)
engine.def_method(@base, "render_standard")
@base.render_standard
end
end
def test_instance_variables_should_work_inside_templates; skip
def test_instance_variables_should_work_inside_templates
@base.instance_variable_set(:@content_for_layout, 'something')
assert_equal("<p>something</p>", render("%p= @content_for_layout").chomp)
@ -163,7 +163,7 @@ class TemplateTest < Haml::TestCase
assert_equal("Catlin", render("= @author").chomp)
end
def test_instance_variables_should_work_inside_attributes; skip
def test_instance_variables_should_work_inside_attributes
@base.instance_eval("@author = 'hcatlin'")
assert_equal("<p class='hcatlin'>foo</p>", render("%p{:class => @author} foo").chomp)
end
@ -258,7 +258,7 @@ HAML
assert_equal("Foo & Bar\n", render('= Haml::Util.html_safe("Foo & Bar")', :action_view))
end
def test_xss_protection_with_bang; skip
def test_xss_protection_with_bang
assert_equal("Foo & Bar\n", render('!= "Foo & Bar"', :action_view))
end
@ -274,7 +274,7 @@ HAML
assert_equal("<div data-html='<foo>bar</foo>'></div>\n", render('%div{ "data-html" => "<foo>bar</foo>".html_safe }', :action_view))
end
def test_xss_protection_with_bang_in_interpolation; skip
def test_xss_protection_with_bang_in_interpolation
assert_equal("Foo & Bar\n", render('! Foo #{"&"} Bar', :action_view))
end

View file

@ -1,6 +1,3 @@
require 'haml'
require 'hamlit'
require 'bundler/setup'
require 'minitest/autorun'
require 'action_pack'
@ -8,6 +5,9 @@ require 'action_controller'
require 'action_view'
require 'rails'
require 'hamlit'
require 'hamlit/template'
require 'minitest/reporters'
Minitest::Reporters.use!