mirror of
https://codeberg.org/catask-org/catask.git
synced 2025-04-20 05:43:41 -05:00
improve markdown rendering, add error handlers, simplify index route logic
This commit is contained in:
parent
4557b166bb
commit
71cbd9e54b
2 changed files with 74 additions and 60 deletions
67
app.py
67
app.py
|
@ -110,47 +110,50 @@ def inject_stuff():
|
||||||
return dict(metadata=func.generateMetadata(), len=len, str=str, const=const, cfg=cfg, logged_in=logged_in, version_id=const.version_id, version=const.version, appName=const.appName, questionCount=questionCount)
|
return dict(metadata=func.generateMetadata(), len=len, str=str, const=const, cfg=cfg, logged_in=logged_in, version_id=const.version_id, version=const.version, appName=const.appName, questionCount=questionCount)
|
||||||
|
|
||||||
# -- template filters --
|
# -- template filters --
|
||||||
|
|
||||||
|
# {text} | render_markdown
|
||||||
@app.template_filter('render_markdown')
|
@app.template_filter('render_markdown')
|
||||||
def render_markdown(text):
|
def render_markdown(text):
|
||||||
|
if text.startswith("![") and not (text.startswith(':') and text.endswith(':')):
|
||||||
|
return text
|
||||||
|
else:
|
||||||
# app.logger.debug("[CatAsk] app.template_filter render_markdown(text) triggered")
|
# app.logger.debug("[CatAsk] app.template_filter render_markdown(text) triggered")
|
||||||
return func.renderMarkdown(text)
|
return func.renderMarkdown(text)
|
||||||
|
|
||||||
|
# render_markdown({text}, fromWho=False|True)
|
||||||
|
@app.template_global('render_markdown')
|
||||||
|
def render_markdown(text, fromWho=False):
|
||||||
|
if (text.startswith("![") or "![" in text) and not (text.startswith(':') and text.endswith(':')):
|
||||||
|
# ensuring that only inline images get escaped, not emojis
|
||||||
|
escaped = text.replace("![", "!\[")
|
||||||
|
return func.renderMarkdown(escaped)
|
||||||
|
else:
|
||||||
|
# don't want people inserting buttons and other stuff into name field
|
||||||
|
allowed_tags = ["p", "img"] if fromWho else None
|
||||||
|
return func.renderMarkdown(text, allowed_tags)
|
||||||
|
|
||||||
|
# -- error handlers --
|
||||||
|
|
||||||
|
@app.errorhandler(404)
|
||||||
|
def notFound(e):
|
||||||
|
return render_template('errors/404.html'), 404
|
||||||
|
|
||||||
|
@app.errorhandler(500)
|
||||||
|
def internalServerError(e):
|
||||||
|
return render_template('errors/500.html'), 500
|
||||||
|
|
||||||
|
@app.errorhandler(502)
|
||||||
|
def badGateway(e):
|
||||||
|
return render_template('errors/502.html'), 502
|
||||||
|
|
||||||
# -- client (frontend) routes --
|
# -- client (frontend) routes --
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
def index():
|
def index():
|
||||||
conn = func.connectToDb()
|
# func.getAllQuestions() returns combined and metadata
|
||||||
cursor = conn.cursor(dictionary=True)
|
func_val = func.getAllQuestions()
|
||||||
|
combined = func_val[0]
|
||||||
app.logger.debug("[CatAsk/Home] SELECT'ing pinned questions")
|
metadata = func_val[1]
|
||||||
|
|
||||||
cursor.execute("SELECT * FROM questions WHERE answered=%s AND pinned=%s ORDER BY creation_date DESC", (True, True))
|
|
||||||
pinned_questions = cursor.fetchall()
|
|
||||||
|
|
||||||
app.logger.debug("[CatAsk/Home] SELECT'ing non-pinned questions")
|
|
||||||
|
|
||||||
cursor.execute("SELECT * FROM questions WHERE answered=%s AND pinned=%s ORDER BY creation_date DESC", (True, False))
|
|
||||||
non_pinned_questions = cursor.fetchall()
|
|
||||||
|
|
||||||
questions = pinned_questions + non_pinned_questions
|
|
||||||
|
|
||||||
app.logger.debug("[CatAsk/Home] SELECT'ing answers")
|
|
||||||
|
|
||||||
cursor.execute("SELECT * FROM answers ORDER BY creation_date DESC")
|
|
||||||
answers = cursor.fetchall()
|
|
||||||
|
|
||||||
metadata = func.generateMetadata()
|
|
||||||
|
|
||||||
combined = []
|
|
||||||
for question in questions:
|
|
||||||
question_answers = [answer for answer in answers if answer['question_id'] == question['id']]
|
|
||||||
combined.append({
|
|
||||||
'question': question,
|
|
||||||
'answers': question_answers
|
|
||||||
})
|
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return render_template('index.html', combined=combined, urllib=urllib, trimContent=func.trimContent, metadata=metadata, getRandomWord=func.getRandomWord, formatRelativeTime=func.formatRelativeTime)
|
return render_template('index.html', combined=combined, urllib=urllib, trimContent=func.trimContent, metadata=metadata, getRandomWord=func.getRandomWord, formatRelativeTime=func.formatRelativeTime)
|
||||||
|
|
||||||
|
|
33
functions.py
33
functions.py
|
@ -506,12 +506,13 @@ def processEmojis(meta_json_path):
|
||||||
|
|
||||||
return processed_emojis
|
return processed_emojis
|
||||||
|
|
||||||
def renderMarkdown(text):
|
def renderMarkdown(text, allowed_tags=None):
|
||||||
plugins = [
|
plugins = [
|
||||||
'strikethrough',
|
'strikethrough',
|
||||||
button,
|
button,
|
||||||
emoji
|
emoji
|
||||||
]
|
]
|
||||||
|
if not allowed_tags:
|
||||||
allowed_tags = [
|
allowed_tags = [
|
||||||
'p',
|
'p',
|
||||||
'em',
|
'em',
|
||||||
|
@ -526,13 +527,23 @@ def renderMarkdown(text):
|
||||||
'ol',
|
'ol',
|
||||||
'li',
|
'li',
|
||||||
'hr',
|
'hr',
|
||||||
'img'
|
'img',
|
||||||
|
'code',
|
||||||
|
'pre'
|
||||||
]
|
]
|
||||||
# allowed_attrs = {
|
allowed_attrs = {
|
||||||
# 'a': 'href',
|
'a': 'href',
|
||||||
# 'button': 'class',
|
'button': 'class',
|
||||||
# # 'img': ['src', 'width', 'height', 'alt', 'class', 'loading', 'title']
|
'img': {
|
||||||
# }
|
'class': lambda value: value == "emoji",
|
||||||
|
'src': True, # Allow specific attributes on emoji images
|
||||||
|
'alt': True,
|
||||||
|
'title': True,
|
||||||
|
'width': True,
|
||||||
|
'height': True,
|
||||||
|
'loading': True
|
||||||
|
}
|
||||||
|
}
|
||||||
# hard_wrap=True means that newlines will be
|
# hard_wrap=True means that newlines will be
|
||||||
# converted into <br> tags
|
# converted into <br> tags
|
||||||
#
|
#
|
||||||
|
@ -545,10 +556,10 @@ def renderMarkdown(text):
|
||||||
plugins=plugins,
|
plugins=plugins,
|
||||||
hard_wrap=True
|
hard_wrap=True
|
||||||
)
|
)
|
||||||
cleaner = Cleaner(tags=allowed_tags)
|
html = md(text)
|
||||||
clean_text = cleaner.clean(text)
|
cleaner = Cleaner(tags=allowed_tags, attributes=allowed_attrs)
|
||||||
html = md(clean_text)
|
clean_html = cleaner.clean(html)
|
||||||
return Markup(html)
|
return Markup(clean_html)
|
||||||
|
|
||||||
def generateMetadata(question=None, answer=None):
|
def generateMetadata(question=None, answer=None):
|
||||||
metadata = {
|
metadata = {
|
||||||
|
|
Loading…
Add table
Reference in a new issue