temple 0.6.0 → 0.6.3

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.
@@ -2,6 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 1.8.7
4
4
  - 1.9.3
5
+ - 2.0.0
5
6
  - ruby-head
6
7
  - jruby-18mode
7
8
  - jruby-19mode
data/CHANGES CHANGED
@@ -1,3 +1,15 @@
1
+ 0.6.3
2
+
3
+ * Fix HTML escaping for HTML::Pretty (Issue #69)
4
+
5
+ 0.6.2
6
+
7
+ * [:html, :js, code] abstraction added
8
+
9
+ 0.6.1
10
+
11
+ * HTML::Pretty improved
12
+
1
13
  0.6.0
2
14
 
3
15
  * HTML::AttributeMerger: rename option :attr_delimiter to :merge_attrs
@@ -238,6 +238,10 @@ List of html attributes [:html, :attr, identifier, sexp]
238
238
 
239
239
  HTML attribute abstraction. Identifier can be a String or a Symbol.
240
240
 
241
+ ### [:html, :js, code]
242
+
243
+ HTML javascript abstraction which wraps the js code in a HTML comment or CDATA depending on document format.
244
+
241
245
  Formal grammar
242
246
  --------------
243
247
 
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source :rubygems
1
+ source 'https://rubygems.org/'
2
2
  gemspec
3
3
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Temple
2
2
  ======
3
3
 
4
- [![Build Status](https://secure.travis-ci.org/judofyr/temple.png?branch=master)](http://travis-ci.org/judofyr/temple) [![Dependency Status](https://gemnasium.com/judofyr/temple.png?travis)](https://gemnasium.com/judofyr/temple) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/judofyr/temple)
4
+ [![Build Status](https://secure.travis-ci.org/judofyr/temple.png?branch=master)](http://travis-ci.org/judofyr/temple) [![Dependency Status](https://gemnasium.com/judofyr/temple.png?travis)](https://gemnasium.com/judofyr/temple) [![Code Climate](https://codeclimate.com/github/judofyr/temple.png)](https://codeclimate.com/github/judofyr/temple)
5
5
 
6
6
  Temple is an abstraction and a framework for compiling templates to pure Ruby.
7
7
  It's all about making it easier to experiment, implement and optimize template
@@ -128,7 +128,7 @@ module Temple
128
128
 
129
129
  # Implements a rails output buffer.
130
130
  #
131
- # @output_buffer = ActionView::SafeBuffer
131
+ # @output_buffer = ActiveSupport::SafeBuffer
132
132
  # @output_buffer.safe_concat "static"
133
133
  # @output_buffer.safe_concat dynamic.to_s
134
134
  # @output_buffer
@@ -30,6 +30,7 @@ module Temple
30
30
  [:html, :doctype, String] |
31
31
  [:html, :comment, Expression] |
32
32
  [:html, :condcomment, String, Expression]|
33
+ [:html, :js, Expression] |
33
34
  [:html, :tag, HTMLIdentifier, Expression, 'Expression?'] |
34
35
  [:html, :attrs, 'HTMLAttr*'] |
35
36
  HTMLAttr
@@ -18,6 +18,10 @@ module Temple
18
18
  [:html, :condcomment, condition, compile(content)]
19
19
  end
20
20
 
21
+ def on_html_js(content)
22
+ [:html, :js, compile(content)]
23
+ end
24
+
21
25
  def on_html_tag(name, attrs, content = nil)
22
26
  result = [:html, :tag, name, compile(attrs)]
23
27
  content ? (result << compile(content)) : result
@@ -23,7 +23,8 @@ module Temple
23
23
 
24
24
  define_options :format => :xhtml,
25
25
  :attr_quote => '"',
26
- :autoclose => %w[meta img link br hr input area param col base]
26
+ :autoclose => %w[meta img link br hr input area param col base],
27
+ :js_wrapper => nil
27
28
 
28
29
  HTML = [:html, :html4, :html5]
29
30
 
@@ -32,6 +33,22 @@ module Temple
32
33
  unless [:xhtml, *HTML].include?(options[:format])
33
34
  raise ArgumentError, "Invalid format #{options[:format].inspect}"
34
35
  end
36
+ wrapper = options[:js_wrapper]
37
+ wrapper = xhtml? ? :cdata : :comment if wrapper == :guess
38
+ @js_wrapper =
39
+ case wrapper
40
+ when :comment
41
+ [ "<!--\n", "\n//-->" ]
42
+ when :cdata
43
+ [ "\n//<![CDATA[\n", "\n//]]>\n" ]
44
+ when :both
45
+ [ "<!--\n//<![CDATA[\n", "\n//]]>\n//-->" ]
46
+ when nil
47
+ when Array
48
+ wrapper
49
+ else
50
+ raise ArgumentError, "Invalid JavaScript wrapper #{wrapper.inspect}"
51
+ end
35
52
  end
36
53
 
37
54
  def xhtml?
@@ -92,6 +109,17 @@ module Temple
92
109
  compile(value),
93
110
  [:static, options[:attr_quote]]]
94
111
  end
112
+
113
+ def on_html_js(content)
114
+ if @js_wrapper
115
+ [:multi,
116
+ [:static, @js_wrapper.first],
117
+ compile(content),
118
+ [:static, @js_wrapper.last]]
119
+ else
120
+ compile(content)
121
+ end
122
+ end
95
123
  end
96
124
  end
97
125
  end
@@ -13,7 +13,7 @@ module Temple
13
13
 
14
14
  def initialize(opts = {})
15
15
  super
16
- @last = :noindent
16
+ @last = nil
17
17
  @indent = 0
18
18
  @pretty = options[:pretty]
19
19
  @pre_tags = Regexp.new(options[:pre_tags].map {|t| "<#{t}" }.join('|'))
@@ -25,24 +25,31 @@ module Temple
25
25
 
26
26
  def on_static(content)
27
27
  if @pretty
28
- content = content.gsub("\n", indent) if @pre_tags !~ content
29
- @last = :noindent
28
+ if @pre_tags !~ content
29
+ content = content.sub(/\A\s*\n?/, "\n") if options[:indent_tags].include?(@last)
30
+ content = content.gsub("\n", indent)
31
+ end
32
+ @last = :static
30
33
  end
31
34
  [:static, content]
32
35
  end
33
36
 
34
37
  def on_dynamic(code)
35
38
  if @pretty
36
- @last = :noindent
37
39
  tmp = unique_name
38
- gsub_code = if ''.respond_to?(:html_safe?)
39
- "#{tmp} = #{tmp}.html_safe? ? #{tmp}.gsub(\"\\n\", #{indent.inspect}).html_safe : #{tmp}.gsub(\"\\n\", #{indent.inspect})"
40
- else
41
- "#{tmp} = #{tmp}.gsub(\"\\n\", #{indent.inspect})"
42
- end
40
+ indent_code = ''
41
+ indent_code << "#{tmp} = #{tmp}.sub(/\\A\\s*\\n?/, \"\\n\"); " if options[:indent_tags].include?(@last)
42
+ indent_code << "#{tmp} = #{tmp}.gsub(\"\n\", #{indent.inspect}); "
43
+ if ''.respond_to?(:html_safe)
44
+ safe = unique_name
45
+ # we have to first save if the string was html_safe
46
+ # otherwise the gsub operation will lose that knowledge
47
+ indent_code = "#{safe} = #{tmp}.html_safe?; #{indent_code}#{tmp} = #{tmp}.html_safe if #{safe}; "
48
+ end
49
+ @last = :dynamic
43
50
  [:multi,
44
51
  [:code, "#{tmp} = (#{code}).to_s"],
45
- [:code, "if #{@pre_tags_name} !~ #{tmp}; #{gsub_code}; end"],
52
+ [:code, "if #{@pre_tags_name} !~ #{tmp}; #{indent_code}end"],
46
53
  [:dynamic, tmp]]
47
54
  else
48
55
  [:dynamic, code]
@@ -56,8 +63,8 @@ module Temple
56
63
 
57
64
  def on_html_comment(content)
58
65
  return super unless @pretty
59
- result = [:multi, [:static, tag_indent(nil)], super]
60
- @last = nil
66
+ result = [:multi, [:static, tag_indent('comment')], super]
67
+ @last = :comment
61
68
  result
62
69
  end
63
70
 
@@ -96,7 +103,7 @@ module Temple
96
103
 
97
104
  # Return indentation before tag
98
105
  def tag_indent(name)
99
- result = @last != :noindent && (options[:indent_tags].include?(@last) || options[:indent_tags].include?(name)) ? indent : ''
106
+ result = @last && (options[:indent_tags].include?(@last) || options[:indent_tags].include?(name)) ? indent : ''
100
107
  @last = name
101
108
  result
102
109
  end
@@ -1,4 +1,4 @@
1
- unless Object.const_defined?(:Rails)
1
+ unless defined?(ActionView)
2
2
  raise "Rails is not loaded - Temple::Templates::Rails cannot be used"
3
3
  end
4
4
 
@@ -1,3 +1,3 @@
1
1
  module Temple
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.3'
3
3
  end
@@ -1,11 +1,5 @@
1
1
  require 'helper'
2
2
 
3
- class HtmlSafeString < String
4
- def html_safe?
5
- true
6
- end
7
- end
8
-
9
3
  describe Temple::Filters::Escapable do
10
4
  before do
11
5
  @filter = Temple::Filters::Escapable.new
@@ -1,12 +1,24 @@
1
1
  require 'bacon'
2
2
  require 'temple'
3
3
 
4
+ class HtmlSafeString < String
5
+ def html_safe?
6
+ true
7
+ end
8
+
9
+ def to_s
10
+ self
11
+ end
12
+ end
13
+
4
14
  module TestHelper
5
- def with_html_safe(flag)
6
- String.send(:define_method, :html_safe?) { flag }
15
+ def with_html_safe
16
+ String.send(:define_method, :html_safe?) { false }
17
+ String.send(:define_method, :html_safe) { HtmlSafeString.new(self) }
7
18
  yield
8
19
  ensure
9
20
  String.send(:undef_method, :html_safe?) if String.method_defined?(:html_safe?)
21
+ String.send(:undef_method, :html_safe) if String.method_defined?(:html_safe)
10
22
  end
11
23
 
12
24
  def grammar_validate(grammar, exp, message)
@@ -20,6 +20,18 @@ describe Temple::HTML::Fast do
20
20
  @html.call([:html, :comment, [:static, 'test']]).should.equal [:multi, [:static, "<!--"], [:static, "test"], [:static, "-->"]]
21
21
  end
22
22
 
23
+ it 'should compile js wrapped in comments' do
24
+ Temple::HTML::Fast.new(:js_wrapper => nil).call([:html, :js, [:static, 'test']]).should.equal [:static, "test"]
25
+ Temple::HTML::Fast.new(:js_wrapper => :comment).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n"], [:static, "test"], [:static, "\n//-->"]]
26
+ Temple::HTML::Fast.new(:js_wrapper => :cdata).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n"]]
27
+ Temple::HTML::Fast.new(:js_wrapper => :both).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n//-->"]]
28
+ end
29
+
30
+ it 'should guess default js comment' do
31
+ Temple::HTML::Fast.new(:js_wrapper => :guess, :format => :xhtml).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "\n//<![CDATA[\n"], [:static, "test"], [:static, "\n//]]>\n"]]
32
+ Temple::HTML::Fast.new(:js_wrapper => :guess, :format => :html).call([:html, :js, [:static, 'test']]).should.equal [:multi, [:static, "<!--\n"], [:static, "test"], [:static, "\n//-->"]]
33
+ end
34
+
23
35
  it 'should compile autoclosed html tag' do
24
36
  @html.call([:html, :tag,
25
37
  'img', [:attrs],
@@ -19,16 +19,15 @@ describe Temple::HTML::Pretty do
19
19
  [:multi],
20
20
  [:static, ">"],
21
21
  [:multi,
22
- [:static, "text"],
22
+ [:static, "\n text"],
23
23
  [:multi,
24
24
  [:code, "_temple_html_pretty2 = (code).to_s"],
25
- [:code, 'if _temple_html_pretty1 !~ _temple_html_pretty2; _temple_html_pretty2 = _temple_html_pretty2.gsub("\n", "\n "); end'],
25
+ [:code, "if _temple_html_pretty1 !~ _temple_html_pretty2; _temple_html_pretty2 = _temple_html_pretty2.gsub(\"\n\", \"\\n \"); end"],
26
26
  [:dynamic, "_temple_html_pretty2"]]],
27
- [:static, "</p>"]],
27
+ [:static, "\n </p>"]],
28
28
  [:static, "\n</div>"]]]
