dom 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dom.rb +57 -34
- 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: 8a2b13484ac83827474f142603e27e42e819b2b6
|
4
|
+
data.tar.gz: 17f28f0f67b993cab130199d85c6832ff6edbdf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffdd0f93b7a99f03814a042025408a6681cfda877fe63a0fa1623f3f0f77ade27477b93a8ed8c28a5f037e11e12d89bb5873381b84b24ae67bc766ee2942cefd
|
7
|
+
data.tar.gz: 83bfd6aa2eb3dc331cba8357c1fa49e16f8d26e014f690ca9d5f61c6258ad196a6bfb7a31d5d02e2bd8bd7c8cab2a8894d0953ed16280b546b64e1766330fb5a
|
data/lib/dom.rb
CHANGED
@@ -14,56 +14,69 @@ module Dom
|
|
14
14
|
def self.nested; singleton_class.class_eval{alias join join_nested} end
|
15
15
|
def self.pre; singleton_class.class_eval{alias join join_pre} end
|
16
16
|
def self.join_compact a, tag
|
17
|
-
a.map{|e| e.to_s.
|
17
|
+
a.map{|e| e.to_s.dom_escape(tag)}.join
|
18
18
|
end
|
19
19
|
def self.join_nested a, tag
|
20
|
-
a.map{|e| e.to_s.
|
20
|
+
a.map{|e| e.to_s.dom_escape(tag).concat($/)}
|
21
21
|
.join.gsub(/^/o, Indent)
|
22
22
|
.prepend($/)
|
23
23
|
end
|
24
24
|
def self.join_pre a, tag
|
25
|
-
a.map{|e| e.to_s.
|
25
|
+
a.map{|e| e.to_s.dom_escape(tag).prepend("-->").concat("<!--#$/")}
|
26
26
|
.join.gsub(/^/o, Indent)
|
27
27
|
.prepend("<!--#$/").concat("-->")
|
28
28
|
end
|
29
|
-
def self.
|
30
|
-
(
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
29
|
+
def self.format tag, h
|
30
|
+
tag = hyphenize(tag)
|
31
|
+
[
|
32
|
+
[
|
33
|
+
tag,
|
34
|
+
*h.map do
|
35
|
+
|k, v|
|
36
|
+
v = case v
|
37
|
+
when nil then next
|
38
|
+
when false then "none"
|
39
|
+
when true then ""
|
40
|
+
else v
|
41
|
+
end
|
42
|
+
"%s=\"%s\"" % [hyphenize(k), v]
|
43
|
+
end
|
44
|
+
].compact.join(" "),
|
45
|
+
tag
|
46
|
+
]
|
47
|
+
end
|
48
|
+
def self.json_format tag, h
|
49
|
+
[
|
50
|
+
hyphenize(tag),
|
51
|
+
*([h.map{|k, v| [hyphenize(k), v]}.to_h] if attr)
|
52
|
+
]
|
40
53
|
end
|
41
|
-
def self.json_attr h; h.map{|k, v| [hyphenize(k), v]}.to_h end
|
42
54
|
def self.hyphenize sym; sym.to_s.tr("_", "-") end
|
43
55
|
end
|
44
56
|
|
45
57
|
module Kernel
|
46
58
|
private def dom tag, mounted: nil, **attr
|
47
|
-
|
48
|
-
.dom_escaped.mounted_set(mounted)
|
59
|
+
"<%s />".%(Dom.format(tag, attr).first).dom_escaped.mounted_set(mounted)
|
49
60
|
end
|
50
61
|
private def jsonml tag, attr = nil
|
51
|
-
|
62
|
+
Dom.json_format(tag, attr)
|
52
63
|
end
|
53
64
|
end
|
54
65
|
|
55
66
|
class String
|
56
67
|
def dom tag, mounted: nil, **attr
|
57
|
-
|
58
|
-
|
68
|
+
open, close = Dom.format(tag, attr)
|
69
|
+
"<%s>%s</%s>".%([open, dom_escape(tag)._ansi2html, close])
|
59
70
|
.dom_escaped.mounted_set(mounted)
|
60
71
|
end
|
72
|
+
def jsonml tag, attr = nil
|
73
|
+
[*Dom.json_format(tag, attr), self]
|
74
|
+
end
|
61
75
|
def mounted; nil end
|
62
|
-
def
|
76
|
+
def dom_escape tag = nil
|
63
77
|
case tag; when :style, :script then self else Dom::Coder.encode(self) end
|
64
78
|
end
|
65
79
|
def dom_escaped; DomString.new(self) end
|
66
|
-
def jsonml tag, attr = nil; [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr), self] end
|
67
80
|
AnsiColor = {
|
68
81
|
"1" => "bold",
|
69
82
|
"4" => "underline",
|
@@ -99,10 +112,8 @@ end
|
|
99
112
|
|
100
113
|
class DomString < String
|
101
114
|
def to_s; self end
|
102
|
-
def
|
115
|
+
def dom_escape tag = nil; self end
|
103
116
|
def dom_escaped; self end
|
104
|
-
# def % v; super(v).dom_escaped end
|
105
|
-
# def + v; super(v).dom_escaped end
|
106
117
|
def mounted_set *mounted
|
107
118
|
mounted.compact!
|
108
119
|
if mounted.empty?
|
@@ -115,20 +126,32 @@ class DomString < String
|
|
115
126
|
end
|
116
127
|
|
117
128
|
class Array
|
118
|
-
def dom
|
119
|
-
|
129
|
+
def dom *tags, mounted: nil, **attr
|
130
|
+
tag = tags.pop
|
131
|
+
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}"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
unless tags.empty?
|
139
|
+
a = a.map{|e| e.dom(*tags)}
|
140
|
+
end
|
141
|
+
error_objs = a.grep_v(String)
|
120
142
|
unless error_objs.empty?
|
121
143
|
raise "All array elements must be a string: #{error_objs.first.inspect}"
|
122
144
|
end
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
Dom.join(self, tag)
|
145
|
+
s = Dom.join(a, tag)
|
146
|
+
unless tag.nil?
|
147
|
+
open, close = Dom.format(tag, attr)
|
148
|
+
s = "<%s>%s</%s>" % [open, s, close]
|
128
149
|
end
|
129
|
-
.dom_escaped.mounted_set(*map(&:mounted), mounted)
|
150
|
+
s.dom_escaped.mounted_set(*a.map(&:mounted), mounted)
|
151
|
+
end
|
152
|
+
def jsonml tag, attr = nil
|
153
|
+
[*Dom.json_format(attr), *self]
|
130
154
|
end
|
131
|
-
def jsonml tag, attr = nil; [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr), *self] end
|
132
155
|
end
|
133
156
|
|
134
157
|
Dom.compact
|