html-tag 3.0.1 → 3.0.6

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: 62429fba4dc4f58b599307927a448ac66ff140053a52f3641188cd9b1c261033
4
- data.tar.gz: e692ee406a52cdd0d7c4750dd0553768ede5614ea17ca55f9c9c9066d0832c4c
3
+ metadata.gz: 6eaa9e68ea5e104f1daf2c3d36b8e64835ade4e33efa9b662aa2c5da5555c482
4
+ data.tar.gz: 6f48a76f0f4aeb814b9a98f5864bcac91a6b3758f4b015056e8d41876b9bc94d
5
5
  SHA512:
6
- metadata.gz: 44aba32e3255ac8c7f843ac14919dde2994f42168d5c8594c96d46233964d0f09567f72c34c6301a4b8d6655694a6e36506fe153fbc2c01b9aee2ada2d62d677
7
- data.tar.gz: f028fe5ee782953b8b6ad78774c2af0307774f069c050e1633c50e6d95886c31feb4d4272bac9cc7f514e12c44da1b913175c12f8824831c1c8575e025211c6b
6
+ metadata.gz: 936d7c82a1fbe23b232136833f1003b4af0c450f31e2f23d5d9683678b77070f1de8341dd8f0daf672676bb42a91a4dc64ea694688500d87d099918178ccd935
7
+ data.tar.gz: 95b4334aeb8a0286875b9638a2486ed8d6eaa87cf61525151aa6c2fcefd4b9deced6c1bb58b0bdd5fdf1f142ee33a8beeb8d193412146d6a3c892e98bfcc9cff
data/.version CHANGED
@@ -1 +1 @@
1
- 3.0.1
1
+ 3.0.6
@@ -1,8 +1,8 @@
1
1
  # Hash
2
2
  unless {}.respond_to?(:tag)
3
3
  class Hash
4
- def tag node_name, inner_html=nil
5
- HtmlTag().send node_name, self, inner_html
4
+ def tag node_name, inner_html=nil, &block
5
+ HtmlTag::Inbound.new.tag(node_name, inner_html, self, &block).join('')
6
6
  end
7
7
  end
8
8
  end
@@ -10,8 +10,8 @@ end
10
10
  # String
11
11
  unless ''.respond_to?(:tag)
12
12
  class String
13
- def tag node_name, opts = nil
14
- HtmlTag().send node_name, self, opts
13
+ def tag node_name, opts = nil, &block
14
+ HtmlTag::Inbound.new.tag(node_name, self, opts, &block).join('')
15
15
  end
16
16
  end
17
17
  end
@@ -19,8 +19,8 @@ end
19
19
  # HtmlTag do ...
20
20
  module HtmlTag
21
21
  class Proxy
22
- def initialize
23
- @pointer = HtmlTag::Inbound.new
22
+ def initialize scope = nil
23
+ @pointer = HtmlTag::Inbound.new scope
24
24
  end
25
25
 
26
26
  def method_missing name, *args, &block
@@ -32,18 +32,30 @@ module HtmlTag
32
32
  end
33
33
 
34
34
  def HtmlTag *args, &block
35
- args[0] ||= :div
35
+ if [Class, Module].include?(args[0].class)
36
+ # imports tag method without poluting ancesstors namespace
37
+ # class SomeClass
38
+ # HtmlTag self
39
+ args[0].define_method :tag do |*tag_args, &tag_block|
40
+ HtmlTag *tag_args, &tag_block
41
+ end
42
+ else
43
+ # HtmlTag do ...
44
+ args[0] ||= :div
36
45
 
37
- if args[0].class == Hash
38
- args[1] = args[0]
39
- args[0] = :div
40
- end
46
+ if args[0].class == Hash
47
+ args[1] = args[0]
48
+ args[0] = :div
49
+ end
41
50
 
42
- if block
43
- out = HtmlTag::Inbound.new self
44
- out.send(*args, &block)
45
- out.render
46
- else
47
- HtmlTag::Proxy.new
51
+ if block
52
+ # HtmlTag(:ul) { li ... }
53
+ out = HtmlTag::Inbound.new self
54
+ out.send(*args, &block)
55
+ out.render
56
+ else
57
+ # HtmlTag._foo 123
58
+ HtmlTag::Proxy.new self
59
+ end
48
60
  end
