hexp 0.4.0.beta1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7da79589fadbf74a0c0fa19ad9f409ca9f0dc855
4
- data.tar.gz: 65e4754146881f1d5a23df35ae4b4f7a964ad886
3
+ metadata.gz: e1f3d62af18c9ae3a5a4c28fdb30e44a082e58f4
4
+ data.tar.gz: 4379e67e153aca5cb338f3b1f002c871b8f71010
5
5
  SHA512:
6
- metadata.gz: 82271f2b2f56f71a71b2897b3668c7dbb2b8283a73df551d4ad6ae790e2cec806fb6901e6253c300f5bf1d2b7a4244bdbb0fde09cf3faf414df00d3fd6d1cc84
7
- data.tar.gz: fd56366470868b9c0df687b945e9da1ce17e2d94fbf81b4be38a454810ca684ba1c1d5bcfcee1f28d8fe80b45320b9072bea9b8ed1269b9e93bf1b3dce0272e0
6
+ metadata.gz: 8c0244e52ce84768cb92828ecdfb0021ebd6a62ae0c72ad3beaeb801f2386f5177f61008055d528b91444896aa26a47baa705269e04cc6baa2939d4c19fabc83
7
+ data.tar.gz: db32b6b92696bd5953c666d5fcbaeb9190b9d87dac0320487eeade385d92536b98067373aeb3ffad753eb0cff9ee5c2ef25e67f77d71fe730da8b8c8a483627a
@@ -1,6 +1,17 @@
1
1
  ### Development
2
2
 
