berns 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ require 'mkmf'
3
+
4
+ create_header
5
+ create_makefile 'berns/berns'
data/lib/berns.rb CHANGED
@@ -1,35 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
  require 'cgi/escape'
3
3
 
4
- # A utility library for generating HTML strings
5
- module Berns
6
- HASH = {}.freeze
7
- SPACE = ' '
4
+ require 'berns/berns'
5
+ require 'berns/version'
6
+
7
+ module Berns # :nodoc:
8
+ class Error < StandardError; end
9
+
8
10
  EMPTY = ''
9
11
 
10
12
  # Regular expression for basic HTML tag sanitizing.
11
13
  SANITIZE_REGEX = /<[^>]+>/.freeze
12
14
 
13
- # Full list of void elements - http://xahlee.info/js/html5_non-closing_tag.html
14
- VOID = %i[area base br col embed hr img input link menuitem meta param source track wbr].freeze
15
-
16
- # Full list of standard HTML5 elements - https://www.w3schools.com/TAgs/default.asp
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
18
-
19
- # Dynamically defined methods that are simple proxies to {#element}.
20
- STANDARD.each do |elm|
21
- define_singleton_method(elm) do |attributes = HASH, &block|
22
- element(elm, attributes, &block)
23
- end
24
- end
25
-
26
- # Dynamically defined methods that are simple proxies to {#void}.
27
- VOID.each do |elm|
28
- define_singleton_method(elm) do |attributes = HASH|
29
- void(elm, attributes)
30
- end
31
- end
32
-
33
15
  # Sanitize text input by stripping HTML tags.
34
16
  #
35
17
  # @example Sanitize some text, removing HTML elements.
@@ -42,98 +24,4 @@ module Berns
42
24
  def self.sanitize(string)
43
25
  string&.gsub(SANITIZE_REGEX, EMPTY)
44
26
  end
45
-
46
- # Generate a simple HTML element.
47
- #
48
- # @example Create an element with simple attributes.
49
- # element(:a, href: '#nerds') { 'Nerds!'} # => "<a href='#nerds'>Nerds!</a>"
50
- #
51
- # @param tag [Symbol, String]
52
- # The tag type to generate e.g. <a> or <script>.
53
- # @param attributes [Hash]
54
- # A hash of attributes to add to the generated element.
55
- # @yieldreturn [String]
56
- # The textual content of the element. May be HTML or plain text.
57
- # @return [String]
58
- def self.element(tag, attributes = HASH)
59
- return "<#{ tag }>#{ yield if block_given? }</#{ tag }>" if attributes.empty?
60
-
61
- "<#{ tag } #{ to_attributes(attributes) }>#{ yield if block_given? }</#{ tag }>"
62
- end
63
-
64
- # Same as above, but generates void elements i.e. ones without any textual
65
- # content.
66
- #
67
- # @example Create a self-closing element.
68
- # void(:br) # => "<br>"
69
- #
70
- # @return [String]
71
- def self.void(tag, attributes = HASH)
72
- return "<#{ tag }>" if attributes.empty?
73
-
74
- "<#{ tag } #{ to_attributes(attributes) }>"
75
- end
76
-
77
- # Converts a hash into HTML attributes by mapping each key/value combination
78
- # to {#to_attribute} which actually does the hard work.
79
- #
80
- # @example A simple, single-level hash.
81
- # to_attributes({ href: '#link' }) # => "href='#link'"
82
- #
83
- # @example A nested hash.
84
- # to_attributes(href: '#nerds', some: { stuff: 'foobar' }) # => "href='#nerds' some-stuff='foobar'"
85
- #
86
- # @param attributes [Hash]
87
- # The hash to convert to HTML attributes.
88
- #
89
- # @return [String]
90
- # The space-joined string containing HTML attributes.
91
- def self.to_attributes(attributes)
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
104
- end
105
-
106
- # Converts a single attribute and value into an HTML attribute string.
107
- #
108
- # @example Obtain a boolean attribute string.
109
- # to_attribute('nerf', true) # => 'nerf'
110
- #
111
- # @param attribute [#to_s]
112
- # The attribute key.
113
- # @param value [String, Boolean, Hash]
114
- # The value to assign to the attribute.
115
- # @param string [String, nil]
116
- # The string modify in place with attributes and values.
117
- #
118
- # @return [String]
119
- # A single HTML attribute.
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!
133
- else
134
- string << %(#{ attribute }="#{ CGI.escapeHTML(value.to_s) }")
135
- end
136
-
137
- string
138
- end
139
27
  end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Berns
3
+ VERSION = '3.0.0'
4
+ 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: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Beck
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-10-01 00:00:00.000000000 Z
12
+ date: 2021-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cgi
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: bundler
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -53,6 +67,20 @@ dependencies:
53
67
  - - ">="
54
68
  - !ruby/object:Gem::Version
55
69
  version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake-compiler
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
  - !ruby/object:Gem::Dependency
57
85
  name: rubocop
58
86
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +95,20 @@ dependencies:
67
95
  - - ">="
68
96
  - !ruby/object:Gem::Version
69
97
  version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop-minitest
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
70
112
  - !ruby/object:Gem::Dependency
71
113
  name: rubocop-performance
72
114
  requirement: !ruby/object:Gem::Requirement
@@ -81,15 +123,43 @@ dependencies:
81
123
  - - ">="
82
124
  - !ruby/object:Gem::Version
83
125
  version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rubocop-rake
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
84
140
  description: A utility library for generating HTML strings.
85
141
  email:
86
142
  - beck.taylorg@gmail.com
87
143
  - evan@lecklider.com
88
144
  executables: []
89
- extensions: []
145
+ extensions:
146
+ - ext/berns/extconf.rb
90
147
  extra_rdoc_files: []
91
148
  files:
149
+ - ".editorconfig"
150
+ - ".github/workflows/main.yml"
151
+ - ".gitignore"
152
+ - ".rubocop.yml"
153
+ - CHANGELOG.org
154
+ - Gemfile
155
+ - LICENSE.txt
156
+ - README.org
157
+ - Rakefile
158
+ - berns.gemspec
159
+ - ext/berns/berns.c
160
+ - ext/berns/extconf.rb
92
161
  - lib/berns.rb
162
+ - lib/berns/version.rb
93
163
  homepage: https://github.com/evanleck/berns
94
164
  licenses:
95
165
  - MIT
@@ -111,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
181
  - !ruby/object:Gem::Version
112
182
  version: '0'
113
183
  requirements: []
114
- rubygems_version: 3.1.4
184
+ rubygems_version: 3.2.15
115
185
  signing_key:
116
186
  specification_version: 4
117
187
  summary: A utility library for generating HTML strings.