mirror of
https://codeberg.org/catask-org/catask.git
synced 2025-04-19 13:23:41 -05:00
add [btn][/btn] rendering, favicon processing...
This commit is contained in:
parent
da8b26ffb2
commit
a4168b17a4
1 changed files with 55 additions and 15 deletions
70
functions.py
70
functions.py
|
@ -4,6 +4,7 @@ from bleach.sanitizer import Cleaner
|
|||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from mistune import HTMLRenderer, escape
|
||||
from PIL import Image
|
||||
import mistune
|
||||
import humanize
|
||||
import mysql.connector
|
||||
|
@ -99,20 +100,34 @@ def getRandomWord():
|
|||
return random.choice(items)
|
||||
|
||||
def trimContent(var, trim):
|
||||
trimmed = var[:trim] + '…' if len(var) >= trim else var
|
||||
trimmed = trimmed.rstrip()
|
||||
return trimmed
|
||||
if int(trim) > 0:
|
||||
trimmed = var[:trim] + '…' if len(var) >= trim else var
|
||||
trimmed = trimmed.rstrip()
|
||||
return trimmed
|
||||
else:
|
||||
return var
|
||||
|
||||
class FixAsciiEmojis(HTMLRenderer):
|
||||
def text(self, text):
|
||||
if text.startswith('>') and text.endswith('<'):
|
||||
return text
|
||||
else:
|
||||
return escape(text)
|
||||
# mistune plugin
|
||||
inlineBtnPattern = r'\[btn\](?P<button_text>.+?)\[/btn\]'
|
||||
|
||||
def parse_inline_button(inline, m, state):
|
||||
text = m.group("button_text")
|
||||
state.append_token({"type": "inline_button", "raw": text})
|
||||
return m.end()
|
||||
|
||||
def render_inline_button(renderer, text):
|
||||
return f"<button class='btn btn-outline-secondary'>{text}</button>"
|
||||
|
||||
|
||||
def button(md):
|
||||
md.inline.register('inline_button', inlineBtnPattern, parse_inline_button, before='link')
|
||||
if md.renderer and md.renderer.NAME == 'html':
|
||||
md.renderer.register('inline_button', render_inline_button)
|
||||
|
||||
def renderMarkdown(text):
|
||||
plugins = [
|
||||
'strikethrough'
|
||||
'strikethrough',
|
||||
button
|
||||
]
|
||||
allowed_tags = [
|
||||
'p',
|
||||
|
@ -123,7 +138,8 @@ def renderMarkdown(text):
|
|||
'br',
|
||||
's',
|
||||
'del',
|
||||
'a'
|
||||
'a',
|
||||
'button'
|
||||
]
|
||||
allowed_attrs = {
|
||||
'a': 'href'
|
||||
|
@ -138,12 +154,11 @@ def renderMarkdown(text):
|
|||
md = mistune.create_markdown(
|
||||
escape=False,
|
||||
plugins=plugins,
|
||||
hard_wrap=True,
|
||||
renderer=FixAsciiEmojis()
|
||||
hard_wrap=True
|
||||
)
|
||||
html = md(text)
|
||||
# cleaner = Cleaner(tags=allowed_tags, attributes=allowed_attrs)
|
||||
# clean_html = cleaner.clean(html)
|
||||
cleaner = Cleaner(tags=allowed_tags, attributes=allowed_attrs)
|
||||
clean_html = cleaner.clean(html)
|
||||
return Markup(html)
|
||||
|
||||
def generateMetadata(question=None, answer=None):
|
||||
|
@ -165,3 +180,28 @@ def generateMetadata(question=None, answer=None):
|
|||
|
||||
# return 'metadata' dictionary
|
||||
return metadata
|
||||
|
||||
allowedFileExtensions = {'png', 'jpg', 'jpeg', 'webp', 'bmp', 'jxl'}
|
||||
|
||||
def allowedFile(filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in allowedFileExtensions
|
||||
|
||||
def generateFavicon(file_name):
|
||||
sizes = {
|
||||
'apple-touch-icon.png': (180, 180),
|
||||
'android-chrome-192x192.png': (192, 192),
|
||||
'android-chrome-512x512.png': (512, 512),
|
||||
'favicon-32x32.png': (32, 32),
|
||||
'favicon-16x16.png': (16, 16),
|
||||
'favicon.ico': (16, 16)
|
||||
}
|
||||
|
||||
img = Image.open(const.faviconDir / file_name)
|
||||
|
||||
if not os.path.exists(const.faviconDir):
|
||||
os.makedirs(const.faviconDir)
|
||||
|
||||
for filename, size in sizes.items():
|
||||
resized_img = img.resize(size)
|
||||
resized_img_absolute_path = const.faviconDir / filename
|
||||
resized_img.save(resized_img_absolute_path)
|
||||
|
|
Loading…
Add table
Reference in a new issue