mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Implement css filter
This commit is contained in:
parent
29c29d578f
commit
172bb2bfc6
3 changed files with 66 additions and 33 deletions
|
@ -1,5 +1,6 @@
|
|||
require 'hamlit/concerns/included'
|
||||
require 'hamlit/concerns/registerable'
|
||||
require 'hamlit/filters/css'
|
||||
require 'hamlit/filters/escaped'
|
||||
require 'hamlit/filters/plain'
|
||||
require 'hamlit/filters/preserve'
|
||||
|
@ -12,6 +13,7 @@ module Hamlit
|
|||
included do
|
||||
extend Concerns::Registerable
|
||||
|
||||
register :css, Filters::Css
|
||||
register :escaped, Filters::Escaped
|
||||
register :plain, Filters::Plain
|
||||
register :preserve, Filters::Preserve
|
||||
|
|
31
lib/hamlit/filters/css.rb
Normal file
31
lib/hamlit/filters/css.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
module Hamlit
|
||||
module Filters
|
||||
class Css
|
||||
def compile(lines)
|
||||
ast = [:haml, :text, compile_lines(lines, indent_width: 2)]
|
||||
ast = [:multi, [:static, "\n"], ast]
|
||||
ast = [:html, :tag, 'style', [:html, :attrs], ast]
|
||||
ast
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def compile_lines(lines, indent_width: 0)
|
||||
text = strip_last(lines).map { |line|
|
||||
' ' * indent_width + line
|
||||
}.join("\n")
|
||||
text += "\n" if text.length > 0
|
||||
text
|
||||
end
|
||||
|
||||
# NOTE: empty line is reserved for preserve filter.
|
||||
def strip_last(lines)
|
||||
lines = lines.dup
|
||||
while lines.last && lines.last.length == 0
|
||||
lines.delete_at(-1)
|
||||
end
|
||||
lines
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,33 +1,33 @@
|
|||
# describe Hamlit::Filters::Css do
|
||||
# describe '#compile' do
|
||||
# it 'renders css' do
|
||||
# assert_render(<<-HAML, <<-HTML)
|
||||
# :css
|
||||
# .foo {
|
||||
# width: 100px;
|
||||
# }
|
||||
# HAML
|
||||
# <style>
|
||||
# .foo {
|
||||
# width: 100px;
|
||||
# }
|
||||
# </style>
|
||||
# HTML
|
||||
# end
|
||||
#
|
||||
# it 'parses string interpolation' do
|
||||
# assert_render(<<-'HAML', <<-HTML)
|
||||
# :css
|
||||
# .foo {
|
||||
# width: #{100 * 3}px;
|
||||
# }
|
||||
# HAML
|
||||
# <style>
|
||||
# .foo {
|
||||
# width: 300px;
|
||||
# }
|
||||
# </style>
|
||||
# HTML
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
describe Hamlit::Filters::Css do
|
||||
describe '#compile' do
|
||||
it 'renders css' do
|
||||
assert_render(<<-HAML, <<-HTML)
|
||||
:css
|
||||
.foo {
|
||||
width: 100px;
|
||||
}
|
||||
HAML
|
||||
<style>
|
||||
.foo {
|
||||
width: 100px;
|
||||
}
|
||||
</style>
|
||||
HTML
|
||||
end
|
||||
|
||||
it 'parses string interpolation' do
|
||||
assert_render(<<-'HAML', <<-HTML)
|
||||
:css
|
||||
.foo {
|
||||
width: #{100 * 3}px;
|
||||
}
|
||||
HAML
|
||||
<style>
|
||||
.foo {
|
||||
width: 300px;
|
||||
}
|
||||
</style>
|
||||
HTML
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue