Skip to content

Commit

Permalink
Tags with only tagsymbols should not be tags
Browse files Browse the repository at this point in the history
Text made up only of tagsymbols should not be treated as a tag.

Closes jrnl-org#1700
  • Loading branch information
comkieffer committed May 25, 2024
1 parent dc12d50 commit d2c842e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
13 changes: 12 additions & 1 deletion jrnl/journals/Entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,21 @@ 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"])
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/test_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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)


0 comments on commit d2c842e

Please sign in to comment.