dom 0.2.10 → 0.2.12

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 +36 -23
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e1219b93b5c18c22d26821c1092a089ee00a5a4
4
- data.tar.gz: 96a575a9fa9ce3159d78f72932abdb2aa1fef53f
3
+ metadata.gz: 3e3e8109c1a95328ff38ea23213be43fa8a06865
4
+ data.tar.gz: d61374e6c2985930d0d7ae0d572a1bf5c2d8856b
5
5
  SHA512:
6
- metadata.gz: 7155b243a44e0614bda027debd95dff6b6d2a05192772d9a53e7337ca36a89b64b7a88cbf1dd7239a8f97b114fe1e21786e36df3d487ce99d9c5604583296b3c
7
- data.tar.gz: 359b50537f6b7c485d00594bb706b6e53ab21fea812d2ba3417e0feb6f3c98a31c1b5e0b76ca9b48e46cfd1828ee7c1a94d007c28b29106ce1526df350e19e6f
6
+ metadata.gz: e880eadaae730af643e02a29ff85f569a2b4ebcddda509ec49eeffa5a7aafd59cdc8804b1c3e233852ffb5596340643919522645e65a50a55aa194da73329de8
7
+ data.tar.gz: 2307e935d2a2fd80321f4d3c71f3ac5d4a7e6424a9b44a4934e042d53368d789460a3765ead8149bf3f9e3f67c83d33255b6dc4cd7a5b39cb2e0eadafdb11d39
data/lib/dom.rb CHANGED
@@ -8,6 +8,7 @@ end
8
8
 
9
9
  module Dom
10
10
  Coder = HTMLEntities.new
11
+ Indent = " "
11
12
  def self.compact; singleton_class.class_eval{alias join join_compact} end
12
13
  def self.nested; singleton_class.class_eval{alias join join_nested} end
13
14
  def self.pre; singleton_class.class_eval{alias join join_pre} end
@@ -15,35 +16,39 @@ module Dom
15
16
  a.map{|e| e.to_s.escape_html(tag)}.join(s)
16
17
  end
17
18
  def self.join_nested a, tag
18
- "#$/#{join_compact(a, tag, $/).gsub(/^/o, " ".freeze)}#$/"
19
+ "#$/%s#$/".freeze % join_compact(a, tag, $/).gsub(/^/o, " ".freeze)
19
20
  end
20
21
  def self.join_pre a, tag
21
- "<!--#$/#{"-->#{join_compact(a, tag, "<!--#$/-->".freeze)}<!--".gsub(/^/o, " ".freeze)}#$/-->"
22
+ "<!--#$/#{Indent}-->%s<!--#$/-->".freeze %
23
+ join_compact(a, tag, "<!--#$/-->".freeze).gsub(/^/o, Indent)
22
24
  end
23
- def self.attr h; h.map{|k, v| " #{hyphenize(k)}=\"#{v}\""}.join end
25
+ def self.attr h; h.map{|k, v| " %s=\"%s\"".freeze % [hyphenize(k), v]}.join end
24
26
  def self.json_attr h; h.map{|k, v| [hyphenize(k), v]}.to_h end
25
27
  def self.hyphenize sym; sym.to_s.tr("_".freeze, "-".freeze) end
26
28
  end
27
29
 
28
30
  module Kernel
29
- private
30
- def dom tag, attr = {}
31
- "<#{Dom.hyphenize(tag)}#{Dom.attr(attr)} />".escaped
31
+ private def dom tag, mounted: nil, **attr
32
+ ("<%s%s />".freeze % [Dom.hyphenize(tag), Dom.attr(attr)])
33
+ .dom_escaped.mounted_set(mounted)
34
+ end
35
+ private def jsonml tag, attr = nil
36
+ [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr)]
32
37
  end
33
- def jsonml tag, attr = nil; [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr)] end
34
38
  end
35
39
 
36
40
  class String
37
- def dom tag, attr = {}
38
- "<#{Dom.hyphenize(tag)}#{Dom.attr(attr)}>"\
39
- "#{escape_html(tag).ansi2html}"\
40
- "</#{Dom.hyphenize(tag)}>".escaped
41
+ def dom tag, mounted: nil, **attr
42
+ _tag = Dom.hyphenize(tag)
43
+ ("<%s%s>%s</%s>".freeze % [_tag, Dom.attr(attr), escape_html(tag).ansi2html, _tag])
44
+ .dom_escaped.mounted_set(mounted)
41
45
  end
46
+ def mounted; nil end
42
47
  def escape_html tag = nil
43
48
  case tag; when :style, :script then self else Dom::Coder.encode(self) end
44
49
  end
45
50
  def unescape_html; Dom::Coder.decode(self) end
46
- def escaped; DomString.new(self) end
51
+ def dom_escaped; DomString.new(self) end
47
52
  def jsonml tag, attr = nil; [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr), self] end
48
53
  AnsiColor = {
49
54
  "1" => "bold",
@@ -71,9 +76,8 @@ class String
71
76
  io.print(
72
77
  if sc.scan(/\e\[0?m/o) then '</span>'.freeze
73
78
  elsif sc.scan(/\e\[0?(\d+)m/o) then '<span class="%s">'.freeze % AnsiColor[sc[1]]
74
- elsif sc.scan(/./mo) then sc.matched
75
- end
76
- ) until sc.eos?
79
+ end ||
80
+ sc.scan(/./mo)) until sc.eos?
77
81
  io.string
78
82
  end
79
83
  end
@@ -81,17 +85,26 @@ end
81
85
  class DomString < String
82
86
  def to_s; self end
83
87
  def escape_html tag = nil; self end
84
- def escaped; self end
85
- def % v; super(v).escaped end
86
- def * v; super(v).escaped end
87
- def + v; super(v).escaped end
88
+ def dom_escaped; self end
89
+ def % v; super(v).dom_escaped end
90
+ def * v; super(v).dom_escaped end
91
+ def + v; super(v).dom_escaped end
92
+ def mounted_set *mounted
93
+ mounted.compact!
94
+ if mounted.empty?
95
+ elsif @mounted then @mounted.concat(mounted.join)
96
+ else @mounted = mounted.join
97
+ end
98
+ self
99
+ end
100
+ attr_reader :mounted
88
101
  end
89
102
 
90
103
  class Array
91
- def dom tag, attr = {}
92
- "<#{Dom.hyphenize(tag)}#{Dom.attr(attr)}>"\
93
- "#{Dom.join(self, tag)}"\
94
- "</#{Dom.hyphenize(tag)}>".escaped
104
+ def dom tag, mounted: nil, **attr
105
+ _tag = Dom.hyphenize(tag)
106
+ ("<%s%s>%s</%s>".freeze % [_tag, Dom.attr(attr), Dom.join(self, tag), _tag])
107
+ .dom_escaped.mounted_set(*map(&:mounted), mounted)
95
108
  end
96
109
  def jsonml tag, attr = nil; [Dom.hyphenize(tag), *([Dom.json_attr(attr)] if attr), *self] end
97
110
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - sawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-24 00:00:00.000000000 Z
11
+ date: 2014-08-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ''
14
14
  email: []
@@ -36,7 +36,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  version: '0'
37
37
  requirements: []
38
38
  rubyforge_project:
39
- rubygems_version: 2.3.0
39
+ rubygems_version: 2.4.1
40
40
  signing_key:
41
41
  specification_version: 4
42
42
  summary: Creates HTML DOM strings