~fabrixxm/public-inbox

Render a README(.rst|.md|.txt|) v1 APPLIED

Elena ``of Valhalla'' Grandi <valhalla@trueelena.org>
Elena ``of Valhalla'' Grandi: 1
 Render a README(.rst|.md|.txt|)

 3 files changed, 69 insertions(+), 3 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~fabrixxm/public-inbox/patches/15064/mbox | git am -3
Learn more about email & git

[PATCH] Render a README(.rst|.md|.txt|) Export this patch

Elena ``of Valhalla'' Grandi <valhalla@trueelena.org>
---
 README.md            | 15 ++++++++++++---
 app.py               | 40 ++++++++++++++++++++++++++++++++++++++++
 templates/about.html | 17 +++++++++++++++++
 3 files changed, 69 insertions(+), 3 deletions(-)
 create mode 100644 templates/about.html

diff --git a/README.md b/README.md
index 012e6f7..49755a7 100644
--- a/README.md
+++ b/README.md
@@ -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 
@@ -44,9 +45,14 @@ as fallback.
- `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:
@@ -61,4 +67,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.
diff --git a/app.py b/app.py
index 619e244..5b24799 100644
--- a/app.py
+++ b/app.py
@@ -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():
diff --git a/templates/about.html b/templates/about.html
new file mode 100644
index 0000000..c4eb73b
--- /dev/null
+++ b/templates/about.html
@@ -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 %}

-- 
2.29.2