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

Inherits:
Object
  • Object
show all
Defined in:
lib/rnote/noun/note/converter.rb,
lib/rnote/noun/note/edit.rb

Overview

converting between text formats and enml

we have two types of conversion

simple, single document conversion. which is enml <=> txt

then our own additional wrappers we put on top of those 2 document types adding metadata to them. yaml_stream <=> notes attributes content is just considered an ‘attribute’ in the latter

the yaml_stream is just a string the note attributes get its own class and thats where we stick the conversion routines.

Defined Under Namespace

Classes: EnmlDocument

Constant Summary collapse

NON_BREAKING_SPACE =
"\xc2\xa0".force_encoding("UTF-8")
ENML_PREAMBLE =
<<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
EOF

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enml_to_format(format, enml) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/rnote/noun/note/converter.rb', line 171

def self.enml_to_format(format,enml)
  case format
    when 'enml'
      enml
    when 'txt'
      enml_to_txt(enml)
    else 
      raise
  end
end

.enml_to_txt(enml) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rnote/noun/note/converter.rb', line 124

def self.enml_to_txt(enml)
  raise 'not given xml' if ! enml.start_with? '<?xml'

  sax_document = EnmlDocument.new
  parser = Nokogiri::XML::SAX::Parser.new(sax_document)
  parser.parse(enml)
  
  enml = sax_document.txt

  enml
end

.format_to_enml(format, formatted_content) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/rnote/noun/note/converter.rb', line 182

def self.format_to_enml(format,formatted_content)
  case format
    when 'enml'
      formatted_content
    when 'txt'
      txt_to_enml(formatted_content)
    else
      raise
  end
end

.txt_to_enml(txt) ⇒ Object



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
161
162
163
164
165
166
167
168
169
# File 'lib/rnote/noun/note/converter.rb', line 136

def self.txt_to_enml(txt)
  raise 'given xml instead of txt' if txt.start_with? '<?xml'
  
  # TODO create a proper DOM, with proper xml entity escapes and tag structure
  
  # escape any entities
  txt = txt.gsub('<','&lt;')
  txt = txt.gsub('>','&gt;')
  
  # replace todo items 
  txt = txt.gsub('[ ] ','<en-todo/>')
  txt = txt.gsub('[X] ','<en-todo checked="true"/>')
  
  # every newline becomes a <div></div>
  # an empty line becomes a <div><br/></div>
  txt = txt.gsub('  ',NON_BREAKING_SPACE + ' ')
  
  lines = txt.split("\n",-1)
  lines = [''] if txt == ''
  raise if lines.length == 0
  
  # every pair of spaces must be swaped to a nbsp
  # don't have to use the identity though. the utf-8 char is fine
  
  xhtml = lines.map { |string|
    if string == ''
      "<div><br/></div>\n"
    else
      "<div>#{string}</div>\n"
    end
  }.join('')
    
  "#{Evernote::EDAM::Type::Note::ENML_PREAMBLE}#{xhtml}</en-note>\n"
end

Instance Method Details

#deep_dupObject



65
66
67
68
69
70
71
# File 'lib/rnote/noun/note/edit.rb', line 65

def deep_dup
  duplicate = self.dup
  duplicate.tagNames = self.tagNames.dup if self.tagNames
  duplicate.tagGuids = self.tagGuids.dup if self.tagGuids
  
  duplicate
end

#diff(new_note) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rnote/noun/note/edit.rb', line 33

def diff(new_note)
  # returns a Note that represents a diff of the 2, used for client.updateNote()
  
  raise "notes aren't copies of the same note" if self.guid != new_note.guid
  
  contains_diff = false
  
  diff_note = Evernote::EDAM::Type::Note.new
  diff_note.guid = new_note.guid
  
  if self.title != new_note
    contains_diff = true
  end
  # always contains a title
  diff_note.title = new_note.title
  
  if self.content != new_note.content
    contains_diff = true
    diff_note.content = new_note.content
  end
  
  if self.tagNames != new_note.tagNames
    contains_diff = true
    diff_note.tagNames = new_note.tagNames
  end
  
  # we dont' diff tagGuids as we always want to modify tagNames instead.
  # so that the evernote api will handle the tag creation and guids itself.
  
  contains_diff ? diff_note : nil
end

#set_yaml_stream(format, yaml_stream) ⇒ Object

The yaml stream is what we give to the user to edit in their editor

Its just a string, but its composed of 2 parts. the note attributes and the note content.

  1. a small yaml document with the note attributes as a hash.

  2. followed by the note content as txt



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rnote/noun/note/converter.rb', line 207

def set_yaml_stream(format,yaml_stream)

  m = yaml_stream.match /^(---.+?---\n)(.*)$/m
  raise "failed to parse yaml stream\n#{yaml_stream}" unless m

  attributes_yaml = m[1]
  txt = m[2]

  enml = self.class.format_to_enml(format,txt)
  attributes_hash = YAML.load(attributes_yaml)
  
  # process tag names
  # allow for comma separated tag list
  tag_names = attributes_hash['tagNames']
  tag_names = tag_names.split(/\s*,\s*/) if tag_names.instance_of?(String)

  self.title = attributes_hash['title']
  self.tagNames = attributes_hash['tagNames']
  self.content = enml
end

#summarizeObject



232
233
234
# File 'lib/rnote/noun/note/converter.rb', line 232

def summarize
  self.txt_content.strip.gsub(/\s+/,' ')[0..100]
end

#txt_contentObject



193
194
195
# File 'lib/rnote/noun/note/converter.rb', line 193

def txt_content
  self.class.enml_to_format('txt',self.content)
end

#txt_content=(txt) ⇒ Object



197
198
199
# File 'lib/rnote/noun/note/converter.rb', line 197

def txt_content=(txt)
  self.content = self.class.format_to_enml('txt', txt)
end

#yaml_stream(format) ⇒ Object



228
229
230
# File 'lib/rnote/noun/note/converter.rb', line 228

def yaml_stream(format)
  YAML.dump({ 'title' => title, 'tagNames' => tagNames }) + "\n---\n" + self.class.enml_to_format(format,content)
end