mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
They should be in the same layer
This commit is contained in:
parent
50ea96a6f7
commit
9a79e887ca
3 changed files with 40 additions and 46 deletions
|
@ -4,7 +4,6 @@ require 'hamlit/attribute_sorter'
|
|||
require 'hamlit/compiler'
|
||||
require 'hamlit/dynamic_formatter'
|
||||
require 'hamlit/filter_compiler'
|
||||
require 'hamlit/filter_formatter'
|
||||
require 'hamlit/html/pretty'
|
||||
require 'hamlit/html/ugly'
|
||||
require 'hamlit/multiline'
|
||||
|
@ -22,7 +21,6 @@ module Hamlit
|
|||
|
||||
use Multiline
|
||||
use Parser
|
||||
use FilterFormatter
|
||||
use FilterCompiler
|
||||
use Compiler
|
||||
use AttributeCompiler
|
||||
|
|
|
@ -8,13 +8,53 @@ module Hamlit
|
|||
class FilterCompiler < Hamlit::Filter
|
||||
extend Concerns::Registerable
|
||||
|
||||
BASE_DEPTH = 2
|
||||
IGNORED_FILTERS = %w[ruby].freeze
|
||||
|
||||
register :javascript, Filters::Javascript
|
||||
register :css, Filters::Css
|
||||
register :ruby, Filters::Ruby
|
||||
|
||||
def on_haml_filter(name, exp)
|
||||
exp = format_expressions(name, exp)
|
||||
|
||||
compiler = FilterCompiler.find(name)
|
||||
compiler.compile(exp)
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
require 'hamlit/filter'
|
||||
|
||||
module Hamlit
|
||||
class FilterFormatter < Hamlit::Filter
|
||||
BASE_DEPTH = 2
|
||||
IGNORED_FILTERS = %w[ruby].freeze
|
||||
|
||||
def on_haml_filter(name, lines)
|
||||
return [:haml, :filter, name, lines] if IGNORED_FILTERS.include?(name)
|
||||
|
||||
lines = [''] if lines.empty?
|
||||
lines = unindent_lines(lines)
|
||||
|
||||
multi = [:multi, [:static, "\n"], *wrap_newlines(lines)]
|
||||
[:haml, :filter, name, multi]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue