2021-01-24 20:39:27 +01:00
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
import re
|
2021-01-24 00:55:49 +01:00
|
|
|
|
2021-01-24 23:24:49 +01:00
|
|
|
from glob import glob
|
|
|
|
from typing import Optional
|
2021-01-24 00:55:49 +01:00
|
|
|
|
2021-01-26 01:49:49 +01:00
|
|
|
from flask import Flask, Response, abort, send_from_directory, render_template
|
2021-01-24 23:24:49 +01:00
|
|
|
from markdown import markdown
|
2021-01-24 00:55:49 +01:00
|
|
|
|
2021-01-24 20:39:27 +01:00
|
|
|
basedir = os.path.abspath(os.path.join(os.path.realpath(__file__), '..', '..'))
|
|
|
|
templates_dir = os.path.join(basedir, 'templates')
|
|
|
|
static_dir = os.path.join(basedir, 'static')
|
|
|
|
pages_dir = os.path.join(static_dir, 'pages')
|
|
|
|
img_dir = os.path.join(static_dir, 'img')
|
|
|
|
css_dir = os.path.join(static_dir, 'css')
|
|
|
|
|
|
|
|
app = Flask(__name__, template_folder=templates_dir)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_page_title(page: str) -> str:
|
|
|
|
if page.endswith('.md'):
|
|
|
|
page = page[:-3]
|
|
|
|
|
|
|
|
return page.replace('-', ' ')
|
|
|
|
|
|
|
|
|
|
|
|
def get_page_metadata(page: str) -> dict:
|
|
|
|
if not page.endswith('.md'):
|
|
|
|
page = page + '.md'
|
|
|
|
|
|
|
|
if not os.path.isfile(os.path.join(pages_dir, page)):
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
metadata = {}
|
|
|
|
with open(os.path.join(pages_dir, page), 'r') as f:
|
2021-01-24 23:24:49 +01:00
|
|
|
metadata['uri'] = '/article/' + page[:-3]
|
|
|
|
|
2021-01-24 20:39:27 +01:00
|
|
|
for line in f.readlines():
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not (m := re.match(r'^\[//]: # \(([^:]+):\s*([^)]+)\)\s*$', line)):
|
|
|
|
break
|
|
|
|
|
2021-01-24 23:24:49 +01:00
|
|
|
if m.group(1) == 'published':
|
|
|
|
metadata[m.group(1)] = datetime.date.fromisoformat(m.group(2))
|
|
|
|
else:
|
|
|
|
metadata[m.group(1)] = m.group(2)
|
2021-01-24 20:39:27 +01:00
|
|
|
|
|
|
|
return metadata
|
|
|
|
|
|
|
|
|
2021-01-26 01:59:18 +01:00
|
|
|
def get_page(page: str, title: Optional[str] = None, skip_header: bool = False):
|
2021-01-24 20:39:27 +01:00
|
|
|
if not page.endswith('.md'):
|
|
|
|
page = page + '.md'
|
|
|
|
|
|
|
|
metadata = get_page_metadata(page)
|
|
|
|
with open(os.path.join(pages_dir, page), 'r') as f:
|
|
|
|
return render_template('article.html',
|
2021-01-24 23:24:49 +01:00
|
|
|
title=title if title else metadata.get('title', 'Platypush blog'),
|
|
|
|
image=metadata.get('image'),
|
|
|
|
description=metadata.get('description'),
|
|
|
|
published=(metadata['published'].strftime('%b %d, %Y')
|
2021-01-24 20:39:27 +01:00
|
|
|
if metadata.get('published') else None),
|
2021-01-26 01:59:18 +01:00
|
|
|
content=markdown(f.read(), extensions=['fenced_code', 'codehilite']),
|
|
|
|
skip_header=skip_header)
|
2021-01-24 20:39:27 +01:00
|
|
|
|
|
|
|
|
2021-01-26 01:59:18 +01:00
|
|
|
def get_pages(with_content: bool = False, skip_header: bool = False) -> list:
|
2021-01-24 23:24:49 +01:00
|
|
|
return sorted([
|
|
|
|
{
|
|
|
|
'path': path,
|
2021-01-26 01:59:18 +01:00
|
|
|
'content': get_page(path, skip_header=skip_header) if with_content else '',
|
2021-01-24 23:24:49 +01:00
|
|
|
**get_page_metadata(os.path.basename(path)),
|
|
|
|
}
|
|
|
|
for path in glob(os.path.join(pages_dir, '*.md'))
|
|
|
|
], key=lambda page: page.get('published'), reverse=True)
|
|
|
|
|
|
|
|
|
2021-01-24 20:39:27 +01:00
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def home_route():
|
2021-01-24 23:24:49 +01:00
|
|
|
return render_template('index.html', pages=get_pages())
|
2021-01-24 20:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/favicon.ico', methods=['GET'])
|
|
|
|
def favicon_route():
|
|
|
|
return send_from_directory(img_dir, 'favicon.ico')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/img/<img>', methods=['GET'])
|
|
|
|
def img_route(img: str):
|
|
|
|
return send_from_directory(img_dir, img)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/css/<style>', methods=['GET'])
|
|
|
|
def css_route(style: str):
|
|
|
|
return send_from_directory(css_dir, style)
|
|
|
|
|
|
|
|
|
2021-01-24 23:24:49 +01:00
|
|
|
@app.route('/article/<article>', methods=['GET'])
|
|
|
|
def article_route(article: str):
|
|
|
|
return get_page(article)
|
2021-01-26 01:49:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/rss', methods=['GET'])
|
|
|
|
def rss_route():
|
2021-01-26 01:59:18 +01:00
|
|
|
pages = get_pages(with_content=True, skip_header=True)
|
2021-01-26 01:49:49 +01:00
|
|
|
|
|
|
|
return Response('''<?xml version="1.0" encoding="UTF-8" ?>
|
2021-01-26 02:02:11 +01:00
|
|
|
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
|
2021-01-26 01:49:49 +01:00
|
|
|
<channel>
|
|
|
|
<title>Platypush blog feeds</title>
|
|
|
|
<link>http://blog.platypush.tech</link>
|
|
|
|
<description>Insights and inspirational projects using Platypush as an automation platform</description>
|
|
|
|
<category>Programming, automation, Python, machine learning, IoT, smart home</category>
|
|
|
|
<image>
|
|
|
|
<url>https://git.platypush.tech/uploads/-/system/appearance/header_logo/1/icon-256.png</url>
|
|
|
|
<title>Platypush</title>
|
|
|
|
<link>https://git.platypush.tech</link>
|
|
|
|
</image>
|
|
|
|
<pubDate>{last_pub_date}</pubDate>
|
|
|
|
<language>en-us</language>
|
|
|
|
|
|
|
|
{items}
|
|
|
|
</channel>
|
|
|
|
</rss>'''.format(
|
|
|
|
last_pub_date=pages[0]['published'].strftime('%a, %d %b %Y %H:%M:%S GMT'),
|
|
|
|
items='\n\n'.join([
|
|
|
|
'''
|
|
|
|
<item>
|
|
|
|
<title>{title}</title>
|
|
|
|
<link>https://blog.platypush.tech{link}</link>
|
|
|
|
<pubDate>{published}</pubDate>
|
|
|
|
<description><![CDATA[{content}]]></description>
|
2021-01-26 01:55:46 +01:00
|
|
|
<media:content medium="image" url="https://blog.platypush.tech/img{image}" width="200" height="150" />
|
2021-01-26 01:49:49 +01:00
|
|
|
</item>
|
|
|
|
'''.format(
|
|
|
|
title=page.get('title', '[No Title]'),
|
|
|
|
link=page.get('uri', ''),
|
|
|
|
published=page['published'].strftime('%a, %d %b %Y %H:%M:%S GMT') if 'published' in page else '',
|
|
|
|
content=page.get('content', ''),
|
|
|
|
image=page.get('image', ''),
|
|
|
|
)
|
|
|
|
for page in pages
|
|
|
|
]),
|
|
|
|
), mimetype='application/rss+xml')
|