~fabrixxm/lesanaweb

e0a413bf4e23fa3b64e4484149b6a68e5d2a3aa5 — Elena ``of Valhalla'' Grandi 1 year, 18 days ago 4313f22
Render a README(.rst|.md|.txt|)
3 files changed, 69 insertions(+), 3 deletions(-)

M README.md
M app.py
A templates/about.html
M README.md => README.md +12 -3
@@ 16,11 16,12 @@ Configuration is read from environment variables:

## Templates

Lesana Web uses two templates, written using [Jinja2](https://jinja.palletsprojects.com/)
Lesana Web uses three templates, written using [Jinja2](https://jinja.palletsprojects.com/)
templating system:

- `index.html` : show list of entries, used as homepage and for search results
- `entry.html` : display entry details
- `about.html` : display the about page

The applications comes with basic templates, but it's possible to customize 
this templates creating that two file in `templates/web` directory under 


@@ 47,9 48,14 @@ Creative Commons Zero ([CC0](https://creativecommons.org/publicdomain/zero/1.0/)
- `entry` : `lesana.collection.Entry` object
- `settings` : collection settings

`about.html` template will get this vars in context:

- `readme`: an html snippet with the rendered README contents
- `settings` : collection settings

### Routes

Lesana Web defines three routes:
Lesana Web defines four routes:

- `index` : the default route. Loads `index.html` template. 
Gets querystring parameter:


@@ 64,4 70,7 @@ not exists, or `400` if the eid matches more than one entry.
Gets url parameter:
  - `eid`, the entry's eid to show


- `about`: loads `about.html`. Tries to load a README file from the root of the
  collection. In order it tries `README.rst` (interpreted as reStructuredText),
  `README.md` (interpreted as markdown) and `README.txt` or `README`, simply
  displayed as-is.

M app.py => app.py +40 -0
@@ 4,6 4,12 @@ import jinja2
from flask import Flask
from flask import render_template, request, abort

try:
    from docutils.core import publish_parts
    import docutils.io
except ImportError:
    publish_parts = None

from lesana.collection import Collection

class ConfigurationError(Exception):


@@ 37,6 43,40 @@ my_loader = jinja2.ChoiceLoader([
app.jinja_loader = my_loader


def _render_text_only(fname):
    with open(fname) as fp:
        res = fp.read()
    return "<pre>" + res + "</pre>"

@app.route('/about.html')
def about():
    base_fname = os.path.join(COLLECTIONPATH, 'README')
    readme = None
    print("Looking for", base_fname)
    if os.path.isfile(base_fname + '.rst'):
        if publish_parts:
            parts = publish_parts(
                source=None,
                source_path=base_fname + '.rst',
                source_class=docutils.io.FileInput,
                writer=None, writer_name='html5'
            )
            print(parts)
            readme = parts['fragment']
        else:
            readme = _render_text_only(base_fname + '.rst')
    if os.path.isfile(base_fname + '.md'):
        # TODO: render also md files
        readme = _render_text_only(base_fname + '.md')
    elif os.path.isfile(base_fname):
        readme = _render_text_only(base_fname)
    elif os.path.isfile(base_fname + '.txt'):
        readme = _render_text_only(base_fname + '.txt')
    return render_template(
        'about.html',
        settings=collection.settings,
        readme=readme,
    )

@app.route('/search.html')
def search():

A templates/about.html => templates/about.html +17 -0
@@ 0,0 1,17 @@
{% extends "base.html" %}

{% block title %}{{ settings.name }}{% endblock %}

{% block content %}
        <header>
            <h1>{{ settings.name }}</h1>
            <nav><a href="{{ url_for('index') }}">home</a></nav>
        </header>
        {% if readme %}
        {{ readme | safe }}
        {% else %}
        This collection doesn't have a README.rst or README.txt file: maybe add
        one?
        {% endif %}
{% endblock %}