html_slice 0.2.1 → 0.2.3

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: 14881c23bd492c5f3153a2ea770b4b633721983cbd1d35558e0e55c396bec87f
4
- data.tar.gz: 3537233117edbadeaa0fa0e7f02c7463d93abdd0ac8c7e6cf54b4f293aaf523c
3
+ metadata.gz: 3242d22727eedd059279b0878df7f1d902005f496913cfcbb23597bd7188ab7e
4
+ data.tar.gz: c68fbc9a261302737414dc60ab047ebf4a5a71f53b3ef0c7528504f6439ff61c
5
5
  SHA512:
6
- metadata.gz: 76107691df8555a630e8e65ba53fa7fe7926f536cf63a5c48defef81049e2f6916a104cafcbfed02be54801d4be663fdc44ab1c7d650caa91aeb6ec9e09927e5
7
- data.tar.gz: d056915ccba22cc4c4fce0a6dc0b0c743a0dec87e67bf8e2092d80e6bf125388bf8b7368e611d90655f5f860e5a901902f270fe09b08b5dba8d3247fc60c75c1
6
+ metadata.gz: 7f2ebbe290eab9076541761dfd02f93397d975ac8d5a832a2f91d8f1743a837bec8e52402da56768cea7597e3b9c0acc1172ce4c291ca7e5f90c2baa85163a3d
7
+ data.tar.gz: 339156f66b60557116b897f98d4ba5e9388e32abcc754ad7e209b96db8f85d20cf03eed9fedf79d27903c509583bb472bbafe34a1bbe5450b95d28a96dbb4750
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HtmlSlice
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.3"
5
5
  end
data/lib/html_slice.rb CHANGED
@@ -5,66 +5,20 @@ require "cgi"
5
5
 
6
6
  module HtmlSlice
7
7
  class Error < StandardError; end
8
- # HTML as a first-class citizen in ruby code
9
- # Faster than ERB.
10
8
 
11
9
  TAGS = %i[
12
- div
13
- title
14
- embed
15
- meta
16
- br
17
- a
18
- em
19
- b
20
- i
21
- ul
22
- ol
23
- li
24
- img
25
- table
26
- tbody
27
- thead
28
- tr
29
- th
30
- td
31
- form
32
- input
33
- button
34
- link
35
- h1
36
- h2
37
- h3
38
- h4
39
- h5
40
- h6
41
- hr
42
- span
43
- label
44
- iframe
45
- template
46
- main
47
- footer
48
- aside
49
- source
50
- section
51
- small
52
- script
53
- nav
54
- area
10
+ div title embed meta br a em b i ul ol li img table tbody thead tr th td
11
+ form input button link h1 h2 h3 h4 h5 h6 hr span label iframe template main
12
+ footer aside source section small nav area
55
13
  ].freeze
56
14
 
57
15
  EMPTY_TAGS = %i[
58
- area
59
- br
60
- embed
61
- hr
62
- img
63
- input
64
- link
65
- meta
66
- source
67
- ].freeze
16
+ area br embed hr img input link meta source
17
+ ].to_set.freeze
18
+
19
+ TAGS_WITHOUT_HTML_ESCAPE = %i[
20
+ style script
21
+ ].to_set.freeze
68
22
 
69
23
  DEFAULT_SLICE = :default
70
24
 
@@ -78,23 +32,32 @@ module HtmlSlice
78
32
 
79
33
  def html_slice(html_slice_current_id = DEFAULT_SLICE, wrap: ["", ""], &block)
80
34
  @html_slice_current_id = html_slice_current_id
35
+ @html_slice ||= {}
36
+
81
37
  if block
82
- @html_slice ||= {}
83
- @html_slice[@html_slice_current_id] = wrap[0].dup
38
+ buffer = String.new
39
+ buffer << wrap[0]
40
+ @html_slice[@html_slice_current_id] = buffer
84
41
  instance_eval(&block)
85
- @html_slice[@html_slice_current_id] << wrap[1]
86
- else
87
- @html_slice[@html_slice_current_id] || ""
42
+ buffer << wrap[1]
88
43
  end
44
+
45
+ (@html_slice[@html_slice_current_id] || "").to_s
89
46
  end
90
47
 
91
48
  TAGS.each do |name|
