mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +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
|
@ -20,19 +20,19 @@ class Entry:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def tag_regex(tagsymbols):
|
def tag_regex(tagsymbols):
|
||||||
pattern = r'(?u)\s([{tags}][-+*#/\w]+)'.format(tags=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):
|
def parse_tags(self):
|
||||||
fulltext = " " + " ".join([self.title, self.body]).lower()
|
fulltext = " " + " ".join([self.title, self.body]).lower()
|
||||||
tagsymbols = self.journal.config['tagsymbols']
|
tagsymbols = self.journal.config['tagsymbols']
|
||||||
tags = re.findall( Entry.tag_regex(tagsymbols), fulltext )
|
tags = re.findall(Entry.tag_regex(tagsymbols), fulltext)
|
||||||
self.tags = tags
|
self.tags = tags
|
||||||
return set(tags)
|
return set(tags)
|
||||||
|
|
||||||
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,13 +48,14 @@ 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(
|
||||||
self.journal.config['linewrap'],
|
(line + " ") if (len(line) == 0) else line,
|
||||||
initial_indent="| ",
|
self.journal.config['linewrap'],
|
||||||
subsequent_indent="| ",
|
initial_indent="| ",
|
||||||
drop_whitespace=False)
|
subsequent_indent="| ",
|
||||||
for line in self.body.rstrip(" \n").splitlines()
|
drop_whitespace=False)
|
||||||
])
|
for line in self.body.rstrip(" \n").splitlines()
|
||||||
|
])
|
||||||
else:
|
else:
|
||||||
title = date_str + " " + self.title.rstrip("\n ")
|
title = date_str + " " + self.title.rstrip("\n ")
|
||||||
body = self.body.rstrip("\n ")
|
body = self.body.rstrip("\n ")
|
||||||
|
@ -83,7 +84,7 @@ class Entry:
|
||||||
or self.body.rstrip() != other.body.rstrip() \
|
or self.body.rstrip() != other.body.rstrip() \
|
||||||
or self.date != other.date \
|
or self.date != other.date \
|
||||||
or self.starred != other.starred:
|
or self.starred != other.starred:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
|
|
|
@ -77,36 +77,36 @@ 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(" []"))
|
||||||
|
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 line.endswith("*"):
|
||||||
if new_date and current_entry:
|
starred = True
|
||||||
entries.append(current_entry)
|
line = line[:-1]
|
||||||
|
else:
|
||||||
|
starred = False
|
||||||
|
|
||||||
if line.endswith("*"):
|
current_entry = Entry.Entry(
|
||||||
starred = True
|
self,
|
||||||
line = line[:-1]
|
date=new_date,
|
||||||
else:
|
title=line[len(date_blob) + 1:],
|
||||||
starred = False
|
starred=starred
|
||||||
|
)
|
||||||
current_entry = Entry.Entry(self, date=new_date, title=line[date_length + 1:], starred=starred)
|
elif current_entry:
|
||||||
except ValueError:
|
# Didn't find a date - keep on feeding to current entry.
|
||||||
# Happens when we can't parse the start of the line as an date.
|
current_entry.body += line + "\n"
|
||||||
# In this case, just append line to our body.
|
|
||||||
if current_entry:
|
|
||||||
current_entry.body += line + u"\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