Class: Evernote::EDAM::Type::Note::EnmlDocument

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/rnote/noun/note/converter.rb

Overview

simple xhtml to txt converter just tries to convert evernotes simple xhtml. the kind its own editors create. Which doesn’t involve much nesting.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnmlDocument

Returns a new instance of EnmlDocument.



40
41
42
43
44
45
46
47
48
# File 'lib/rnote/noun/note/converter.rb', line 40

def initialize
  @_txt = ''
  @in_pre = false
  @next_number = 1
  @in_ul = false
  @in_ol = false
  @nested_div_count = 0
  super
end

Instance Attribute Details

#_txtObject

Nokogiri SAX parser



38
39
40
# File 'lib/rnote/noun/note/converter.rb', line 38

def _txt
  @_txt
end

#in_olObject

Nokogiri SAX parser



38
39
40
# File 'lib/rnote/noun/note/converter.rb', line 38

def in_ol
  @in_ol
end

#in_preObject

Nokogiri SAX parser



38
39
40
# File 'lib/rnote/noun/note/converter.rb', line 38

def in_pre
  @in_pre
end

#in_ulObject

Nokogiri SAX parser



38
39
40
# File 'lib/rnote/noun/note/converter.rb', line 38

def in_ul
  @in_ul
end

#nested_div_countObject

Nokogiri SAX parser



38
39
40
# File 'lib/rnote/noun/note/converter.rb', line 38

def nested_div_count
  @nested_div_count
end

#next_numberObject

Nokogiri SAX parser



38
39
40
# File 'lib/rnote/noun/note/converter.rb', line 38

def next_number
  @next_number
end

Instance Method Details

#characters(string) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/rnote/noun/note/converter.rb', line 54

def characters string
  
  if ! self.in_div and ! self.in_pre and string == "\n"
    # ignore lone newlines that occur outside a div
  else
    self._txt << string
  end
end

#end_element(name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rnote/noun/note/converter.rb', line 93

def end_element name
  case name
    when 'div'
      raise "unmatched <div> tags" if self.nested_div_count <= 0
      self.nested_div_count -= 1
      # a newline for every div (whether its got a <br> in it or not)
      self._txt << "\n"
    when 'pre'
      self.in_pre = false
    when 'br'
      # ignore it, as its always in a div, and every div will be a newline anyways
    when 'ul'
      self.in_ul = false
    when 'ol'
      self.in_ol = false
    when 'li'
      # unnecessary, as we're in a div, so the other \n is kept
      # self._txt << "\n"
    else
      # nothing
  end
end

#in_divObject



50
51
52
# File 'lib/rnote/noun/note/converter.rb', line 50

def in_div
  nested_div_count > 0
end

#start_element(name, attrs = []) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rnote/noun/note/converter.rb', line 63

def start_element name, attrs = []
  case name
    when 'en-todo'
      if Hash[attrs]['checked'] == 'true'
        self._txt << '[X] '
      else
        self._txt << '[ ] '
      end
    when 'div'
      self.nested_div_count += 1
    when 'pre'
      self.in_pre = true
    when 'ol'
      self.next_number = 1
      self.in_ol = true
    when 'ul'
      self.next_number = 1
      self.in_ul = true
    when 'li'
      if in_ol
        self._txt << next_number.to_s + '. '
        self.next_number += 1
      elsif in_ul
        self._txt << '* '
      end
    else
      # nothing
  end
end

#txtObject



116
117
118
119
# File 'lib/rnote/noun/note/converter.rb', line 116

def txt
  # always remove the last newline. to match up with WYSIWYG interfaces.
  self._txt.chomp
end