hdh 0.0.0 → 0.1.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/hdh.rb +11 -3
- data/lib/hdh/helpers.rb +65 -0
- data/lib/hdh/version.rb +3 -0
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4bdfd1de926a89d5bb76e7befb112f6bc40dc30600b8e1ac5fb3193245c9c0e0
|
|
4
|
+
data.tar.gz: 35c8bfc6f03a9d466027b32068e6a222eb345b3c9412f4e7cb812b03421f27cb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 450b88bf8010b821025d11a9dd3a022007ab60e3fcabfb9f48fcecad5e54a42f1bcb111ab5e87c5b555c998d630e7469ef1eb3362e8bdff98616675e205201ef
|
|
7
|
+
data.tar.gz: 7f6a09ffbbd4f9a0d2df8dddf59297009beb488107f33abfb19983c1185923280d0e1dc35b8dbd4b92912e3d7f923d1868a5b477a8f8c7bf96dfa5b353ad167d
|
data/lib/hdh.rb
CHANGED
data/lib/hdh/helpers.rb
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module Hdh::Helpers
|
|
2
|
+
def render(h)
|
|
3
|
+
return escape(h) unless h.is_a?(Array)
|
|
4
|
+
|
|
5
|
+
h.any? ? render_tag(h) : ''
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
alias html render
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
VOID_ELEMENTS = %i[area base br col embed hr img input link meta param source track wbr].freeze
|
|
13
|
+
|
|
14
|
+
def render_tag(h)
|
|
15
|
+
t, *cs = h
|
|
16
|
+
attrs, *cs = cs if cs.first.is_a?(Hash)
|
|
17
|
+
t, attrs = parse_shorthands(t, attrs)
|
|
18
|
+
|
|
19
|
+
opening = "#{t}#{render_tag_attributes(attrs)}"
|
|
20
|
+
if VOID_ELEMENTS.include?(t.to_sym) ||
|
|
21
|
+
(cs.empty? && t =~ /^[A-Z]/)
|
|
22
|
+
"<#{opening} />"
|
|
23
|
+
else
|
|
24
|
+
"<#{opening}>#{cs.map(&r).join}</#{t}>"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def parse_shorthands(tag_name, attrs)
|
|
29
|
+
attrs = attrs&.dup || {}
|
|
30
|
+
tid, *cl = tag_name.to_s.split('.')
|
|
31
|
+
t, id, *other_ids = tid.split('#')
|
|
32
|
+
raise ArgumentError, "Invalid HTML tag: #{tag_name}" unless t
|
|
33
|
+
raise ArgumentError, "Invalid HTML tag with multiple ids: #{tag_name}" if other_ids.any?
|
|
34
|
+
|
|
35
|
+
attrs[:id] = id if id
|
|
36
|
+
attrs[:class] = Array(attrs[:class]) + cl if cl.any?
|
|
37
|
+
[t || :div, attrs]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def render_tag_attributes(attrs, prefix: '')
|
|
41
|
+
return '' unless attrs
|
|
42
|
+
|
|
43
|
+
attrs.map do |k, v|
|
|
44
|
+
case v
|
|
45
|
+
when Hash
|
|
46
|
+
render_tag_attributes(v, prefix: "#{prefix}#{k}-")
|
|
47
|
+
when Array
|
|
48
|
+
" #{k}=\"#{v.map(&method(:escape)).join(' ')}\""
|
|
49
|
+
else
|
|
50
|
+
" #{k}=\"#{escape(v)}\""
|
|
51
|
+
end
|
|
52
|
+
end.join
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def escape(s)
|
|
56
|
+
case s
|
|
57
|
+
when String then s
|
|
58
|
+
else s.to_s
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def r
|
|
63
|
+
method(:render)
|
|
64
|
+
end
|
|
65
|
+
end
|
data/lib/hdh/version.rb
ADDED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hdh
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Artur Malabarba
|
|
@@ -17,6 +17,8 @@ extensions: []
|
|
|
17
17
|
extra_rdoc_files: []
|
|
18
18
|
files:
|
|
19
19
|
- lib/hdh.rb
|
|
20
|
+
- lib/hdh/helpers.rb
|
|
21
|
+
- lib/hdh/version.rb
|
|
20
22
|
homepage: http://rubygems.org/gems/hdh
|
|
21
23
|
licenses:
|
|
22
24
|
- MIT
|