mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-11 09:08:32 +02:00
Cuddle timestamp in brackets to fix #318
This commit is contained in:
parent
a5f08e6081
commit
57007a8266
2 changed files with 36 additions and 38 deletions
|
@ -32,7 +32,7 @@ class Entry:
|
||||||
def __unicode__(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.rstrip("\n ")
|
title = "[{}] {}".format(date_str, self.title.rstrip("\n "))
|
||||||
if self.starred:
|
if self.starred:
|
||||||
title += " *"
|
title += " *"
|
||||||
return "{title}{sep}{body}\n".format(
|
return "{title}{sep}{body}\n".format(
|
||||||
|
@ -48,7 +48,8 @@ class Entry:
|
||||||
if not short and self.journal.config['linewrap']:
|
if not short and self.journal.config['linewrap']:
|
||||||
title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap'])
|
title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap'])
|
||||||
body = "\n".join([
|
body = "\n".join([
|
||||||
textwrap.fill((line + " ") if (len(line) == 0) else line,
|
textwrap.fill(
|
||||||
|
(line + " ") if (len(line) == 0) else line,
|
||||||
self.journal.config['linewrap'],
|
self.journal.config['linewrap'],
|
||||||
initial_indent="| ",
|
initial_indent="| ",
|
||||||
subsequent_indent="| ",
|
subsequent_indent="| ",
|
||||||
|
|
|
@ -77,22 +77,19 @@ class Journal(object):
|
||||||
|
|
||||||
def _parse(self, journal_txt):
|
def _parse(self, journal_txt):
|
||||||
"""Parses a journal that's stored in a string and returns a list of entries"""
|
"""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
|
# Initialise our current entry
|
||||||
entries = []
|
entries = []
|
||||||
current_entry = None
|
current_entry = None
|
||||||
|
date_blob_re = re.compile("^\[.+\] ")
|
||||||
for line in journal_txt.splitlines():
|
for line in journal_txt.splitlines():
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
try:
|
date_blob = date_blob_re.findall(line)
|
||||||
# try to parse line as date => new entry begins
|
if date_blob:
|
||||||
new_date = datetime.strptime(line[:date_length], self.config['timeformat'])
|
date_blob = date_blob[0]
|
||||||
|
new_date = time.parse(date_blob.strip(" []"))
|
||||||
# parsing successful => save old entry and create new one
|
if new_date:
|
||||||
if new_date and current_entry:
|
# Found a date at the start of the line: This is a new entry.
|
||||||
|
if current_entry:
|
||||||
entries.append(current_entry)
|
entries.append(current_entry)
|
||||||
|
|
||||||
if line.endswith("*"):
|
if line.endswith("*"):
|
||||||
|
@ -101,12 +98,15 @@ class Journal(object):
|
||||||
else:
|
else:
|
||||||
starred = False
|
starred = False
|
||||||
|
|
||||||
current_entry = Entry.Entry(self, date=new_date, title=line[date_length + 1:], starred=starred)
|
current_entry = Entry.Entry(
|
||||||
except ValueError:
|
self,
|
||||||
# Happens when we can't parse the start of the line as an date.
|
date=new_date,
|
||||||
# In this case, just append line to our body.
|
title=line[len(date_blob) + 1:],
|
||||||
if current_entry:
|
starred=starred
|
||||||
current_entry.body += line + u"\n"
|
)
|
||||||
|
elif current_entry:
|
||||||
|
# Didn't find a date - keep on feeding to current entry.
|
||||||
|
current_entry.body += line + "\n"
|
||||||
|
|
||||||
# Append last entry
|
# Append last entry
|
||||||
if current_entry:
|
if current_entry:
|
||||||
|
@ -238,9 +238,6 @@ class Journal(object):
|
||||||
|
|
||||||
|
|
||||||
class PlainJournal(Journal):
|
class PlainJournal(Journal):
|
||||||
def __init__(self, name='default', **kwargs):
|
|
||||||
super(PlainJournal, self).__init__(name, **kwargs)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _create(cls, filename):
|
def _create(cls, filename):
|
||||||
with codecs.open(filename, "a", "utf-8"):
|
with codecs.open(filename, "a", "utf-8"):
|
||||||
|
|
Loading…
Add table
Reference in a new issue