3
- [full diff](http://github.com/plexus/hexp/compare/v0.3.3...master)
3
+ [full diff](http://github.com/plexus/hexp/compare/v0.4.1...master)
4
+
5
+ ### v0.4.1
6
+
7
+ * Make Hexp::List#+ return a Hexp::List
8
+ * Add Hexp::Node#append as a convenient API for adding child nodes
9
+ * Make Unparser Adamantium-immutable so instances can be included in
10
+ Adamantiumized objects
11
+ * Skip escaping inside <script> tags
12
+ * Add a "rails integration" (ugh) to play nice with
13
+ ActiveSupport::SafeBuffer. use `gem 'hexp', require: 'hexp-rails'`
14
+ in your Gemfile
4
15
 
5
16
  ### v0.4.0.beta1
6
17
 
@@ -0,0 +1,31 @@
1
+ require 'hexp'
2
+ require 'hexp/core_ext/nil' # Opt-in to NilClass#to_hexp
3
+
4
+ module Hexp
5
+ # Transparently use Hexp nodes in templates
6
+ class Node
7
+ def to_s
8
+ to_html.html_safe
9
+ end
10
+ end
11
+
12
+ class List
13
+ def to_s
14
+ to_html.html_safe
15
+ end
16
+ end
17
+
18
+ class TextNode
19
+ def to_s
20
+ to_html.html_safe
21
+ end
22
+ end
23
+
24
+ # Honor ActiveSupport::SafeBuffer's "html_safe" flag
25
+ class Unparser
26
+ alias orig_escape_text escape_text
27
+ def escape_text(text)
28
+ text.html_safe? ? text : orig_escape_text(text)
29
+ end
30
+ end
31
+ end
@@ -32,6 +32,7 @@ module Hexp
32
32
  #
33
33
  # @api private
34
34
  def self.included(klass)
35
+ super
35
36
  def klass.[](*members)
36
37
  new(members)
37
38
  end
@@ -87,5 +87,9 @@ module Hexp
87
87
  def to_html
88
88
  each_with_object('') {|n,s| s << n.to_html}
89
89
  end
90
+
91
+ def +(other)
92
+ self.class[*to_ary, *other.to_ary]
93
+ end
90
94
  end
91
95
  end
@@ -96,6 +96,9 @@ module Hexp
96
96
  H[tag, attributes, children.map(&blk)]
97
97
  end
98
98
 
99
+ def append(*nodes)
100
+ H[tag, attributes, children + nodes]
101
+ end
99
102
  end
100
103
  end
101
104
  end
@@ -1,5 +1,7 @@
1
1
  module Hexp
2
2
  class Unparser
3
+ include Adamantium
4
+
3
5
  APOS = ?'.freeze
4
6
  QUOT = ?".freeze
5
7
  LT = '<'.freeze
@@ -24,46 +26,59 @@ module Hexp
24
26
  ESCAPE_TEXT_REGEX = Regexp.new("[#{ESCAPE_TEXT.keys.join}]")
25
27
 
26
28
  DEFAULT_OPTIONS = {
27
- encoding: Encoding.default_external
29
+ encoding: Encoding.default_external,
30
+ no_escape: [:script]
28
31
  }
29
32
 
33
+ attr_reader :options
34
+
30
35
  def initialize(options)
31
36
  @options = DEFAULT_OPTIONS.merge(options)
32
37
  end
33
38
 
34
39
  def call(node)
35
- @buffer = String.new.force_encoding(@options[:encoding])
36
- add_node(node)
37
- @buffer.freeze
40
+ buffer = String.new.force_encoding(options[:encoding])
41
+ add_node(buffer, node)
42
+ buffer.freeze
38
43
  end
39
44
 
40
45
  private
41
46
 
42
- def add_node(node)
47
+ def add_node(buffer, node)
43
48
  if node.text?
44
- @buffer << escape_text(node)
49
+ buffer << escape_text(node)
45
50
  else
46
- add_tag(node.tag, node.attributes, node.children)
51
+ add_tag(buffer, node.tag, node.attributes, node.children)
47
52
  end
48
53
  end
49
54
 
50
- def add_tag(tag, attrs, children)
51
- @buffer << LT << tag.to_s
55
+ def add_tag(buffer, tag, attrs, children)
56
+ buffer << LT << tag.to_s
52
57
  unless attrs.empty?
53
- attrs.each {|k,v| add_attr(k,v)}
58
+ attrs.each {|k,v| add_attr(buffer, k,v)}
59
+ end
60
+ buffer << GT
61
+ add_child_nodes(buffer, tag, children)
62
+ buffer << LT << FSLASH << tag.to_s << GT
63
+ end
64
+
65
+ def add_child_nodes(buffer, tag, children)
66
+ # TODO look into the special parsing mode that browsers use inside <script> tags,
67
+ # at the least we should throw an error if the text contains </script>
68
+ if options[:no_escape].include?(tag) && children.length == 1 && children.first.text?
69
+ buffer << children.first
70
+ else
71
+ children.each {|node| add_node(buffer, node) }
54
72
  end
55
- @buffer << GT
56
- children.each(&method(:add_node))
57
- @buffer << LT << FSLASH << tag.to_s << GT
58
73
  end
59
74
 
60
- def add_attr(key, value)
61
- @buffer << SPACE << key << EQ
62
- add_attr_value(value)
75
+ def add_attr(buffer, key, value)
76
+ buffer << SPACE << key << EQ
77
+ add_attr_value(buffer, value)
63
78
  end
64
79
 
65
- def add_attr_value(value)
66
- @buffer << APOS << value.gsub(ESCAPE_ATTR_APOS_REGEX, ESCAPE_ATTR_APOS) << APOS
80
+ def add_attr_value(buffer, value)
81
+ buffer << APOS << value.gsub(ESCAPE_ATTR_APOS_REGEX, ESCAPE_ATTR_APOS) << APOS
67
82
  end
68
83
 
69
84
  def escape_text(text)
@@ -1,3 +1,3 @@
1
1
  module Hexp
2
- VERSION = '0.4.0.beta1'
2
+ VERSION = '0.4.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hexp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.beta1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arne Brasseur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-20 00:00:00.000000000 Z
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sass
@@ -167,6 +167,7 @@ files:
167
167
  - examples/todo.rb
168
168
  - examples/widget.rb
169
169
  - hexp.gemspec
170
+ - lib/hexp-rails.rb
170
171
  - lib/hexp.rb
171
172
  - lib/hexp/builder.rb
172
173
  - lib/hexp/core_ext/nil.rb
@@ -241,12 +242,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
241
242
  version: '0'
242
243
  required_rubygems_version: !ruby/object:Gem::Requirement
243
244
  requirements:
244
- - - ">"
245
+ - - ">="
245
246
  - !ruby/object:Gem::Version
246
- version: 1.3.1
247
+ version: '0'
247
248
  requirements: []
248
249
  rubyforge_project:
249
- rubygems_version: 2.4.2
250
+ rubygems_version: 2.2.2
250
251
  signing_key:
251
252
  specification_version: 4
252
253
  summary: Generate and manipulate HTML documents and nodes.