html_slice 0.2.3 → 0.2.4

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: 3242d22727eedd059279b0878df7f1d902005f496913cfcbb23597bd7188ab7e
4
- data.tar.gz: c68fbc9a261302737414dc60ab047ebf4a5a71f53b3ef0c7528504f6439ff61c
3
+ metadata.gz: 47ec58956a24443617b6ec8ace88ef81b7bcbe835bef1708995b824f8080645c
4
+ data.tar.gz: ee26639d56eb776d55ab9284833ff92fe681383c410a6e5f0c7d7c8cf8cc2af7
5
5
  SHA512:
6
- metadata.gz: 7f2ebbe290eab9076541761dfd02f93397d975ac8d5a832a2f91d8f1743a837bec8e52402da56768cea7597e3b9c0acc1172ce4c291ca7e5f90c2baa85163a3d
7
- data.tar.gz: 339156f66b60557116b897f98d4ba5e9388e32abcc754ad7e209b96db8f85d20cf03eed9fedf79d27903c509583bb472bbafe34a1bbe5450b95d28a96dbb4750
6
+ metadata.gz: aa31246b60c626d96704dcc4bc358dafbeef40a9e68f6b59afad184bc7127f13be3e5f9dd17b0daa7b999d95147aa19a238e864ff710ebc7b5f14d686deb763a
7
+ data.tar.gz: 653305df476ebde0cf5e370eb0fb43a8b4e8e329dcf1319a8271d7c1f4edc8fecb04630ee6b676851a5d113ed55ea774ba8fbddc75cb605f9582eb18ef34bb95
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HtmlSlice
4
- VERSION = "0.2.3"
4
+ VERSION = "0.2.4"
5
5
  end
data/lib/html_slice.rb CHANGED
@@ -14,100 +14,98 @@ module HtmlSlice
14
14
 
15
15
  EMPTY_TAGS = %i[
16
16
  area br embed hr img input link meta source
17
- ].to_set.freeze
17
+ ].freeze
18
18
 
19
19
  TAGS_WITHOUT_HTML_ESCAPE = %i[
20
20
  style script
21
- ].to_set.freeze
21
+ ].freeze
22
22
 
23
23
  DEFAULT_SLICE = :default
24
+ ATTRIBUTE_CACHE = {}
24
25
 
25
- def html_layout(html_slice_current_id = DEFAULT_SLICE, &block)
26
- html_slice(
27
- html_slice_current_id,
28
- wrap: ["<!DOCTYPE html><html>", "</html>"],
29
- &block
30
- )
26
+ def html_layout(slice_id = DEFAULT_SLICE, &block)
27
+ html_slice(slice_id, wrap: ["<!DOCTYPE html><html>", "</html>"], &block)
31
28
  end
32
29
 
33
- def html_slice(html_slice_current_id = DEFAULT_SLICE, wrap: ["", ""], &block)
34
- @html_slice_current_id = html_slice_current_id
30
+ def html_slice(slice_id = DEFAULT_SLICE, wrap: ["", ""], &block)
31
+ @html_slice_current_id = slice_id
35
32
  @html_slice ||= {}
36
33
 
37
- if block
38
- buffer = String.new
34
+ if block_given?
35
+ buffer = +""
39
36
  buffer << wrap[0]
40
- @html_slice[@html_slice_current_id] = buffer
37
+ @html_slice[slice_id] = buffer
41
38
  instance_eval(&block)
42
39
  buffer << wrap[1]
43
40
  end
44
41
 
45
- (@html_slice[@html_slice_current_id] || "").to_s
42
+ @html_slice[slice_id].to_s
46
43
  end
47
44
 
48
- TAGS.each do |name|
49
- define_method(name) { |*args, &block| tag(name, *args, &block) }
45
+ TAGS_WITHOUT_HTML_ESCAPE.each do |tag_name|
46
+ name_str = tag_name.to_s.freeze
47
+ define_method(name_str) do |*args, &block|
48
+ content, attrs = parse_args(args, escape: false)
49
+ render_tag(name_str, tag_name, content, attrs, &block)
50
+ end
50
51
  end
