From 17f5e82a2a568ed0fb7d79883e9553ed6c80b297 Mon Sep 17 00:00:00 2001 From: Darks Date: Sun, 24 Apr 2022 17:50:46 +0200 Subject: [PATCH] pclinks: switched to <> as delimiters (#108) And some other enhancements --- app/templates/widgets/download_button.html | 8 ++++++ app/utils/markdown_extensions/pclinks.py | 32 ++++++---------------- 2 files changed, 16 insertions(+), 24 deletions(-) create mode 100644 app/templates/widgets/download_button.html diff --git a/app/templates/widgets/download_button.html b/app/templates/widgets/download_button.html new file mode 100644 index 0000000..e0f9b41 --- /dev/null +++ b/app/templates/widgets/download_button.html @@ -0,0 +1,8 @@ +{% macro download_button(file) %} + + TLTitre.g3a + {{ file.size | humanize(unit='o') }} + +{% endmacro %} + +{{ download_button(file) if file }} diff --git a/app/utils/markdown_extensions/pclinks.py b/app/utils/markdown_extensions/pclinks.py index 58911fd..f674f60 100644 --- a/app/utils/markdown_extensions/pclinks.py +++ b/app/utils/markdown_extensions/pclinks.py @@ -36,7 +36,7 @@ class PCLinkExtension(Extension): self.md = md # append to end of inline patterns - PCLINK_RE = r'\[\[([a-z]+): ?(\w+)\]\]' + PCLINK_RE = r'<([a-z]+): ?(\w+)>' pclinkPattern = PCLinksInlineProcessor(PCLINK_RE, self.getConfigs()) pclinkPattern.md = md md.inlinePatterns.register(pclinkPattern, 'pclink', 135) @@ -73,12 +73,10 @@ class PCLinksInlineProcessor(InlineProcessor): # - either an xml.etree.ElementTree def handlePoll(content_id, context): - if not context.startswith("[[") or not context.endswith("]]"): - return "[Sondage invalide]" try: id = int(content_id) except ValueError: - return "[ID du sondage invalide]" + return "[ID de sondage invalide]" poll = Poll.query.get(content_id) @@ -93,7 +91,7 @@ def handleTopic(content_id, context): try: id = int(content_id) except ValueError: - return "[ID du topic invalide]" + return "[ID de topic invalide]" topic = Topic.query.get(content_id) @@ -129,27 +127,13 @@ def handleFile(content_id, context): try: content_id = int(content_id) except ValueError: - return "[Fichier invalide]" + return "[ID de fichier invalide]" file = Attachment.query.get(content_id) if file is None: - return "[Fichier inconnu]" + return "[Fichier non trouvé]" - # Build element manually to avoid code injection - container = etree.Element('div') - container.set('class', 'dl-button') - - xtitle = etree.SubElement(container, 'a') - xtitle.text = file.name - xtitle.set('href', file.url) - - xsize = etree.SubElement(container, 'span') - xsize.text = humanize(file.size, unit='o') - - # - # TLTitre.g3a - # {{ file.size | humanize(unit='o') }} - # - - return container + html = render_template('widgets/download_button.html', file=file) + html = html.replace('\n', '') # Needed to avoid lots of
due to etree + return etree.fromstring(html)