From 2f1894c7659628a092204dd55a570a94a379f61d Mon Sep 17 00:00:00 2001 From: Kurt Neufeld Date: Thu, 31 Jul 2014 17:22:49 -0600 Subject: [PATCH] using a common tag regex for searching and highlighting --- jrnl/Entry.py | 8 +++++++- jrnl/Journal.py | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/jrnl/Entry.py b/jrnl/Entry.py index f04d9e0f..fa2650ca 100755 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -16,9 +16,15 @@ class Entry: self.starred = starred self.modified = False + @staticmethod + def tag_regex(tagsymbols): + pattern = r'(?u)\s([{tags}][-+*#/\w]+)'.format(tags=tagsymbols) + return re.compile( pattern, re.UNICODE ) + def parse_tags(self): fulltext = " " + " ".join([self.title, self.body]).lower() - tags = re.findall(r'(?u)\s([{tags}][-+*#/\w]+)'.format(tags=self.journal.config['tagsymbols']), fulltext, re.UNICODE) + tagsymbols = self.journal.config['tagsymbols'] + tags = re.findall( Entry.tag_regex(tagsymbols), fulltext ) self.tags = tags return set(tags) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 2dab065a..b94a5d36 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -165,9 +165,9 @@ class Journal(object): lambda match: util.colorize(match.group(0)), pp, re.UNICODE) else: - pp = re.sub(r"(?u)([{tags}]\w+)".format(tags=self.config['tagsymbols']), - lambda match: util.colorize(match.group(0)), - pp) + pp = re.sub( Entry.Entry.tag_regex(self.config['tagsymbols']), + lambda match: util.colorize(match.group(0)), + pp) return pp def __repr__(self):