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 try: import simplejson as json
except ImportError: import 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): def to_json(journal):
"""Returns a JSON representation of the 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('-' * 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_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('--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('--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('--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('--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('--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")
@ -66,7 +67,7 @@ def guess_mode(args, config):
"""Guesses the mode (compose, read or export) from the given arguments""" """Guesses the mode (compose, read or export) from the given arguments"""
compose = True compose = True
export = False 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 compose = False
export = True export = True
elif args.start_date or args.end_date or args.limit or args.strict or args.short: elif args.start_date or args.end_date or args.limit or args.strict or args.short:
@ -103,7 +104,7 @@ def encrypt(journal, filename=None):
print("Journal encrypted to %s." % journal.config['journal']) print("Journal encrypted to %s." % journal.config['journal'])
else: else:
journal.write(filename) 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): def decrypt(journal, filename=None):
""" Decrypts into new file. If filename is not set, we encrypt the journal file itself. """ """ Decrypts into new file. If filename is not set, we encrypt the journal file itself. """
@ -115,7 +116,7 @@ def decrypt(journal, filename=None):
print("Journal decrypted to %s." % journal.config['journal']) print("Journal decrypted to %s." % journal.config['journal'])
else: else:
journal.write(filename) 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): def print_tags(journal):
"""Prints a list of all tags and the number of occurances.""" """Prints a list of all tags and the number of occurances."""
@ -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.") 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) sys.exit(-1)
args = parse_args() args = parse_args()
# If the first textual argument points to a journal file, # If the first textual argument points to a journal file,
# use this! # use this!
@ -193,7 +194,7 @@ def cli():
print(journal) print(journal)
# Various export modes # Various export modes
elif args.tags: elif args.tags:
print_tags(journal) print_tags(journal)
elif args.json: # export to json elif args.json: # export to json
@ -202,6 +203,10 @@ def cli():
elif args.markdown: # export to json elif args.markdown: # export to json
print(exporters.to_md(journal)) 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: 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.")
@ -220,4 +225,3 @@ def cli():
if __name__ == "__main__": if __name__ == "__main__":
cli() cli()