mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
change(FastAPI): decouple rendercomment logic from main
This commit decouples most of the rendercomment.py logic into a function, `update_comment_render`, which can be used by other Python modules to perform comment rendering. In addition, we silence some deprecation warnings from python-markdown by removing `md_globals` parameters from python-markdown callbacks. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
7961fa932a
commit
0d8216e8ea
1 changed files with 27 additions and 16 deletions
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import bleach
|
import bleach
|
||||||
|
@ -9,6 +10,7 @@ import pygit2
|
||||||
import aurweb.config
|
import aurweb.config
|
||||||
import aurweb.db
|
import aurweb.db
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
repo_path = aurweb.config.get('serve', 'repo-path')
|
repo_path = aurweb.config.get('serve', 'repo-path')
|
||||||
commit_uri = aurweb.config.get('options', 'commit_uri')
|
commit_uri = aurweb.config.get('options', 'commit_uri')
|
||||||
|
|
||||||
|
@ -24,7 +26,7 @@ class LinkifyExtension(markdown.extensions.Extension):
|
||||||
_urlre = (r'(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?'
|
_urlre = (r'(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?'
|
||||||
r'(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$)))')
|
r'(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$)))')
|
||||||
|
|
||||||
def extendMarkdown(self, md, md_globals):
|
def extendMarkdown(self, md):
|
||||||
processor = markdown.inlinepatterns.AutolinkInlineProcessor(self._urlre, md)
|
processor = markdown.inlinepatterns.AutolinkInlineProcessor(self._urlre, md)
|
||||||
# Register it right after the default <>-link processor (priority 120).
|
# Register it right after the default <>-link processor (priority 120).
|
||||||
md.inlinePatterns.register(processor, 'linkify', 119)
|
md.inlinePatterns.register(processor, 'linkify', 119)
|
||||||
|
@ -46,7 +48,7 @@ class FlysprayLinksInlineProcessor(markdown.inlinepatterns.InlineProcessor):
|
||||||
|
|
||||||
|
|
||||||
class FlysprayLinksExtension(markdown.extensions.Extension):
|
class FlysprayLinksExtension(markdown.extensions.Extension):
|
||||||
def extendMarkdown(self, md, md_globals):
|
def extendMarkdown(self, md):
|
||||||
processor = FlysprayLinksInlineProcessor(r'\bFS#(\d+)\b', md)
|
processor = FlysprayLinksInlineProcessor(r'\bFS#(\d+)\b', md)
|
||||||
md.inlinePatterns.register(processor, 'flyspray-links', 118)
|
md.inlinePatterns.register(processor, 'flyspray-links', 118)
|
||||||
|
|
||||||
|
@ -90,9 +92,12 @@ class GitCommitsExtension(markdown.extensions.Extension):
|
||||||
self._head = head
|
self._head = head
|
||||||
super(markdown.extensions.Extension, self).__init__()
|
super(markdown.extensions.Extension, self).__init__()
|
||||||
|
|
||||||
def extendMarkdown(self, md, md_globals):
|
def extendMarkdown(self, md):
|
||||||
processor = GitCommitsInlineProcessor(md, self._head)
|
try:
|
||||||
md.inlinePatterns.register(processor, 'git-commits', 117)
|
processor = GitCommitsInlineProcessor(md, self._head)
|
||||||
|
md.inlinePatterns.register(processor, 'git-commits', 117)
|
||||||
|
except pygit2.GitError:
|
||||||
|
logger.error(f"No git repository found for '{self._head}'.")
|
||||||
|
|
||||||
|
|
||||||
class HeadingTreeprocessor(markdown.treeprocessors.Treeprocessor):
|
class HeadingTreeprocessor(markdown.treeprocessors.Treeprocessor):
|
||||||
|
@ -105,7 +110,7 @@ class HeadingTreeprocessor(markdown.treeprocessors.Treeprocessor):
|
||||||
|
|
||||||
|
|
||||||
class HeadingExtension(markdown.extensions.Extension):
|
class HeadingExtension(markdown.extensions.Extension):
|
||||||
def extendMarkdown(self, md, md_globals):
|
def extendMarkdown(self, md):
|
||||||
# Priority doesn't matter since we don't conflict with other processors.
|
# Priority doesn't matter since we don't conflict with other processors.
|
||||||
md.treeprocessors.register(HeadingTreeprocessor(md), 'heading', 30)
|
md.treeprocessors.register(HeadingTreeprocessor(md), 'heading', 30)
|
||||||
|
|
||||||
|
@ -123,19 +128,20 @@ def save_rendered_comment(conn, commentid, html):
|
||||||
[html, commentid])
|
[html, commentid])
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def update_comment_render(commentid):
|
||||||
commentid = int(sys.argv[1])
|
|
||||||
|
|
||||||
conn = aurweb.db.Connection()
|
conn = aurweb.db.Connection()
|
||||||
|
|
||||||
text, pkgbase = get_comment(conn, commentid)
|
text, pkgbase = get_comment(conn, commentid)
|
||||||
html = markdown.markdown(text, extensions=['fenced_code',
|
html = markdown.markdown(text, extensions=[
|
||||||
LinkifyExtension(),
|
'fenced_code',
|
||||||
FlysprayLinksExtension(),
|
LinkifyExtension(),
|
||||||
GitCommitsExtension(pkgbase),
|
FlysprayLinksExtension(),
|
||||||
HeadingExtension()])
|
GitCommitsExtension(pkgbase),
|
||||||
allowed_tags = (bleach.sanitizer.ALLOWED_TAGS +
|
HeadingExtension()
|
||||||
['p', 'pre', 'h4', 'h5', 'h6', 'br', 'hr'])
|
])
|
||||||
|
|
||||||
|
allowed_tags = (bleach.sanitizer.ALLOWED_TAGS
|
||||||
|
+ ['p', 'pre', 'h4', 'h5', 'h6', 'br', 'hr'])
|
||||||
html = bleach.clean(html, tags=allowed_tags)
|
html = bleach.clean(html, tags=allowed_tags)
|
||||||
save_rendered_comment(conn, commentid, html)
|
save_rendered_comment(conn, commentid, html)
|
||||||
|
|
||||||
|
@ -143,5 +149,10 @@ def main():
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
commentid = int(sys.argv[1])
|
||||||
|
update_comment_render(commentid)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue