Class: Code::Object::Html

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

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #as_json, as_json, #code_and_operator, code_and_operator, code_as_json, #code_as_json, code_different, #code_different, code_equal_equal, #code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, code_self, #code_self, code_to_json, #code_to_json, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, #to_code, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

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

Returns a new instance of Html.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/code/object/html.rb', line 6

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

Class Method Details

.call(**args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/code/object/html.rb', line 19

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

  case code_operator.to_s
  when "link_to"
    sig(args) { [Object.maybe, Object.maybe] }
    code_link_to(*code_arguments.raw)
  when "escape"
    sig(args) { Object.maybe }
    code_escape(*code_arguments.raw)
  else
    super
  end
end

.code_escape(string = nil) ⇒ Object



46
47
48
49
50
# File 'lib/code/object/html.rb', line 46

def self.code_escape(string = nil)
  code_string = string.to_code

  String.new(CGI.escapeHTML(string.to_s))
end


35
36
37
38
39
40
41
42
43
44
# File 'lib/code/object/html.rb', line 35

def self.code_link_to(text = nil, href = nil)
  code_text = text.to_code
  code_href = href.to_code

  String.new("    <a\n      href=\"\#{CGI.escapeHTML(code_href.to_s)}\"\n    >\#{CGI.escapeHTML(code_text.to_s)}</a>\n  LINK\nend\n".strip)

Instance Method Details

#call(**args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/code/object/html.rb', line 52

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 "map"
    sig(args) { Function }
    code_map(code_value, **globals)
  when "to_string"
    sig(args)
    code_to_string
  else
    super
  end
end

#code_css(query) ⇒ Object



73
74
75
76
77
# File 'lib/code/object/html.rb', line 73

def code_css(query)
  code_query = query.to_code

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

#code_map(argument, **globals) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/code/object/html.rb', line 79

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
  )
end

#code_to_stringObject



94
95
96
# File 'lib/code/object/html.rb', line 94

def code_to_string
  String.new(raw.text)
end