memegen.routes.index._load_readme()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 0
dl 0
loc 10
rs 9.95
c 0
b 0
f 0
1
import random
2
from pathlib import Path
3
4
from flask import (Blueprint, Markup,
5
                   render_template, current_app, make_response)
6
from markdown import markdown
7
8
from ._utils import samples
9
10
11
blueprint = Blueprint('index-page', __name__)
12
13
14
@blueprint.route("/")
15
def get():
16
    template_images = list(samples(blank=True))
17
    html = render_template(
18
        "index.html",
19
        template_images=template_images,
20
        default_template=random.choice(template_images)['key'],
21
        readme=_load_readme(),
22
        config=current_app.config,
23
    )
24
    response = make_response(html)
25
    response.headers['Cache-Control'] = f'max-age={60*60*12}'
26
    return response
27
28
29
def _load_readme():
30
    path = Path(current_app.config['ROOT'], 'README.md')
31
    with path.open() as f:
32
        text = f.read()
33
        content = text.split('<!--content-->')[-1]
34
        html = markdown(content, extensions=[
35
            'markdown.extensions.tables',
36
            'pymdownx.magiclink',
37
        ])
38
        return Markup(html)
39