temple 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
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(thing)
48
- chain.inject(thing) {|m, e| e.compile(m) }
48
+ def compile(input)
49
+ chain.inject(input) {|m, e| e.compile(m) }
49
50
  end
50
51
 
51
52
  protected
@@ -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, options[:pretty] && !@preformatted ? content.gsub("\n", indent) : content]
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, options[:pretty] && !@preformatted ? %{(#{content}).to_s.gsub("\n", #{indent.inspect})} : content]
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 if !options[:pretty]
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 if !options[:pretty] || @preformatted
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
- @preformatted = options[:pre_tags].include?(name)
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
- @preformatted = false
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
@@ -2,6 +2,10 @@ module Temple
2
2
  module Utils
3
3
  extend self
4
4
 
5
+ def indent(text, indent, pre_tags)
6
+ pre_tags =~ text ? text : text.gsub("\n", indent)
7
+ end
8
+
5
9
  # Returns an escaped copy of `html`.
6
10
  # Strings which are declared as html_safe are not escaped.
7
11
  #
@@ -1,3 +1,3 @@
1
1
  module Temple
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.7'
3
3
  end
data/temple.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.version = Temple::VERSION
7
7
 
8
8
  s.authors = ["Magnus Holm"]
9
- s.date = %q{2010-11-22}
9
+ s.date = %q{2011-01-19}
10
10
  s.email = %q{judofyr@gmail.com}
11
11
  s.homepage = %q{http://dojo.rubyforge.org/}
12
12
  s.require_paths = ["lib"]
@@ -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
- [:static, "<div"],
13
- [:multi],
14
- [:static, ">"],
12
+ [:block, "_temple_pre_tags = /<pre|<textarea/"],
15
13
  [:multi,
16
- [:static, "\n <p"],
14
+ [:static, "<div"],
17
15
  [:multi],
18
16
  [:static, ">"],
19
- [:static, "text"],
20
- [:static, "</p>"]],
21
- [:static, "\n</div>"]]
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
- [:static, "<pre"],
30
- [:multi],
31
- [:static, ">"],
33
+ [:block, "_temple_pre_tags = /<pre|<textarea/"],
32
34
  [:multi,
33
- [:static, "<p"],
35
+ [:static, "<pre"],
34
36
  [:multi],
35
37
  [:static, ">"],
36
- [:static, "text"],
37
- [:static, "</p>"]],
38
- [:static, "</pre>"]]
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
- - 6
9
- version: 0.1.6
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: 2010-11-22 00:00:00 +01:00
17
+ date: 2011-01-19 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency