Skip to content

Commit

Permalink
Merge pull request #2 from collective/issue-1-keywords
Browse files Browse the repository at this point in the history
Rewrite keywords with spaces and dashes
  • Loading branch information
ericof authored Aug 26, 2023
2 parents 28833ff + 1151607 commit 551f9b4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
- Nothing changed yet.


## 1.0.0a1 (2023-08-25)
## 1.0.0a1 (2023-08-25)

- Initial release [@ericof]
1 change: 0 additions & 1 deletion news/0.feature

This file was deleted.

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 551f9b4

Please sign in to comment.