49
61
  end
@@ -11,28 +11,15 @@ module HtmlTag
11
11
  format: false
12
12
  }
13
13
 
14
- TAGS ||= Set.new %i(
15
- a b button code colgroup dd div dl dt em fieldset form h1 h2 h3 h4 h5 h6
16
- header i iframe label legend li main map nav noscript object ol optgroup option p pre q
17
- script section select small span strong summary table tbody td textarea tfoot th thead title tr u ul video
18
- )
19
-
20
- EMPTY_TAGS ||= Set.new %w(area base br col embed hr img input keygen link meta param source track wbr)
21
-
22
14
  def tag *args, &block
23
- if block
24
- HtmlTag *args, &block
25
- else
26
- # HtmlTag()
27
- ::HtmlTag::Outbound
28
- end
15
+ HtmlTag *args, &block
29
16
  end
30
17
 
31
18
  # forward to class only if
32
19
  def method_missing tag_name, *args, &block
33
20
  if self === HtmlTag
34
- # Proxy.new.tag(tag_name, args[0], args[1], &block)
35
- Outbound.tag(tag_name, args[0], args[1], &block)
21
+ # Outbound.tag(tag_name, args[0], args[1], &block)
22
+ Proxy.new.tag(tag_name, args[0], args[1], &block)
36
23
  else
37
24
  super
38
25
  end
@@ -1,24 +1,26 @@
1
- # improved builder that is not pasing node pointers
2
-
3
- # HtmlTag do
4
- # form do
5
- # input name: q
6
- # end
7
- # end
1
+ # improved HTML builder that doess not need node pointers
8
2
 
9
3
  module HtmlTag
10
4
  class Inbound
11
- IVARS ||= Struct.new :HtmlTagInboundIvars, :context, :data, :depth, :inbound
5
+ IVARS ||= Struct.new :HtmlTagInboundIvars, :context, :data, :depth
6
+
7
+ TAGS ||= Set.new %i(
8
+ a article b button code center colgroup dd div dl dt em fieldset form h1 h2 h3 h4 h5 h6
9
+ header i iframe label legend li main map nav noscript object ol optgroup option p pre q
10
+ script section select small span sub strong style summary table tbody td textarea tfoot th thead title tr u ul video
11
+ )
12
+
13
+ EMPTY_TAGS ||= Set.new %w(area base br col embed hr img input keygen link meta param source track wbr)
12
14
 
13
15
  # allows to add cusom tags if needed
14
16
  # HtmlTag::Inbound.define :foo
15
- def self.define tag, empty: false
17
+ def self.define name, empty: false
16
18
  if empty
17
- EMPTY_TAGS.add tag
19
+ EMPTY_TAGS.add name
18
20
  end
19
21
 
20
- define_method tag do |*args, &block|
21
- tag tag, *args, &block
22
+ define_method name do |*args, &block|
23
+ tag name, *args, &block
22
24
  end
23
25
  end
24
26
 
@@ -38,15 +40,18 @@ module HtmlTag
38
40
 
39
41
  # lets keep all instance vars in one object
40
42
  @_iv = IVARS.new
41
- @_iv.context = context
42
- @_iv.data = []
43
- @_iv.depth = 0
44
- @_iv.inbound = true
43
+ @_iv.context = context
44
+ @_iv.data = []
45
+ @_iv.depth = 0
45
46
  end
46
47
 
47
48
  # access parent context via parent / context / this
48
49
  # h1 class: this.class_name
49
50
  def parent &block
51
+ unless @_iv.context
52
+ raise 'Host scope is not available'
53
+ end
54
+
50
55
  if block
51
56
  @_iv.context.instance_exec(&block)
52
57
  else
@@ -61,7 +66,7 @@ module HtmlTag
61
66
  @_iv.data
62
67
  .join('')
63
68
  .gsub(/\n+/, $/)
64
- .gsub(/([\w>])[[:blank:]]+</, '\1<')
69
+ #.gsub(/([\w>])[[:blank:]]+</, '\1<')
65
70
  end
