Skip to content

Commit

Permalink
Rewrite keywords with spaces and dashes. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
ericof committed Aug 26, 2023
1 parent 5ea0394 commit 1151607
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/1.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rewrite keywords with spaces and dashes [@ericof]
20 changes: 19 additions & 1 deletion src/collective/mastodon/interpolators/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@
from zope.component import adapter


TRANSLATION_TABLE = str.maketrans(
{
"-": "",
"_": "",
"#": "",
"$": "",
".": "",
" ": "",
}
)


def tag_from_keyword(keyword: str) -> str:
"""Convert a keyword to a tag."""
keyword = keyword.translate(TRANSLATION_TABLE)
return f"#{keyword}"


@adapter(IDublinCore)
class TagsSubstitution(BaseSubstitution):
category = _PM("Dublin Core")
description = _("Tags")

def safe_call(self):
tags = [f"#{item}" for item in self.context.Subject()]
tags = [tag_from_keyword(item) for item in self.context.Subject()]
return " ".join(tags)
19 changes: 19 additions & 0 deletions tests/interpolators/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
from collective.mastodon.interpolators.adapters import tag_from_keyword
from plone import api
from plone.stringinterp.interfaces import IStringInterpolator

import pytest


class TestTagFromKeyword:
@pytest.mark.parametrize(
"keyword,tag",
[
("#Plone", "#Plone"),
("Plone", "#Plone"),
("plone", "#plone"),
("Open Source", "#OpenSource"),
("open source", "#opensource"),
("PloneGov-BR", "#PloneGovBR"),
("#PloneGov-BR", "#PloneGovBR"),
("IPO$", "#IPO"),
],
)
def test_tag_from_keyword(self, keyword: str, tag: str):
assert tag_from_keyword(keyword) == tag


class TestInterpolatorsTags:
@pytest.fixture(autouse=True)
def _init(self, portal):
Expand Down

0 comments on commit 1151607

Please sign in to comment.