From 57007a8266d43e7ccfefe773fc9704d0a1028fa8 Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Sun, 5 Apr 2015 02:19:03 +0400 Subject: [PATCH] Cuddle timestamp in brackets to fix #318 --- jrnl/Entry.py | 25 +++++++++++++------------ jrnl/Journal.py | 49 +++++++++++++++++++++++-------------------------- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/jrnl/Entry.py b/jrnl/Entry.py index 34503cd9..4504982b 100755 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -20,19 +20,19 @@ class Entry: @staticmethod def tag_regex(tagsymbols): pattern = r'(?u)\s([{tags}][-+*#/\w]+)'.format(tags=tagsymbols) - return re.compile( pattern, re.UNICODE ) + return re.compile(pattern, re.UNICODE) def parse_tags(self): - fulltext = " " + " ".join([self.title, self.body]).lower() + fulltext = " " + " ".join([self.title, self.body]).lower() tagsymbols = self.journal.config['tagsymbols'] - tags = re.findall( Entry.tag_regex(tagsymbols), fulltext ) + tags = re.findall(Entry.tag_regex(tagsymbols), fulltext) self.tags = tags return set(tags) 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.rstrip("\n ") + title = "[{}] {}".format(date_str, self.title.rstrip("\n ")) if self.starred: title += " *" return "{title}{sep}{body}\n".format( @@ -48,13 +48,14 @@ class Entry: if not short and self.journal.config['linewrap']: title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap']) body = "\n".join([ - textwrap.fill((line + " ") if (len(line) == 0) else line, - self.journal.config['linewrap'], - initial_indent="| ", - subsequent_indent="| ", - drop_whitespace=False) - for line in self.body.rstrip(" \n").splitlines() - ]) + textwrap.fill( + (line + " ") if (len(line) == 0) else line, + self.journal.config['linewrap'], + initial_indent="| ", + subsequent_indent="| ", + drop_whitespace=False) + for line in self.body.rstrip(" \n").splitlines() + ]) else: title = date_str + " " + self.title.rstrip("\n ") body = self.body.rstrip("\n ") @@ -83,7 +84,7 @@ class Entry: or self.body.rstrip() != other.body.rstrip() \ or self.date != other.date \ or self.starred != other.starred: - return False + return False return True def __ne__(self, other): diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 7e3a36c6..ef42f2cd 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -77,36 +77,36 @@ class Journal(object): def _parse(self, journal_txt): """Parses a journal that's stored in a string and returns a list of entries""" - - # Entries start with a line that looks like 'date title' - let's figure out how - # long the date will be by constructing one - date_length = len(datetime.today().strftime(self.config['timeformat'])) - # Initialise our current entry entries = [] current_entry = None + date_blob_re = re.compile("^\[.+\] ") for line in journal_txt.splitlines(): line = line.rstrip() - try: - # try to parse line as date => new entry begins - new_date = datetime.strptime(line[:date_length], self.config['timeformat']) + date_blob = date_blob_re.findall(line) + if date_blob: + date_blob = date_blob[0] + new_date = time.parse(date_blob.strip(" []")) + if new_date: + # Found a date at the start of the line: This is a new entry. + if current_entry: + entries.append(current_entry) - # parsing successful => save old entry and create new one - if new_date and current_entry: - entries.append(current_entry) + if line.endswith("*"): + starred = True + line = line[:-1] + else: + starred = False - if line.endswith("*"): - starred = True - line = line[:-1] - else: - starred = False - - current_entry = Entry.Entry(self, date=new_date, title=line[date_length + 1:], starred=starred) - except ValueError: - # Happens when we can't parse the start of the line as an date. - # In this case, just append line to our body. - if current_entry: - current_entry.body += line + u"\n" + current_entry = Entry.Entry( + self, + date=new_date, + title=line[len(date_blob) + 1:], + starred=starred + ) + elif current_entry: + # Didn't find a date - keep on feeding to current entry. + current_entry.body += line + "\n" # Append last entry if current_entry: @@ -238,9 +238,6 @@ class Journal(object): class PlainJournal(Journal): - def __init__(self, name='default', **kwargs): - super(PlainJournal, self).__init__(name, **kwargs) - @classmethod def _create(cls, filename): with codecs.open(filename, "a", "utf-8"):