From 44c1f55dead91cd9bb09909b6e51d09e18528b85 Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Thu, 5 Jul 2012 12:31:50 +0200 Subject: [PATCH] Refractored exporting --- jrnl/Journal.py | 20 -------------------- jrnl/exporters.py | 25 +++++++++++++++++++++++++ jrnl/jrnl.py | 5 +++-- 3 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 jrnl/exporters.py diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 495a20ab..4560fc33 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -149,26 +149,6 @@ class Journal: 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): return "" % len(self.entries) diff --git a/jrnl/exporters.py b/jrnl/exporters.py new file mode 100644 index 00000000..dfca5d78 --- /dev/null +++ b/jrnl/exporters.py @@ -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) diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 17b30ee3..8adb6267 100755 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -8,6 +8,7 @@ """ import Journal +import exporters from install import * import os import tempfile @@ -196,10 +197,10 @@ def cli(): print_tags(journal) elif args.json: # export to json - print(journal.to_json()) + print(exporters.to_json(journal)) 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: print("PyCrypto not found. To encrypt or decrypt your journal, install the PyCrypto package from http://www.pycrypto.org.")