html-tag 3.0.1 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62429fba4dc4f58b599307927a448ac66ff140053a52f3641188cd9b1c261033
4
- data.tar.gz: e692ee406a52cdd0d7c4750dd0553768ede5614ea17ca55f9c9c9066d0832c4c
3
+ metadata.gz: 371d71aa213375cfe4bbdcddc096f98b456c394e566d7c848479ae515f5848ea
4
+ data.tar.gz: 7dc7321d44062b12aa20beb91595f215af56d088493486b92c81b68e2b27c764
5
5
  SHA512:
6
- metadata.gz: 44aba32e3255ac8c7f843ac14919dde2994f42168d5c8594c96d46233964d0f09567f72c34c6301a4b8d6655694a6e36506fe153fbc2c01b9aee2ada2d62d677
7
- data.tar.gz: f028fe5ee782953b8b6ad78774c2af0307774f069c050e1633c50e6d95886c31feb4d4272bac9cc7f514e12c44da1b913175c12f8824831c1c8575e025211c6b
6
+ metadata.gz: 81e54a74195a0b92fbd04753b05990c4f0f88db1dc7c229cb44a4f967c69b2f111011b206cb1f841be21e2993f26e5af3e7592050fa9883a9d87ff132f86dac9
7
+ data.tar.gz: 177f240dfd9524e1adacac728f469648b4610ab8bb87daff2862d1e11d3121110caf59d5725416e40a7780bad4ed1d9b34d0265dab6431526c9d9b3341b020e4
data/.version CHANGED
@@ -1 +1 @@
1
- 3.0.1
1
+ 3.0.3
@@ -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
@@ -40,10 +40,12 @@ def HtmlTag *args, &block
40
40
  end
41
41
 
42
42
  if block
43
+ # HtmlTag(:ul) { li ... }
43
44
  out = HtmlTag::Inbound.new self
44
45
  out.send(*args, &block)
45
46
  out.render
46
47
  else
47
- HtmlTag::Proxy.new
48
+ # HtmlTag._foo 123
49
+ HtmlTag::Proxy.new self
48
50
  end
49
51
  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
 
@@ -41,12 +43,15 @@ module HtmlTag
41
43
  @_iv.context = context
42
44
  @_iv.data = []
43
45
  @_iv.depth = 0
44
- @_iv.inbound = true
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
@@ -103,14 +108,18 @@ module HtmlTag
103
108
  if block
104
109
  @_iv.depth += 1
105
110
 
106
- if @_iv.context
111
+ block_data = if @_iv.context
107
112
  # HtmlTag scope
108
- instance_exec(&block)
113
+ instance_exec(self, &block)
109
114
  else
110
115
  # outbound scope
111
116
  block.call(self)
112
117
  end
113
118
 
119
+ if block_data.class == String
120
+ @_iv.data << block_data
121
+ end
122
+
114
123
  @_iv.depth -= 1
115
124
  end
116
125
 
@@ -121,11 +130,19 @@ module HtmlTag
121
130
  @_iv.data << _depth_spaces
122
131
  end
123
132
 
133
+ if opt_data.class == Array
134
+ opt_data = opt_data.join('')\
135
+ end
136
+
124
137
  @_iv.data << '%s</%s>%s' % [opt_data, name, _depth_new_line]
125
138
  end
126
139
  end
127
140
 
128
- def push data
141
+ def push data = nil
142
+ if block_given?
143
+ data = yield
144
+ end
145
+
129
146
  @_iv.data << data
130
147
  end
131
148
 
@@ -134,6 +151,8 @@ module HtmlTag
134
151
 
135
152
  if klass.start_with?('_')
136
153
  tag klass, *args, &block
154
+ elsif @_iv.context
155
+ @_iv.context.send name, *args, &block
137
156
  else
138
157
  message = [
139
158
  %{HTML tag "#{name}" not found.},
@@ -152,7 +171,7 @@ module HtmlTag
152
171
  # allow any arragement of vars
153
172
  # div class: :foo, 123
154
173
  # div 123, class: :foo
155
- if opt_hash && opt_hash.class != Hash
174
+ if opt_data.class == Hash || (opt_hash && opt_hash.class != Hash)
156
175
  opt_hash, opt_data = opt_data, opt_hash
157
176
  end
158
177
 
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.3
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-07-30 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