92
- define_method name do |*args, &block|
93
- tag(name, *args, &block)
49
+ define_method(name) { |*args, &block| tag(name, *args, &block) }
50
+ end
51
+
52
+ TAGS_WITHOUT_HTML_ESCAPE.each do |name|
53
+ define_method(name) do |*args, &block|
54
+ content, attributes = parse_html_tag_arguments(args, escape: false)
55
+ generate_and_append_html_tag(name, content, attributes, &block)
94
56
  end
95
57
  end
96
58
 
97
59
  def _(content)
60
+ ensure_html_slice
98
61
  @html_slice[@html_slice_current_id] << content.to_s
99
62
  end
100
63
 
@@ -105,43 +68,46 @@ module HtmlSlice
105
68
 
106
69
  private
107
70
 
108
- def parse_html_tag_arguments(args)
71
+ def ensure_html_slice
72
+ @html_slice ||= {}
73
+ @html_slice_current_id ||= DEFAULT_SLICE
74
+ @html_slice[@html_slice_current_id] ||= String.new
75
+ end
76
+
77
+ def parse_html_tag_arguments(args, escape: true)
109
78
  content = ""
110
79
  attributes = {}
111
80
 
112
- first_argument = args.shift
113
- if first_argument.is_a?(String)
114
- content = ::CGI.escapeHTML(first_argument)
81
+ first = args.shift
82
+ if first.is_a?(String)
83
+ content = escape && !first.empty? ? CGI.escapeHTML(first) : first
115
84
  attributes = args.pop || {}
116
- elsif first_argument.is_a?(Hash)
117
- attributes = first_argument
85
+ elsif first.is_a?(Hash)
86
+ attributes = first
118
87
  end
119
88
 
120
89
  [content, attributes]
121
90
  end
122
91
 
123
92
  def generate_and_append_html_tag(tag_name, content, attributes, &block)
124
- open_tag = build_html_open_tag(tag_name, attributes)
125
- @html_slice ||= {}
126
- @html_slice_current_id ||= DEFAULT_SLICE
127
- @html_slice[@html_slice_current_id] ||= +""
93
+ ensure_html_slice
94
+ buffer = @html_slice[@html_slice_current_id]
95
+ buffer << "<" << tag_name.to_s
96
+
97
+ unless attributes.empty?
98
+ attributes.each do |key, value|
99
+ buffer << " " << key.to_s.tr("_", "-") << "='" << value.to_s << "'"
100
+ end
101
+ end
128
102
 
129
103
  if block
130
- @html_slice[@html_slice_current_id] << open_tag << ">"
104
+ buffer << ">"
131
105
  instance_eval(&block)
132
- @html_slice[@html_slice_current_id] << "</#{tag_name}>"
106
+ buffer << "</" << tag_name.to_s << ">"
133
107
  elsif content.empty? && EMPTY_TAGS.include?(tag_name)
134
- @html_slice[@html_slice_current_id] << open_tag << "/>"
108
+ buffer << "/>"
135
109
  else
136
- @html_slice[@html_slice_current_id] << open_tag << ">" << content << "</#{tag_name}>"
137
- end
138
- end
139
-
140
- def build_html_open_tag(tag_name, attributes)
141
- open_tag = "<#{tag_name}"
142
- attributes.each do |key, value|
143
- open_tag << " #{key.to_s.tr("_", "-")}='#{value}'"
110
+ buffer << ">" << content << "</" << tag_name.to_s << ">"
144
111
  end
145
- open_tag
146
112
  end
147
113
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_slice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - henrique-ft
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2025-01-02 00:00:00.000000000 Z
10
+ date: 2025-04-21 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Enable Ruby classes the ability to generate reusable pieces of html
14
13
  email:
@@ -19,12 +18,10 @@ extra_rdoc_files: []
19
18
  files:
20
19
  - lib/html_slice.rb
21
20
  - lib/html_slice/version.rb
22
- homepage:
23
21
  licenses: []
24
22
  metadata:
25
23
  source_code_uri: https://github.com/henrique-ft/html_slice
26
24
  changelog_uri: https://github.com/henrique-ft/html_slice/blob/master/CHANGELOG.md
27
- post_install_message:
28
25
  rdoc_options: []
29
26
  require_paths:
30
27
  - lib
@@ -39,8 +36,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
36
  - !ruby/object:Gem::Version
40
37
  version: '0'
41
38
  requirements: []
42
- rubygems_version: 3.5.16
43
- signing_key:
39
+ rubygems_version: 3.6.2
44
40
  specification_version: 4
45
41
  summary: Enable Ruby classes the ability to generate reusable pieces of html
46
42
  test_files: []