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

Refactor css and js filters

This commit is contained in:
Takashi Kokubun 2015-06-27 14:48:18 +09:00
parent 7a52e9624d
commit ce306e3de2
3 changed files with 33 additions and 46 deletions

View file

@ -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, " //<![CDATA[\n"], ast, [:static, " //]]>\n"]]
when 'text/css'
[[:static, " /*<![CDATA[*/\n"], ast, [:static, " /*]]>*/\n"]]
end
end
end
end
end

View file

@ -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, " /*<![CDATA[*/\n"]
multi << [:haml, :text, compile_lines(lines, indent_width: 4)]
multi << [:static, " /*]]>*/\n"]
[:html, :tag, 'style', attrs, multi]
compile_html('style', lines)
end
end
end

View file

@ -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, " //<![CDATA[\n"]
multi << [:haml, :text, compile_lines(lines, indent_width: 4)]
multi << [:static, " //]]>\n"]
[:html, :tag, 'script', attrs, multi]
compile_html('script', lines)
end
end
end