diff --git a/jrnl/journals/Entry.py b/jrnl/journals/Entry.py index 02a0da906..3626de2c2 100644 --- a/jrnl/journals/Entry.py +++ b/jrnl/journals/Entry.py @@ -90,10 +90,22 @@ def tag_regex(tagsymbols: str) -> re.Pattern: def _parse_tags(self) -> set[str]: tagsymbols = self.journal.config["tagsymbols"] - return { + if len(tagsymbols) == 0: + return set() + + # We start by looking for everything that could be a tag (basically, any word + # that starts with a tag symbol) + tag_candidates = { tag.lower() for tag in re.findall(Entry.tag_regex(tagsymbols), self.text) } + # Then we drop all the candidates that are exclusively composed of tag symbols + actual_tags = { + tag for tag in tag_candidates if any([c not in tagsymbols for c in tag]) + } + + return actual_tags + def __str__(self): """Returns string representation of the entry to be written to journal file.""" date_str = self.date.strftime(self.journal.config["timeformat"]) diff --git a/tests/unit/test_entry.py b/tests/unit/test_entry.py new file mode 100644 index 000000000..18feeed41 --- /dev/null +++ b/tests/unit/test_entry.py @@ -0,0 +1,36 @@ +import pytest + +from jrnl.journals.Entry import Entry +from jrnl.journals.Journal import Journal + + +@pytest.mark.parametrize( + "text, tag_symbols, expected_tags", + [ + ("This has no tag symbols", "", []), + ("This has no tags and a single tag symbol", "@", []), + ("This has no tags and multiple tag symbols", "@#", []), + ("This has a @simple tag", "@", ["@simple"]), + ("Tag can contain some punctuation @simple-tag.", "@", ["@simple-tag"]), + ("This has a tag at the end of a sentence @simple.", "@", ["@simple"]), + ("This has an empty @ tag", "@", []), + ("This text has @multiple @tags", "@", ["@multiple", "@tags"]), + ("@@@@ This text has no tags", "@", []), + ("@@@# This text has no @### tags", "@#", []), + ("@@@#tag1 This text has two #@#@tags", "@#", ["@@@#tag1", "#@#@tags"]), + ( + "@prefix#tag1 This text has two #prefix@tag2", + "@#", + ["@prefix#tag1", "#prefix@tag2"], + ), + ], +) +def test_tag_extraction(text, tag_symbols, expected_tags): + jrnl = Journal() + jrnl.config["tagsymbols"] = tag_symbols + + entry = Entry(jrnl, date=None, text=text) + if entry.tags != expected_tags: + pass + + assert sorted(entry.tags) == sorted(expected_tags)