mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
[Export] Imporved functionality for exporting
Now exports can be done via single command and output file can also be specified. Signed-off-by: Aniket Pant <me@aniketpant.com>
This commit is contained in:
parent
7c264367e7
commit
ef00e6adf3
2 changed files with 53 additions and 37 deletions
|
@ -3,14 +3,19 @@
|
|||
|
||||
import re
|
||||
import os
|
||||
try: from slugify import slugify
|
||||
except ImportError: import slugify
|
||||
try: import simplejson as json
|
||||
except ImportError: import json
|
||||
|
||||
def to_json(journal):
|
||||
def to_json(journal, output):
|
||||
"""Returns a JSON representation of the Journal."""
|
||||
return json.dumps([e.to_dict() for e in journal.entries], indent=2)
|
||||
result = json.dumps([e.to_dict() for e in journal.entries], indent=2)
|
||||
if output is not False:
|
||||
write_file(result, output)
|
||||
return result
|
||||
|
||||
def to_md(journal):
|
||||
def to_md(journal, output):
|
||||
"""Returns a markdown representation of the Journal"""
|
||||
out = []
|
||||
year, month = -1, -1
|
||||
|
@ -24,26 +29,39 @@ def to_md(journal):
|
|||
out.append(e.date.strftime("%B"))
|
||||
out.append('-' * len(e.date.strftime("%B")) + "\n")
|
||||
out.append(e.to_md())
|
||||
return "\n".join(out)
|
||||
result = "\n".join(out)
|
||||
if output is not False:
|
||||
write_file(result, output)
|
||||
return result
|
||||
|
||||
def to_files(journal, directory, extension):
|
||||
def to_txt(journal, output):
|
||||
"""Returns the complete text of the Journal."""
|
||||
if output is not False:
|
||||
write_file(str(journal), output)
|
||||
return journal
|
||||
|
||||
def to_files(journal, output):
|
||||
"""Turns your journal into separate files for each entry."""
|
||||
if extension:
|
||||
ext = "." + extension
|
||||
else:
|
||||
ext = ''
|
||||
|
||||
if not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
|
||||
if output is False:
|
||||
output = os.path.expanduser('~/journal/*.txt') # default path
|
||||
path, extension = os.path.splitext(os.path.expanduser(output))
|
||||
head, tail = os.path.split(path)
|
||||
if tail == '*': # if wildcard is specified
|
||||
path = head + '/'
|
||||
if not os.path.exists(path): # if the folder doesn't exist, create it
|
||||
os.makedirs(path)
|
||||
for e in journal.entries:
|
||||
date = e.date.strftime('%Y-%m-%d')
|
||||
title = re.sub('[^\w-]', '', re.sub(' ', '-', e.title.lower()))
|
||||
filename = date + '-' + title + ext
|
||||
f = open(directory + "/" + filename, 'w+')
|
||||
if extension == 'md':
|
||||
f.write(str(e.to_md()))
|
||||
else:
|
||||
f.write(str(e))
|
||||
f.close()
|
||||
return ("Journal exported to directory '" + directory + "' as <filename>" + ext)
|
||||
title = slugify(e.title)
|
||||
filename = date + '-' + title
|
||||
result = str(e)
|
||||
fullpath = path + filename + extension
|
||||
print fullpath
|
||||
write_file(result, fullpath)
|
||||
return ("Journal exported to '" + path + "'")
|
||||
|
||||
def write_file(content, path):
|
||||
"""Writes content to the file provided"""
|
||||
f = open(path, 'w+')
|
||||
f.write(content)
|
||||
f.close()
|
||||
|
|
28
jrnl/jrnl.py
28
jrnl/jrnl.py
|
@ -42,25 +42,20 @@ def parse_args():
|
|||
reading.add_argument('-short', dest='short', action="store_true", help='Show only titles or line containing the search tags')
|
||||
|
||||
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('--json', dest='json', action="store_true", help='Returns a JSON-encoded version of the Journal')
|
||||
exporting.add_argument('--markdown', dest='markdown', action="store_true", help='Returns a Markdown-formated version of the Journal')
|
||||
exporting.add_argument('--tags', dest='tags', action="store_true", help='Returns a list of all tags and number of occurrences')
|
||||
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('-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('--decrypt', metavar='FILENAME', dest='decrypt', help='Decrypts your journal and stores it in plain text', nargs='?', default=False, const=None)
|
||||
exporting.add_argument('--delete-last', dest='delete_last', help='Deletes the last entry from your journal file.', action="store_true")
|
||||
|
||||
exporting_to_files = parser.add_argument_group('Export to files', 'Options for exporting your journal to individual files')
|
||||
exporting_to_files.add_argument('--files', dest='files', action="store_true", help='Turns your journal into separate files for each entry')
|
||||
exporting_to_files.add_argument('--dir', metavar='DIRECTORY', dest='directory', help='The directory you want to export the files to', nargs='?', default='journal', const=None)
|
||||
exporting_to_files.add_argument('--ext', metavar='EXTENSION', dest='extension', help='The extension of the exported files', nargs='?', default=False, const=None)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
def guess_mode(args, config):
|
||||
"""Guesses the mode (compose, read or export) from the given arguments"""
|
||||
compose = True
|
||||
export = False
|
||||
if args.json or args.decrypt is not False or args.encrypt is not False or args.markdown or args.tags or args.delete_last:
|
||||
if args.decrypt is not False or args.encrypt is not False or args.export is not False or args.tags or args.delete_last:
|
||||
compose = False
|
||||
export = True
|
||||
elif args.start_date or args.end_date or args.limit or args.strict or args.short:
|
||||
|
@ -201,14 +196,17 @@ def cli():
|
|||
elif args.tags:
|
||||
print_tags(journal)
|
||||
|
||||
elif args.json: # export to json
|
||||
print(exporters.to_json(journal))
|
||||
elif args.export == 'json': # export to json
|
||||
print(exporters.to_json(journal, args.output))
|
||||
|
||||
elif args.markdown: # export to markdown
|
||||
print(exporters.to_md(journal))
|
||||
elif args.export == 'markdown' or args.export == 'md': # export to markdown
|
||||
print(exporters.to_md(journal, args.output))
|
||||
|
||||
elif args.files: # export to files
|
||||
print(exporters.to_files(journal, args.directory, args.extension))
|
||||
elif args.export == 'text' or args.export == 'txt': # export to text
|
||||
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:
|
||||
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