From 5bb8f9c567f0c70191a56866e6e597ee91ff312d Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Sun, 9 Jun 2013 15:55:03 -0700 Subject: [PATCH] Better Unicode support Closes #72 --- CHANGELOG.md | 1 + jrnl/Entry.py | 10 +++++----- jrnl/Journal.py | 8 ++++---- jrnl/exporters.py | 2 +- jrnl/jrnl.py | 5 +++-- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02b3e293..f028be36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Changelog * [New] JSON export exports tags as well. * [Improved] Nicer error message when there is a syntactical error in your config file. +* [Improved] Unicode support #### 1.0.5 diff --git a/jrnl/Entry.py b/jrnl/Entry.py index 8f3663ab..624b6197 100644 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -15,16 +15,16 @@ class Entry: def parse_tags(self): fulltext = " ".join([self.title, self.body]).lower() - tags = re.findall(r"([%s]\w+)" % self.journal.config['tagsymbols'], fulltext) + tags = re.findall(ur'([{}]\w+)'.format(self.journal.config['tagsymbols']), fulltext, re.UNICODE) self.tags = set(tags) - def __str__(self): + def __unicode__(self): """Returns a string representation of the entry to be written into a journal file.""" date_str = self.date.strftime(self.journal.config['timeformat']) title = date_str + " " + self.title body = self.body.strip() - return "{title}{sep}{body}\n".format( + return u"{title}{sep}{body}\n".format( title=title, sep="\n" if self.body else "", body=body @@ -50,7 +50,7 @@ class Entry: # Suppress bodies that are just blanks and new lines. has_body = len(self.body) > 20 or not all(char in (" ", "\n") for char in self.body) - return "{title}{sep}{body}\n".format( + return u"{title}{sep}{body}\n".format( title=title, sep="\n" if has_body else "", body=body if has_body else "", @@ -74,7 +74,7 @@ class Entry: space = "\n" md_head = "###" - return "{md} {date}, {title} {body} {space}".format( + return u"{md} {date}, {title} {body} {space}".format( md=md_head, date=date_str, title=self.title, diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 35f59f3a..9cbe41e2 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -158,7 +158,7 @@ class Journal(object): entry.parse_tags() return entries - def __str__(self): + def __unicode__(self): """Prettyprints the journal's entries""" sep = "\n" pp = sep.join([e.pprint() for e in self.entries]) @@ -168,9 +168,9 @@ class Journal(object): tagre = re.compile(re.escape(tag), re.IGNORECASE) pp = re.sub(tagre, lambda match: self._colorize(match.group(0)), - pp) + pp, re.UNICODE) else: - pp = re.sub(r"([%s]\w+)" % self.config['tagsymbols'], + pp = re.sub(ur"(?u)([{}]\w+)".format(self.config['tagsymbols']), lambda match: self._colorize(match.group(0)), pp) return pp @@ -181,7 +181,7 @@ class Journal(object): def write(self, filename=None): """Dumps the journal into the config file, overwriting it""" filename = filename or self.config['journal'] - journal = "\n".join([str(e) for e in self.entries]) + journal = "\n".join([unicode(e) 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 490af6e2..4a4b0245 100644 --- a/jrnl/exporters.py +++ b/jrnl/exporters.py @@ -25,7 +25,7 @@ def to_tag_list(journal): elif min(tag_counts)[0] == 0: tag_counts = filter(lambda x: x[0] > 1, tag_counts) result += '[Removed tags that appear only once.]\n' - result += "\n".join("{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=False)) + result += "\n".join(u"{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=False)) return result def to_json(journal): diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 4fc368c8..2af9b60c 100755 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -171,7 +171,8 @@ def cli(): # Writing mode if mode_compose: raw = " ".join(args.text).strip() - entry = journal.new_entry(raw, args.date) + unicode_raw = raw.decode(sys.getfilesystemencoding()) + entry = journal.new_entry(unicode_raw, args.date) entry.starred = args.star print("[Entry added to {0} journal]".format(journal_name)) journal.write() @@ -183,7 +184,7 @@ def cli(): strict=args.strict, short=args.short) journal.limit(args.limit) - print(journal) + print(unicode(journal)) # Various export modes elif args.tags: