dom 0.4.3 → 0.5.0
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.
- checksums.yaml +4 -4
- data/lib/dom.rb +26 -22
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c371c4f7be5e0536541081e18552a3ed70684e9f
|
4
|
+
data.tar.gz: 6884bdb2da46a6f21a6b452aa489cb6d9e166fb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,
|
29
|
+
def self.format tag, attr
|
30
30
|
tag = hyphenize(tag)
|
31
31
|
[
|
32
32
|
[
|
33
33
|
tag,
|
34
|
-
*
|
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,
|
48
|
+
def self.json_format tag, attr
|
49
49
|
[
|
50
50
|
hyphenize(tag),
|
51
|
-
*([
|
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
|
-
|
69
|
-
|
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
|
137
|
+
*recurse, tag = tags
|
131
138
|
a = self
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
raise
|
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
|
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
|