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

Inherits:
Object
  • Object
show all
Defined in:
lib/rnote/converter.rb,
lib/rnote/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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enml_to_format(format, enml) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/rnote/converter.rb', line 137

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



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rnote/converter.rb', line 89

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



148
149
150
151
152
153
154
155
156
157
# File 'lib/rnote/converter.rb', line 148

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rnote/converter.rb', line 101

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.gsub!('<','&lt;')
  txt.gsub!('>','&gt;')
  
  # replace todo items 
  txt.gsub!('[ ]','<en-todo checked="false"/>')
  txt.gsub!('[X]','<en-todo checked="true"/>')
  
  # every newline becomes a <div></div>
  # an empty line becomes a <div><br/></div>
  
  lines = txt.split("\n",-1)
  lines = [''] if txt == ''
  raise if lines.length == 0
  
  xhtml = lines.map { |string|
    if string == ''
      "<div><br/></div>\n"
    else
      "<div>#{string}</div>\n"
    end
  }.join('')
    
  <<EOF
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
#{xhtml}</en-note>
EOF
end

Instance Method Details

#deep_dupObject



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

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



32
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
# File 'lib/rnote/edit.rb', line 32

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rnote/converter.rb', line 169

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



194
195
196
# File 'lib/rnote/converter.rb', line 194

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

#txt_contentObject



159
160
161
# File 'lib/rnote/converter.rb', line 159

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

#yaml_stream(format) ⇒ Object



190
191
192
# File 'lib/rnote/converter.rb', line 190

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