66
71
 
67
72
  # render single node
@@ -78,7 +83,7 @@ module HtmlTag
78
83
 
79
84
  if value.class == Hash
80
85
  for el in value
81
- t.push "%s-%s='%s'" % [key, el[0], _escape_param(el[1])]
86
+ t.push '%s-%s=%s' % [key, el[0], _escape_param(el[1])]
82
87
  end
83
88
  else
84
89
  if value.class == Array
@@ -87,7 +92,7 @@ module HtmlTag
87
92
 
88
93
  key = key.to_s.sub(/^data_/, 'data-')
89
94
 
90
- t.push "%s='%s'" % [key, _escape_param(value)]
95
+ t.push '%s=%s' % [key, _escape_param(value)]
91
96
  end
92
97
  t
93
98
  end.join(' ')
@@ -102,15 +107,20 @@ module HtmlTag
102
107
  # nested blocks
103
108
  if block
104
109
  @_iv.depth += 1
110
+ node_count = @_iv.data.length
105
111
 
106
- if @_iv.context
112
+ block_data = if @_iv.context
107
113
  # HtmlTag scope
108
- instance_exec(&block)
114
+ instance_exec(self, &block)
109
115
  else
110
116
  # outbound scope
111
117
  block.call(self)
112
118
  end
113
119
 
120
+ if block_data.class == String && node_count == @_iv.data.length
121
+ @_iv.data << block_data
122
+ end
123
+
114
124
  @_iv.depth -= 1
115
125
  end
116
126
 
@@ -121,11 +131,19 @@ module HtmlTag
121
131
  @_iv.data << _depth_spaces
122
132
  end
123
133
 
134
+ if opt_data.class == Array
135
+ opt_data = opt_data.join('')\
136
+ end
137
+
124
138
  @_iv.data << '%s</%s>%s' % [opt_data, name, _depth_new_line]
125
139
  end
126
140
  end
127
141
 
128
- def push data
142
+ def push data = nil
143
+ if block_given?
144
+ data = yield
145
+ end
146
+
129
147
  @_iv.data << data
130
148
  end
131
149
 
@@ -134,6 +152,8 @@ module HtmlTag
134
152
 
135
153
  if klass.start_with?('_')
136
154
  tag klass, *args, &block
155
+ elsif @_iv.context
156
+ @_iv.context.send name, *args, &block
137
157
  else
