From 4928deec987a2fabd77aa397d6d9a28cbf306e7c Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sun, 29 Mar 2015 01:30:59 +0900 Subject: [PATCH] Destroy a filter compiler because its design is not well-considered. --- lib/hamlit/compilers/filter.rb | 48 +++----------------------------- lib/hamlit/filters/css.rb | 9 ------ lib/hamlit/filters/javascript.rb | 9 ------ lib/hamlit/filters/ruby.rb | 9 ------ 4 files changed, 4 insertions(+), 71 deletions(-) delete mode 100644 lib/hamlit/filters/css.rb delete mode 100644 lib/hamlit/filters/javascript.rb delete mode 100644 lib/hamlit/filters/ruby.rb diff --git a/lib/hamlit/compilers/filter.rb b/lib/hamlit/compilers/filter.rb index 1017fbb2..90a683e9 100644 --- a/lib/hamlit/compilers/filter.rb +++ b/lib/hamlit/compilers/filter.rb @@ -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 diff --git a/lib/hamlit/filters/css.rb b/lib/hamlit/filters/css.rb deleted file mode 100644 index 3fab2ff8..00000000 --- a/lib/hamlit/filters/css.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Hamlit - module Filters - class Css - def compile(exp) - [:html, :tag, 'style', [:html, :attrs], exp] - end - end - end -end diff --git a/lib/hamlit/filters/javascript.rb b/lib/hamlit/filters/javascript.rb deleted file mode 100644 index fc41c261..00000000 --- a/lib/hamlit/filters/javascript.rb +++ /dev/null @@ -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 diff --git a/lib/hamlit/filters/ruby.rb b/lib/hamlit/filters/ruby.rb deleted file mode 100644 index 2c972106..00000000 --- a/lib/hamlit/filters/ruby.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Hamlit - module Filters - class Ruby - def compile(exp) - [:multi, [:code, exp.join("\n")], [:newline]] - end - end - end -end