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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a69088bfba6dde926e5a431578bce1d2aaf0d1e629783f0f2eee7f7ae623a60
4
- data.tar.gz: 155c2b0b1568fa16c1d0b4c9f207ffbb4eef9c0ba7f860f29a27552676346b6a
3
+ metadata.gz: 4bdfd1de926a89d5bb76e7befb112f6bc40dc30600b8e1ac5fb3193245c9c0e0
4
+ data.tar.gz: 35c8bfc6f03a9d466027b32068e6a222eb345b3c9412f4e7cb812b03421f27cb
5
5
  SHA512:
6
- metadata.gz: ac967c1bb4147ea146ea92e3e6445034a365c274f6f2b4715f51634f0d82f09d30c592b448b44737f57fb7b92daf598690b07f4b3634d8fd081062976081d852
7
- data.tar.gz: e20934dafa4ec02da5b4c68f62454a32243fd2d77131ae8ecabcca4b25786e1715f333afb21ece7230cb1fe345358f867cfa515136ce8a6c4c4581ca627e35a1
6
+ metadata.gz: 450b88bf8010b821025d11a9dd3a022007ab60e3fcabfb9f48fcecad5e54a42f1bcb111ab5e87c5b555c998d630e7469ef1eb3362e8bdff98616675e205201ef
7
+ data.tar.gz: 7f6a09ffbbd4f9a0d2df8dddf59297009beb488107f33abfb19983c1185923280d0e1dc35b8dbd4b92912e3d7f923d1868a5b477a8f8c7bf96dfa5b353ad167d
data/lib/hdh.rb CHANGED
@@ -1,5 +1,13 @@
1
- class Hdh
2
- def self.hi
3
- puts "Hello world!"
1
+ module Hdh
2
+ end
3
+
4
+ require 'hdh/version'
5
+ require 'hdh/helpers'
6
+
7
+ module Hdh
8
+ extend ::Hdh::Helpers
9
+
10
+ class Base
11
+ include ::Hdh::Helpers
4
12
  end
5
13
  end
@@ -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
@@ -0,0 +1,3 @@
1
+ module Hdh
2
+ VERSION = '0.1.0'.freeze
3
+ end
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.0.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