138
158
  message = [
139
159
  %{HTML tag "#{name}" not found.},
@@ -152,7 +172,7 @@ module HtmlTag
152
172
  # allow any arragement of vars
153
173
  # div class: :foo, 123
154
174
  # div 123, class: :foo
155
- if opt_hash && opt_hash.class != Hash
175
+ if opt_data.class == Hash || (opt_hash && opt_hash.class != Hash)
156
176
  opt_hash, opt_data = opt_data, opt_hash
157
177
  end
158
178
 
@@ -187,7 +207,16 @@ module HtmlTag
187
207
  end
188
208
 
189
209
  def _escape_param el
190
- el.to_s.gsub(/'/, '&apos;')
210
+ data = el.to_s
211
+
212
+ if data.include?('"')
213
+ # if we dump json export to a tag attribute, there will be a lot of qutes
214
+ # it is much more readable to use quot(') insted of quote (") in this case
215
+ "'%s'" % data.gsub(/'/, '&apos;')
216
+ else
217
+ # regualr node
218
+ '"%s"' % data
219
+ end
191
220
  end
192
221
  end
193
222
  end
data/lib/html-tag.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require_relative './html-tag/html_tag'
2
- require_relative './html-tag/outbound'
3
2
  require_relative './html-tag/inbound'
4
3
  require_relative './html-tag/globals'
5
4
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-tag
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dino Reic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-29 00:00:00.000000000 Z
11
+ date: 2022-08-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fast and powerful tag builder, upgrade to Rails tag helper, framework
14
14
  agnostic.
@@ -22,7 +22,6 @@ files:
22
22
  - "./lib/html-tag/globals.rb"
23
23
  - "./lib/html-tag/html_tag.rb"
24
24
  - "./lib/html-tag/inbound.rb"
25
- - "./lib/html-tag/outbound.rb"
26
25
  homepage: https://github.com/dux/html-tag
27
26
  licenses:
28
27
  - MIT
@@ -1,127 +0,0 @@
1
- module HtmlTag
2
- class Outbound
3
- class << self
4
- # tag.div -> tag.tag :div
5
- def method_missing name, *args, &block
6
- tag name, args[0], args[1], &block
7
- end
8
-
9
- # tag :div, { 'class'=>'iform' } do
10
- def tag name, *args
11
- data, opts =
12
- if args[1]
13
- args[0].class == Hash ? args.reverse : args
14
- elsif args[0]
15
- if args[0].class == Symbol
16
- # tag.div(:a) { 1 } -> <div class="a">1</div>
17
- [nil, { class: args[0].to_s.gsub('__', ' ').gsub('_', '-') }]
18
- elsif args[0].class == Hash
19
- [nil, args[0]]
20
- else
21
- [args[0], {}]
22
- end
23
- else
24
- [nil, {}]
25
- end
26
-
27
- opts ||= {}
28
-
29
- unless opts.class == Hash
30
- raise ArgumentError.new('HtmlTag: bad agrument, attributes are no a hash')
31
- end
32
-
33
- if data.class == Hash
34
- raise ArgumentError.new('HtmlTag: bad agrument, data sent as hash')
35
- end
36
-
37
- # covert n._row_foo to n(class: 'row-foo')
38
- name = name.to_s
39
- if name.to_s[0, 1] == '_'
40
- classes = name
41
- .sub('_', '')
42
- .split('__')
43
- .map{|it| it.gsub('_', '-') }
44
- .join(' ')
45
-
46
- opts ||= {}
47
- opts[:class] = "#{classes} #{opts[:class]}".sub(/\s+$/, '')
48
- name = :div
49
- end
50
-
51
- if block_given?
52
- if data
53
- raise ArgumentError.new('HtmlTag: data is allreay defined and block is given')
54
- end
55
-
56
- stack = new
57
- data = yield(stack, opts)
58
-
59
- # if data is pushed to passed node, use that data
60
- data = stack.data if stack.data.first
61
- end
62
-
63
- data = data.join('') if data.is_a?(Array)
64
-
65
- build name, opts, data
66
- end
67
-
68
- # build html node
69
- def build node, attrs={}, text=nil
70
- node = node.to_s
71
-
72
- opts = []
73
- attrs.each do |attr_key, attr_value|
74
- if attr_value.is_a?(Hash)
75
- for data_key, data_value in attr_value
76
- __add_opts opts, "#{attr_key}-#{data_key}", data_value
77
- end
78
- else
79
- __add_opts opts, attr_key, attr_value
80
- end
81
- end
82
-
83
- opts = opts.first ? ' '+opts.join(' ') : ''
84
-
85
- if node
86
- text ||= '' unless EMPTY_TAGS.include?(node)
87
- text ? %{<#{node}#{opts}>#{text}</#{node}>} : %{<#{node}#{opts} />}
88
- else
89
- opts
90
- end
91
- end
92
-
93
- # tag.div(class: 'klasa') do -> tag.('klasa') do
94
- def call class_name, &block
95
- tag(:div, class_name, &block)
96
- end
97
-
98
- def __add_opts opts, key, value
99
- unless value.to_s == ''
100
- value = value.join(' ') if value.is_a?(Array)
101
- key = key.to_s.gsub(/data_/,'data-')
102
- opts.push key+'="'+value.to_s.gsub(/"/,'&quot;')+'"'
103
- end
104
- end
105
- end
106
-
107
- ###
108
-
109
- attr_reader :data
110
-
111
- def initialize
112
- @data = []
113
- end
114
-
115
- # push data to stack
116
- def push data=nil
117
- @data.push block_given? ? yield : data
118
- end
119
-
120
- private
121
-
122
- # forward to class
123
- def method_missing tag_name, *args, &block
124
- @data.push self.class.tag(tag_name, args[0], args[1], &block)
125
- end
126
- end
127
- end