Update for Flask 2.0

This commit is contained in:
RunasSudo 2022-04-10 15:12:25 +10:00
parent 5e4d32b20d
commit 7acc25f9c8
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 18 additions and 16 deletions

View File

@ -1,5 +1,5 @@
# WikiNote3 # WikiNote3
# Copyright © 2020 Lee Yingtong Li (RunasSudo) # Copyright © 2020, 2022 Lee Yingtong Li (RunasSudo)
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by # it under the terms of the GNU Affero General Public License as published by
@ -17,6 +17,7 @@
from .markup import WNMarkdown from .markup import WNMarkdown
import flask import flask
from werkzeug.security import safe_join
import os import os
import pickle import pickle
@ -37,9 +38,9 @@ def get_children(path):
return os.path.splitext(c)[0] return os.path.splitext(c)[0]
children = [] children = []
if os.path.isdir(flask.safe_join('./data/pages', path)): if os.path.isdir(safe_join('./data/pages', path)):
for child in os.listdir(flask.safe_join('./data/pages', path)): for child in os.listdir(safe_join('./data/pages', path)):
child_path = flask.safe_join('./data/pages', path, child) child_path = safe_join('./data/pages', path, child)
if child.startswith('_'): if child.startswith('_'):
continue continue
@ -80,7 +81,7 @@ def index_page():
@app.route('/page/<path:path>') @app.route('/page/<path:path>')
def page_view(path): def page_view(path):
fname = flask.safe_join('./data/pages', path) + '.md' fname = safe_join('./data/pages', path) + '.md'
if os.path.islink(fname): if os.path.islink(fname):
redir_page = '/'.join(path.split('/')[:-1]) + '/' + os.path.splitext(os.readlink(fname))[0] redir_page = '/'.join(path.split('/')[:-1]) + '/' + os.path.splitext(os.readlink(fname))[0]
@ -133,7 +134,7 @@ def page_view(path):
@app.route('/preview/<path:path>') @app.route('/preview/<path:path>')
def page_preview(path): def page_preview(path):
fname = flask.safe_join('./data/pages', path) + '.md' fname = safe_join('./data/pages', path) + '.md'
if not os.path.exists(fname): if not os.path.exists(fname):
return '' return ''
@ -175,12 +176,12 @@ def page_preview(path):
@app.route('/image/<name>') @app.route('/image/<name>')
def image_view(name): def image_view(name):
fname = flask.safe_join(os.getcwd(), './data/images', name[0].upper(), name) fname = safe_join(os.getcwd(), './data/images', name[0].upper(), name)
return flask.send_file(fname) return flask.send_file(fname)
@app.route('/image/<name>/about') @app.route('/image/<name>/about')
def image_about(name): def image_about(name):
fname = flask.safe_join(os.getcwd(), './data/images', name[0].upper(), os.path.splitext(name)[0] + '.md') fname = safe_join(os.getcwd(), './data/images', name[0].upper(), os.path.splitext(name)[0] + '.md')
with(open(fname, 'r')) as f: with(open(fname, 'r')) as f:
page_source = f.read() page_source = f.read()
@ -220,10 +221,10 @@ def cli_index():
base_path = './data/pages' base_path = './data/pages'
for dirpath, dirnames, filenames in os.walk(base_path): for dirpath, dirnames, filenames in os.walk(base_path):
for fname in filenames: for fname in filenames:
if fname.endswith('.md') and not os.path.islink(flask.safe_join(dirpath, fname)): if fname.endswith('.md') and not os.path.islink(safe_join(dirpath, fname)):
page_path = dirpath[len(base_path)+1:] + '/' + fname[:-3] page_path = dirpath[len(base_path)+1:] + '/' + fname[:-3]
with(open(flask.safe_join(dirpath, fname), 'r')) as f: with(open(safe_join(dirpath, fname), 'r')) as f:
page_source = f.read() page_source = f.read()
md = WNMarkdown() md = WNMarkdown()
md.convert(page_source) md.convert(page_source)
@ -234,7 +235,7 @@ def cli_index():
tags[tag].append({'kind': 'page', 'path': page_path}) tags[tag].append({'kind': 'page', 'path': page_path})
for ref in md.meta.get('refs', []): for ref in md.meta.get('refs', []):
fname_ref = flask.safe_join('./data/pages', ref) + '.md' fname_ref = safe_join('./data/pages', ref) + '.md'
if os.path.islink(fname_ref): if os.path.islink(fname_ref):
ref = '/'.join(ref.split('/')[:-1]) + '/' + os.path.splitext(os.readlink(fname_ref))[0] ref = '/'.join(ref.split('/')[:-1]) + '/' + os.path.splitext(os.readlink(fname_ref))[0]
@ -249,7 +250,7 @@ def cli_index():
if fname.endswith('.md'): if fname.endswith('.md'):
continue continue
md_path = flask.safe_join(dirpath, os.path.splitext(fname)[0] + '.md') md_path = safe_join(dirpath, os.path.splitext(fname)[0] + '.md')
if os.path.exists(md_path): if os.path.exists(md_path):
with(open(md_path, 'r')) as f: with(open(md_path, 'r')) as f:
page_source = f.read() page_source = f.read()
@ -280,7 +281,7 @@ def cli_redlinks():
if fname.endswith('.md'): if fname.endswith('.md'):
page_path = dirpath[len(base_path)+1:] + '/' + fname[:-3] page_path = dirpath[len(base_path)+1:] + '/' + fname[:-3]
with(open(flask.safe_join(dirpath, fname), 'r')) as f: with(open(safe_join(dirpath, fname), 'r')) as f:
page_source = f.read() page_source = f.read()
md = WNMarkdown() md = WNMarkdown()
md.convert(page_source) md.convert(page_source)

View File

@ -1,5 +1,5 @@
# WikiNote3 # WikiNote3
# Copyright © 2020 Lee Yingtong Li (RunasSudo) # Copyright © 2020, 2022 Lee Yingtong Li (RunasSudo)
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by # it under the terms of the GNU Affero General Public License as published by
@ -15,6 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import flask import flask
from werkzeug.security import safe_join
import os.path import os.path
import re import re
@ -83,7 +84,7 @@ directives['lastmod'] = DirectiveLastmod
class DirectiveInclude(Directive): class DirectiveInclude(Directive):
def render(self): def render(self):
el = DirectiveElement('div') el = DirectiveElement('div')
with open(flask.safe_join('./data', self.arg), 'r') as f: with open(safe_join('./data', self.arg), 'r') as f:
self.md.parser.parseChunk(el, f.read()) self.md.parser.parseChunk(el, f.read())
return el return el
@ -109,7 +110,7 @@ def make_role_ref(is_upper):
# Link # Link
a = ET.SubElement(el, 'a') a = ET.SubElement(el, 'a')
if os.path.exists(flask.safe_join('./data/pages', path + '.md')): if os.path.exists(safe_join('./data/pages', path + '.md')):
a.set('class', 'ref') a.set('class', 'ref')
else: else:
a.set('class', 'ref redlink') a.set('class', 'ref redlink')