From 42e408859b5466388eb67e6379028ae1954b5cb6 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Sun, 2 Oct 2022 16:51:12 +0200 Subject: [PATCH] refactor!: unify slash handling It is hard to remember to add `/` at the end of the URL when configuring a new etherpad URL. Also, web server proxies tend to do weird stuff when you assume that you have a slash on the end of each URL. So, I took a look at how to avoid doing that. It turns out that both urljoin / os.path.join are kinda bad for handling URL building. Also, APPLICATION_ROOT didn't seem that necessary since Flask knows what to do with a `/` at the start of a URL, so I dropped it. I think this doesn't break anything! --- octomode.py | 57 ++++++++++++++++++++---------------------- templates/base.html | 24 ++++++++++-------- templates/pagedjs.html | 6 ++--- templates/pdf.html | 4 +-- templates/preview.html | 2 +- templates/start.html | 4 +-- 6 files changed, 49 insertions(+), 48 deletions(-) diff --git a/octomode.py b/octomode.py index a8a3c1c..73ec3a2 100755 --- a/octomode.py +++ b/octomode.py @@ -1,6 +1,6 @@ import os import json -from flask import Flask, request, render_template, redirect, url_for +from flask import Flask, request, render_template, redirect from urllib.request import urlopen from urllib.parse import urlencode @@ -15,10 +15,9 @@ import pypandoc import markdown class Config(object): - APPLICATION_ROOT = '/' PORTNUMBER = int(os.environ.get('OCTOMODE_PORTNUMBER', 5001)) - PAD_URL = os.environ.get('OCTOMODE_PAD_URL', 'https://pad.vvvvvvaria.org/' ) - PAD_API_URL = os.environ.get('OCTOMODE_PAD_API_URL', 'https://pad.vvvvvvaria.org/api/1.2.15/') + PAD_URL = os.environ.get('OCTOMODE_PAD_URL', 'https://pad.vvvvvvaria.org' ) + PAD_API_URL = os.environ.get('OCTOMODE_PAD_API_URL', 'https://pad.vvvvvvaria.org/api/1.2.15') PAD_API_KEY = os.environ.get('OCTOMODE_PAD_API_KEY', '') APP = Flask(__name__) @@ -42,14 +41,14 @@ def get_pad_content(pad_name, ext=""): 'apikey' : APP.config['PAD_API_KEY'] } api_call = 'getText' - response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode())) + response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }/{ api_call }", data=urlencode(arguments).encode())) # create pad in case it does not yet exist if response['code'] == 1 and 'padID does not exist' == response['message']: api_call = 'createPad' - urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode()) + urlopen(f"{ APP.config['PAD_API_URL'] }/{ api_call }", data=urlencode(arguments).encode()) api_call = 'getText' - response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode())) + response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }/{ api_call }", data=urlencode(arguments).encode())) content = response['data']['text'] return content @@ -59,7 +58,7 @@ def all_pads(): 'apikey' : APP.config['PAD_API_KEY'], } api_call = 'listAllPads' - response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode())) + response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }/{ api_call }", data=urlencode(arguments).encode())) return response @@ -83,7 +82,7 @@ def create_pad_on_first_run(name, ext): 'text' : default_template } api_call = 'createPad' - json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode())) + json.load(urlopen(f"{ APP.config['PAD_API_URL'] }/{ api_call }", data=urlencode(arguments).encode())) def md_to_html(md_pad_content): # Convert Markdown to HTML @@ -120,35 +119,33 @@ def index(): exts = ['.md', '.css'] for ext in exts: create_pad_on_first_run(name, ext) - return redirect(f'{ APP.config["APPLICATION_ROOT"] }{ name }/pad/') + return redirect(f"/{ name }/pad") else: - return render_template('start.html', application_root=APP.config["APPLICATION_ROOT"]) + return render_template('start.html') -@APP.route('//') +@APP.route('/') def main(name): - return redirect(f'{ APP.config["APPLICATION_ROOT"] }{ name }/pad/') + return redirect(f"/{ name }/pad") -@APP.route('//pad/') +@APP.route('//pad') def pad(name): - pad_name = f'{ name }.md' - url = os.path.join(APP.config['PAD_URL'], pad_name) - return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"]) + url = f"{ APP.config['PAD_URL'] }/{ name }.md" + return render_template('iframe.html', url=url, name=name.strip()) -@APP.route('//stylesheet/') +@APP.route('//stylesheet') def stylesheet(name): - pad_name = f'{ name }.css' - url = os.path.join(APP.config['PAD_URL'], pad_name) - return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"]) + url = f"{ APP.config['PAD_URL'] }/{ name }.css" + return render_template('iframe.html', url=url, name=name.strip()) -@APP.route('//html/') +@APP.route('//html') def html(name): - url = os.path.join(APP.config["APPLICATION_ROOT"], name, 'preview.html') - return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"]) + url = f"/{ name }/preview.html" + return render_template('iframe.html', url=url, name=name.strip()) -@APP.route('//pdf/') +@APP.route('//pdf') def pdf(name): - url = os.path.join(APP.config["APPLICATION_ROOT"], name, 'pagedjs.html') - return render_template('pdf.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"]) + url = f"/{name}/pagedjs.html" + return render_template('pdf.html', url=url, name=name.strip()) # ////////////////// # RENDERED RESOURCES @@ -175,7 +172,7 @@ def preview(name): lang = "en" title = "No title" - return render_template('preview.html', name=name.strip(), pad_content=html, lang=lang, title=title, application_root=APP.config["APPLICATION_ROOT"]) + return render_template('preview.html', name=name.strip(), pad_content=html, lang=lang, title=title) @APP.route('//pagedjs.html') def pagedjs(name): @@ -186,10 +183,10 @@ def pagedjs(name): lang = metadata['language'][0] title = metadata['title'][0] - return render_template('pagedjs.html', name=name.strip(), pad_content=html, lang=lang, title=title, application_root=APP.config["APPLICATION_ROOT"]) + return render_template('pagedjs.html', name=name.strip(), pad_content=html, lang=lang, title=title) # ////////////////// if __name__ == '__main__': APP.debug=True - APP.run(host="0.0.0.0", port=f'{ APP.config["PORTNUMBER"] }', threaded=True) + APP.run(host="0.0.0.0", port=APP.config["PORTNUMBER"], threaded=True) diff --git a/templates/base.html b/templates/base.html index cb46317..6dfb7aa 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,7 +3,7 @@ {{ name }} in octomode - + {% block head %} {% endblock %} @@ -18,17 +18,21 @@ window.addEventListener('load', function () { // Insert the nav buttons, after the page is loaded const nav = document.createElement('div'); - nav.id = 'nav'; - const name = '{{ name }}'; - const application_root = '{{ application_root }}'; - + nav.id = 'nav'; + nav.innerHTML = ` -

${ name } in octomode

+

{{ name }} in octomode

- : - : - - + + : + + + : + + + + +
`; document.body.insertBefore(nav, document.body.firstChild); diff --git a/templates/pagedjs.html b/templates/pagedjs.html index af3a551..db221cd 100644 --- a/templates/pagedjs.html +++ b/templates/pagedjs.html @@ -3,9 +3,9 @@ - - - + + + {{ title }} diff --git a/templates/pdf.html b/templates/pdf.html index 36e1ef1..aaf5754 100644 --- a/templates/pdf.html +++ b/templates/pdf.html @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.html" %} {% block content %} @@ -16,7 +16,7 @@ window.addEventListener('load', function () { // Load the main.css again, to load the stylesheet for the nav var cssLink = document.createElement('link'); cssLink.rel = 'stylesheet'; - cssLink.href = '{{ application_root }}/static/main.css'; + cssLink.href = '{{ url_for("static", filename="main.css") }}'; var head = document.getElementsByTagName('head')[0]; head.insertBefore(cssLink, head.firstChild); diff --git a/templates/preview.html b/templates/preview.html index 17910f0..9300215 100644 --- a/templates/preview.html +++ b/templates/preview.html @@ -3,7 +3,7 @@ - + {{ title }} diff --git a/templates/start.html b/templates/start.html index b33372c..aa7b5cc 100644 --- a/templates/start.html +++ b/templates/start.html @@ -3,10 +3,10 @@ octomode - + -
+

in octomode