dom 0.4.2 → 0.4.3

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 +57 -34
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88b038b2977ed6d5ebca9f8d1069576c54af8173
4
- data.tar.gz: fa7ba3f5e0791bff501b4027828a790346ef4627
3
+ metadata.gz: 8a2b13484ac83827474f142603e27e42e819b2b6
4
+ data.tar.gz: 17f28f0f67b993cab130199d85c6832ff6edbdf3
5
5
  SHA512:
6
- metadata.gz: 421b0495c1e9fc83ea6512cd23a8db67a8a06352d8f7759e742fdc2f467c91d6204b3635fc95049741b525fc8a8282df44d9c9b417b5c74ace943e4da0111bd0
7
- data.tar.gz: 424a460427f206fbb60330bbd1edf8fabde0bba3bb3f58e66d9acb04d1e38c4dc4aa6b9d7b83836ddbc8ac5244ce68516cda99df9c8cc15d45173ebe6eba6a64
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.escape_html(tag)}.join
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.escape_html(tag).concat($/)}
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.escape_html(tag).prepend("-->").concat("<!--#$/")}
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.attr h
30
- (+" ").concat(h.map do
31
- |k, v|
32
- v = case v
33
- when nil then next
34
- when false then "none"
35
- when true then ""
36
- else v
37
- end
38
- "%s=\"%s\"" % [hyphenize(k), v]
39
- end.compact.join" ")
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
- ("<%s%s />" % [Dom.hyphenize(tag), Dom.attr(attr)])
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
- [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr)]
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
- _tag = Dom.hyphenize(tag)
58
- ("<%s%s>%s</%s>" % [_tag, Dom.attr(attr), escape_html(tag)._ansi2html, _tag])
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 escape_html tag = nil
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 escape_html tag = nil; self end
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 tag = nil, mounted: nil, **attr
119
- error_objs = select{|e| e.kind_of?(String).!}
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
- if tag
124
- _tag = Dom.hyphenize(tag)
125
- "<%s%s>%s</%s>" % [_tag, Dom.attr(attr), Dom.join(self, tag), _tag]
126
- else
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
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.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - sawa