Fix XML export

This commit is contained in:
Manuel Ebert 2015-12-28 13:01:33 -08:00
parent 4adb5c252a
commit b2542db5e5
3 changed files with 23 additions and 6 deletions

View file

@ -17,6 +17,10 @@ class Entry:
self.starred = starred self.starred = starred
self.modified = False self.modified = False
@property
def fulltext(self):
return self.title + " " + self.body
@staticmethod @staticmethod
def tag_regex(tagsymbols): def tag_regex(tagsymbols):
pattern = r'(?u)\s([{tags}][-+*#/\w]+)'.format(tags=tagsymbols) pattern = r'(?u)\s([{tags}][-+*#/\w]+)'.format(tags=tagsymbols)

View file

@ -28,6 +28,16 @@ class XMLExporter(JSONExporter):
else: else:
return entry_el return entry_el
@classmethod
def entry_to_xml(cls, entry, doc):
entry_el = doc.createElement('entry')
entry_el.setAttribute('date', entry.date.isoformat())
if hasattr(entry, "uuid"):
entry_el.setAttribute('uuid', u(entry.uuid))
entry_el.setAttribute('starred', u(entry.starred))
entry_el.appendChild(doc.createTextNode(entry.fulltext))
return entry_el
@classmethod @classmethod
def export_journal(cls, journal): def export_journal(cls, journal):
"""Returns an XML representation of an entire journal.""" """Returns an XML representation of an entire journal."""
@ -36,12 +46,12 @@ class XMLExporter(JSONExporter):
xml = doc.createElement('journal') xml = doc.createElement('journal')
tags_el = doc.createElement('tags') tags_el = doc.createElement('tags')
entries_el = doc.createElement('entries') entries_el = doc.createElement('entries')
for tag in tags: for count, tag in tags:
tag_el = doc.createElement('tag') tag_el = doc.createElement('tag')
tag_el.setAttribute('name', tag[1]) tag_el.setAttribute('name', tag)
count_node = doc.createTextNode(u(tag[0])) count_node = doc.createTextNode(u(count))
tag.appendChild(count_node) tag_el.appendChild(count_node)
tags_el.appendChild(tag) tags_el.appendChild(tag_el)
for entry in journal.entries: for entry in journal.entries:
entries_el.appendChild(cls.entry_to_xml(entry, doc)) entries_el.appendChild(cls.entry_to_xml(entry, doc))
xml.appendChild(entries_el) xml.appendChild(entries_el)

View file

@ -76,10 +76,13 @@ def set_keychain(journal_name, password):
def u(s): def u(s):
"""Mock unicode function for python 2 and 3 compatibility.""" """Mock unicode function for python 2 and 3 compatibility."""
if not isinstance(s, str):
s = str(s)
return s if PY3 or type(s) is unicode else s.decode("utf-8") return s if PY3 or type(s) is unicode else s.decode("utf-8")
def py2encode(s): def py2encode(s):
"""Encodes to UTF-8 in Python 2 but not r.""" """Encodes to UTF-8 in Python 2 but not in Python 3."""
return s.encode("utf-8") if PY2 and type(s) is unicode else s return s.encode("utf-8") if PY2 and type(s) is unicode else s