mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Improve export functionality
Signed-off-by: Aniket Pant <me@aniketpant.com>
This commit is contained in:
parent
a93e9fff13
commit
55fc36a0d2
2 changed files with 71 additions and 24 deletions
|
@ -2,6 +2,7 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import string
|
||||||
try: from slugify import slugify
|
try: from slugify import slugify
|
||||||
except ImportError: import slugify
|
except ImportError: import slugify
|
||||||
try: import simplejson as json
|
try: import simplejson as json
|
||||||
|
@ -20,7 +21,7 @@ def get_tags_count(journal):
|
||||||
return tag_counts
|
return tag_counts
|
||||||
|
|
||||||
def to_tag_list(journal):
|
def to_tag_list(journal):
|
||||||
"""Prints a list of all tags and the number of occurances."""
|
"""Prints a list of all tags and the number of occurrences."""
|
||||||
tag_counts = get_tags_count(journal)
|
tag_counts = get_tags_count(journal)
|
||||||
result = ""
|
result = ""
|
||||||
if not tag_counts:
|
if not tag_counts:
|
||||||
|
@ -39,8 +40,14 @@ def to_json(journal, output):
|
||||||
"entries": [e.to_dict() for e in journal.entries]
|
"entries": [e.to_dict() for e in journal.entries]
|
||||||
}
|
}
|
||||||
if output is not False:
|
if output is not False:
|
||||||
write_file(json.dumps(result, indent=2), output)
|
path = output_path('json', output)
|
||||||
return json.dumps(result, indent=2)
|
if not is_globable(path):
|
||||||
|
message = write_file(json.dumps(result, indent=2), path)
|
||||||
|
else:
|
||||||
|
message = to_files(journal, path)
|
||||||
|
return message
|
||||||
|
else:
|
||||||
|
return json.dumps(result, indent=2)
|
||||||
|
|
||||||
def to_md(journal, output):
|
def to_md(journal, output):
|
||||||
"""Returns a markdown representation of the Journal"""
|
"""Returns a markdown representation of the Journal"""
|
||||||
|
@ -58,35 +65,78 @@ def to_md(journal, output):
|
||||||
out.append(e.to_md())
|
out.append(e.to_md())
|
||||||
result = "\n".join(out)
|
result = "\n".join(out)
|
||||||
if output is not False:
|
if output is not False:
|
||||||
write_file(result, output)
|
path = output_path('md', output)
|
||||||
return result
|
if not is_globable(path):
|
||||||
|
message = write_file(result, path)
|
||||||
|
else:
|
||||||
|
message = to_files(journal, path)
|
||||||
|
return message
|
||||||
|
else:
|
||||||
|
return result
|
||||||
|
|
||||||
def to_txt(journal, output):
|
def to_txt(journal, output):
|
||||||
"""Returns the complete text of the Journal."""
|
"""Returns the complete text of the Journal."""
|
||||||
if output is not False:
|
if output is not False:
|
||||||
write_file(unicode(journal), output)
|
path = output_path('txt', output)
|
||||||
return unicode(journal)
|
if not is_globable(path):
|
||||||
|
message = write_file(unicode(journal), path)
|
||||||
|
else:
|
||||||
|
message = to_files(journal, path)
|
||||||
|
return message
|
||||||
|
else:
|
||||||
|
return unicode(journal)
|
||||||
|
|
||||||
def to_files(journal, output):
|
def to_files(journal, output):
|
||||||
"""Turns your journal into separate files for each entry."""
|
"""Turns your journal into separate files for each entry."""
|
||||||
if output is False:
|
path, extension = os.path.splitext(os.path.expanduser(output))
|
||||||
output = journal.config['folder'] + "*.txt" # default path
|
|
||||||
|
for e in journal.entries:
|
||||||
|
content = ""
|
||||||
|
date = e.date.strftime('%C-%m-%d')
|
||||||
|
title = slugify(unicode(e.title))
|
||||||
|
|
||||||
|
filename = string.replace(path, "%C-%m-%d", date)
|
||||||
|
filename = string.replace(filename, "slug", title)
|
||||||
|
|
||||||
|
fullpath = filename + extension
|
||||||
|
|
||||||
|
if extension == '.json':
|
||||||
|
content = json.dumps(e.to_dict(), indent=2)
|
||||||
|
elif extension == '.md':
|
||||||
|
content = e.to_md()
|
||||||
|
elif extension == '.txt':
|
||||||
|
content = unicode(e)
|
||||||
|
write_file(content, fullpath)
|
||||||
|
|
||||||
|
return ("Journal exported.")
|
||||||
|
|
||||||
|
def is_globable(output):
|
||||||
path, extension = os.path.splitext(os.path.expanduser(output))
|
path, extension = os.path.splitext(os.path.expanduser(output))
|
||||||
head, tail = os.path.split(path)
|
head, tail = os.path.split(path)
|
||||||
if tail == '*': # if wildcard is specified
|
|
||||||
path = head + '/'
|
if tail == "%C-%m-%d_slug":
|
||||||
if not os.path.exists(path): # if the folder doesn't exist, create it
|
return True
|
||||||
os.makedirs(path)
|
else:
|
||||||
for e in journal.entries:
|
return False
|
||||||
date = e.date.strftime('%Y-%m-%d')
|
|
||||||
title = slugify(unicode(e.title))
|
def output_path(file_ext, output):
|
||||||
filename = date + '-' + title
|
path, extension = os.path.splitext(os.path.expanduser(output))
|
||||||
fullpath = path + filename + extension
|
|
||||||
write_file(unicode(e), fullpath)
|
head, tail = os.path.split(path)
|
||||||
return ("Journal exported")
|
if head != '':
|
||||||
|
if not os.path.exists(head): # if the folder doesn't exist, create it
|
||||||
|
os.makedirs(head)
|
||||||
|
fullpath = head + '/' + tail + '.' + file_ext
|
||||||
|
else:
|
||||||
|
fullpath = tail + '.' + file_ext
|
||||||
|
|
||||||
|
return fullpath
|
||||||
|
|
||||||
def write_file(content, path):
|
def write_file(content, path):
|
||||||
"""Writes content to the file provided"""
|
"""Writes content to the file provided"""
|
||||||
|
|
||||||
f = open(path, 'w+')
|
f = open(path, 'w+')
|
||||||
f.write(content)
|
f.write(content)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
return ("File exported to " + path)
|
||||||
|
|
|
@ -45,7 +45,7 @@ def parse_args():
|
||||||
|
|
||||||
exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal')
|
exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal')
|
||||||
exporting.add_argument('--tags', dest='tags', action="store_true", help='Returns a list of all tags and number of occurences')
|
exporting.add_argument('--tags', dest='tags', action="store_true", help='Returns a list of all tags and number of occurences')
|
||||||
exporting.add_argument('--export', metavar='TYPE', dest='export', help='Export your journal to Markdown, JSON, Text or multiple files', nargs='?', default=False, const=None)
|
exporting.add_argument('--export', metavar='TYPE', dest='export', help='Export your journal to Markdown, JSON or Text', nargs='?', default=False, const=None)
|
||||||
exporting.add_argument('-o', metavar='OUTPUT', dest='output', help='The output of the file can be provided when using with --export', nargs='?', default=False, const=None)
|
exporting.add_argument('-o', metavar='OUTPUT', dest='output', help='The output of the file can be provided when using with --export', nargs='?', default=False, const=None)
|
||||||
exporting.add_argument('--encrypt', metavar='FILENAME', dest='encrypt', help='Encrypts your existing journal with a new password', nargs='?', default=False, const=None)
|
exporting.add_argument('--encrypt', metavar='FILENAME', dest='encrypt', help='Encrypts your existing journal with a new password', nargs='?', default=False, const=None)
|
||||||
exporting.add_argument('--decrypt', metavar='FILENAME', dest='decrypt', help='Decrypts your journal and stores it in plain text', nargs='?', default=False, const=None)
|
exporting.add_argument('--decrypt', metavar='FILENAME', dest='decrypt', help='Decrypts your journal and stores it in plain text', nargs='?', default=False, const=None)
|
||||||
|
@ -199,9 +199,6 @@ def cli():
|
||||||
elif args.export == 'text' or args.export == 'txt': # export to text
|
elif args.export == 'text' or args.export == 'txt': # export to text
|
||||||
print(exporters.to_txt(journal, args.output))
|
print(exporters.to_txt(journal, args.output))
|
||||||
|
|
||||||
elif args.export == 'files': # export to files
|
|
||||||
print(exporters.to_files(journal, args.output))
|
|
||||||
|
|
||||||
elif (args.encrypt is not False or args.decrypt is not False) and not PYCRYPTO:
|
elif (args.encrypt is not False or args.decrypt is not False) and not PYCRYPTO:
|
||||||
print("PyCrypto not found. To encrypt or decrypt your journal, install the PyCrypto package from http://www.pycrypto.org.")
|
print("PyCrypto not found. To encrypt or decrypt your journal, install the PyCrypto package from http://www.pycrypto.org.")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue