Skip to content

Commit

Permalink
first draft of pretty search jrnl-org#38
Browse files Browse the repository at this point in the history
  • Loading branch information
dedan committed Aug 10, 2012
1 parent 9796f5b commit 4a42ec2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
29 changes: 29 additions & 0 deletions jrnl/exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@

try: import simplejson as json
except ImportError: import json
import webbrowser
# TODO: add markdown to dependency
import markdown
import tempfile
import os
import codecs

html_skeleton = '''
<html>
<head>
<title>%s</title>
</head>
<body>
%s
</body>
</html>'''


def to_json(journal):
"""Returns a JSON representation of the Journal."""
Expand All @@ -23,3 +40,15 @@ def to_md(journal):
out.append('-' * len(e.date.strftime("%B")) + "\n")
out.append(e.to_md())
return "\n".join(out)

def to_html(journal, open_in_browser=False):
"""renders the given journal to html
and can open it in the default browser"""
bla = to_md(journal)
html_body = markdown.markdown(bla.decode('utf-8'))
print html_body
tmp_file = os.path.join(tempfile.gettempdir(), "pretty.html")
url = 'file://' + tmp_file
output_file = codecs.open(tmp_file, "w", encoding="utf8")
output_file.write(html_skeleton % (journal.config['journal'], html_body))
webbrowser.open(url)
16 changes: 10 additions & 6 deletions jrnl/jrnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def parse_args():
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('--html', dest='html', action="store_true", help='Opens html-formated version of the Journal in browser')
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")
Expand All @@ -66,7 +67,7 @@ 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.html or args.json or args.decrypt is not False or args.encrypt is not False or args.markdown 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:
Expand Down Expand Up @@ -103,7 +104,7 @@ def encrypt(journal, filename=None):
print("Journal encrypted to %s." % journal.config['journal'])
else:
journal.write(filename)
print("Journal encrypted to %s." % os.path.realpath(filename))
print("Journal encrypted to %s." % os.path.realpath(filename))

def decrypt(journal, filename=None):
""" Decrypts into new file. If filename is not set, we encrypt the journal file itself. """
Expand All @@ -115,7 +116,7 @@ def decrypt(journal, filename=None):
print("Journal decrypted to %s." % journal.config['journal'])
else:
journal.write(filename)
print("Journal encrypted to %s." % os.path.realpath(filename))
print("Journal encrypted to %s." % os.path.realpath(filename))

def print_tags(journal):
"""Prints a list of all tags and the number of occurances."""
Expand Down Expand Up @@ -151,7 +152,7 @@ def cli():
print("According to your jrnl_conf, your journal is encrypted, however PyCrypto was not found. To open your journal, install the PyCrypto package from http://www.pycrypto.org.")
sys.exit(-1)

args = parse_args()
args = parse_args()

# If the first textual argument points to a journal file,
# use this!
Expand Down Expand Up @@ -193,7 +194,7 @@ def cli():
print(journal)

# Various export modes
elif args.tags:
elif args.tags:
print_tags(journal)

elif args.json: # export to json
Expand All @@ -202,6 +203,10 @@ def cli():
elif args.markdown: # export to json
print(exporters.to_md(journal))

elif args.html: #export to html and open in browser
print 'test'
exporters.to_html(journal, True)

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.")

Expand All @@ -220,4 +225,3 @@ def cli():
if __name__ == "__main__":
cli()


0 comments on commit 4a42ec2

Please sign in to comment.