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

Destroy a filter compiler

because its design is not well-considered.
This commit is contained in:
Takashi Kokubun 2015-03-29 01:30:59 +09:00
parent 7a2e33f883
commit 4928deec98
4 changed files with 4 additions and 71 deletions

View file

@ -1,65 +1,25 @@
require 'hamlit/concerns/included'
require 'hamlit/concerns/registerable'
require 'hamlit/filters/css'
require 'hamlit/filters/javascript'
require 'hamlit/filters/ruby'
module Hamlit
module Compilers
module Filter
extend Concerns::Included
BASE_DEPTH = 2
IGNORED_FILTERS = %w[ruby].freeze
included do
extend Concerns::Registerable
register :javascript, Filters::Javascript
register :css, Filters::Css
register :ruby, Filters::Ruby
end
def on_haml_filter(name, exp)
exp = format_expressions(name, exp)
ast = Compiler.find(name).compile(exp)
ast = compile_filter(name, exp)
compile(ast)
end
private
def format_expressions(name, lines)
return lines if IGNORED_FILTERS.include?(name)
lines = [''] if lines.empty?
lines = unindent_lines(lines)
[:multi, [:static, "\n"], *wrap_newlines(lines)]
end
def unindent_lines(lines)
base = lines.first.index(/[^\s]/) || 0
lines.map do |line|
change_indent(line, BASE_DEPTH - base)
end
end
def change_indent(line, diff)
if diff >= 0
((' ' * diff) + line).gsub(/ *\Z/, '')
else
line.gsub(/^[[:blank:]]{#{-1 * diff}}/, '')
end
end
def wrap_newlines(lines)
ast = []
lines.each do |line|
ast << [:haml, :text, line]
ast << [:static, "\n"]
end
ast
def compile_filter(name, exp)
compiler = Compiler.find(name)
compiler.compile(exp)
end
end
end

View file

@ -1,9 +0,0 @@
module Hamlit
module Filters
class Css
def compile(exp)
[:html, :tag, 'style', [:html, :attrs], exp]
end
end
end
end

View file

@ -1,9 +0,0 @@
module Hamlit
module Filters
class Javascript
def compile(exp)
[:html, :tag, 'script', [:html, :attrs], [:html, :js, exp]]
end
end
end
end

View file

@ -1,9 +0,0 @@
module Hamlit
module Filters
class Ruby
def compile(exp)
[:multi, [:code, exp.join("\n")], [:newline]]
end
end
end
end