Last active
January 12, 2017 21:02
-
-
Save fnordfish/25098891d6337a083a4c0e23a7adfd5e to your computer and use it in GitHub Desktop.
Padrino plugin for using Using hamlit as redering engine instead of haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Use hamlit instead of haml for rendering | |
# This is basically a copy of the existing integration for slim | |
RENDERING=<<'RUBY' | |
module Padrino | |
module Helpers | |
module OutputHelpers | |
## | |
# Handler for Hamlit templates. | |
# | |
class HamlitHandler < AbstractHandler | |
## | |
# Returns true if the block is for Hamlit. | |
# | |
def engine_matches?(block) | |
block.binding.eval('defined? __in_hamlit_template') | |
end | |
end | |
OutputHelpers.register(:haml, HamlitHandler) | |
end | |
end | |
end | |
module Padrino | |
module Rendering | |
class HamlitOutputBuffer < Temple::Generators::StringBuffer | |
define_options :buffer_class => 'SafeBuffer' | |
def call(exp) | |
[preamble, compile(exp), postamble].flatten.compact.join('; '.freeze) | |
end | |
def create_buffer | |
"#{buffer} = #{options[:buffer_class]}.new" | |
end | |
def concat(str) | |
"#{buffer}.safe_concat((#{str}))" | |
end | |
end | |
class HamlitTemplate < Hamlit::Template | |
include SafeTemplate | |
def precompiled_preamble(locals) | |
"__in_hamlit_template = true\n" << super | |
end | |
end | |
end | |
end | |
Tilt.prefer(Padrino::Rendering::HamlitTemplate, :haml) | |
Padrino::Rendering.engine_configurations[:haml] = { | |
:generator => Padrino::Rendering::HamlitOutputBuffer, | |
:buffer => "@_out_buf", | |
:use_html_safe => true, | |
} | |
RUBY | |
create_file destination_root('lib/padrino-hamlit.rb'), RENDERING | |
require_dependencies 'hamlit' | |
say "Unless you need haml, you can remove it from your Gemfile." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment