mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
[Export to files] Added new utility
Signed-off-by: Aniket Pant <me@aniketpant.com>
This commit is contained in:
parent
6f9a644185
commit
7c264367e7
2 changed files with 32 additions and 0 deletions
|
@ -1,6 +1,8 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
import re
|
||||||
|
import os
|
||||||
try: import simplejson as json
|
try: import simplejson as json
|
||||||
except ImportError: import json
|
except ImportError: import json
|
||||||
|
|
||||||
|
@ -23,3 +25,25 @@ def to_md(journal):
|
||||||
out.append('-' * len(e.date.strftime("%B")) + "\n")
|
out.append('-' * len(e.date.strftime("%B")) + "\n")
|
||||||
out.append(e.to_md())
|
out.append(e.to_md())
|
||||||
return "\n".join(out)
|
return "\n".join(out)
|
||||||
|
|
||||||
|
def to_files(journal, directory, extension):
|
||||||
|
"""Turns your journal into separate files for each entry."""
|
||||||
|
if extension:
|
||||||
|
ext = "." + extension
|
||||||
|
else:
|
||||||
|
ext = ''
|
||||||
|
|
||||||
|
if not os.path.exists(directory):
|
||||||
|
os.makedirs(directory)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
|
@ -49,6 +49,11 @@ def parse_args():
|
||||||
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)
|
||||||
exporting.add_argument('--delete-last', dest='delete_last', help='Deletes the last entry from your journal file.', action="store_true")
|
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()
|
return parser.parse_args()
|
||||||
|
|
||||||
def guess_mode(args, config):
|
def guess_mode(args, config):
|
||||||
|
@ -202,6 +207,9 @@ def cli():
|
||||||
elif args.markdown: # export to markdown
|
elif args.markdown: # export to markdown
|
||||||
print(exporters.to_md(journal))
|
print(exporters.to_md(journal))
|
||||||
|
|
||||||
|
elif args.files: # export to files
|
||||||
|
print(exporters.to_files(journal, args.directory, args.extension))
|
||||||
|
|
||||||
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