From e0c74011ca1f2f31010462ffd174fff0e2624a21 Mon Sep 17 00:00:00 2001 From: timetoplatypus Date: Tue, 29 Aug 2017 21:28:59 -0400 Subject: [PATCH] Adds feature to export a journal to a static HTML page (with inline CSS) --- jrnl/Entry.py | 28 ++++++++++++++++++++++++++++ jrnl/cli.py | 2 +- jrnl/exporters.py | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/jrnl/Entry.py b/jrnl/Entry.py index 66b32f6c..2da9c216 100755 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -109,3 +109,31 @@ class Entry: body=body, space=space ) + + def to_html(self): + html = "\n" + html += "\n" + html += "\t\n" + html += "\t\t\n" + html += "\t\n\n" + html += "\t\n" + html += "\t\t\n" + html += "\t\t\t

Journal

\n" + html += "\t\t\t
\n" + # date time title body + html +="\t\t\t\t

" + self.date.strftime(self.journal.config['timeformat']) + "\t" + str(self.title) + "

\n" + html +="\t\t\t\t

" + str(self.body) + "

\n\t\t\t
\n\t\t\t
\n" + html +="\t\t
\n" + html +="\t\n" + html +="" + return html diff --git a/jrnl/cli.py b/jrnl/cli.py index fb067aa3..a974df1c 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -47,7 +47,7 @@ def parse_args(args=None): exporting.add_argument('--short', dest='short', action="store_true", help='Show only titles or line containing the search tags') exporting.add_argument('--tags', dest='tags', action="store_true", help='Returns a list of all tags and number of occurences') exporting.add_argument('--import-json', metavar='FILEPATH', dest='import_json', help='Import a journal from a JSON file.', default=False, const=None) - exporting.add_argument('--export', metavar='TYPE', dest='export', choices=['text', 'txt', 'markdown', 'md', 'json'], help='Export your journal. TYPE can be json, markdown, or text.', default=False, const=None) + exporting.add_argument('--export', metavar='TYPE', dest='export', choices=['text', 'txt', 'markdown', 'md', 'json', 'html'], help='Export your journal. TYPE can be json, markdown, html, or text.', default=False, const=None) exporting.add_argument('-o', metavar='OUTPUT', dest='output', help='Optionally specifies output file when using --export. If OUTPUT is a directory, exports each entry into an individual file instead.', 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) diff --git a/jrnl/exporters.py b/jrnl/exporters.py index ad2c3186..881f3781 100644 --- a/jrnl/exporters.py +++ b/jrnl/exporters.py @@ -66,9 +66,38 @@ def to_txt(journal): return journal.pprint() +def to_html(journal): + html = "\n" + html += "\n" + html += "\t\n" + html += "\t\t\n" + html += "\t\n\n" + html += "\t\n" + html += "\t\t\n" + html += "\t\t\t

Journal

\n" + html += "\t\t\t
\n" + for element in journal.entries: + # date time title body + html += "\t\t\t\t

" + str(element.date) + "\t" + element.title + "

\n" + html += "\t\t\t\t

" + element.body + "

\n\t\t\t
\n\t\t\t
\n" + html += "\t\t
\n" + html += "\t\n" + html += "" + return html + def export(journal, format, output=None): """Exports the journal to various formats. - format should be one of json, txt, text, md, markdown. + format should be one of json, txt, text, md, markdown, html. If output is None, returns a unicode representation of the output. If output is a directory, exports entries into individual files. Otherwise, exports to the given output file. @@ -78,10 +107,11 @@ def export(journal, format, output=None): "txt": to_txt, "text": to_txt, "md": to_md, - "markdown": to_md + "markdown": to_md, + "html": to_html } if format not in maps: - return "[ERROR: can't export to '{0}'. Valid options are 'md', 'txt', and 'json']".format(format) + return "[ERROR: can't export to '{0}'. Valid options are 'md', 'txt', 'html', and 'json']".format(format) if output and os.path.isdir(output): # multiple files return write_files(journal, output, format) else: @@ -109,6 +139,8 @@ def write_files(journal, path, format): content = e.to_md() elif format in ('txt', 'text'): content = e.__unicode__() + elif format == 'html': + content = e.to_html() with codecs.open(full_path, "w", "utf-8") as f: f.write(content) return "[Journal exported individual files in {0}]".format(path)