diff options
-rw-r--r-- | server.py | 14 | ||||
-rw-r--r-- | siteconfig.py | 20 | ||||
-rw-r--r-- | views.py | 52 |
3 files changed, 43 insertions, 43 deletions
@@ -6,6 +6,7 @@ app = Flask(__name__) from views import * + def setup(): """ setup - sets up the app according to the settings specified (or not @@ -20,21 +21,18 @@ def setup(): if siteconfig.MAIN_SITE_DIRS: app.config.update({'MAIN_SITE_DIRS': siteconfig.MAIN_SITE_DIRS}) else: - s = './templates/site/' + s = "./templates/site/" top_dirs = [ x for x in os.listdir(s) \ - if os.path.isdir(s + x) and not x.startswith('.') + if os.path.isdir(s + x) and not x.startswith(".") ] - app.config.update( - { - 'MAIN_SITE_DIRS': sorted(top_dirs) - } - ) + app.config.update({'MAIN_SITE_DIRS': sorted(top_dirs)}) app.config.update({'DOMAIN': siteconfig.DOMAIN}) app.config.update({'HOME_TITLE': siteconfig.HOME_TITLE}) + # Setup needs to come first to be compatible with wsgi setup() -if __name__ == '__main__': +if __name__ == "__main__": app.run() diff --git a/siteconfig.py b/siteconfig.py index 8dbe006..3a0229f 100644 --- a/siteconfig.py +++ b/siteconfig.py @@ -1,9 +1,10 @@ class siteconfig: # REQUIRED SETTINGS# - DOMAIN = "example.net" # Your domain name, or site title - HOME_TITLE = "WELCOME" # "HELLO WORLD!" - LINKS_FILE = ".links" # ".lnx" - DESC_FILE = ".description" # ".desc`" + + DOMAIN = "example.net" # Your site here! + HOME_TITLE = "WELCOME" # "HELLO WORLD!" + LINKS_FILE = ".links" # ".lnx" if you like + DESC_FILE = ".description" # ".desc" DEFAULT_MIMETYPE = "application/octet-stream" # "text/plain" MIMETYPES = {} @@ -13,7 +14,12 @@ class siteconfig: MIMETYPES.update({ext: mime}) # OPTIONAL SETTINGS # - # Remove 'None' to add custom values + + # Be default, ALL directories in the site toolbar are contained in + # ./templates/site/. You can change this to only specific directories, but + # these still have to be in ./templates/site MAIN_SITE_DIRS = None # ["dir1", "dir2", "dir3"] - # b"\xca\x05\x80\xa3|\xdbh@\xec<\xcd\x19\xf4\nRN:)\x13\x917|km\xb0'>'\x1d&G\xe8" - SECRET_KEY = None + # Set a custom secret key. If not set, it will be generated + # Most of the time, you don't need to set this! + SECRET_KEY = None # Something random. + @@ -7,12 +7,8 @@ from server import app # bit of a hack. # Brackets don't play nicely with Jinja so instead of using .format, # we just replace the special character $ -CONTENT_BLOCK = ( - "{% extends 'base.html' %}" - "{% block content %}" - "$" - "{% endblock %}" -) +CONTENT_BLOCK = "{% extends 'base.html' %}{% block content %}${% endblock %}" + def default_context(): """ @@ -21,8 +17,8 @@ def default_context(): make up the navbar """ return { - 'domain': app.config['DOMAIN'], - 'navbar': sorted(app.config['MAIN_SITE_DIRS']) + 'domain': app.config['DOMAIN'], + 'navbar': sorted(app.config['MAIN_SITE_DIRS']), } @@ -88,12 +84,7 @@ def home(): edit, though you can optionally change the title here if you wish """ context = default_context() - context.update( - { - 'title': app.config['HOME_TITLE'], - 'parent_dir': '/site/' - } - ) + context.update({'title': app.config['HOME_TITLE'], 'parent_dir': '/site/'}) return render_template("site/home.html", **context) @@ -115,14 +106,16 @@ def render_file(path): context.update( { 'title': path.split('.')[0].upper(), - 'parent_dir': '/site/' + '/'.join(path.split('/')[:-1]) + 'parent_dir': '/site/' + '/'.join(path.split('/')[:-1]), } ) if os.path.isfile(abs_path): if abs_path.endswith('.html'): with open(abs_path, 'rb') as f: content = f.read().decode("UTF-8") - return render_template_string(CONTENT_BLOCK.replace('$', content), **context) + return render_template_string( + CONTENT_BLOCK.replace('$', content), **context + ) elif abs_path.endswith('.html!'): return render_template("site/" + path, **context) else: @@ -131,9 +124,8 @@ def render_file(path): 'templates/site/', path, mimetype=siteconfig.MIMETYPES.get( - f".{ path.split('.')[-1] }", - siteconfig.DEFAULT_MIMETYPE - ) + f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE + ), ) elif os.path.isdir(abs_path): dirs, files, links, description = index_dir(abs_path) @@ -144,7 +136,7 @@ def render_file(path): 'dirs': dirs, 'files': files, 'links': links, - 'description': description + 'description': description, } ) return render_template("index.html", **context) @@ -160,10 +152,12 @@ def send_file_from_site(path): `render_file`, send the raw file to the user, when site is replace with path. """ - return send_from_directory('templates/site/', path, - mimetype=siteconfig.MIMETYPES.get( - f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE - ) + return send_from_directory( + 'templates/site/', + path, + mimetype=siteconfig.MIMETYPES.get( + f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE + ), ) @@ -173,8 +167,10 @@ def send_file_from_static(path): send_file - instead of rendering a file within a template as with `render_file`, send the raw file to the user """ - return send_from_directory('static/', path, - mimetype=siteconfig.MIMETYPES.get( - f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE - ) + return send_from_directory( + 'static/', + path, + mimetype=siteconfig.MIMETYPES.get( + f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE + ), ) |