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

Allow html compiler to be switched

This commit is contained in:
Takashi Kokubun 2015-03-27 14:53:54 +09:00
parent 21de0d4874
commit 5be7a132c7
4 changed files with 28 additions and 9 deletions

View file

@ -18,6 +18,7 @@ module Hamlit
generator: Temple::Generators::ArrayBuffer,
format: :html,
attr_quote: "'",
ugly: false,
)
use MultilinePreprocessor
@ -31,17 +32,28 @@ module Hamlit
use ScriptCompiler
use TextCompiler
use DynamicFormatter
use HTML
use :Html, -> { create(html_compiler) }
filter :Escapable
filter :ControlFlow
filter :MultiFlattener
filter :StaticMerger
use :Generator, -> { create(options[:generator]) }
use :Generator do
private
def create(klass)
valid_options = options.to_hash.select do |key, value|
options[:generator].options.valid_key?(key)
klass.options.valid_key?(key)
end
klass.new(valid_options)
end
def html_compiler
if options[:ugly]
Temple::HTML::Fast
else
Html
end
options[:generator].new(valid_options)
end
end
end

View file

@ -1,7 +1,7 @@
require 'temple/html/fast'
module Hamlit
class HTML < Temple::HTML::Fast
class Html < Temple::HTML::Fast
define_options :format
def initialize(opts = {})
@ -10,7 +10,7 @@ module Hamlit
private
# Temple's warning is noisy.
# Temple's warning is noisy in haml-spec.
def rewrite_format(options)
options = options.dup
options[:format] = normalize_format(options[:format]) if options[:format]
@ -18,8 +18,6 @@ module Hamlit
end
def normalize_format(format)
return :html unless format
case format
when :html4, :html5
:html

View file

@ -4,7 +4,11 @@ describe Hamlit::Engine do
let(:buffer) { '_a' }
let(:options) do
{ buffer: buffer, generator: Temple::Generators::ArrayBuffer }
{
buffer: buffer,
generator: Temple::Generators::ArrayBuffer,
ugly: true,
}
end
it 'accepts generator valid options' do

View file

@ -2,11 +2,16 @@ require 'hamlit'
require 'unindent'
module HamlitSpecHelper
DEFAULT_OPTIONS = {
ugly: true,
}.freeze
def parse_string(str)
Hamlit::Parser.new.call(str)
end
def render_string(str, options = {})
options = DEFAULT_OPTIONS.merge(options)
eval Hamlit::Engine.new(options).call(str)
end