first draft of pretty search #38

This commit is contained in:
Stephan Gabler 2012-08-10 21:05:16 +02:00
parent 9796f5b0b0
commit 4a42ec20cb
2 changed files with 39 additions and 6 deletions

View file

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

View file

@ -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")
@ -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:
@ -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.")
@ -220,4 +225,3 @@ def cli():
if __name__ == "__main__":
cli()