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

View file

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

View file

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

View file

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