temple 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/temple/engine.rb +4 -3
- data/lib/temple/html/pretty.rb +18 -7
- data/lib/temple/utils.rb +4 -0
- data/lib/temple/version.rb +1 -1
- data/temple.gemspec +1 -1
- data/test/html/test_pretty.rb +21 -15
- metadata +3 -3
data/lib/temple/engine.rb
CHANGED
@@ -29,8 +29,9 @@ module Temple
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def self.use(filter, *options, &block)
|
32
|
+
default_options = Hash === options.last ? options.pop : {}
|
32
33
|
chain << proc do |opts|
|
33
|
-
filter.new(Hash[*opts.select {|k,v| options.include?(k) }.flatten], &block)
|
34
|
+
filter.new(default_options.merge(Hash[*opts.select {|k,v| options.include?(k) }.flatten]), &block)
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
@@ -44,8 +45,8 @@ module Temple
|
|
44
45
|
use(Temple::Generators.const_get(compiler), *options, &block)
|
45
46
|
end
|
46
47
|
|
47
|
-
def compile(
|
48
|
-
chain.inject(
|
48
|
+
def compile(input)
|
49
|
+
chain.inject(input) {|m, e| e.compile(m) }
|
49
50
|
end
|
50
51
|
|
51
52
|
protected
|
data/lib/temple/html/pretty.rb
CHANGED
@@ -3,7 +3,7 @@ module Temple
|
|
3
3
|
class Pretty < Fast
|
4
4
|
set_default_options :indent => ' ',
|
5
5
|
:pretty => true,
|
6
|
-
:indent_tags => %w(base dd div dl doctype dt fieldset form head h1 h2 h3
|
6
|
+
:indent_tags => %w(base body dd div dl doctype dt fieldset form head h1 h2 h3
|
7
7
|
h4 h5 h6 hr html img input li link meta ol p script
|
8
8
|
style table tbody td tfoot th thead title tr ul).freeze,
|
9
9
|
:pre_tags => %w(pre textarea).freeze
|
@@ -12,16 +12,21 @@ module Temple
|
|
12
12
|
super
|
13
13
|
@last = nil
|
14
14
|
@indent = 0
|
15
|
+
@pretty = options[:pretty]
|
16
|
+
end
|
17
|
+
|
18
|
+
def compile(exp)
|
19
|
+
[:multi, preamble, compile!(exp)]
|
15
20
|
end
|
16
21
|
|
17
22
|
def on_static(content)
|
18
23
|
@last = nil
|
19
|
-
[:static,
|
24
|
+
[:static, @pretty ? content.gsub("\n", indent) : content]
|
20
25
|
end
|
21
26
|
|
22
27
|
def on_dynamic(content)
|
23
28
|
@last = nil
|
24
|
-
[:dynamic,
|
29
|
+
[:dynamic, @pretty ? "Temple::Utils.indent((#{content}), #{indent.inspect}, _temple_pre_tags)" : content]
|
25
30
|
end
|
26
31
|
|
27
32
|
def on_html_doctype(type)
|
@@ -30,34 +35,40 @@ module Temple
|
|
30
35
|
end
|
31
36
|
|
32
37
|
def on_html_comment(content)
|
33
|
-
return super
|
38
|
+
return super unless @pretty
|
34
39
|
[:multi, [:static, indent], super]
|
35
40
|
end
|
36
41
|
|
37
42
|
def on_html_tag(name, attrs, closed, content)
|
38
|
-
return super
|
43
|
+
return super unless @pretty
|
39
44
|
|
40
45
|
closed ||= options[:autoclose].include?(name)
|
41
46
|
raise "Closed tag #{name} has content" if closed && !empty_exp?(content)
|
42
47
|
|
48
|
+
@pretty = false
|
43
49
|
result = [:multi, [:static, "#{tag_indent(name)}<#{name}"], compile!(attrs)]
|
44
50
|
result << [:static, ' /'] if closed && xhtml?
|
45
51
|
result << [:static, '>']
|
46
52
|
|
47
53
|
@last = name
|
48
|
-
@
|
54
|
+
@pretty = !options[:pre_tags].include?(name)
|
49
55
|
@indent += 1
|
50
56
|
result << compile!(content)
|
51
57
|
@indent -= 1
|
52
58
|
|
53
59
|
result << [:static, "#{tag_indent(name)}</#{name}>"] if !closed
|
54
60
|
@last = name
|
55
|
-
@
|
61
|
+
@pretty = true
|
56
62
|
result
|
57
63
|
end
|
58
64
|
|
59
65
|
protected
|
60
66
|
|
67
|
+
def preamble
|
68
|
+
regexp = options[:pre_tags].map {|t| "<#{t}" }.join('|')
|
69
|
+
[:block, "_temple_pre_tags = /#{regexp}/"]
|
70
|
+
end
|
71
|
+
|
61
72
|
# Return indentation if not in pre tag
|
62
73
|
def indent
|
63
74
|
"\n" + (options[:indent] || '') * @indent
|
data/lib/temple/utils.rb
CHANGED
data/lib/temple/version.rb
CHANGED
data/temple.gemspec
CHANGED
data/test/html/test_pretty.rb
CHANGED
@@ -7,18 +7,22 @@ describe Temple::HTML::Pretty do
|
|
7
7
|
|
8
8
|
it 'should indent nested tags' do
|
9
9
|
@html.compile([:html, :tag, 'div', [:multi], false,
|
10
|
-
[:html, :tag, 'p', [:multi], false, [:static, 'text']]
|
10
|
+
[:html, :tag, 'p', [:multi], false, [:multi, [:static, 'text'], [:dynamic, 'code']]]
|
11
11
|
]).should.equal [:multi,
|
12
|
-
[:
|
13
|
-
[:multi],
|
14
|
-
[:static, ">"],
|
12
|
+
[:block, "_temple_pre_tags = /<pre|<textarea/"],
|
15
13
|
[:multi,
|
16
|
-
[:static, "
|
14
|
+
[:static, "<div"],
|
17
15
|
[:multi],
|
18
16
|
[:static, ">"],
|
19
|
-
[:
|
20
|
-
|
21
|
-
|
17
|
+
[:multi,
|
18
|
+
[:static, "\n <p"],
|
19
|
+
[:multi],
|
20
|
+
[:static, ">"],
|
21
|
+
[:multi,
|
22
|
+
[:static, "text"],
|
23
|
+
[:dynamic, 'Temple::Utils.indent((code), "\n ", _temple_pre_tags)']],
|
24
|
+
[:static, "</p>"]],
|
25
|
+
[:static, "\n</div>"]]]
|
22
26
|
end
|
23
27
|
|
24
28
|
|
@@ -26,15 +30,17 @@ describe Temple::HTML::Pretty do
|
|
26
30
|
@html.compile([:html, :tag, 'pre', [:multi], false,
|
27
31
|
[:html, :tag, 'p', [:multi], false, [:static, 'text']]
|
28
32
|
]).should.equal [:multi,
|
29
|
-
[:
|
30
|
-
[:multi],
|
31
|
-
[:static, ">"],
|
33
|
+
[:block, "_temple_pre_tags = /<pre|<textarea/"],
|
32
34
|
[:multi,
|
33
|
-
[:static, "<
|
35
|
+
[:static, "<pre"],
|
34
36
|
[:multi],
|
35
37
|
[:static, ">"],
|
36
|
-
[:
|
37
|
-
|
38
|
-
|
38
|
+
[:multi,
|
39
|
+
[:static, "<p"],
|
40
|
+
[:multi],
|
41
|
+
[:static, ">"],
|
42
|
+
[:static, "text"],
|
43
|
+
[:static, "</p>"]],
|
44
|
+
[:static, "</pre>"]]]
|
39
45
|
end
|
40
46
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 7
|
9
|
+
version: 0.1.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Magnus Holm
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-19 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|