pclinks: switched to <> as delimiters (#108)

And some other enhancements
This commit is contained in:
Darks 2022-04-24 17:50:46 +02:00
parent 2119329997
commit 17f5e82a2a
Signed by: Darks
GPG Key ID: 7515644268BE1433
2 changed files with 16 additions and 24 deletions

View File

@ -0,0 +1,8 @@
{% macro download_button(file) %}
<span class="dl-button">
<a href="{{ file.url }}">TLTitre.g3a</a>
<span>{{ file.size | humanize(unit='o') }}</span>
</span>
{% endmacro %}
{{ download_button(file) if file }}

View File

@ -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')
# <span class="dl-button">
# <a href="{{ file.url }}">TLTitre.g3a</a>
# <span>{{ file.size | humanize(unit='o') }}</span>
# </span>
return container
html = render_template('widgets/download_button.html', file=file)
html = html.replace('\n', '') # Needed to avoid lots of <br> due to etree
return etree.fromstring(html)