temple 0.1.5 → 0.1.6
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 +11 -15
- data/lib/temple/generators.rb +40 -11
- data/lib/temple/html/pretty.rb +15 -13
- data/lib/temple/mixins.rb +6 -2
- data/lib/temple/template.rb +1 -1
- data/lib/temple/version.rb +1 -1
- data/temple.gemspec +1 -1
- data/test/filters/test_escape_html.rb +15 -0
- data/test/html/test_pretty.rb +17 -0
- metadata +3 -3
data/lib/temple/engine.rb
CHANGED
@@ -24,12 +24,14 @@ module Temple
|
|
24
24
|
class Engine
|
25
25
|
include Mixins::Options
|
26
26
|
|
27
|
-
def self.
|
28
|
-
@
|
27
|
+
def self.chain
|
28
|
+
@chain ||= []
|
29
29
|
end
|
30
30
|
|
31
31
|
def self.use(filter, *options, &block)
|
32
|
-
|
32
|
+
chain << proc do |opts|
|
33
|
+
filter.new(Hash[*opts.select {|k,v| options.include?(k) }.flatten], &block)
|
34
|
+
end
|
33
35
|
end
|
34
36
|
|
35
37
|
# Shortcut for <tt>use Temple::Filters::parser</tt>
|
@@ -42,20 +44,14 @@ module Temple
|
|
42
44
|
use(Temple::Generators.const_get(compiler), *options, &block)
|
43
45
|
end
|
44
46
|
|
45
|
-
def
|
46
|
-
|
47
|
-
|
48
|
-
@chain = self.class.filters.map do |filter, opt, block|
|
49
|
-
result = {}
|
50
|
-
opt.each do |key|
|
51
|
-
result[key] = options[key] if options.has_key?(key)
|
52
|
-
end
|
53
|
-
filter.new(result, &block)
|
54
|
-
end
|
47
|
+
def compile(thing)
|
48
|
+
chain.inject(thing) {|m, e| e.compile(m) }
|
55
49
|
end
|
56
50
|
|
57
|
-
|
58
|
-
|
51
|
+
protected
|
52
|
+
|
53
|
+
def chain
|
54
|
+
@chain ||= self.class.chain.map { |f| f.call(options) }
|
59
55
|
end
|
60
56
|
end
|
61
57
|
end
|
data/lib/temple/generators.rb
CHANGED
@@ -91,16 +91,11 @@ module Temple
|
|
91
91
|
end
|
92
92
|
|
93
93
|
def on_capture(name, block)
|
94
|
-
capture_generator.new(:buffer => name).compile(block)
|
94
|
+
options[:capture_generator].new(:buffer => name).compile(block)
|
95
95
|
end
|
96
96
|
|
97
97
|
protected
|
98
98
|
|
99
|
-
def capture_generator
|
100
|
-
@capture_generator ||=
|
101
|
-
options[:capture_generator] || Temple::Generators::StringBuffer
|
102
|
-
end
|
103
|
-
|
104
99
|
def buffer
|
105
100
|
options[:buffer]
|
106
101
|
end
|
@@ -121,8 +116,13 @@ module Temple
|
|
121
116
|
# end
|
122
117
|
# _buf.join
|
123
118
|
class ArrayBuffer < Generator
|
124
|
-
def preamble
|
125
|
-
|
119
|
+
def preamble
|
120
|
+
"#{buffer} = []"
|
121
|
+
end
|
122
|
+
|
123
|
+
def postamble
|
124
|
+
"#{buffer} = #{buffer}.join"
|
125
|
+
end
|
126
126
|
|
127
127
|
def on_static(text)
|
128
128
|
concat(text.inspect)
|
@@ -139,7 +139,9 @@ module Temple
|
|
139
139
|
|
140
140
|
# Just like ArrayBuffer, but doesn't call #join on the array.
|
141
141
|
class Array < ArrayBuffer
|
142
|
-
def postamble
|
142
|
+
def postamble
|
143
|
+
buffer
|
144
|
+
end
|
143
145
|
end
|
144
146
|
|
145
147
|
# Implements a string buffer.
|
@@ -152,11 +154,38 @@ module Temple
|
|
152
154
|
# end
|
153
155
|
# _buf
|
154
156
|
class StringBuffer < Array
|
155
|
-
def preamble
|
157
|
+
def preamble
|
158
|
+
"#{buffer} = ''"
|
159
|
+
end
|
156
160
|
|
157
161
|
def on_dynamic(code)
|
158
|
-
concat(code)
|
162
|
+
concat("(#{code}).to_s")
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
# Implements a rails output buffer.
|
167
|
+
#
|
168
|
+
# @output_buffer = ActionView::OutputBuffer
|
169
|
+
# @output_buffer.safe_concat "static"
|
170
|
+
# @output_buffer.safe_concat dynamic.to_s
|
171
|
+
# block do
|
172
|
+
# @output_buffer << "more static"
|
173
|
+
# end
|
174
|
+
# @output_buffer
|
175
|
+
class RailsOutputBuffer < StringBuffer
|
176
|
+
set_default_options :buffer_class => 'ActionView::OutputBuffer',
|
177
|
+
:buffer => '@output_buffer',
|
178
|
+
:capture_generator => RailsOutputBuffer
|
179
|
+
|
180
|
+
def preamble
|
181
|
+
"#{buffer} = #{options[:buffer_class]}.new"
|
182
|
+
end
|
183
|
+
|
184
|
+
def concat(str)
|
185
|
+
"#{buffer}.safe_concat((#{str}))"
|
159
186
|
end
|
160
187
|
end
|
161
188
|
end
|
189
|
+
|
190
|
+
Generator.default_options[:capture_generator] = Temple::Generators::StringBuffer
|
162
191
|
end
|
data/lib/temple/html/pretty.rb
CHANGED
@@ -2,26 +2,26 @@ module Temple
|
|
2
2
|
module HTML
|
3
3
|
class Pretty < Fast
|
4
4
|
set_default_options :indent => ' ',
|
5
|
-
:pretty => true
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
:pretty => true,
|
6
|
+
:indent_tags => %w(base dd div dl doctype dt fieldset form head h1 h2 h3
|
7
|
+
h4 h5 h6 hr html img input li link meta ol p script
|
8
|
+
style table tbody td tfoot th thead title tr ul).freeze,
|
9
|
+
:pre_tags => %w(pre textarea).freeze
|
10
10
|
|
11
11
|
def initialize(opts = {})
|
12
12
|
super
|
13
13
|
@last = nil
|
14
|
-
@
|
14
|
+
@indent = 0
|
15
15
|
end
|
16
16
|
|
17
17
|
def on_static(content)
|
18
18
|
@last = nil
|
19
|
-
[:static, options[:pretty] ? content.gsub("\n", indent) : content]
|
19
|
+
[:static, options[:pretty] && !@preformatted ? content.gsub("\n", indent) : content]
|
20
20
|
end
|
21
21
|
|
22
22
|
def on_dynamic(content)
|
23
23
|
@last = nil
|
24
|
-
[:dynamic, options[:pretty] ? %{(#{content}).to_s.gsub("\n", #{indent.inspect})} : content]
|
24
|
+
[:dynamic, options[:pretty] && !@preformatted ? %{(#{content}).to_s.gsub("\n", #{indent.inspect})} : content]
|
25
25
|
end
|
26
26
|
|
27
27
|
def on_html_doctype(type)
|
@@ -35,7 +35,7 @@ module Temple
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def on_html_tag(name, attrs, closed, content)
|
38
|
-
return super if !options[:pretty]
|
38
|
+
return super if !options[:pretty] || @preformatted
|
39
39
|
|
40
40
|
closed ||= options[:autoclose].include?(name)
|
41
41
|
raise "Closed tag #{name} has content" if closed && !empty_exp?(content)
|
@@ -44,13 +44,15 @@ module Temple
|
|
44
44
|
result << [:static, ' /'] if closed && xhtml?
|
45
45
|
result << [:static, '>']
|
46
46
|
|
47
|
-
@stack << name
|
48
47
|
@last = name
|
48
|
+
@preformatted = options[:pre_tags].include?(name)
|
49
|
+
@indent += 1
|
49
50
|
result << compile!(content)
|
50
|
-
@
|
51
|
+
@indent -= 1
|
51
52
|
|
52
53
|
result << [:static, "#{tag_indent(name)}</#{name}>"] if !closed
|
53
54
|
@last = name
|
55
|
+
@preformatted = false
|
54
56
|
result
|
55
57
|
end
|
56
58
|
|
@@ -58,12 +60,12 @@ module Temple
|
|
58
60
|
|
59
61
|
# Return indentation if not in pre tag
|
60
62
|
def indent
|
61
|
-
|
63
|
+
"\n" + (options[:indent] || '') * @indent
|
62
64
|
end
|
63
65
|
|
64
66
|
# Return indentation before tag
|
65
67
|
def tag_indent(name)
|
66
|
-
@last && (
|
68
|
+
@last && (options[:indent_tags].include?(@last) || options[:indent_tags].include?(name)) ? indent : ''
|
67
69
|
end
|
68
70
|
end
|
69
71
|
end
|
data/lib/temple/mixins.rb
CHANGED
@@ -57,8 +57,12 @@ module Temple
|
|
57
57
|
default_options.merge!(opts)
|
58
58
|
end
|
59
59
|
|
60
|
-
def default_options
|
61
|
-
@default_options ||= superclass.respond_to?(:default_options)
|
60
|
+
def default_options
|
61
|
+
@default_options ||= if superclass.respond_to?(:default_options)
|
62
|
+
Hash.new {|hash, key| superclass.default_options[key] }
|
63
|
+
else
|
64
|
+
{}
|
65
|
+
end
|
62
66
|
end
|
63
67
|
end
|
64
68
|
end
|
data/lib/temple/template.rb
CHANGED
data/lib/temple/version.rb
CHANGED
data/temple.gemspec
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
+
class HtmlSafeString < String
|
4
|
+
def html_safe?
|
5
|
+
true
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
3
9
|
describe Temple::Filters::EscapeHTML do
|
4
10
|
before do
|
5
11
|
@filter = Temple::Filters::EscapeHTML.new
|
@@ -29,4 +35,13 @@ describe Temple::Filters::EscapeHTML do
|
|
29
35
|
exp = [:multi, [:dynamic, 'foo']]
|
30
36
|
@filter.compile(exp).should.equal exp
|
31
37
|
end
|
38
|
+
|
39
|
+
it 'should have use_html_safe option' do
|
40
|
+
filter = Temple::Filters::EscapeHTML.new(:use_html_safe => true)
|
41
|
+
filter.compile([:multi,
|
42
|
+
[:escape, :static, HtmlSafeString.new("a < b")],
|
43
|
+
]).should.equal [:multi,
|
44
|
+
[:static, "a < b"],
|
45
|
+
]
|
46
|
+
end
|
32
47
|
end
|
data/test/html/test_pretty.rb
CHANGED
@@ -20,4 +20,21 @@ describe Temple::HTML::Pretty do
|
|
20
20
|
[:static, "</p>"]],
|
21
21
|
[:static, "\n</div>"]]
|
22
22
|
end
|
23
|
+
|
24
|
+
|
25
|
+
it 'should not indent preformatted tags' do
|
26
|
+
@html.compile([:html, :tag, 'pre', [:multi], false,
|
27
|
+
[:html, :tag, 'p', [:multi], false, [:static, 'text']]
|
28
|
+
]).should.equal [:multi,
|
29
|
+
[:static, "<pre"],
|
30
|
+
[:multi],
|
31
|
+
[:static, ">"],
|
32
|
+
[:multi,
|
33
|
+
[:static, "<p"],
|
34
|
+
[:multi],
|
35
|
+
[:static, ">"],
|
36
|
+
[:static, "text"],
|
37
|
+
[:static, "</p>"]],
|
38
|
+
[:static, "</pre>"]]
|
39
|
+
end
|
23
40
|
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
|
+
- 6
|
9
|
+
version: 0.1.6
|
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: 2010-11-
|
17
|
+
date: 2010-11-22 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|