code-ruby 1.7.8 → 1.8.1

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: d066f06aab2a2ae00bcbcbeedea154305c0fbc19f920ca13cb4cef1d787a1884
4
+ data.tar.gz: eca3f011e3bd8811743a3412947fff667c8c1e0f5838ccc8c4e7d3e2360ab6e8
5
5
  SHA512:
6
- metadata.gz: 8026a99f580dacac3bc10d30a97b0e3ee2a34d94e1a61773a0ed55f1b79981076255308fdf9aeabbafbdd77cde5ad884275d5b542f6b785937576f0b3bff8896
7
- data.tar.gz: b474157be88e84ef3661f17ba1b73447ec16f8c140d478e3def89f7471c57b3958f3d1a30dcbfd7391935628c4a0d4103dfab709df25cd44e3099dbc5b8ceac0
6
+ metadata.gz: bdc06dddc2a05ce5c88f8dabfa25eb8b3a44811f94d99dcb65011dc8ed05daeabcbc68a2431d3705ec90435838f13fe88663a6cd4299f1dbb1da454972694c57
7
+ data.tar.gz: 0cc4c89d00285ff43b96916c05855dbd74671c001b115dcf4a303323af6f563b2b813e872080cc115f4a26f3b5cc30f812011e191044ae70d94125b7ae6cd5b6
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.1)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.7.8
1
+ 1.8.1
@@ -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,141 @@ 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.maybe, Object.maybe] }
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(first = nil, second = nil, **globals)
110
+ if second.is_a?(Function)
111
+ code_contents = second.to_code.call(**globals)
112
+ code_separator = first.to_code
113
+ else
114
+ code_contents = first.to_code
115
+ code_separator = second.to_code
116
+ end
117
+
118
+ fragment = Nokogiri::HTML::DocumentFragment.parse("")
119
+
120
+ return Html.new(fragment) if code_contents.nothing?
121
+ return Html.new(fragment) unless code_contents.is_a?(List)
122
+
123
+ code_contents.raw.each.with_index do |code_content, index|
124
+ if code_content.is_an?(Html)
125
+ content = Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
126
+ else
127
+ content = Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
128
+ end
129
+
130
+ if code_separator.is_an?(Html)
131
+ separator = Nokogiri::HTML::DocumentFragment.parse(code_separator.to_html)
132
+ else
133
+ separator = Nokogiri::XML::Text.new(code_separator.to_s, fragment.document)
134
+ end
135
+
136
+ fragment.add_child(separator) unless index.zero?
137
+ fragment.add_child(content)
138
+ end
139
+
140
+ Html.new(fragment)
141
+ end
142
+
143
+ def self.code_text(value_or_function = nil, **globals)
144
+ if value_or_function.is_a?(Function)
145
+ code_value = value_or_function.to_code.call(**globals)
146
+ else
147
+ code_value = value_or_function.to_code
148
+ end
149
+
150
+ fragment = Nokogiri::HTML::DocumentFragment.parse("")
151
+ fragment.add_child(Nokogiri::XML::Text.new(code_value.to_s, fragment.document))
152
+
153
+ Html.new(fragment)
154
+ end
155
+
156
+ def self.code_raw(value_or_function = nil, **globals)
157
+ if value_or_function.is_a?(Function)
158
+ code_value = value_or_function.to_code.call(**globals)
159
+ else
160
+ code_value = value_or_function.to_code
161
+ end
162
+
163
+ if code_value.is_an?(Html)
164
+ Html.new(Nokogiri::HTML::DocumentFragment.parse(code_value.to_html))
165
+ else
166
+ Html.new(Nokogiri::HTML::DocumentFragment.parse(code_value.to_s))
167
+ end
35
168
  end
36
169
 
37
170
  def call(**args)
@@ -53,6 +186,9 @@ class Code
53
186
  when "to_string"
54
187
  sig(args)
55
188
  code_to_string
189
+ when "to_html"
190
+ sig(args)
191
+ code_to_html
56
192
  else
57
193
  super
58
194
  end
@@ -89,6 +225,14 @@ class Code
89
225
  raw.text
90
226
  end
91
227
 
228
+ def to_html
229
+ raw.to_html
230
+ end
231
+
232
+ def code_to_html
233
+ String.new(raw.to_html)
234
+ end
235
+
92
236
  def code_to_string
93
237
  String.new(raw.text)
94
238
  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 }], Html.br).to_html",
389
+ "'<p>hello</p><br><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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié