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 +4 -4
- data/lib/html_slice/version.rb +1 -1
- data/lib/html_slice.rb +54 -57
- 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
|
@@ -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(
|
|
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
|
-
|
|
34
|
+
if block_given?
|
|
35
|
+
buffer = +""
|
|
36
|
+
buffer << wrap[0]
|
|
37
|
+
@html_slice[slice_id] = buffer
|
|
39
38
|
instance_eval(&block)
|
|
40
|
-
|
|
39
|
+
buffer << wrap[1]
|
|
41
40
|
end
|
|
42
41
|
|
|
43
|
-
@html_slice[
|
|
42
|
+
@html_slice[slice_id].to_s
|
|
44
43
|
end
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
|
58
|
-
|
|
59
|
-
|
|
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
|
|
63
|
-
|
|
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
|
|
70
|
-
|
|
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.
|
|
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
|
|
91
|
-
|
|
92
|
-
|
|
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
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
elsif content.empty? && EMPTY_TAGS.include?(
|
|
99
|
-
|
|
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
|
-
|
|
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.
|
|
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: []
|