51
52
 
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)
53
+ TAGS.each do |tag_name|
54
+ name_str = tag_name.to_s.freeze
55
+ define_method(name_str) do |*args, &block|
56
+ content, attrs = parse_args(args, escape: true)
57
+ render_tag(name_str, tag_name, content, attrs, &block)
56
58
  end
57
59
  end
58
60
 
59
- def _(content)
60
- ensure_html_slice
61
- @html_slice[@html_slice_current_id] << content.to_s
61
+ def tag(tag_name, *args, &block)
62
+ content, attrs = parse_args(args, escape: true)
63
+ render_tag(tag_name.to_s, tag_name, content, attrs, &block)
62
64
  end
63
65
 
64
- def tag(tag_name, *args, &block)
65
- content, attributes = parse_html_tag_arguments(args)
66
- generate_and_append_html_tag(tag_name, content, attributes, &block)
66
+ def _(text)
67
+ @html_slice[@html_slice_current_id] << text.to_s
67
68
  end
68
69
 
69
70
  private
70
71
 
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)
72
+ def parse_args(args, escape: true)
78
73
  content = ""
79
74
  attributes = {}
80
-
81
- first = args.shift
75
+ first = args[0]
82
76
  if first.is_a?(String)
83
77
  content = escape && !first.empty? ? CGI.escapeHTML(first) : first
84
- attributes = args.pop || {}
78
+ attributes = args.size > 1 ? args[-1] : {}
85
79
  elsif first.is_a?(Hash)
86
80
  attributes = first
87
81
  end
88
-
89
82
  [content, attributes]
90
83
  end
91
84
 
92
- def generate_and_append_html_tag(tag_name, content, attributes, &block)
93
- ensure_html_slice
85
+ def render_tag(name_str, tag_sym, content, attributes, &block)
86
+ @html_slice ||= {}
87
+ @html_slice_current_id ||= DEFAULT_SLICE
88
+ @html_slice[@html_slice_current_id] ||= +""
94
89
  buffer = @html_slice[@html_slice_current_id]
95
- buffer << "<" << tag_name.to_s
96
90
 
91
+ buffer << "<" << name_str
97
92
  unless attributes.empty?
98
- attributes.each do |key, value|
99
- buffer << " " << key.to_s.tr("_", "-") << "='" << value.to_s << "'"
93
+ attributes_string = ATTRIBUTE_CACHE[attributes.dup] ||= begin
94
+ attributes.map do |key, value|
95
+ " #{key.to_s.tr('_', '-')}='#{value}'"
96
+ end.join
100
97
  end
98
+ buffer << attributes_string
101
99
  end
102
100
 
103
- if block
101
+ if block_given?
104
102
  buffer << ">"
105
- instance_eval(&block)
106
- buffer << "</" << tag_name.to_s << ">"
107
- elsif content.empty? && EMPTY_TAGS.include?(tag_name)
103
+ instance_exec(&block)
104
+ buffer << "</#{name_str}>"
105
+ elsif content.empty? && EMPTY_TAGS.include?(tag_sym)
108
106
  buffer << "/>"
109
107
  else
110
- buffer << ">" << content << "</" << tag_name.to_s << ">"
108
+ buffer << ">" << content << "</#{name_str}>"
111
109
  end
112
110
  end
113
111
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_slice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - henrique-ft
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-21 00:00:00.000000000 Z
10
+ date: 2025-12-21 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Enable Ruby classes the ability to generate reusable pieces of html
13
13
  email:
@@ -36,7 +36,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
38
  requirements: []
39
- rubygems_version: 3.6.2
39
+ rubygems_version: 3.6.6
40
40
  specification_version: 4
41
41
  summary: Enable Ruby classes the ability to generate reusable pieces of html
42
42
  test_files: []