From ce306e3de20c7a931aec111315cad690b0bec1c9 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 27 Jun 2015 14:48:18 +0900 Subject: [PATCH] Refactor css and js filters --- lib/hamlit/filters/base.rb | 25 +++++++++++++++++++++++++ lib/hamlit/filters/css.rb | 27 ++++----------------------- lib/hamlit/filters/javascript.rb | 27 ++++----------------------- 3 files changed, 33 insertions(+), 46 deletions(-) diff --git a/lib/hamlit/filters/base.rb b/lib/hamlit/filters/base.rb index 34e5d044..d0861657 100644 --- a/lib/hamlit/filters/base.rb +++ b/lib/hamlit/filters/base.rb @@ -39,6 +39,31 @@ module Hamlit end lines end + + def compile_html(tag, lines) + ast = [:haml, :text, compile_lines(lines, indent_width: 2), false] + ast = [:multi, [:static, "\n"], ast] + ast = [:html, :tag, tag, [:html, :attrs], ast] + ast + end + + def compile_xhtml(tag, type, lines) + attr = [:html, :attr, 'type', [:static, type]] + attrs = [:html, :attrs, attr] + + content = [:haml, :text, compile_lines(lines, indent_width: 4)] + multi = [:multi, [:static, "\n"], *cdata_for(type, content)] + [:html, :tag, tag, attrs, multi] + end + + def cdata_for(type, ast) + case type + when 'text/javascript' + [[:static, " //\n"]] + when 'text/css' + [[:static, " /**/\n"]] + end + end end end end diff --git a/lib/hamlit/filters/css.rb b/lib/hamlit/filters/css.rb index 63b7e542..542073a6 100644 --- a/lib/hamlit/filters/css.rb +++ b/lib/hamlit/filters/css.rb @@ -4,30 +4,11 @@ module Hamlit module Filters class Css < Base def compile(lines) - return compile_xhtml(lines) if options[:format] == :xhtml + if options[:format] == :xhtml + return compile_xhtml('style', 'text/css', lines) + end - compile_html(lines) - end - - private - - def compile_html(lines) - ast = [:haml, :text, compile_lines(lines, indent_width: 2), false] - ast = [:multi, [:static, "\n"], ast] - ast = [:html, :tag, 'style', [:html, :attrs], ast] - ast - end - - def compile_xhtml(lines) - attr = [:html, :attr, 'type', [:static, 'text/css']] - attrs = [:html, :attrs, attr] - - multi = [:multi, [:static, "\n"]] - multi << [:static, " /**/\n"] - - [:html, :tag, 'style', attrs, multi] + compile_html('style', lines) end end end diff --git a/lib/hamlit/filters/javascript.rb b/lib/hamlit/filters/javascript.rb index b57d10a5..490835d8 100644 --- a/lib/hamlit/filters/javascript.rb +++ b/lib/hamlit/filters/javascript.rb @@ -4,30 +4,11 @@ module Hamlit module Filters class Javascript < Base def compile(lines) - return compile_xhtml(lines) if options[:format] == :xhtml + if options[:format] == :xhtml + return compile_xhtml('script', 'text/javascript', lines) + end - compile_html(lines) - end - - private - - def compile_html(lines) - ast = [:haml, :text, compile_lines(lines, indent_width: 2), false] - ast = [:multi, [:static, "\n"], ast] - ast = [:html, :tag, 'script', [:html, :attrs], ast] - ast - end - - def compile_xhtml(lines) - attr = [:html, :attr, 'type', [:static, 'text/javascript']] - attrs = [:html, :attrs, attr] - - multi = [:multi, [:static, "\n"]] - multi << [:static, " //\n"] - - [:html, :tag, 'script', attrs, multi] + compile_html('script', lines) end end end