diff --git a/articles/__init__.py b/articles/__init__.py index 0ccbfe8..107d60c 100644 --- a/articles/__init__.py +++ b/articles/__init__.py @@ -1,31 +1,69 @@ +"""Route requests for articles according to shoofle's rules. + +This is a simple module which is basically entirely here to provide access, in a sensible location, +to the `bloop` object. It's a blueprint which describes how to route requests for articles on my website +(possibly located at http://shoofle.net, possibly not located there). Most of the interesting stuff is in +`render_article`; Go team!""" + import os from flask import Blueprint, render_template, abort -from jinja2.exceptions import TemplatesNotFound folder = "articles" +article_base_template = os.path.join(folder, "article.template.html") bloop = Blueprint("articles", __name__, template_folder="") @bloop.route("/") def main_page(): + """Renders the list of articles/projects.""" return render_template("project_list.html") -@bloop.route("/a//") -def apply_base_template(page_name): +@bloop.route("/raw//") +def render_file(page_name): + """Does nothing. Not really sure why I have this.""" file_name = os.path.join(folder, page_name.replace("-", "_")) @bloop.route("//") def render_article(page_name): - # Arguably, the various options for how to render (templates, articles, flat html) could be stuck into various subdirectories. - # Ultimately I don't want to do this because it's supposed to be lightweight - I should be able to chuck this __init__.py file into - # any folder and start generating stuff correctly. But whatever! + """Renders a requested article! This should always be @routed last, because it catches a + wide variety of requests. As a result, other things need to be @routed first, because they + might never get called if this catches them first.""" + # Arguably, the various options for how to render (templates, articles, flat html) could be stuck into various + # subdirectories. Ultimately I don't want to do this because I want this to be lightweight - this __init__.py file + # can be chucked into any folder and start showing pages correctly. But whatever! - # Let's find some test filenames! - file_name = os.path.join(folder, page_name.replace("-", "_")) - - # Is there a template by that name? This list is in the priority order for rendering. - extensions = [".template.html", ".article.html", ".html", ""] - - try: - return render_template([ file_name + suffix for suffix in extensions ]) - except TemplatesNotFound as e: - abort(404) \ No newline at end of file + # In the examples, let's think about a request for "example.com/some-article". + + # First, we convert the important part of the requested page into a filename + # "example.com/Some-Article/" => folder="Some-Article" => file_name = "articles/some_article" + file_name = os.path.join(folder, page_name.replace("-", "_").lower()) + + # Here's the priority list for file rendering! + if os.path.isfile(file_name + ".template.html"): + # If the file "articles/some_article.template.html" exists, then there's a specific {template} written + # for this path. Specific page {templates} take priority. + return render_template(file_name + ".template.html") + if os.path.isfile(file_name + ".article.html"): + # If "articles/some_article.article.html" exists but there's no template, then we should render that + # {article fragment}, but using the {article base template}. In the future, this should possibly also + # extract the title from the {article fragment} and feed it into the {article base template} as well. + return render_template(article_base_template, target=file_name + ".article.html") + if os.path.isfile(file_name + ".html"): + # If we haven't found any results yet, check to see if "articles/some_article.html" exists. If it does, + # just display it plain. This also provides a clean way to access the raw form of files like + # "articles/some_article.template.html" - just make a request for "example.com/some-article.template/" + # and it will be caught by this rule and rendered. + return render_template(file_name + ".html") + if os.path.isfile(file_name): + # If it didn't match any other rules, then just render the file that has precisely the requested name. + return render_template(file_name); + + # I *believe* there's one instance that can't be accessed by this kind of routing in any way: + # If the files "articles/some_article" and "articles/some_article.html" both exist, then no request will + # convince this blueprint to return the former. However, as sacrifices go, I don't think it's too bad, and + # that should be the only case when this happens. + + # If we didn't find any files, throw up a 404. + + abort(404) + +"""Th-th-th-that's all, folks!""" \ No newline at end of file diff --git a/articles/article.template.html b/articles/article.template.html index 2efba9f..35c7d94 100644 --- a/articles/article.template.html +++ b/articles/article.template.html @@ -11,6 +11,7 @@ {% block body -%} + {%- include target -%} {%- endblock %} diff --git a/articles/auriga.template.html b/articles/templates_old/auriga.template.html similarity index 100% rename from articles/auriga.template.html rename to articles/templates_old/auriga.template.html diff --git a/articles/distributed_speakers.template.html b/articles/templates_old/distributed_speakers.template.html similarity index 100% rename from articles/distributed_speakers.template.html rename to articles/templates_old/distributed_speakers.template.html diff --git a/articles/game_log.template.html b/articles/templates_old/game_log.template.html similarity index 100% rename from articles/game_log.template.html rename to articles/templates_old/game_log.template.html diff --git a/articles/language_for_games.template.html b/articles/templates_old/language_for_games.template.html similarity index 100% rename from articles/language_for_games.template.html rename to articles/templates_old/language_for_games.template.html diff --git a/articles/mindjail_engine.template.html b/articles/templates_old/mindjail_engine.template.html similarity index 100% rename from articles/mindjail_engine.template.html rename to articles/templates_old/mindjail_engine.template.html diff --git a/articles/oauth.template.html b/articles/templates_old/oauth.template.html similarity index 100% rename from articles/oauth.template.html rename to articles/templates_old/oauth.template.html diff --git a/articles/play_for_x.template.html b/articles/templates_old/play_for_x.template.html similarity index 100% rename from articles/play_for_x.template.html rename to articles/templates_old/play_for_x.template.html diff --git a/articles/shoof_shoof_revolution.template.html b/articles/templates_old/shoof_shoof_revolution.template.html similarity index 100% rename from articles/shoof_shoof_revolution.template.html rename to articles/templates_old/shoof_shoof_revolution.template.html diff --git a/articles/spinning.template.html b/articles/templates_old/spinning.template.html similarity index 100% rename from articles/spinning.template.html rename to articles/templates_old/spinning.template.html diff --git a/articles/tamari.template.html b/articles/templates_old/tamari.template.html similarity index 100% rename from articles/tamari.template.html rename to articles/templates_old/tamari.template.html diff --git a/articles/text_editors_with_contenteditable.template.html b/articles/templates_old/text_editors_with_contenteditable.template.html similarity index 100% rename from articles/text_editors_with_contenteditable.template.html rename to articles/templates_old/text_editors_with_contenteditable.template.html diff --git a/tornado_server.py b/tornado_server.py deleted file mode 100644 index dfbb3ba..0000000 --- a/tornado_server.py +++ /dev/null @@ -1,12 +0,0 @@ -import tornado.ioloop -import tornado.web - - -application = tornado.web.Application([ - (r"/(.*)", tornado.web.StaticFileHandler, {"path":"./"}) -], debug=True) - -if __name__ == "__main__": - application.listen(80) - tornado.ioloop.IOLoop.instance().start() -