Skip to content

Commit

Permalink
double comma barrier for tags
Browse files Browse the repository at this point in the history
With a double comma before tags,
hashes can be anywhere in the activity, category or description
  • Loading branch information
ederag committed Nov 20, 2019
1 parent 20ef585 commit 93bed5f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/hamster/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -336,7 +338,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()]
Expand All @@ -363,7 +365,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 {}
Expand Down
6 changes: 3 additions & 3 deletions tests/stuff_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ 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
assert activity.end_time is None

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")
Expand Down

0 comments on commit 93bed5f

Please sign in to comment.