temple 0.1.4 → 0.1.5

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.
@@ -22,33 +22,35 @@ module Temple
22
22
  # engine.compile(something)
23
23
  #
24
24
  class Engine
25
+ include Mixins::Options
26
+
25
27
  def self.filters
26
28
  @filters ||= []
27
29
  end
28
30
 
29
- def self.use(filter, *args, &blk)
30
- filters << [filter, args, blk]
31
+ def self.use(filter, *options, &block)
32
+ filters << [filter, options, block]
31
33
  end
32
34
 
33
35
  # Shortcut for <tt>use Temple::Filters::parser</tt>
34
- def self.filter(filter, *args, &blk)
35
- use(Temple::Filters.const_get(filter), *args, &blk)
36
+ def self.filter(filter, *options, &block)
37
+ use(Temple::Filters.const_get(filter), *options, &block)
36
38
  end
37
39
 
38
40
  # Shortcut for <tt>use Temple::Generators::parser</tt>
39
- def self.generator(compiler, *args, &blk)
40
- use(Temple::Generators.const_get(compiler), *args, &blk)
41
+ def self.generator(compiler, *options, &block)
42
+ use(Temple::Generators.const_get(compiler), *options, &block)
41
43
  end
42
44
 
43
- def initialize(options = {})
44
- @chain = self.class.filters.map do |filter, args, blk|
45
- opt = args.last.is_a?(Hash) ? args.last.dup : {}
46
- opt = args.inject(opt) do |memo, ele|
47
- memo[ele] = options[ele] if options.has_key?(ele)
48
- memo
49
- end
45
+ def initialize(opts = {})
46
+ super
50
47
 
51
- filter.new(opt, &blk)
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)
52
54
  end
53
55
  end
54
56
 
@@ -2,10 +2,21 @@ module Temple
2
2
  module Filters
3
3
  # Filter which prints Temple expression
4
4
  class Debugger < Filter
5
+ default_options[:debug_pretty] = true
6
+
7
+ def initialize(opts = {})
8
+ super
9
+ require 'pp' if options[:debug_pretty]
10
+ end
11
+
5
12
  def compile(exp)
6
13
  if options[:debug]
7
- puts options[:prefix] if options[:prefix]
8
- puts exp.inspect
14
+ puts options[:debug_prefix] if options[:debug_prefix]
15
+ if options[:debug_pretty]
16
+ pp exp
17
+ else
18
+ p exp
19
+ end
9
20
  puts
10
21
  end
11
22
  exp
@@ -3,8 +3,11 @@ module Temple
3
3
  class EscapeHTML < Filter
4
4
  temple_dispatch :escape, :html
5
5
 
6
+ # Activate the usage of html_safe if it is available (for Rails 3 for example)
7
+ default_options[:use_html_safe] = ''.respond_to?(:html_safe)
8
+
6
9
  def on_escape_static(value)
7
- [:static, options[:user_html_safe] ? escape_html_safe(value) : escape_html(value)]
10
+ [:static, options[:use_html_safe] ? escape_html_safe(value) : escape_html(value)]
8
11
  end
9
12
 
10
13
  def on_escape_dynamic(value)
@@ -4,8 +4,9 @@ module Temple
4
4
  set_default_options :indent => ' ',
5
5
  :pretty => true
6
6
 
7
- INDENT_TAGS = %w(base div doctype form head html img input li link meta ol
8
- script style table tbody td th thead title tr ul).freeze
7
+ INDENT_TAGS = %w(base dd div dl doctype dt fieldset form head h1 h2 h3
8
+ h4 h5 h6 hr html img input li link meta ol p script
9
+ style table tbody td tfoot th thead title tr ul).freeze
9
10
 
10
11
  def initialize(opts = {})
11
12
  super
@@ -14,21 +15,13 @@ module Temple
14
15
  end
15
16
 
16
17
  def on_static(content)
17
- if options[:pretty]
18
- @last = nil
19
- [:static, content.gsub("\n", indent)]
20
- else
21
- [:static, content]
22
- end
18
+ @last = nil
19
+ [:static, options[:pretty] ? content.gsub("\n", indent) : content]
23
20
  end
24
21
 
25
22
  def on_dynamic(content)
26
- if options[:pretty]
27
- @last = nil
28
- [:dynamic, %{(#{content}).to_s.gsub("\n", #{indent.inspect})}]
29
- else
30
- [:dynamic, content]
31
- end
23
+ @last = nil
24
+ [:dynamic, options[:pretty] ? %{(#{content}).to_s.gsub("\n", #{indent.inspect})} : content]
32
25
  end
33
26
 
34
27
  def on_html_doctype(type)
@@ -61,6 +54,8 @@ module Temple
61
54
  result
62
55
  end
63
56
 
57
+ protected
58
+
64
59
  # Return indentation if not in pre tag
65
60
  def indent
66
61
  @stack.include?('pre') ? '' : ("\n" + ((options[:indent] || '') * @stack.size))
@@ -1,3 +1,3 @@
1
1
  module Temple
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -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-03}
9
+ s.date = %q{2010-11-09}
10
10
  s.email = %q{judofyr@gmail.com}
11
11
  s.homepage = %q{http://dojo.rubyforge.org/}
12
12
  s.require_paths = ["lib"]
@@ -17,5 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
 
20
+ # Tilt is only development dependency because most parts of Temple
21
+ # can be used without it.
22
+ s.add_development_dependency('tilt')
20
23
  s.add_development_dependency('bacon')
21
24
  end
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+
3
+ describe Temple::HTML::Pretty do
4
+ before do
5
+ @html = Temple::HTML::Pretty.new
6
+ end
7
+
8
+ it 'should indent nested tags' do
9
+ @html.compile([:html, :tag, 'div', [:multi], false,
10
+ [:html, :tag, 'p', [:multi], false, [:static, 'text']]
11
+ ]).should.equal [:multi,
12
+ [:static, "<div"],
13
+ [:multi],
14
+ [:static, ">"],
15
+ [:multi,
16
+ [:static, "\n <p"],
17
+ [:multi],
18
+ [:static, ">"],
19
+ [:static, "text"],
20
+ [:static, "</p>"]],
21
+ [:static, "\n</div>"]]
22
+ end
23
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Magnus Holm
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-03 00:00:00 +01:00
17
+ date: 2010-11-09 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: bacon
21
+ name: tilt
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
@@ -29,6 +29,18 @@ dependencies:
29
29
  version: "0"
30
30
  type: :development
31
31
  version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: bacon
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
32
44
  description:
33
45
  email: judofyr@gmail.com
34
46
  executables: []
@@ -69,6 +81,7 @@ files:
69
81
  - test/filters/test_static_merger.rb
70
82
  - test/helper.rb
71
83
  - test/html/test_fast.rb
84
+ - test/html/test_pretty.rb
72
85
  - test/test_erb.rb
73
86
  - test/test_generator.rb
74
87
  - test/test_utils.rb
@@ -109,6 +122,7 @@ test_files:
109
122
  - test/filters/test_static_merger.rb
110
123
  - test/helper.rb
111
124
  - test/html/test_fast.rb
125
+ - test/html/test_pretty.rb
112
126
  - test/test_erb.rb
113
127
  - test/test_generator.rb
114
128
  - test/test_utils.rb