Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,6 @@ module Filters | |
|
||
module HTML | ||
autoload :Fast, 'temple/html/fast' | ||
autoload :Pretty, 'temple/html/pretty' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Temple | ||
module HTML | ||
class Pretty < Fast | ||
set_default_options :indent => ' ', | ||
:pretty => true | ||
|
||
INDENT_TAGS = %w(base div form head html img input li link meta ol | ||
script style table tbody td th thead title tr ul).freeze | ||
|
||
def initialize(opts = {}) | ||
super | ||
@last = nil | ||
@stack = [] | ||
end | ||
|
||
def on_static(content) | ||
if options[:pretty] | ||
@last = nil | ||
[:static, content.gsub("\n", indent)] | ||
else | ||
[:static, content] | ||
end | ||
end | ||
|
||
def on_dynamic(content) | ||
if options[:pretty] | ||
@last = nil | ||
[:dynamic, %{(#{content}).to_s.gsub("\n", #{indent.inspect})}] | ||
else | ||
[:dynamic, content] | ||
end | ||
end | ||
|
||
def on_html_comment(content) | ||
return super if !options[:pretty] | ||
[:multi, [:static, indent], super] | ||
end | ||
|
||
def on_html_tag(name, attrs, closed, content) | ||
return super if !options[:pretty] | ||
|
||
closed ||= options[:autoclose].include?(name) | ||
raise "Closed tag #{name} has content" if closed && !empty_exp?(content) | ||
|
||
result = [:multi, [:static, "#{tag_indent(name)}<#{name}"], compile!(attrs)] | ||
result << [:static, ' /'] if closed && xhtml? | ||
result << [:static, '>'] | ||
|
||
@stack << name | ||
@last = name | ||
result << compile!(content) | ||
@stack.pop | ||
|
||
result << [:static, "#{tag_indent(name)}</#{name}>"] if !closed | ||
@last = name | ||
result | ||
end | ||
|
||
def indent | ||
@stack.include?('pre') ? '' : ("\n" + ((options[:indent] || '') * @stack.size)) | ||
end | ||
|
||
def tag_indent(name) | ||
@last && (INDENT_TAGS.include?(@last) || INDENT_TAGS.include?(name)) ? indent : '' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters