code-ruby 1.7.8 → 1.8.0

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: 863226aa89094a7ba1eaff88e1dbb209e93a09149e12f60849ea90122fdcf750
4
- data.tar.gz: 84c2dbfcf70ba9ecd4bcbf5f7a5b9cabe1ccaaf2173d69648907b3da723b3968
3
+ metadata.gz: 93a5aca92120dbf487443b4cf2f88e3c407326246c38b059bd865f2db1fba0c3
4
+ data.tar.gz: bd1b6b84eb2adb4f63ce11ccdb81e7666feeece6339ba3cca9a84b33490f076b
5
5
  SHA512:
6
- metadata.gz: 8026a99f580dacac3bc10d30a97b0e3ee2a34d94e1a61773a0ed55f1b79981076255308fdf9aeabbafbdd77cde5ad884275d5b542f6b785937576f0b3bff8896
7
- data.tar.gz: b474157be88e84ef3661f17ba1b73447ec16f8c140d478e3def89f7471c57b3958f3d1a30dcbfd7391935628c4a0d4103dfab709df25cd44e3099dbc5b8ceac0
6
+ metadata.gz: eedb4f5942efc5ef5ea2f4ee28a1cac8f136d05a9f465949cc8d2b4ad81bdc8f4ef1008358dfcbabb3cf939cbdfe4662f817204a652fdbb1b393f9649a1b0ec9
7
+ data.tar.gz: '089de3a0fb083295533c6f5ea5024754eaa450e84201792416313dff0544638f75c12e13442714ae3461d8c239b679edb5b2c38300c555d4ded3316cb3f5fd0b'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (1.7.8)
4
+ code-ruby (1.8.0)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.7.8
1
+ 1.8.0
@@ -107,6 +107,10 @@ class Code
107
107
  end
108
108
  end + [Object.repeat]
109
109
  end
110
+
111
+ def code_to_string
112
+ String.new("<#{self.class.name} #{raw}>")
113
+ end
110
114
  end
111
115
  end
112
116
  end
@@ -178,11 +178,7 @@ class Code
178
178
  end
179
179
  when "Url"
180
180
  sig(args) { Object.repeat }
181
- if code_arguments.any?
182
- Url.new(*code_arguments.raw)
183
- else
184
- Class.new(Url)
185
- end
181
+ code_arguments.any? ? Url.new(*code_arguments.raw) : Class.new(Url)
186
182
  else
187
183
  code_context = code_context.code_lookup!(code_operator)
188
184
  code_result = code_context.code_fetch(code_operator)
@@ -3,6 +3,18 @@
3
3
  class Code
4
4
  class Object
5
5
  class Html < Object
6
+ TAGS = %w[
7
+ a abbr address area article aside audio b base bdi bdo blockquote body br
8
+ button canvas caption cite code col colgroup data datalist dd del details
9
+ dfn dialog div dl dt em embed fieldset figcaption figure footer form h1
10
+ h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd
11
+ label legend li link main map mark meta meter nav noscript object ol
12
+ optgroup option output p picture pre progress q rp rt ruby s samp script
13
+ section select slot small source span strong style sub summary sup table
14
+ tbody td template textarea tfoot th thead time title tr track u ul var
15
+ video wbr
16
+ ].freeze
17
+
6
18
  def initialize(*args, **_kargs, &_block)
7
19
  self.raw =
8
20
  if args.first.is_an?(Html)
@@ -18,20 +30,129 @@ class Code
18
30
  def self.call(**args)
19
31
  code_operator = args.fetch(:operator, nil).to_code
20
32
  code_arguments = args.fetch(:arguments, []).to_code
33
+ globals = multi_fetch(args, *GLOBALS)
21
34
 
22
35
  case code_operator.to_s
23
36
  when "escape"
24
37
  sig(args) { Object.maybe }
25
- code_escape(*code_arguments.raw)
38
+ code_escape(*code_arguments.raw, **globals)
39
+ when "join"
40
+ sig(args) { Object.repeat }
41
+ code_join(*code_arguments.raw, **globals)
42
+ when "text"
43
+ sig(args) { Object.maybe }
44
+ code_text(code_arguments.code_first, **globals)
45
+ when "raw"
46
+ sig(args) { Object.maybe }
47
+ code_raw(code_arguments.code_first, **globals)
26
48
  else
27
- super
49
+ if TAGS.include?(code_operator.to_s.downcase)
50
+ sig(args) { [Dictionary.maybe, Function.maybe] }
51
+ code_tag(code_operator, *code_arguments.raw, **globals)
52
+ else
53
+ super
54
+ end
55
+ end
56
+ end
57
+
58
+ def self.code_tag(name, attributes_or_function = {}, function = nil, **globals)
59
+ code_name = name.to_code
60
+
61
+ if attributes_or_function.is_a?(Function)
62
+ code_attributes = {}.to_code
63
+ code_function = attributes_or_function.to_code
64
+ else
65
+ code_attributes = attributes_or_function.to_code
66
+ code_function = function.to_code
67
+ end
68
+
69
+ fragment = Nokogiri::HTML::DocumentFragment.parse("")
70
+ node = Nokogiri::XML::Node.new(code_name.to_s.downcase, fragment.document)
71
+
72
+ code_attributes.raw.each do |code_key, code_value|
73
+ next if code_key.nothing?
74
+ next if code_value.nothing?
75
+
76
+ node[code_key.to_s] = code_value.to_s
77
+ end
78
+
79
+ if code_function.something?
80
+ code_content = code_function.call(
81
+ arguments: List.new([code_name, code_attributes]),
82
+ **globals
83
+ )
84
+
85
+ if code_content.is_an?(Html)
86
+ content = Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
87
+ else
88
+ content = Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
89
+ end
90
+
91
+ node.add_child(content)
28
92
  end