29
29
  end
30
30
 
31
-
32
31
  it 'should not indent preformatted tags' do
33
32
  @html.call([:html, :tag, 'pre', [:multi],
34
33
  [:html, :tag, 'p', [:multi], [:static, 'text']]
@@ -46,4 +45,17 @@ describe Temple::HTML::Pretty do
46
45
  [:static, "</p>"]],
47
46
  [:static, "</pre>"]]]
48
47
  end
48
+
49
+ it 'should not escape html_safe strings' do
50
+ with_html_safe do
51
+ @html.call(
52
+ [:dynamic, '"text<".html_safe']
53
+ ).should.equal [:multi,
54
+ [:code, "_temple_html_pretty1 = /<code|<pre|<textarea/"],
55
+ [:multi,
56
+ [:code, "_temple_html_pretty2 = (\"text<\".html_safe).to_s"],
57
+ [:code, "if _temple_html_pretty1 !~ _temple_html_pretty2; _temple_html_pretty3 = _temple_html_pretty2.html_safe?; _temple_html_pretty2 = _temple_html_pretty2.gsub(\"\n\", \"\\n\"); _temple_html_pretty2 = _temple_html_pretty2.html_safe if _temple_html_pretty3; end"],
58
+ [:dynamic, "_temple_html_pretty2"]]]
59
+ end
60
+ end
49
61
  end
@@ -26,14 +26,14 @@ describe Temple::Utils do
26
26
  end
27
27
 
28
28
  it 'should escape unsafe html strings' do
29
- with_html_safe(false) do
29
+ with_html_safe do
30
30
  Temple::Utils.escape_html_safe('<').should.equal '&lt;'
31
31
  end
32
32
  end
33
33
 
34
34
  it 'should not escape safe html strings' do
35
- with_html_safe(true) do
36
- Temple::Utils.escape_html_safe('<').should.equal '<'
35
+ with_html_safe do
36
+ Temple::Utils.escape_html_safe('<'.html_safe).should.equal '<'
37
37
  end
38
38
  end
39
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: temple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-21 00:00:00.000000000 Z
13
+ date: 2013-04-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: tilt
@@ -182,4 +182,3 @@ test_files:
182
182
  - test/test_grammar.rb
183
183
  - test/test_hash.rb
184
184
  - test/test_utils.rb
185
- has_rdoc: