Better Unicode support

Closes #72
This commit is contained in:
Manuel Ebert 2013-06-09 15:55:03 -07:00
parent cf7581c0c0
commit 5bb8f9c567
5 changed files with 14 additions and 12 deletions

View file

@ -5,6 +5,7 @@ Changelog
* [New] JSON export exports tags as well. * [New] JSON export exports tags as well.
* [Improved] Nicer error message when there is a syntactical error in your config file. * [Improved] Nicer error message when there is a syntactical error in your config file.
* [Improved] Unicode support
#### 1.0.5 #### 1.0.5

View file

@ -15,16 +15,16 @@ class Entry:
def parse_tags(self): def parse_tags(self):
fulltext = " ".join([self.title, self.body]).lower() 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) self.tags = set(tags)
def __str__(self): def __unicode__(self):
"""Returns a string representation of the entry to be written into a journal file.""" """Returns a string representation of the entry to be written into a journal file."""
date_str = self.date.strftime(self.journal.config['timeformat']) date_str = self.date.strftime(self.journal.config['timeformat'])
title = date_str + " " + self.title title = date_str + " " + self.title
body = self.body.strip() body = self.body.strip()
return "{title}{sep}{body}\n".format( return u"{title}{sep}{body}\n".format(
title=title, title=title,
sep="\n" if self.body else "", sep="\n" if self.body else "",
body=body body=body
@ -50,7 +50,7 @@ class Entry:
# Suppress bodies that are just blanks and new lines. # 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) 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, title=title,
sep="\n" if has_body else "", sep="\n" if has_body else "",
body=body if has_body else "", body=body if has_body else "",
@ -74,7 +74,7 @@ class Entry:
space = "\n" space = "\n"
md_head = "###" md_head = "###"
return "{md} {date}, {title} {body} {space}".format( return u"{md} {date}, {title} {body} {space}".format(
md=md_head, md=md_head,
date=date_str, date=date_str,
title=self.title, title=self.title,

View file

@ -158,7 +158,7 @@ class Journal(object):
entry.parse_tags() entry.parse_tags()
return entries return entries
def __str__(self): def __unicode__(self):
"""Prettyprints the journal's entries""" """Prettyprints the journal's entries"""
sep = "\n" sep = "\n"
pp = sep.join([e.pprint() for e in self.entries]) 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) tagre = re.compile(re.escape(tag), re.IGNORECASE)
pp = re.sub(tagre, pp = re.sub(tagre,
lambda match: self._colorize(match.group(0)), lambda match: self._colorize(match.group(0)),
pp) pp, re.UNICODE)
else: 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)), lambda match: self._colorize(match.group(0)),
pp) pp)
return pp return pp
@ -181,7 +181,7 @@ class Journal(object):
def write(self, filename=None): def write(self, filename=None):
"""Dumps the journal into the config file, overwriting it""" """Dumps the journal into the config file, overwriting it"""
filename = filename or self.config['journal'] 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']: if self.config['encrypt']:
journal = self._encrypt(journal) journal = self._encrypt(journal)
with open(filename, 'wb') as journal_file: with open(filename, 'wb') as journal_file:

View file

@ -25,7 +25,7 @@ def to_tag_list(journal):
elif min(tag_counts)[0] == 0: elif min(tag_counts)[0] == 0:
tag_counts = filter(lambda x: x[0] > 1, tag_counts) tag_counts = filter(lambda x: x[0] > 1, tag_counts)
result += '[Removed tags that appear only once.]\n' 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 return result
def to_json(journal): def to_json(journal):

View file

@ -171,7 +171,8 @@ def cli():
# Writing mode # Writing mode
if mode_compose: if mode_compose:
raw = " ".join(args.text).strip() 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 entry.starred = args.star
print("[Entry added to {0} journal]".format(journal_name)) print("[Entry added to {0} journal]".format(journal_name))
journal.write() journal.write()
@ -183,7 +184,7 @@ def cli():
strict=args.strict, strict=args.strict,
short=args.short) short=args.short)
journal.limit(args.limit) journal.limit(args.limit)
print(journal) print(unicode(journal))
# Various export modes # Various export modes
elif args.tags: elif args.tags: