From 823bf2762c82cff1a054495c2ea07c7b0af601dd Mon Sep 17 00:00:00 2001 From: ederag Date: Wed, 20 Nov 2019 01:11:06 +0100 Subject: [PATCH] double comma barrier for tags With a double comma before tags, hashes can be anywhere in the activity, category or description --- src/hamster/lib/__init__.py | 14 +++++++++----- tests/stuff_test.py | 6 +++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/hamster/lib/__init__.py b/src/hamster/lib/__init__.py index 017226a25..963cb4041 100644 --- a/src/hamster/lib/__init__.py +++ b/src/hamster/lib/__init__.py @@ -21,10 +21,12 @@ # match #tag followed by any space or # that will be ignored # tag must not contain #, comma, or any space character tag_re = re.compile(r""" - [\s,]* # any spaces or commas (or nothing) \# # hash character - ([^#\s,]+) # the tag (anything but #, spaces or comma) - [\s#,]* # any spaces, #, or commas (or nothing) + ([^#,]+) # the tag (anything but hash or comma) + \s* # maybe spaces + # forbid double comma (that would be the left barrier of tags) + ,? # single comma (or none) + \s* # maybe space $ # end of text """, flags=re.VERBOSE) @@ -195,6 +197,8 @@ def serialized_name(self): res += ", %s" % self.description if self.tags: + # double comma is a left barrier for tags + res += ",, " res += " %s" % " ".join("#%s" % tag for tag in self.tags) return res @@ -336,7 +340,7 @@ def parse_fact(text, phase=None, res=None, date=None): m = re.search(tag_re, remaining_text) if not m: break - tag = m.group(1) + tag = m.group(1).strip() # strip the matched string (including #) backup_text = remaining_text remaining_text = remaining_text[:m.start()] @@ -363,7 +367,7 @@ def parse_fact(text, phase=None, res=None, date=None): if "category" in phases: category, _, description = text.partition(",") res["category"] = category.lstrip("@").strip() or None - res["description"] = description.strip() or None + res["description"] = description.strip(", ") or None return res return {} diff --git a/tests/stuff_test.py b/tests/stuff_test.py index 13bb70a0e..8aba70159 100644 --- a/tests/stuff_test.py +++ b/tests/stuff_test.py @@ -82,9 +82,9 @@ def test_description(self): def test_tags(self): # plain activity name - activity = Fact.parse("#case, with added #de description #and, #some #tägs") + activity = Fact.parse("#case, description with #hash,, #and, #some #tägs") self.assertEqual(activity.activity, "#case") - self.assertEqual(activity.description, "with added #de description") + self.assertEqual(activity.description, "description with #hash") self.assertEqual(set(activity.tags), set(["and", "some", "tägs"])) assert not activity.category assert activity.start_time is None @@ -92,7 +92,7 @@ def test_tags(self): def test_full(self): # plain activity name - activity = Fact.parse("1225-1325 case@cat, description #ta non-tag #tag #bäg") + activity = Fact.parse("1225-1325 case@cat, description #ta non-tag,, #tag #bäg") self.assertEqual(activity.start_time.strftime("%H:%M"), "12:25") self.assertEqual(activity.end_time.strftime("%H:%M"), "13:25") self.assertEqual(activity.activity, "case")