diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 60118184..4c49ad39 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -179,13 +179,16 @@ class Journal(object): pp) return pp + def pprint(self): + return self.__unicode__() + def __repr__(self): return "".format(len(self.entries)) def write(self, filename=None): """Dumps the journal into the config file, overwriting it""" filename = filename or self.config['journal'] - journal = "\n".join([unicode(e) for e in self.entries]) + journal = "\n".join([e.__unicode__() for e in self.entries]) if self.config['encrypt']: journal = self._encrypt(journal) with open(filename, 'wb') as journal_file: diff --git a/jrnl/exporters.py b/jrnl/exporters.py index 5bd687cf..4e2a9492 100644 --- a/jrnl/exporters.py +++ b/jrnl/exporters.py @@ -7,6 +7,9 @@ try: from slugify import slugify except ImportError: import slugify try: import simplejson as json except ImportError: import json +try: from .util import u +except (SystemError, ValueError): from util import u + def get_tags_count(journal): """Returns a set of tuples (count, tag) for all tags present in the journal.""" @@ -60,7 +63,7 @@ def to_md(journal): def to_txt(journal): """Returns the complete text of the Journal.""" - return unicode(journal) + return journal.pprint() def export(journal, format, output=None): """Exports the journal to various formats. @@ -93,7 +96,7 @@ def export(journal, format, output=None): def write_files(journal, path, format): """Turns your journal into separate files for each entry. Format should be either json, md or txt.""" - make_filename = lambda entry: e.date.strftime("%C-%m-%d_{}.{}".format(slugify(unicode(e.title)), format)) + make_filename = lambda entry: e.date.strftime("%C-%m-%d_{}.{}".format(slugify(u(e.title)), format)) for e in journal.entries: full_path = os.path.join(path, make_filename(e)) if format == 'json': @@ -101,7 +104,7 @@ def write_files(journal, path, format): elif format == 'md': content = e.to_md() elif format == 'txt': - content = unicode(e) + content = u(e) with open(full_path, 'w') as f: f.write(content) return "[Journal exported individual files in {}]".format(path) diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 3eba8700..37a310b7 100755 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -169,7 +169,7 @@ def cli(manual_args=None): # Writing mode if mode_compose: raw = " ".join(args.text).strip() - if type(raw) is not unicode: + if util.PY2 and type(raw) is not unicode: raw = raw.decode(sys.getfilesystemencoding()) entry = journal.new_entry(raw, args.date) entry.starred = args.star @@ -183,7 +183,7 @@ def cli(manual_args=None): strict=args.strict, short=args.short) journal.limit(args.limit) - print(unicode(journal)) + print(journal.pprint()) # Various export modes elif args.tags: diff --git a/jrnl/util.py b/jrnl/util.py index 5f7f0c52..4279bab0 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -3,10 +3,15 @@ import sys import os from tzlocal import get_localzone +PY3 = sys.version_info[0] == 3 +PY2 = sys.version_info[0] == 2 + +def u(s): + """Mock unicode function for python 2 and 3 compatibility.""" + return s if PY3 else unicode(s, "unicode_escape") STDIN = sys.stdin STDOUT = sys.stdout - __cached_tz = None def py23_input(msg):