berns 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/berns.rb +47 -32
- metadata +33 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f544945cd9c43afe1ce6d3bdf6e7a3c026b6386925cbe7ac9a6ba36ba228b76
|
4
|
+
data.tar.gz: 21add20e6f88c5c56ba48a5b60d48c80e4f4be0b9b351540345479f2a5f94896
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40ac56793d022c1191c775aba8020221e6251b9c9be9f61485bd4e0ad3b90e3b46e2e1f4aa1070a3fadce6026b2f48f4b31da156bdba89930fe7c3d557fff06f
|
7
|
+
data.tar.gz: 1af75731f2ce07e6978653b45f077ff20784d950cecbfce74970185c3c2a487c306187d821fed30edba3f4e920a5a7741c9bd009c584f86c7e1c6a0a3d9272fe
|
data/lib/berns.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
require 'cgi/escape'
|
3
|
+
|
2
4
|
# A utility library for generating HTML strings
|
3
5
|
module Berns
|
6
|
+
HASH = {}.freeze
|
4
7
|
SPACE = ' '
|
5
8
|
EMPTY = ''
|
6
9
|
|
@@ -13,17 +16,17 @@ module Berns
|
|
13
16
|
# Full list of standard HTML5 elements - https://www.w3schools.com/TAgs/default.asp
|
14
17
|
STANDARD = %i[a abbr address article aside audio b bdi bdo blockquote body button canvas caption cite code colgroup datalist dd del details dfn dialog div dl dt em fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header html i iframe ins kbd label legend li main map mark menu meter nav noscript object ol optgroup option output p picture pre progress q rp rt ruby s samp script section select small span strong style sub summary table tbody td template textarea tfoot th thead time title tr u ul var video].freeze
|
15
18
|
|
16
|
-
# Dynamically defined methods that are simple proxies to {
|
19
|
+
# Dynamically defined methods that are simple proxies to {#element}.
|
17
20
|
STANDARD.each do |elm|
|
18
|
-
define_singleton_method(elm) do |
|
19
|
-
element(elm,
|
21
|
+
define_singleton_method(elm) do |attributes = HASH, &block|
|
22
|
+
element(elm, attributes, &block)
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
23
|
-
# Dynamically defined methods that are simple proxies to {
|
26
|
+
# Dynamically defined methods that are simple proxies to {#void}.
|
24
27
|
VOID.each do |elm|
|
25
|
-
define_singleton_method(elm) do |
|
26
|
-
void(elm,
|
28
|
+
define_singleton_method(elm) do |attributes = HASH|
|
29
|
+
void(elm, attributes)
|
27
30
|
end
|
28
31
|
end
|
29
32
|
|
@@ -52,14 +55,10 @@ module Berns
|
|
52
55
|
# @yieldreturn [String]
|
53
56
|
# The textual content of the element. May be HTML or plain text.
|
54
57
|
# @return [String]
|
55
|
-
def self.element(tag, attributes =
|
56
|
-
|
57
|
-
|
58
|
-
# Move stuff around unless the attributes are empty.
|
59
|
-
attrs = to_attributes(attributes)
|
60
|
-
attrs = " #{ attrs }" unless attrs.empty?
|
58
|
+
def self.element(tag, attributes = HASH)
|
59
|
+
return "<#{ tag }>#{ yield if block_given? }</#{ tag }>" if attributes.empty?
|
61
60
|
|
62
|
-
"<#{ tag }#{
|
61
|
+
"<#{ tag } #{ to_attributes(attributes) }>#{ yield if block_given? }</#{ tag }>"
|
63
62
|
end
|
64
63
|
|
65
64
|
# Same as above, but generates void elements i.e. ones without any textual
|
@@ -69,12 +68,10 @@ module Berns
|
|
69
68
|
# void(:br) # => "<br>"
|
70
69
|
#
|
71
70
|
# @return [String]
|
72
|
-
def self.void(tag, attributes =
|
73
|
-
|
74
|
-
attrs = to_attributes(attributes)
|
75
|
-
attrs = " #{ attrs }" unless attrs.empty?
|
71
|
+
def self.void(tag, attributes = HASH)
|
72
|
+
return "<#{ tag }>" if attributes.empty?
|
76
73
|
|
77
|
-
"<#{ tag }#{
|
74
|
+
"<#{ tag } #{ to_attributes(attributes) }>"
|
78
75
|
end
|
79
76
|
|
80
77
|
# Converts a hash into HTML attributes by mapping each key/value combination
|
@@ -88,12 +85,22 @@ module Berns
|
|
88
85
|
#
|
89
86
|
# @param attributes [Hash]
|
90
87
|
# The hash to convert to HTML attributes.
|
88
|
+
#
|
91
89
|
# @return [String]
|
92
90
|
# The space-joined string containing HTML attributes.
|
93
91
|
def self.to_attributes(attributes)
|
94
|
-
|
95
|
-
|
96
|
-
|
92
|
+
return EMPTY if attributes.empty?
|
93
|
+
|
94
|
+
string = +''
|
95
|
+
|
96
|
+
attributes.each do |attr, value|
|
97
|
+
string << SPACE
|
98
|
+
|
99
|
+
to_attribute(attr, value, string)
|
100
|
+
end
|
101
|
+
|
102
|
+
string.strip!
|
103
|
+
string
|
97
104
|
end
|
98
105
|
|
99
106
|
# Converts a single attribute and value into an HTML attribute string.
|
@@ -105,20 +112,28 @@ module Berns
|
|
105
112
|
# The attribute key.
|
106
113
|
# @param value [String, Boolean, Hash]
|
107
114
|
# The value to assign to the attribute.
|
115
|
+
# @param string [String, nil]
|
116
|
+
# The string modify in place with attributes and values.
|
117
|
+
#
|
108
118
|
# @return [String]
|
109
119
|
# A single HTML attribute.
|
110
|
-
def self.to_attribute(attribute, value)
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
+
def self.to_attribute(attribute, value, string = +'')
|
121
|
+
if value.is_a?(FalseClass) # rubocop:disable Style/CaseLikeIf
|
122
|
+
# noop
|
123
|
+
elsif value.is_a?(TrueClass)
|
124
|
+
string << attribute.to_s
|
125
|
+
elsif value.is_a?(Hash)
|
126
|
+
value.each do |attr, subval|
|
127
|
+
string << SPACE
|
128
|
+
|
129
|
+
to_attribute(attr.nil? ? attribute : "#{ attribute }-#{ attr }", subval, string)
|
130
|
+
end
|
131
|
+
|
132
|
+
string.strip!
|
120
133
|
else
|
121
|
-
|
134
|
+
string << %(#{ attribute }="#{ CGI.escapeHTML(value.to_s) }")
|
122
135
|
end
|
136
|
+
|
137
|
+
string
|
123
138
|
end
|
124
139
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: berns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Taylor Beck
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2020-10-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -53,9 +53,37 @@ dependencies:
|
|
53
53
|
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rubocop
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rubocop-performance
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
56
84
|
description: A utility library for generating HTML strings.
|
57
85
|
email:
|
58
|
-
-
|
86
|
+
- beck.taylorg@gmail.com
|
59
87
|
- evan@lecklider.com
|
60
88
|
executables: []
|
61
89
|
extensions: []
|
@@ -76,14 +104,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
104
|
requirements:
|
77
105
|
- - ">="
|
78
106
|
- !ruby/object:Gem::Version
|
79
|
-
version: 2.
|
107
|
+
version: 2.5.0
|
80
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
109
|
requirements:
|
82
110
|
- - ">="
|
83
111
|
- !ruby/object:Gem::Version
|
84
112
|
version: '0'
|
85
113
|
requirements: []
|
86
|
-
rubygems_version: 3.
|
114
|
+
rubygems_version: 3.1.4
|
87
115
|
signing_key:
|
88
116
|
specification_version: 4
|
89
117
|
summary: A utility library for generating HTML strings.
|