Class: Oga::XML::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_patch/oga_document.rb

Instance Method Summary collapse

Instance Method Details

#html_nodeObject



20
21
22
# File 'lib/monkey_patch/oga_document.rb', line 20

def html_node
  at_xpath('html')
end

#interlace(other, level:) ⇒ Object

Parameters:

  • other (Oga::XML::Document)

    other to interlace from. Note this will have side-effects on it, so be aware not to persist it.

  • level (String)

    CSS selector to interlace



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/monkey_patch/oga_document.rb', line 36

def interlace(other, level:)
  nodes = css(level).to_a
  nodes_other = other.css(level).to_a

  root_lang = lang
  root_lang_other = other.lang

  nodes.each.with_index do |node, i|
    node_other = nodes_other[i]

    node.lang = root_lang
    node_other.lang = root_lang_other

    next if node.text() == nodes_other[i].text()

    node.after(node_other)
  end
end

#langObject



24
25
26
# File 'lib/monkey_patch/oga_document.rb', line 24

def lang
  html_node&.lang
end

#lang=(lang) ⇒ Object

TODO: handle non-HTML



29
30
31
# File 'lib/monkey_patch/oga_document.rb', line 29

def lang=(lang)
  html_node&.lang = lang
end