diff --git a/docs/changes.rst b/docs/changes.rst index 33fa47aad..8cd8bb077 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -12,6 +12,9 @@ Changelog [reinhardt] - Support openpyxl 3.x [reinhardt] +- List links in tool + (`#2757 `_) + [reinhardt] 16.2.5 (2024-12-09) diff --git a/src/euphorie/content/browser/configure.zcml b/src/euphorie/content/browser/configure.zcml index 8bad46495..6a5bb08eb 100644 --- a/src/euphorie/content/browser/configure.zcml +++ b/src/euphorie/content/browser/configure.zcml @@ -522,4 +522,13 @@ layer="plonetheme.nuplone.skin.interfaces.NuPloneSkin" /> + + diff --git a/src/euphorie/content/browser/survey.py b/src/euphorie/content/browser/survey.py index 923615f31..aa4931fed 100644 --- a/src/euphorie/content/browser/survey.py +++ b/src/euphorie/content/browser/survey.py @@ -42,6 +42,8 @@ from zope.event import notify from zope.i18n import translate +import re + class SurveyBase(BrowserView): def _morph(self, child): @@ -520,3 +522,51 @@ def tools(self): # we're done if we found at least one measure break return tools + + +class ListLinks(BrowserView): + attributes_checked = [ + "description", + "introduction", + "solution_direction", + "legal_reference", + "action", + "action_plan", + "prevention_plan", + "requirements", + ] + url_regex = re.compile( + r"https?://[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b" + r"(?:[-a-zA-Z0-9()@:%_+.~#?&/=]*)" + ) + + def extract_links(self, obj): + links = [] + for attrib in self.attributes_checked: + value = getattr(aq_base(obj), attrib, "") + if value: + links.extend(self.url_regex.findall(value)) + if links: + yield { + "object": obj, + "links": [{"url": link, "text": link} for link in links], + } + if hasattr(obj, "objectValues"): + for child in obj.objectValues(): + for link in self.extract_links(child): + yield link + + @property + def items(self): + """Retrieves subobjects (modules, risks, etc.) which contain links. + Returns a list of dictionaries like this: + [ + { + "object": self.context.objectValues()[0], + "links": [ + {"url": "https://example.org/test", "text": "Example link"}, + ], + } + ] + """ + return self.extract_links(self.context) diff --git a/src/euphorie/content/browser/templates/list-links.pt b/src/euphorie/content/browser/templates/list-links.pt new file mode 100644 index 000000000..4e8c723ae --- /dev/null +++ b/src/euphorie/content/browser/templates/list-links.pt @@ -0,0 +1,35 @@ + + + + List of Links in ${context/title} + + + +