93
+
94
+ fragment.add_child(node)
95
+
96
+ Html.new(fragment)
29
97
  end
30
98
 
31
- def self.code_escape(string = nil)
32
- code_string = string.to_code
99
+ def self.code_escape(value_or_function = nil, **globals)
100
+ if value_or_function.is_a?(Function)
101
+ code_value = value_or_function.to_code.call(**globals)
102
+ else
103
+ code_value = value_or_function.to_code
104
+ end
33
105
 
34
- String.new(CGI.escapeHTML(string.to_s))
106
+ String.new(CGI.escapeHTML(value.to_s))
107
+ end
108
+
109
+ def self.code_join(*contents_or_function, **globals)
110
+ if contents_or_function.is_a?(Function)
111
+ code_contents = contents_or_function.to_code.call(**globals)
112
+ else
113
+ code_contents = contents_or_function.to_code
114
+ end
115
+
116
+ fragment = Nokogiri::HTML::DocumentFragment.parse("")
117
+
118
+ code_contents.raw.each do |code_content|
119
+ if code_content.is_an?(Html)
120
+ content = Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
121
+ else
122
+ content = Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
123
+ end
124
+
125
+ fragment.add_child(content)
126
+ end
127
+
128
+ Html.new(fragment)
129
+ end
130
+
131
+ def self.code_text(value_or_function = nil, **globals)
132
+ if value_or_function.is_a?(Function)
133
+ code_value = value_or_function.to_code.call(**globals)
134
+ else
135
+ code_value = value_or_function.to_code
136
+ end
137
+
138
+ fragment = Nokogiri::HTML::DocumentFragment.parse("")
139
+ fragment.add_child(Nokogiri::XML::Text.new(code_value.to_s, fragment.document))
140
+
141
+ Html.new(fragment)
142
+ end
143
+
144
+ def self.code_raw(value_or_function = nil, **globals)
145
+ if value_or_function.is_a?(Function)
146
+ code_value = value_or_function.to_code.call(**globals)
147
+ else
148
+ code_value = value_or_function.to_code
149
+ end
150
+
151
+ if code_value.is_an?(Html)
152
+ Html.new(Nokogiri::HTML::DocumentFragment.parse(code_value.to_html))
153
+ else
154
+ Html.new(Nokogiri::HTML::DocumentFragment.parse(code_value.to_s))
155
+ end
35
156
  end
36
157
 
37
158
  def call(**args)
@@ -53,6 +174,9 @@ class Code
53
174
  when "to_string"
54
175
  sig(args)
55
176
  code_to_string
177
+ when "to_html"
178
+ sig(args)
179
+ code_to_html
56
180
  else
57
181
  super
58
182
  end
@@ -89,6 +213,14 @@ class Code
89
213
  raw.text
90
214
  end
91
215
 
216
+ def to_html
217
+ raw.to_html
218
+ end
219
+
220
+ def code_to_html
221
+ String.new(raw.to_html)
222
+ end
223
+
92
224
  def code_to_string
93
225
  String.new(raw.text)
94
226
  end
data/lib/code/type/sig.rb CHANGED
@@ -134,6 +134,7 @@ class Code
134
134
 
135
135
  actual_arguments.each do |actual|
136
136
  expected = expected_arguments[expected_index]
137
+
137
138
  if expected.is_a?(Repeat)
138
139
  if valid_for?(expected: expected, actual: actual)
139
140
  repeat_index += 1
@@ -149,6 +150,8 @@ class Code
149
150
  elsif valid_for?(expected: expected, actual: actual)
150
151
  expected_index += 1
151
152
  repeat_index = 0
153
+ elsif expected.is_a?(Maybe)
154
+ repeat_index = 0
152
155
  else
153
156
  raise(
154
157
  Error,
data/spec/code_spec.rb CHANGED
@@ -374,6 +374,29 @@ RSpec.describe Code do
374
374
  ["a = [1, 2] a.reject!(&:even?) a", "[1]"],
375
375
  ["[1, 2].map(&:even?)", "[false, true]"],
376
376
  ["a = [1, 2] a.map!(&:even?) a", "[false, true]"],
377
+ ["Html.p { \"Hello\" }.to_html", "'<p>Hello</p>'"],
378
+ ["Html.br.to_html", "'<br>'"],
379
+ [
380
+ "Html.div { Html.div { Html.span { :Nested } } }.to_html",
381
+ "'<div><div><span>Nested</span></div></div>'"
382
+ ],
383
+ [
384
+ "Html.div { Html.p { Html.span { :hello } } }.to_html",
385
+ "'<div><p><span>hello</span></p></div>'"
386
+ ],
387
+ [
388
+ "Html.join(Html.p { :hello }, Html.p { :world }).to_html",
389
+ "'<p>hello</p><p>world</p>'"
390
+ ],
391
+ ["Html.join.to_html", "''"],
392
+ [
393
+ "Html.div { Html.text(\"<span>\") }.to_html",
394
+ "'<div>&lt;span&gt;</div>'"
395
+ ],
396
+ [
397
+ "Html.div { Html.raw(\"<span>ok</span>\") }.to_html",
398
+ "'<div><span>ok</span></div>'"
399
+ ],
377
400
  ["[1, 2, 3].any?", "true"],
378
401
  ["[1, 2, 3].any?(&:even?)", "true"],
379
402
  ["[1, 2, 3].none?", "false"],
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.8
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié