Refractored exporting

This commit is contained in:
Manuel Ebert 2012-07-05 12:31:50 +02:00
parent 3cbef42e2b
commit 7e59e8ad3b
3 changed files with 28 additions and 22 deletions

View file

@ -149,26 +149,6 @@ class Journal:
pp) pp)
return pp return pp
def to_json(self):
"""Returns a JSON representation of the Journal."""
return json.dumps([e.to_dict() for e in self.entries], indent=2)
def to_md(self):
"""Returns a markdown representation of the Journal"""
out = []
year, month = -1, -1
for e in self.entries:
if not e.date.year == year:
year = e.date.year
out.append(str(year))
out.append("=" * len(str(year)) + "\n")
if not e.date.month == month:
month = e.date.month
out.append(e.date.strftime("%B"))
out.append('-' * len(e.date.strftime("%B")) + "\n")
out.append(e.to_md())
return "\n".join(out)
def __repr__(self): def __repr__(self):
return "<Journal with %d entries>" % len(self.entries) return "<Journal with %d entries>" % len(self.entries)

25
jrnl/exporters.py Normal file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env python
# encoding: utf-8
try: import simplejson as json
except ImportError: import json
def to_json(journal):
"""Returns a JSON representation of the Journal."""
return json.dumps([e.to_dict() for e in journal.entries], indent=2)
def to_md(journal):
"""Returns a markdown representation of the Journal"""
out = []
year, month = -1, -1
for e in journal.entries:
if not e.date.year == year:
year = e.date.year
out.append(str(year))
out.append("=" * len(str(year)) + "\n")
if not e.date.month == month:
month = e.date.month
out.append(e.date.strftime("%B"))
out.append('-' * len(e.date.strftime("%B")) + "\n")
out.append(e.to_md())
return "\n".join(out)

View file

@ -8,6 +8,7 @@
""" """
import Journal import Journal
import exporters
from install import * from install import *
import os import os
import tempfile import tempfile
@ -196,10 +197,10 @@ def cli():
print_tags(journal) print_tags(journal)
elif args.json: # export to json elif args.json: # export to json
print(journal.to_json()) print(exporters.to_json(journal))
elif args.markdown: # export to json elif args.markdown: # export to json
print(journal.to_md()) print(exporters.to_md(journal))
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.")