diff --git a/app/routes.py b/app/routes.py index 7d988cbb..4d22a7c0 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,10 +1,10 @@ import sys -from flask import Flask, render_template, request +from flask import Flask, render_template, request, redirect from items import get_items -from random import choice +from random import choice, sample if sys.version_info.major < 3: # XXX OOF @@ -32,14 +32,14 @@ def addPage(): def addContent(): bucket = request.form['bucket'] text = request.form['entry'] - title = text.split('\n')[0].lower().replace(".","").replace("/","").replace(" ","_").strip() + title = text.partition('\n')[0].lower().replace(".","").replace("/","").replace(" ","_").strip() if bucket in ["Items","Places","Characters","Spells"]: f = open("../" + bucket + "/" + title + ".txt",'x') f.write(text) f.close() return render_template('add.html') else: - return "

bruh

" + return "

bruh

Pls no.

" @app.route('/search', methods=['POST']) def search(): @@ -73,21 +73,30 @@ def search(): return render_template('index.html',featureType=featureType,featureText=item, searchResults=results) -@app.route('/items') -def items(): - return render_template('listing.html',title="Items",items=get_items("../Items/*")) - -@app.route('/places') -def places(): - return render_template('listing.html',title="Places",items=get_items("../Places/*")) - -@app.route('/characters') -def characters(): - return render_template('listing.html',title="Characters",items=get_items("../Characters/*")) - -@app.route('/spells') -def spells(): - return render_template('listing.html',title="Spells",items=get_items("../Spells/*")) +def bucket_route(uri, dir, sing=None): + if sing is None: + sing = dir[:-1] # minus "s" + + def render_bucket(): + return render_template('listing.html', title=dir, uri=uri, items=get_items(f'../{dir}/*')) + app.add_url_rule(f'/{uri}', f'render_{uri}', render_bucket) + + def random_bucket(): + nm = choice(get_items(f'../{dir}/*'))[0] + return redirect(f'/{uri}#{nm}') + app.add_url_rule(f'/{uri}/random', f'random_{uri}', random_bucket) + + def bucket_table(amt): + try: + return render_template('table.html', title=f'{sing} Table', items=sample(get_items(f'../{dir}/*'), amt), roll=amt) + except ValueError: + return render_template('table.html', title='you dun goofed', error="Can't construct that table (not enough items?)") + app.add_url_rule(f'/{uri}/table/', f'{uri}_table', bucket_table) + +bucket_route('items', 'Items') +bucket_route('places', 'Places') +bucket_route('characters', 'Characters') +bucket_route('spells', 'Spells') if __name__ == "__main__": app.run() diff --git a/app/templates/style.css b/app/static/style.css similarity index 88% rename from app/templates/style.css rename to app/static/style.css index 2c43438d..9ca62c37 100644 --- a/app/templates/style.css +++ b/app/static/style.css @@ -12,6 +12,10 @@ html { color: #fff; } +:target { + background-color: #442; +} + h1 { font-variant: small-caps; font-size: 60px; @@ -60,6 +64,10 @@ ul.headlist li { display: inline-block; } +ul.headlist.special a { + color: #aff; +} + table,th,td { border: 1px solid #66c; } @@ -96,6 +104,15 @@ table.props th { vertical-align: top; } +table.rolls { + border-collapse: collapse; + margin: 1em auto; +} + +table.rolls td, table.rolls th { + border: 1px solid #aff; +} + .freeform { white-space: pre-wrap; } diff --git a/app/templates/base.html b/app/templates/base.html new file mode 100644 index 00000000..3103b76b --- /dev/null +++ b/app/templates/base.html @@ -0,0 +1,13 @@ +{% import "macros.html" as macros %} + + + + + + {% block title %}COSI D&D Archive{% endblock %} + + +

COSI D&D Archive

+ {% block content %}{% endblock %} + + diff --git a/app/templates/index.html b/app/templates/index.html index 488f52a4..16186981 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -1,66 +1,34 @@ - - - - - - COSI D&D Archive - - -

COSI D&D Archive

-
- Search Type: - - +{% extends "base.html" %} +{% block content %} + + Search Type: + + - -
- + + + - {% if searchResults is defined %} - - {% for (name,heads,text) in searchResults %} - - - - - {% endfor %} -
- {{name}} - - {% for (key, value) in heads %} - - {% endfor %} -
{{key}} {{value}}
-
{{text}}
- {% endif %} + {% if searchResults is defined %} + {{ macros.itemTable(searchResults) }} + {% endif %} -
-

Featured {{featureType}}:

- - - - - -
- {{featureText[0]}} - - {% for (key, value) in featureText[1] %} - - {% endfor %} -
{{key}} {{value}}
-
{{featureText[2]}}
-
- - + {% if featureText is defined %} +
+

Featured {{featureType}}:

+ {{ macros.itemTable([featureText]) }} +
+ {% endif %} +{% endblock %} diff --git a/app/templates/listing.html b/app/templates/listing.html index 1862dadd..0381bca2 100644 --- a/app/templates/listing.html +++ b/app/templates/listing.html @@ -1,33 +1,15 @@ - - - - - - {{ title }} - COSI D&D Archive - - -

COSI D&D Archive

-

{{title}}

- - - {% for (name,heads,text) in items %} - - - - - {% endfor %} -
- {{name}} - - {% for (key, value) in heads %} - - {% endfor %} -
{{key}} {{value}}
-
{{text}}
- Scroll to Top - - +{% extends "base.html" %} +{% block content %} +

{{title}}

+ + + + {{ macros.itemTable(items) }} + Scroll to Top +{% endblock %} diff --git a/app/templates/macros.html b/app/templates/macros.html new file mode 100644 index 00000000..eecd8110 --- /dev/null +++ b/app/templates/macros.html @@ -0,0 +1,21 @@ +{% macro itemRow(name, heads, text) -%} + + + {{name}} + + {% for (key, value) in heads %} + + {% endfor %} +
{{key}}{{value}}
+ + {{text}} + +{%- endmacro %} + +{% macro itemTable(rows) -%} + + {% for (name, heads, text) in rows %} + {{ itemRow(name, heads, text) }} + {% endfor %} +
+{%- endmacro %} diff --git a/app/templates/table.html b/app/templates/table.html new file mode 100644 index 00000000..fcb59c67 --- /dev/null +++ b/app/templates/table.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} +{% block content %} +

{{title}}

+ {% if error is defined %} +
{{ error }}
+ {% else %} + + + {% for (name, _, _) in items %} + + {% endfor %} +
d{{roll}}Result
{{ loop.index }}{{name}}
+ {{ macros.itemTable(items) }} + {% endif %} + Scroll to Top +{% endblock %}