html_slice 0.2.3 → 0.2.5

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: 15e5d2ae2bdf50596a619f5fd7a4252146b749ee9c51606741c96cfc49bab2d0
4
+ data.tar.gz: ed9d95769f41c8cfc8193ae6c21cfcd02619bb67c54170757e7a5e7ed8f2ea23
5
5
  SHA512:
6
- metadata.gz: 7f2ebbe290eab9076541761dfd02f93397d975ac8d5a832a2f91d8f1743a837bec8e52402da56768cea7597e3b9c0acc1172ce4c291ca7e5f90c2baa85163a3d
7
- data.tar.gz: 339156f66b60557116b897f98d4ba5e9388e32abcc754ad7e209b96db8f85d20cf03eed9fedf79d27903c509583bb472bbafe34a1bbe5450b95d28a96dbb4750
6
+ metadata.gz: ab59522b9c479992f00386c9b93a77ffd7322e89a9a91f2a9faa2a4ec0c5fd30705e513b13f65b51de6d023f6a0631d6c03cb2165e9d50de51dd15f56d19cb84
7
+ data.tar.gz: 57a60eefdf900156c4e34c15557e2468f26c3482644669200aee1ebda6a0dcf26cb59437a8a647ea40db70bd352ffe611327347fd62ab25fe68f3c85916fa5bc
@@ -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.5"
5
5
  end
data/lib/html_slice.rb CHANGED
@@ -14,100 +14,108 @@ 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 self.slice(slice_id = DEFAULT_SLICE, wrap: ["", ""], &block)
27
+ @class ||= Class.new { include HtmlSlice }.new
28
+ @class.html_slice(slice_id, wrap: wrap, &block)
31
29
  end
32
30
 
33
- def html_slice(html_slice_current_id = DEFAULT_SLICE, wrap: ["", ""], &block)
34
- @html_slice_current_id = html_slice_current_id
31
+ def self.layout(slice_id = DEFAULT_SLICE, &block)
32
+ @class ||= Class.new { include HtmlSlice }.new
33
+ @class.html_layout(slice_id, &block)
34
+ end
35
+
36
+ def html_layout(slice_id = DEFAULT_SLICE, &block)
37
+ html_slice(slice_id, wrap: ["<!DOCTYPE html><html>", "</html>"], &block)
38
+ end
39
+
40
+ def html_slice(slice_id = DEFAULT_SLICE, wrap: ["", ""], &block)
41
+ @html_slice_current_id = slice_id
35
42
  @html_slice ||= {}
36
43
 
37
- if block
38
- buffer = String.new
44
+ if block_given?
45
+ buffer = +""
39
46
  buffer << wrap[0]
40
- @html_slice[@html_slice_current_id] = buffer
47
+ @html_slice[slice_id] = buffer
41
48
  instance_eval(&block)
42
49
  buffer << wrap[1]
43
50
  end
44
51
 
45
- (@html_slice[@html_slice_current_id] || "").to_s
52
+ @html_slice[slice_id].to_s
46
53
  end
47
54
 
48
- TAGS.each do |name|
49
- define_method(name) { |*args, &block| tag(name, *args, &block) }
55
+ TAGS_WITHOUT_HTML_ESCAPE.each do |tag_name|
56
+ name_str = tag_name.to_s.freeze
57
+ define_method(name_str) do |*args, &block|
58
+ content, attrs = parse_args(args, escape: false)
59
+ render_tag(name_str, tag_name, content, attrs, &block)
60
+ end
50
61
  end
51
62
 
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)
63
+ TAGS.each do |tag_name|
64
+ name_str = tag_name.to_s.freeze
65
+ define_method(name_str) do |*args, &block|
66
+ content, attrs = parse_args(args, escape: true)
67
+ render_tag(name_str, tag_name, content, attrs, &block)
56
68
  end
57
69
  end
58
70
 
