Class: Code::Object::Html

Inherits:
Code::Object show all
Defined in:
lib/code/object/html.rb

Constant Summary collapse

TAGS =
%w[
  a
  abbr
  address
  area
  article
  aside
  audio
  b
  base
  bdi
  bdo
  blockquote
  body
  br
  button
  canvas
  caption
  cite
  code
  col
  colgroup
  data
  datalist
  dd
  del
  details
  dfn
  dialog
  div
  dl
  dt
  em
  embed
  fieldset
  figcaption
  figure
  footer
  form
  h1
  h2
  h3
  h4
  h5
  h6
  head
  header
  hgroup
  hr
  html
  i
  iframe
  img
  input
  ins
  kbd
  label
  legend
  li
  link
  main
  map
  mark
  meta
  meter
  nav
  noscript
  object
  ol
  optgroup
  option
  output
  p
  picture
  pre
  progress
  q
  rp
  rt
  ruby
  s
  samp
  script
  section
  select
  slot
  small
  source
  span
  strong
  style
  sub
  summary
  sup
  table
  tbody
  td
  template
  textarea
  tfoot
  th
  thead
  time
  title
  tr
  track
  u
  ul
  var
  video
  wbr
].freeze

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes included from Concerns::Shared

#methods, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

code_new, #code_new, maybe, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #code_and, #code_as_json, #code_blank?, #code_compare, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, #code_fetch, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_inclusive_range, #code_inspect, #code_less, #code_less_or_equal, #code_methods, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_self, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #truthy?

Constructor Details

#initialize(*args, **_kargs, &_block) ⇒ Html

Returns a new instance of Html.



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/code/object/html.rb', line 119

def initialize(*args, **_kargs, &_block)
  self.raw =
    if args.first.is_an?(Html)
      args.first.raw
    elsif args.first.is_a?(::Nokogiri::XML::NodeSet) ||
          args.first.is_a?(Nokogiri::XML::Node)
      args.first
    else
      Nokogiri.HTML(args.first.to_s)
    end
end

Class Method Details

