html_slice 0.2.1 → 0.2.2

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: 677ccd64e0e49f6488478cf3f432fa19a52d39b053310f8ff8fc95f0e838d664
4
+ data.tar.gz: dae8024850dde1d0ebe919496d163975aa7b94421eb7731df7ddcc7f8acd529e
5
5
  SHA512:
6
- metadata.gz: 76107691df8555a630e8e65ba53fa7fe7926f536cf63a5c48defef81049e2f6916a104cafcbfed02be54801d4be663fdc44ab1c7d650caa91aeb6ec9e09927e5
7
- data.tar.gz: d056915ccba22cc4c4fce0a6dc0b0c743a0dec87e67bf8e2092d80e6bf125388bf8b7368e611d90655f5f860e5a901902f270fe09b08b5dba8d3247fc60c75c1
6
+ metadata.gz: bfd1dbadb0fccfbb128659a3d55ea489c15a26dd1721dbb864c0799af4ade39aaa2ff304cac374ec287bed53fc06fdc765af2309830cc7d4cdc1c8badcfd1a6f
7
+ data.tar.gz: fd182ec12728971df04a0f5853faac255bbadba9f302640fe1aa1b4de86282b7454f05a2b4d2e0f14f7c1dfd8550fdd2217e9ce83e289078fcdef27ad3f44e5d
@@ -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.2"
5
5
  end
data/lib/html_slice.rb CHANGED
@@ -5,65 +5,19 @@ 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
16
+ area br embed hr img input link meta source
17
+ ].freeze
18
+
19
+ TAGS_WITHOUT_HTML_ESCAPE = %i[
20
+ style script
67
21
  ].freeze
68
22
 
69
23
  DEFAULT_SLICE = :default
@@ -78,23 +32,30 @@ 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
38
  @html_slice[@html_slice_current_id] = wrap[0].dup
84
39
  instance_eval(&block)
85
40
  @html_slice[@html_slice_current_id] << wrap[1]
86
- else
87
- @html_slice[@html_slice_current_id] || ""
88
41
  end
42
+
43
+ @html_slice[@html_slice_current_id] || ""
89
44
  end
90
45
 
91
46
  TAGS.each do |name|
92
- define_method name do |*args, &block|
93
- tag(name, *args, &block)
47
+ define_method(name) { |*args, &block| tag(name, *args, &block) }
48
+ end
49
+
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)
94
54
  end
95
55
  end
96
56
 
97
57
  def _(content)
58
+ ensure_html_slice
98
59
  @html_slice[@html_slice_current_id] << content.to_s
99
60
  end
100
61
 
@@ -105,26 +66,30 @@ module HtmlSlice
105
66
 
106
67
  private
107
68
 
108
- def parse_html_tag_arguments(args)
109
- content = ""
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 = +""
110
77
  attributes = {}
111
78
 
112
- first_argument = args.shift
113
- if first_argument.is_a?(String)
114
- content = ::CGI.escapeHTML(first_argument)
79
+ first = args.shift
80
+ if first.is_a?(String)
81
+ content = escape ? CGI.escapeHTML(first) : first
115
82
  attributes = args.pop || {}
116
- elsif first_argument.is_a?(Hash)
117
- attributes = first_argument
83
+ elsif first.is_a?(Hash)
84
+ attributes = first
118
85
  end
119
86
 
120
87
  [content, attributes]
121
88
  end
122
89
 
123
90
  def generate_and_append_html_tag(tag_name, content, attributes, &block)
91
+ ensure_html_slice
124
92
  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] ||= +""
128
93
 
129
94
  if block
130
95
  @html_slice[@html_slice_current_id] << open_tag << ">"
@@ -138,10 +103,12 @@ module HtmlSlice
138
103
  end
139
104
 
140
105
  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}'"
144
- end
145
- open_tag
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}"
146
113
  end
147
114
  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.2
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-06 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: []