59
- def _(content)
60
- ensure_html_slice
61
- @html_slice[@html_slice_current_id] << content.to_s
71
+ def tag(tag_name, *args, &block)
72
+ content, attrs = parse_args(args, escape: true)
73
+ render_tag(tag_name.to_s, tag_name, content, attrs, &block)
62
74
  end
63
75
 
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)
76
+ def _(text)
77
+ @html_slice[@html_slice_current_id] << text.to_s
67
78
  end
68
79
 
69
80
  private
70
81
 
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)
82
+ def parse_args(args, escape: true)
78
83
  content = ""
79
84
  attributes = {}
80
-
81
- first = args.shift
85
+ first = args[0]
82
86
  if first.is_a?(String)
83
87
  content = escape && !first.empty? ? CGI.escapeHTML(first) : first
84
- attributes = args.pop || {}
88
+ attributes = args.size > 1 ? args[-1] : {}
85
89
  elsif first.is_a?(Hash)
86
90
  attributes = first
87
91
  end
88
-
89
92
  [content, attributes]
90
93
  end
91
94
 
92
- def generate_and_append_html_tag(tag_name, content, attributes, &block)
93
- ensure_html_slice
95
+ def render_tag(name_str, tag_sym, content, attributes, &block)
96
+ @html_slice ||= {}
97
+ @html_slice_current_id ||= DEFAULT_SLICE
98
+ @html_slice[@html_slice_current_id] ||= +""
94
99
  buffer = @html_slice[@html_slice_current_id]
95
- buffer << "<" << tag_name.to_s
96
100
 
101
+ buffer << "<" << name_str
97
102
  unless attributes.empty?
98
- attributes.each do |key, value|
99
- buffer << " " << key.to_s.tr("_", "-") << "='" << value.to_s << "'"
103
+ attributes_string = ATTRIBUTE_CACHE[attributes.dup] ||= begin
104
+ attributes.map do |key, value|
105
+ " #{key.to_s.tr('_', '-')}='#{value}'"
106
+ end.join
100
107
  end
108
+ buffer << attributes_string
101
109
  end
102
110
 
103
- if block
111
+ if block_given?
104
112
  buffer << ">"
105
- instance_eval(&block)
106
- buffer << "</" << tag_name.to_s << ">"
107
- elsif content.empty? && EMPTY_TAGS.include?(tag_name)
113
+ instance_exec(&block)
114
+ buffer << "</#{name_str}>"
115
+ elsif content.empty? && EMPTY_TAGS.include?(tag_sym)
108
116
  buffer << "/>"
109
117
  else
110
- buffer << ">" << content << "</" << tag_name.to_s << ">"
118
+ buffer << ">" << content << "</#{name_str}>"
111
119
  end
112
120
  end
113
121
  end
metadata CHANGED
@@ -1,13 +1,14 @@
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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - henrique-ft
8
+ autorequire:
8
9
  bindir: exe
9
10
  cert_chain: []
10
- date: 2025-04-21 00:00:00.000000000 Z
11
+ date: 2026-04-13 00:00:00.000000000 Z
11
12
  dependencies: []
12
13
  description: Enable Ruby classes the ability to generate reusable pieces of html
13
14
  email:
@@ -18,10 +19,12 @@ extra_rdoc_files: []
18
19
  files:
19
20
  - lib/html_slice.rb
20
21
  - lib/html_slice/version.rb
22
+ homepage:
21
23
  licenses: []
22
24
  metadata:
23
25
  source_code_uri: https://github.com/henrique-ft/html_slice
24
26
  changelog_uri: https://github.com/henrique-ft/html_slice/blob/master/CHANGELOG.md
27
+ post_install_message:
25
28
  rdoc_options: []
26
29
  require_paths:
27
30
  - lib
@@ -36,7 +39,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
39
  - !ruby/object:Gem::Version
37
40
  version: '0'
38
41
  requirements: []
39
- rubygems_version: 3.6.2
42
+ rubygems_version: 3.3.3
43
+ signing_key:
40
44
  specification_version: 4
41
45
  summary: Enable Ruby classes the ability to generate reusable pieces of html
42
46
  test_files: []