mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Fix XML export
This commit is contained in:
parent
4adb5c252a
commit
b2542db5e5
3 changed files with 23 additions and 6 deletions
|
@ -17,6 +17,10 @@ class Entry:
|
|||
self.starred = starred
|
||||
self.modified = False
|
||||
|
||||
@property
|
||||
def fulltext(self):
|
||||
return self.title + " " + self.body
|
||||
|
||||
@staticmethod
|
||||
def tag_regex(tagsymbols):
|
||||
pattern = r'(?u)\s([{tags}][-+*#/\w]+)'.format(tags=tagsymbols)
|
||||
|
|
|
@ -28,6 +28,16 @@ class XMLExporter(JSONExporter):
|
|||
else:
|
||||
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
|
||||
def export_journal(cls, journal):
|
||||
"""Returns an XML representation of an entire journal."""
|
||||
|
@ -36,12 +46,12 @@ class XMLExporter(JSONExporter):
|
|||
xml = doc.createElement('journal')
|
||||
tags_el = doc.createElement('tags')
|
||||
entries_el = doc.createElement('entries')
|
||||
for tag in tags:
|
||||
for count, tag in tags:
|
||||
tag_el = doc.createElement('tag')
|
||||
tag_el.setAttribute('name', tag[1])
|
||||
count_node = doc.createTextNode(u(tag[0]))
|
||||
tag.appendChild(count_node)
|
||||
tags_el.appendChild(tag)
|
||||
tag_el.setAttribute('name', tag)
|
||||
count_node = doc.createTextNode(u(count))
|
||||
tag_el.appendChild(count_node)
|
||||
tags_el.appendChild(tag_el)
|
||||
for entry in journal.entries:
|
||||
entries_el.appendChild(cls.entry_to_xml(entry, doc))
|
||||
xml.appendChild(entries_el)
|
||||
|
|
|
@ -76,10 +76,13 @@ def set_keychain(journal_name, password):
|
|||
|
||||
def u(s):
|
||||
"""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")
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue