From 05a163775421d22b0ded64797e479fdb185fa0d7 Mon Sep 17 00:00:00 2001 From: km0 Date: Wed, 1 Mar 2023 01:16:21 +0100 Subject: [PATCH] first test --- .gitignore | 5 ++++ index.yaml | 4 +++ prefix.py | 16 ++++++++++ readme.md | 7 +++++ readme.py | 61 ++++++++++++++++++++++++++++++++++++++ static/style.css | 69 +++++++++++++++++++++++++++++++++++++++++++ templates/render.html | 19 ++++++++++++ update.sh | 1 + 8 files changed, 182 insertions(+) create mode 100644 .gitignore create mode 100644 index.yaml create mode 100644 prefix.py create mode 100644 readme.md create mode 100644 readme.py create mode 100644 static/style.css create mode 100644 templates/render.html create mode 100755 update.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff3d501 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +contents/ +venv/ +__pycache___ +*.swp +index.html diff --git a/index.yaml b/index.yaml new file mode 100644 index 0000000..758771c --- /dev/null +++ b/index.yaml @@ -0,0 +1,4 @@ +- chapters/00_intro.md +- chapters/01_who_is_reading.md +- chapters/02_who_is_writing.md +- chapters/03_hello_worlding.md diff --git a/prefix.py b/prefix.py new file mode 100644 index 0000000..9619ff0 --- /dev/null +++ b/prefix.py @@ -0,0 +1,16 @@ +class PrefixMiddleware(object): + + def __init__(self, app, prefix=''): + self.app = app + self.prefix = prefix + + def __call__(self, environ, start_response): + + if environ['PATH_INFO'].startswith(self.prefix): + environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):] + environ['SCRIPT_NAME'] = self.prefix + return self.app(environ, start_response) + else: + start_response('404', [('Content-Type', 'text/plain')]) + return ["This url does not belong to the app.".encode()] + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..b79a090 --- /dev/null +++ b/readme.md @@ -0,0 +1,7 @@ +# Readme: a thesis viewer + +Fetch contents from [thesis](https://git.xpub.nl/kamo/thesis) and render them onto a static HTML page. A way to share the writing progress with the world outside Gitea. + +Contents are update via [Gitea webhooks](https://docs.gitea.io/en-us/webhooks/): at every push on `kamo/thesis`, Gitea sends a GET request to the Readme server, that in turn pulls the new contents and re-render the HTML page. + + diff --git a/readme.py b/readme.py new file mode 100644 index 0000000..d23cc99 --- /dev/null +++ b/readme.py @@ -0,0 +1,61 @@ +import os +import subprocess +from flask import Flask, render_template, request, send_from_directory +from prefix import PrefixMiddleware +from dotenv import load_dotenv +from markdown import markdown +import yaml + + +load_dotenv() + +PORT = os.environ.get("PORT", 3000) +DEBUG = os.environ.get("DEBUG", True) +PREFIX = os.environ.get("PREFIX", "") +REPO = os.environ.get("REPO", "https://git.xpub.nl/kamo/thesis.git") +SERVER_NAME = os.environ.get("SERVER_NAME", "localhost:3000") + +def pull(): + print('Retrieving contents') + subprocess.call(['sh', 'update.sh', REPO]) + +def render(): + + with open('index.yaml',"r") as i: + index = yaml.safe_load(i) + + entries = [] + for entry in index: + with open(os.path.join('contents', entry)) as e: + html = markdown(e.read(), extensions=[ + 'markdown.extensions.attr_list', + 'markdown.extensions.codehilite', + 'markdown.extensions.fenced_code' + ]) + entries.append(html) + + with open('index.html', 'w') as r: + r.write(render_template("render.html", contents=entries)) + + return 'rendered' + +app = Flask(__name__) +app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=PREFIX) +app.config['SERVER_NAME'] = SERVER_NAME + + +pull(); +with app.app_context(): + render(); + +@app.route('/') +def home(): + return send_from_directory(app.root_path, 'index.html') + +@app.route('/update', methods=['GET', 'POST']) +def update(): + if request.method == 'POST': + pull(); + return 'GET method not supported' + +app.run(port=PORT, debug=DEBUG) diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..4d11043 --- /dev/null +++ b/static/style.css @@ -0,0 +1,69 @@ +* { + box-sizing: border-box; +} + +html, body { + margin: 0; + font-size: 24px; + font-family: serif; +} + +.chapter { + max-width: 80ch; + margin-inline: auto; +} + +.chapter + .chapter { + margin-top: 64px; +} + +h1, h2, h3 { + font-family: sans-serif; + margin: 2em auto; +} + +h2 { + text-align: center; + color: tomato; +} + +a { + color: tomato; + text-decoration: none; +} + +pre, code { +white-space: pre-wrap; +} + +.language-note, +.language-todo { + margin: 32px auto; + font-family: sans-serif; + display: inline-block; + width: auto; + padding: 1ch; + color: #aaa; + border: 1px solid #aaa; + position: relative; + white-space: pre-wrap; +} + +.language-note:before, +.language-todo:before { + padding: 0.1ch; + position: absolute; + top: 0; + translate: calc(-1ch + -1px) -100%; + display: inline-block; + background-color: #aaa; + color: white; +} + +.language-note:before { + content: 'Note' +} + +.language-todo:before { + content: 'TO DO' +} diff --git a/templates/render.html b/templates/render.html new file mode 100644 index 0000000..5ffbe50 --- /dev/null +++ b/templates/render.html @@ -0,0 +1,19 @@ + + + + + Kamo is writing a thesis about code documentation! + + + + + + {% for content in contents %} +
+ {{content|safe}} +
+ {% endfor %} + + + + diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..b96274d --- /dev/null +++ b/update.sh @@ -0,0 +1 @@ +git -C contents pull || git clone ${1} contents