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 +4 -4
- data/lib/html_slice/version.rb +1 -1
- data/lib/html_slice.rb +45 -47
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 47ec58956a24443617b6ec8ace88ef81b7bcbe835bef1708995b824f8080645c
|
|
4
|
+
data.tar.gz: ee26639d56eb776d55ab9284833ff92fe681383c410a6e5f0c7d7c8cf8cc2af7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aa31246b60c626d96704dcc4bc358dafbeef40a9e68f6b59afad184bc7127f13be3e5f9dd17b0daa7b999d95147aa19a238e864ff710ebc7b5f14d686deb763a
|
|
7
|
+
data.tar.gz: 653305df476ebde0cf5e370eb0fb43a8b4e8e329dcf1319a8271d7c1f4edc8fecb04630ee6b676851a5d113ed55ea774ba8fbddc75cb605f9582eb18ef34bb95
|
data/lib/html_slice/version.rb
CHANGED
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
|
-
].
|
|
17
|
+
].freeze
|
|
18
18
|
|
|
19
19
|
TAGS_WITHOUT_HTML_ESCAPE = %i[
|
|
20
20
|
style script
|
|
21
|
-
].
|
|
21
|
+
].freeze
|
|
22
22
|
|
|
23
23
|
DEFAULT_SLICE = :default
|
|
24
|
+
ATTRIBUTE_CACHE = {}
|
|
24
25
|
|
|
25
|
-
def html_layout(
|
|
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(
|
|
34
|
-
@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
|
|
38
|
-
buffer =
|
|
34
|
+
if block_given?
|
|
35
|
+
buffer = +""
|
|
39
36
|
buffer << wrap[0]
|
|
40
|
-
@html_slice[
|
|
37
|
+
@html_slice[slice_id] = buffer
|
|
41
38
|
instance_eval(&block)
|
|
42
39
|
buffer << wrap[1]
|
|
43
40
|
end
|
|
44
41
|
|
|
45
|
-
|
|
42
|
+
@html_slice[slice_id].to_s
|
|
46
43
|
end
|
|
47
44
|
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
|
60
|
-
|
|
61
|
-
|
|
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
|
|
65
|
-
|
|
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
|
|
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.
|
|
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
|
|
93
|
-
|
|
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.
|
|
99
|
-
|
|
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
|
|
101
|
+
if block_given?
|
|
104
102
|
buffer << ">"
|
|
105
|
-
|
|
106
|
-
buffer << "
|
|
107
|
-
elsif content.empty? && EMPTY_TAGS.include?(
|
|
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 << "
|
|
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.
|
|
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-
|
|
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.
|
|
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: []
|