.call(**args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/code/object/html.rb', line 131

def self.call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  globals = multi_fetch(args, *GLOBALS)

  case code_operator.to_s
  when "escape"
    sig(args) { Object.maybe }
    code_escape(*code_arguments.raw, **globals)
  when "unescape"
    sig(args) { Object.maybe }
    code_unescape(*code_arguments.raw, **globals)
  when "join"
    sig(args) { [Object.maybe, Object.maybe] }
    code_join(*code_arguments.raw, **globals)
  when "text"
    sig(args) { Object.maybe }
    code_text(code_arguments.code_first, **globals)
  when "raw"
    sig(args) { Object.maybe }
    code_raw(code_arguments.code_first, **globals)
  else
    if TAGS.include?(code_operator.to_s.downcase)
      sig(args) { [Dictionary.maybe, Function.maybe] }
      code_tag(code_operator, *code_arguments.raw, **globals)
    else
      super
    end
  end
end

.code_escape(value_or_function = nil, **globals) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/code/object/html.rb', line 217

def self.code_escape(value_or_function = nil, **globals)
  code_value =
    if value_or_function.is_a?(Function)
      begin
        value_or_function.to_code.call(**globals)
      rescue Error::Next => e
        e.code_value
      end
    else
      value_or_function.to_code
    end

  String.new(CGI.escapeHTML(code_value.to_s))
rescue Error::Break => e
  e.code_value
end

.code_join(first = nil, second = nil, **globals) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/code/object/html.rb', line 251

def self.code_join(first = nil, second = nil, **globals)
  if second.is_a?(Function)
    code_contents =
      begin
        second.to_code.call(**globals)
      rescue Error::Next => e
        e.code_value
      end
    code_separator = first.to_code
  else
    code_contents = first.to_code
    code_separator = second.to_code
  end

  fragment = Nokogiri::HTML::DocumentFragment.parse("")

  return Html.new(fragment) if code_contents.nothing?
  return Html.new(fragment) unless code_contents.is_a?(List)

  code_contents.raw.each.with_index do |code_content, index|
    content =
      if code_content.is_an?(Html)
        Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
      else
        Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
      end

    separator =
      if code_separator.is_an?(Html)
        Nokogiri::HTML::DocumentFragment.parse(code_separator.to_html)
      else
        Nokogiri::XML::Text.new(code_separator.to_s, fragment.document)
      end

    fragment.add_child(separator) unless index.zero?
    fragment.add_child(content)
  end

  Html.new(fragment)
rescue Error::Break => e
  e.code_value
end

.code_raw(value_or_function = nil, **globals) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/code/object/html.rb', line 316

def self.code_raw(value_or_function = nil, **globals)
  code_value =
    if value_or_function.is_a?(Function)
      begin
        value_or_function.to_code.call(**globals)
      rescue Error::Next => e
        e.code_value
      end
    else
      value_or_function.to_code
    end

  if code_value.is_an?(Html)
    Html.new(Nokogiri::HTML::DocumentFragment.parse(code_value.to_html))
  else
    Html.new(Nokogiri::HTML::DocumentFragment.parse(code_value.to_s))
  end
rescue Error::Break => e
  e.code_value
end

.code_tag(name, attributes_or_function = {}, function = nil, **globals) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/code/object/html.rb', line 162

def self.code_tag(
  name,
  attributes_or_function = {},
  function = nil,
  **globals
)
  code_name = name.to_code

  if attributes_or_function.is_a?(Function)
    code_attributes = {}.to_code
    code_function = attributes_or_function.to_code
  else
    code_attributes = attributes_or_function.to_code
    code_function = function.to_code
  end

  fragment = Nokogiri::HTML::DocumentFragment.parse("")
  node =
    Nokogiri::XML::Node.new(code_name.to_s.downcase, fragment.document)

  code_attributes.raw.each do |code_key, code_value|
    next if code_key.nothing?
    next if code_value.nothing?

    node[code_key.to_s] = code_value.to_s
  end

  if code_function.something?
    code_content =
      begin
        code_function.call(
          arguments: List.new([code_name, code_attributes]),
          **globals
        )
      rescue Error::Next => e
        e.code_value
      end

    content =
      if code_content.is_an?(Html)
        Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
      else
        Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
      end

    node.add_child(content)
  end

  fragment.add_child(node)

  Html.new(fragment)
rescue Error::Break => e
  e.code_value
end

.code_text(value_or_function = nil, **globals) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/code/object/html.rb', line 294

def self.code_text(value_or_function = nil, **globals)
  code_value =
    if value_or_function.is_a?(Function)
      begin
        value_or_function.to_code.call(**globals)
      rescue Error::Next => e
        e.code_value
      end
    else
      value_or_function.to_code
    end

  fragment = Nokogiri::HTML::DocumentFragment.parse("")
  fragment.add_child(
    Nokogiri::XML::Text.new(code_value.to_s, fragment.document)
  )

  Html.new(fragment)
rescue Error::Break => e
  e.code_value
end

.code_unescape(value_or_function = nil, **globals) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/code/object/html.rb', line 234

def self.code_unescape(value_or_function = nil, **globals)
  code_value =
    if value_or_function.is_a?(Function)
      begin
        value_or_function.to_code.call(**globals)
      rescue Error::Next => e
        e.code_value
      end
    else
      value_or_function.to_code
    end

  String.new(Nokogiri::HTML.fragment(code_value.to_s).text)
rescue Error::Break => e
  e.code_value
end

Instance Method Details

#call(**args) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/code/object/html.rb', line 337

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first
  globals = multi_fetch(args, *GLOBALS)

  case code_operator.to_s
  when "css"
    sig(args) { String }
    code_css(code_value)
  when "at_css"
    sig(args) { String }
    code_at_css(code_value)
  when "map"
    sig(args) { Function }
    code_map(code_value, **globals)
  when "to_string"
    sig(args)
    code_to_string
  when "to_html"
    sig(args)
    code_to_html
  when "attribute"
    sig(args) { String }
    code_attribute(code_value)
  else
    super
  end
end

#code_at_css(query) ⇒ Object



373
374
375
376
377
# File 'lib/code/object/html.rb', line 373

def code_at_css(query)
  code_query = query.to_code

  Html.new(raw.at_css(code_query.raw))
end

#code_attribute(value = nil) ⇒ Object



412
413
414
415
# File 'lib/code/object/html.rb', line 412

def code_attribute(value = nil)
  code_value = value.to_code
  String.new(raw.attr(code_value.to_s))
end

#code_css(query) ⇒ Object



367
368
369
370
371
# File 'lib/code/object/html.rb', line 367

def code_css(query)
  code_query = query.to_code

  Html.new(raw.css(code_query.raw))
end

#code_map(argument, **globals) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/code/object/html.rb', line 379

def code_map(argument, **globals)
  code_argument = argument.to_code

  List.new(
    raw.map.with_index do |element, index|
      code_argument.call(
        arguments: List.new([element.to_code, Integer.new(index), self]),
        **globals
      )
    rescue Error::Next => e
      e.code_value
    end
  )
rescue Error::Break => e
  e.code_value
end

#code_to_htmlObject



404
405
406
# File 'lib/code/object/html.rb', line 404

def code_to_html
  String.new(raw.to_html)
end

#code_to_stringObject



408
409
410
# File 'lib/code/object/html.rb', line 408

def code_to_string
  String.new(raw.text)
end

#to_htmlObject



400
401
402
# File 'lib/code/object/html.rb', line 400

def to_html
  raw.to_html
end

#to_sObject



396
397
398
# File 'lib/code/object/html.rb', line 396

def to_s
  raw.text
end