mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
parent
3eb911f11b
commit
d8ccb928e0
1 changed files with 16 additions and 9 deletions
|
@ -13,13 +13,13 @@ def get_tags_count(journal):
|
|||
# Astute reader: should the following line leave you as puzzled as me the first time
|
||||
# I came across this construction, worry not and embrace the ensuing moment of enlightment.
|
||||
tags = [tag
|
||||
for entry in journal.entries
|
||||
for tag in set(entry.tags)
|
||||
]
|
||||
for entry in journal.entries
|
||||
for tag in set(entry.tags)]
|
||||
# To be read: [for entry in journal.entries: for tag in set(entry.tags): tag]
|
||||
tag_counts = set([(tags.count(tag), tag) for tag in tags])
|
||||
return tag_counts
|
||||
|
||||
|
||||
def to_tag_list(journal):
|
||||
"""Prints a list of all tags and the number of occurrences."""
|
||||
tag_counts = get_tags_count(journal)
|
||||
|
@ -32,6 +32,7 @@ def to_tag_list(journal):
|
|||
result += "\n".join(u"{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=True))
|
||||
return result
|
||||
|
||||
|
||||
def to_json(journal):
|
||||
"""Returns a JSON representation of the Journal."""
|
||||
tags = get_tags_count(journal)
|
||||
|
@ -41,6 +42,7 @@ def to_json(journal):
|
|||
}
|
||||
return json.dumps(result, indent=2)
|
||||
|
||||
|
||||
def to_md(journal):
|
||||
"""Returns a markdown representation of the Journal"""
|
||||
out = []
|
||||
|
@ -58,10 +60,12 @@ def to_md(journal):
|
|||
result = "\n".join(out)
|
||||
return result
|
||||
|
||||
|
||||
def to_txt(journal):
|
||||
"""Returns the complete text of the Journal."""
|
||||
return journal.pprint()
|
||||
|
||||
|
||||
def export(journal, format, output=None):
|
||||
"""Exports the journal to various formats.
|
||||
format should be one of json, txt, text, md, markdown.
|
||||
|
@ -76,7 +80,9 @@ def export(journal, format, output=None):
|
|||
"md": to_md,
|
||||
"markdown": to_md
|
||||
}
|
||||
if output and os.path.isdir(output): # multiple files
|
||||
if format not in maps:
|
||||
return u"[ERROR: can't export to {0}. Valid options are 'md', 'txt', and 'json']".format(format)
|
||||
if output and os.path.isdir(output): # multiple files
|
||||
return write_files(journal, output, format)
|
||||
else:
|
||||
content = maps[format](journal)
|
||||
|
@ -84,12 +90,13 @@ def export(journal, format, output=None):
|
|||
try:
|
||||
with codecs.open(output, "w", "utf-8") as f:
|
||||
f.write(content)
|
||||
return "[Journal exported to {0}]".format(output)
|
||||
return u"[Journal exported to {0}]".format(output)
|
||||
except IOError as e:
|
||||
return "[ERROR: {0} {1}]".format(e.filename, e.strerror)
|
||||
return u"[ERROR: {0} {1}]".format(e.filename, e.strerror)
|
||||
else:
|
||||
return content
|
||||
|
||||
|
||||
def write_files(journal, path, format):
|
||||
"""Turns your journal into separate files for each entry.
|
||||
Format should be either json, md or txt."""
|
||||
|
@ -98,10 +105,10 @@ def write_files(journal, path, format):
|
|||
full_path = os.path.join(path, make_filename(e))
|
||||
if format == 'json':
|
||||
content = json.dumps(e.to_dict(), indent=2) + "\n"
|
||||
elif format == 'md':
|
||||
elif format in ('md', 'markdown'):
|
||||
content = e.to_md()
|
||||
elif format == 'txt':
|
||||
elif format in ('txt', 'text'):
|
||||
content = u(e)
|
||||
with codecs.open(full_path, "w", "utf-8") as f:
|
||||
f.write(content)
|
||||
return "[Journal exported individual files in {0}]".format(path)
|
||||
return u"[Journal exported individual files in {0}]".format(path)
|
||||
|
|
Loading…
Add table
Reference in a new issue