commit aa469e758df37bafcaae279c1993aaae72b3f78d Author: kam (from the studio) Date: Fri Dec 2 17:03:53 2022 +0100 init diff --git a/.env b/.env new file mode 100644 index 0000000..147ae0a --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DEBUG=True diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4802c2b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv/ +.DS_Store +txt/ \ No newline at end of file diff --git a/prefix.py b/prefix.py new file mode 100644 index 0000000..f16d2b1 --- /dev/null +++ b/prefix.py @@ -0,0 +1,15 @@ +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/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2e570eb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +click==8.1.3 +Flask==2.2.2 +importlib-metadata==5.1.0 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.1 +python-dotenv==0.21.0 +python-frontmatter==1.0.0 +PyYAML==6.0 +Werkzeug==2.2.2 +zipp==3.11.0 diff --git a/souptxt.py b/souptxt.py new file mode 100644 index 0000000..f585b20 --- /dev/null +++ b/souptxt.py @@ -0,0 +1,80 @@ +import os +from flask import Flask, render_template, url_for, send_from_directory +import frontmatter +import markdown +from prefix import PrefixMiddleware +from dotenv import load_dotenv +from time import strftime, localtime +import random +import glob + + + +load_dotenv() +prefix = os.environ.get('URL_PREFIX', '') +port = os.environ.get('PORT', 3000) +debug = os.environ.get('DEBUG', False) + +# --- +# Functions +# --- + +def list_files(folder, remove_ext=False): + ''' Read all the functions in a folder ''' + names = [] + for entry in os.scandir(folder): + # add to the list only proper files + if entry.is_file(follow_symlinks=False): + # remove the extension from the filename + n = entry.name + if remove_ext: + n = os.path.splitext(entry.name)[0] + names.append(n) + return names + +def txt_list(): + ''' Generate list of writings ''' + files = sorted(filter(os.path.isfile, glob.glob('txt/[!.]*.md')), key=lambda file: os.path.getmtime(file), reverse=True) + writings = [] + for file in files: + with open(file) as f: + meta, content = frontmatter.parse(f.read()) + meta['slug'] = os.path.splitext(os.path.basename(file))[0] + meta['last_edit'] = strftime('%d.%m.%Y', localtime(os.path.getmtime(file))) + writings.append(meta) + + return writings + + +# --- +# Create Flask App +# --- + +app = Flask(__name__) +app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=prefix) + + +# --- +# Routes +# --- + +@app.route('/') +def home(): + return render_template('home.html', writings=txt_list()) + +@app.route('/txt/') +def txt(slug): + try: + with open(f'txt/{slug}.md') as f: + meta, content = frontmatter.parse(f.read()) + text = {**meta} + text['content']=markdown.markdown(content) + return render_template('text.html', text=text) + except FileNotFoundError: + return 'File not found!' + +# --- +# Run the app +# --- + +app.run(port=port, debug=debug) diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..65bd3f3 --- /dev/null +++ b/static/style.css @@ -0,0 +1,57 @@ +html, +body { + font-family: sans-serif; +} + +* { + padding: 0; + font-size: 1rem; +} + +* + * { + margin-top: 4px; +} + +body { + margin: 16px; +} + +blockquote { + margin: 8px 0; + font-family: serif; + font-size: 1.25rem; +} + +.excerpt { + margin: 16px 0; + max-width: 60ch; + background-color: #eee; +} + +ul { + list-style: none; +} + +a { + color: currentColor; + background-color: #eee; + text-decoration: none; +} + +.last-edit { + opacity: 0.3; +} + +main { + max-width: 80ch; + line-height: 1.4; +} + +main ul, +main ol { + list-style-position: inside; +} + +main code { + white-space: pre; +} diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..c10d737 --- /dev/null +++ b/templates/home.html @@ -0,0 +1,23 @@ + + + + + + + Souptxt + + + +

Souptxt

+

Writings for the thesis

+ + + + diff --git a/templates/text.html b/templates/text.html new file mode 100644 index 0000000..ce43cd4 --- /dev/null +++ b/templates/text.html @@ -0,0 +1,15 @@ + + + + + + + {{text.title}} + + + +

{{text.title}}

+ back +
{{text.content | safe}}
+ + diff --git a/update.sh b/update.sh new file mode 100644 index 0000000..e69de29