html_slice 0.2.2 → 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: 677ccd64e0e49f6488478cf3f432fa19a52d39b053310f8ff8fc95f0e838d664
4
- data.tar.gz: dae8024850dde1d0ebe919496d163975aa7b94421eb7731df7ddcc7f8acd529e
3
+ metadata.gz: 47ec58956a24443617b6ec8ace88ef81b7bcbe835bef1708995b824f8080645c
4
+ data.tar.gz: ee26639d56eb776d55ab9284833ff92fe681383c410a6e5f0c7d7c8cf8cc2af7
5
5
  SHA512:
6
- metadata.gz: bfd1dbadb0fccfbb128659a3d55ea489c15a26dd1721dbb864c0799af4ade39aaa2ff304cac374ec287bed53fc06fdc765af2309830cc7d4cdc1c8badcfd1a6f
7
- data.tar.gz: fd182ec12728971df04a0f5853faac255bbadba9f302640fe1aa1b4de86282b7454f05a2b4d2e0f14f7c1dfd8550fdd2217e9ce83e289078fcdef27ad3f44e5d
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.2"
4
+ VERSION = "0.2.4"
5
5
  end
data/lib/html_slice.rb CHANGED
@@ -21,94 +21,91 @@ module HtmlSlice
21
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
- @html_slice[@html_slice_current_id] = wrap[0].dup
34
+ if block_given?
35
+ buffer = +""
36
+ buffer << wrap[0]
37
+ @html_slice[slice_id] = buffer
39
38
  instance_eval(&block)
40
- @html_slice[@html_slice_current_id] << wrap[1]
39
+ buffer << wrap[1]
41
40
  end
42
41
 
43
- @html_slice[@html_slice_current_id] || ""
42
+ @html_slice[slice_id].to_s
44
43
  end
45
44
 
46
- TAGS.each do |name|
47
- 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
48
51
  end
49
52
 
50
- TAGS_WITHOUT_HTML_ESCAPE.each do |name|
51
- define_method(name) do |*args, &block|
52
- content, attributes = parse_html_tag_arguments(args, escape: false)
53
- 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)
54
58
  end
55
59
  end
56
60
 
57
- def _(content)
58
- ensure_html_slice
59
- @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)
60
64
  end
61
65
 
62
- def tag(tag_name, *args, &block)
63
- content, attributes = parse_html_tag_arguments(args)
64
- generate_and_append_html_tag(tag_name, content, attributes, &block)
66
+ def _(text)
67
+ @html_slice[@html_slice_current_id] << text.to_s
65
68
  end
66
69
 
67
70
  private
68
71
 
69
- def ensure_html_slice
70
- @html_slice ||= {}
71
- @html_slice_current_id ||= DEFAULT_SLICE
72
- @html_slice[@html_slice_current_id] ||= +""
73
- end
74
-
75
- def parse_html_tag_arguments(args, escape: true)
76
- content = +""
72
+ def parse_args(args, escape: true)
73
+ content = ""
77
74
  attributes = {}
78
-
79
- first = args.shift
75
+ first = args[0]
80
76
  if first.is_a?(String)
81
- content = escape ? CGI.escapeHTML(first) : first
82
- attributes = args.pop || {}
77
+ content = escape && !first.empty? ? CGI.escapeHTML(first) : first
78
+ attributes = args.size > 1 ? args[-1] : {}
83
79
  elsif first.is_a?(Hash)
84
80
  attributes = first
85
81
  end
86
-
87
82
  [content, attributes]
88
83
  end
89
84
 
90
- def generate_and_append_html_tag(tag_name, content, attributes, &block)
91
- ensure_html_slice
92
- open_tag = build_html_open_tag(tag_name, attributes)
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] ||= +""
89
+ buffer = @html_slice[@html_slice_current_id]
90
+
91
+ buffer << "<" << name_str
92
+ unless attributes.empty?
93
+ attributes_string = ATTRIBUTE_CACHE[attributes.dup] ||= begin
94
+ attributes.map do |key, value|
95
+ " #{key.to_s.tr('_', '-')}='#{value}'"
96
+ end.join
97
+ end
98
+ buffer << attributes_string
99
+ end
93
100
 
94
- if block
95
- @html_slice[@html_slice_current_id] << open_tag << ">"
96
- instance_eval(&block)
97
- @html_slice[@html_slice_current_id] << "</#{tag_name}>"
98
- elsif content.empty? && EMPTY_TAGS.include?(tag_name)
99
- @html_slice[@html_slice_current_id] << open_tag << "/>"
101
+ if block_given?
102
+ buffer << ">"
103
+ instance_exec(&block)
104
+ buffer << "</#{name_str}>"
105
+ elsif content.empty? && EMPTY_TAGS.include?(tag_sym)
106
+ buffer << "/>"
100
107
  else
101
- @html_slice[@html_slice_current_id] << open_tag << ">" << content << "</#{tag_name}>"
108
+ buffer << ">" << content << "</#{name_str}>"
102
109
  end
103
110
  end
104
-
105
- def build_html_open_tag(tag_name, attributes)
106
- return "<#{tag_name}" if attributes.empty?
107
-
108
- attr_string = attributes.map do |key, value|
109
- " #{key.to_s.tr("_", "-")}='#{value}'"
110
- end.join
111
-
112
- "<#{tag_name}#{attr_string}"
113
- end
114
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.2
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-06 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: []