Class: Evertils::Helper::ApiEnmlHandler

Inherits:
Object
  • Object
show all
Includes:
Nokogiri::XML
Defined in:
lib/evertils/helpers/api-enml-handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ ApiEnmlHandler

Returns a new instance of ApiEnmlHandler.

Since:

  • 0.3.7



8
9
10
11
# File 'lib/evertils/helpers/api-enml-handler.rb', line 8

def initialize(conf)
  @config = conf
  self
end

Instance Method Details

#clear_emptyObject

Since:

  • 0.3.15



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/evertils/helpers/api-enml-handler.rb', line 45

def clear_empty
  @xml.css('div').each do |node|
    children = node.children

    if children.size == 1 && children.first.is_a?(Nokogiri::XML::Text)
      node.remove if node.text.strip.empty?
    end
  end

  @xml
end

#convert_to_xml(enml) ⇒ Object Also known as: to_xml

Since:

  • 0.3.7



27
28
29
30
# File 'lib/evertils/helpers/api-enml-handler.rb', line 27

def convert_to_xml(enml)
  @xml = from_str(enml)
  self
end

#fix_dtdObject

Sometimes, the Doctype declaration gets borked by the XML parser lets replace it with a new DTD if that is the case

Since:

  • 0.3.15



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/evertils/helpers/api-enml-handler.rb', line 60

def fix_dtd
  node = @xml.children[0]

  # the node we are looking at is actually the XML node, skip it
  if node.is_a?(ProcessingInstruction)
    node = node.next
  end

  # remove the existing broken DTD
  node.remove
  # create a new one (note: output is overridden in DTD class defined
  # below ApiEnmlHandler)
  dtd = DTD.new('DOCTYPE', @xml)

  @xml.children.first.before(dtd)
end

#from_str(str) ⇒ Object

Since:

  • 0.3.13



15
16
17
18
19
20
21
22
23
# File 'lib/evertils/helpers/api-enml-handler.rb', line 15

def from_str(str)
  str.sub!("\n", '')
  @xml = DocumentFragment.parse(str) do |conf|
    conf.noblanks
  end

  fix_dtd
  clear_empty
end

#prepareObject

Since:

  • 0.3.5



35
36
37
38
39
40
41
# File 'lib/evertils/helpers/api-enml-handler.rb', line 35

def prepare
  note_xml = @xml.search('en-note')

  # remove <br> tags
  note_xml.search('br').each(&:remove)
  note_xml.inner_html.to_s
end

#to_enml(hash) ⇒ Object

Since:

  • 0.3.1



79
80
81
# File 'lib/evertils/helpers/api-enml-handler.rb', line 79

def to_enml(hash)
  Evertils::Helper::EvernoteENML.with_list(hash)
end