dom 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dom.rb +26 -22
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a2b13484ac83827474f142603e27e42e819b2b6
4
- data.tar.gz: 17f28f0f67b993cab130199d85c6832ff6edbdf3
3
+ metadata.gz: c371c4f7be5e0536541081e18552a3ed70684e9f
4
+ data.tar.gz: 6884bdb2da46a6f21a6b452aa489cb6d9e166fb3
5
5
  SHA512:
6
- metadata.gz: ffdd0f93b7a99f03814a042025408a6681cfda877fe63a0fa1623f3f0f77ade27477b93a8ed8c28a5f037e11e12d89bb5873381b84b24ae67bc766ee2942cefd
7
- data.tar.gz: 83bfd6aa2eb3dc331cba8357c1fa49e16f8d26e014f690ca9d5f61c6258ad196a6bfb7a31d5d02e2bd8bd7c8cab2a8894d0953ed16280b546b64e1766330fb5a
6
+ metadata.gz: 868d459e18ffe454e7d493ff1d0908e592027868eb0116e96f2681f7042a1321a50e2413b511145dcd130045d2e578fe249ec55cfb95c3baba25b2579917bdc7
7
+ data.tar.gz: ed067fd5c127d6d7daf589867d1374345dd2d9c2277ca432516740f33ffde2fb82bc6933875b868837bc79b8863743367c3130ca6afa050e3d80d362aea367eb
data/lib/dom.rb CHANGED
@@ -26,12 +26,12 @@ module Dom
26
26
  .join.gsub(/^/o, Indent)
27
27
  .prepend("<!--#$/").concat("-->")
28
28
  end
29
- def self.format tag, h
29
+ def self.format tag, attr
30
30
  tag = hyphenize(tag)
31
31
  [
32
32
  [
33
33
  tag,
34
- *h.map do
34
+ *attr.map do
35
35
  |k, v|
36
36
  v = case v
37
37
  when nil then next
@@ -45,10 +45,10 @@ module Dom
45
45
  tag
46
46
  ]
47
47
  end
48
- def self.json_format tag, h
48
+ def self.json_format tag, attr
49
49
  [
50
50
  hyphenize(tag),
51
- *([h.map{|k, v| [hyphenize(k), v]}.to_h] if attr)
51
+ *([attr.map{|k, v| [hyphenize(k), v]}.to_h] if attr)
52
52
  ]
53
53
  end
54
54
  def self.hyphenize sym; sym.to_s.tr("_", "-") end
@@ -63,10 +63,17 @@ module Kernel
63
63
  end
64
64
  end
65
65
 
66
+ class NilClass
67
+ public :dom
68
+ public :jsonml
69
+ end
70
+
66
71
  class String
67
72
  def dom tag, mounted: nil, **attr
68
- open, close = Dom.format(tag, attr)
69
- "<%s>%s</%s>".%([open, dom_escape(tag)._ansi2html, close])
73
+ "<%s>%s</%s>".%(
74
+ Dom.format(tag, attr)
75
+ .insert(1, (block_given? ? yield(self) : self).dom_escape(tag)._ansi2html)
76
+ )
70
77
  .dom_escaped.mounted_set(mounted)
71
78
  end
72
79
  def jsonml tag, attr = nil
@@ -127,26 +134,23 @@ end
127
134
 
128
135
  class Array
129
136
  def dom *tags, mounted: nil, **attr
130
- tag = tags.pop
137
+ *recurse, tag = tags
131
138
  a = self
132
- if tags.length > 1
133
- error_objs = a.grep_v(Array)
134
- unless error_objs.empty?
135
- raise "Array not deep enough for tag #{tags[-2].inspect}: #{error_objs.first.inspect}"
139
+ a = block_given? ? map(&Proc.new) : flatten if recurse.empty?
140
+ if recurse.length <= 1
141
+ if e = a.find{|e| e.kind_of?(String).! and e.kind_of?(Array).!}
142
+ raise ArgumentError
143
+ .new("Expecting all array elements to be a string: `#{e.class}:#{e.inspect}'")
144
+ end
145
+ else
146
+ if e = a.find{|e| e.kind_of?(Array).!}
147
+ raise ArgumentError
148
+ .new("Cannot apply tag `#{recurse[-2].inspect}' to `#{e.class}:#{e.inspect}'")
136
149
  end
137
150
  end
138
- unless tags.empty?
139
- a = a.map{|e| e.dom(*tags)}
140
- end
141
- error_objs = a.grep_v(String)
142
- unless error_objs.empty?
143
- raise "All array elements must be a string: #{error_objs.first.inspect}"
144
- end
151
+ a = a.map{|e| e.dom(*recurse, &(Proc.new if block_given?))} unless recurse.empty?
145
152
  s = Dom.join(a, tag)
146
- unless tag.nil?
147
- open, close = Dom.format(tag, attr)
148
- s = "<%s>%s</%s>" % [open, s, close]
149
- end
153
+ s = "<%s>%s</%s>" % Dom.format(tag, attr).insert(1, s) unless tag.nil?
150
154
  s.dom_escaped.mounted_set(*a.map(&:mounted), mounted)
151
155
  end
152
156
  def jsonml tag, attr = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sawa