From d4f9dbeb2ea45bd967353a40765f3e9fa5e4dffd Mon Sep 17 00:00:00 2001 From: TiynGER Date: Fri, 22 May 2020 21:02:11 +0200 Subject: [PATCH] initial commit --- app.py | 108 +++++++++++++++++++++++++++++++ templates/archive.html | 11 ++++ templates/entry/test-entry1.html | 2 + templates/entry/test-entry2.html | 2 + templates/error.html | 9 +++ templates/index.html | 15 +++++ templates/rss.xml | 17 +++++ templates/template.html | 13 ++++ 8 files changed, 177 insertions(+) create mode 100644 app.py create mode 100644 templates/archive.html create mode 100644 templates/entry/test-entry1.html create mode 100644 templates/entry/test-entry2.html create mode 100644 templates/error.html create mode 100644 templates/index.html create mode 100644 templates/rss.xml create mode 100644 templates/template.html diff --git a/app.py b/app.py new file mode 100644 index 0000000..d181e88 --- /dev/null +++ b/app.py @@ -0,0 +1,108 @@ +from flask import Flask, flash, make_response, render_template, request, redirect +import datetime +from datetime import datetime +import os +from os import path +import pathlib + + +app = Flask(__name__) + +website = 'localhost:5000' + +@app.errorhandler(404) +def page_not_found(e): + return render_template('error.html', title='Error 404', errorcode='404'), 404 + + +@app.route('/') +@app.route('/index.html') +def index(): + content = gen_index_string() + print('content is: ', content) + return render_template('index.html', title='Blog', content_string=content) + + +@app.route('/archive') +@app.route('/archive.html') +def blog_archive(): + content = gen_arch_string() + return render_template('archive.html', title='Blog Archive', content_string=content) + + +@app.route('/feed.xml') +@app.route('/rss.xml') +def feed(): + content = get_rss_string() + rss_xml = render_template('rss.xml', content_string=content) + response = make_response(rss_xml) + response.headers['Content-Type'] = 'application/rss+xml' + return response + + +def gen_arch_string(): + path_ex = 'templates/entry' + if path.exists(path_ex): + name_list = os.listdir(path_ex) + full_list = [os.path.join(path_ex,i) for i in name_list] + contents = sorted(full_list, key=os.path.getctime) + content_string = '' + for file in contents: + filename = pathlib.PurePath(file) + title = open(filename).readline().rstrip('\n') + filename = filename.name + if filename[0] != '.': + filename = filename.split('.',1)[0] + content_string += '' + title + '
\n' + return content_string + +def gen_index_string(): + path_ex = 'templates/entry' + content_string = '' + if path.exists(path_ex): + name_list = os.listdir(path_ex) + full_list = [os.path.join(path_ex,i) for i in name_list] + contents = sorted(full_list, key=os.path.getctime) + for file in contents: + filename = pathlib.PurePath(file) + title = open(filename).readline().rstrip('\n') + text = open(filename).readlines()[1:] + filename = filename.name + if filename[0] != '.': + filename = filename.split('.',1)[0] + content_string += '
\n' + content_string += '

' + title + '

\n' + for line in text: + content_string += line + content_string += '
' + datetime.fromtimestamp(os.path.getctime(file)).strftime('%Y-%m-%d') + '' + content_string += '
' + return content_string + +def get_rss_string(): + path_ex = 'templates/entry' + if path.exists(path_ex): + name_list = os.listdir(path_ex) + full_list = [os.path.join(path_ex,i) for i in name_list] + contents = sorted(full_list, key=os.path.getctime) + content_string = '' + for file in contents: + filename = pathlib.PurePath(file) + title = open(filename).readline().rstrip('\n') + text = open(filename).readlines()[1:] + filename = filename.name + if filename[0] != '.': + filename = filename.split('.',1)[0] + content_string += '\n' + content_string += '' + title + '\n' + content_string += '' + '/index.html#' + filename + '\n' + content_string += '' + datetime.fromtimestamp(os.path.getctime(file)).strftime('%Y-%m-%d') + '\n' + content_string += '' + for line in text: + content_string += line + content_string += '\n' + content_string += '\n' + return content_string + + +if __name__ == '__main__': + app.run(host='0.0.0.0') diff --git a/templates/archive.html b/templates/archive.html new file mode 100644 index 0000000..e1ae910 --- /dev/null +++ b/templates/archive.html @@ -0,0 +1,11 @@ +{% extends 'template.html' %} +{% block content %} +
+
+

Blog-Archive


+ {% autoescape off %} + {{ content_string }} + {% endautoescape %} +
+
+{% endblock %} diff --git a/templates/entry/test-entry1.html b/templates/entry/test-entry1.html new file mode 100644 index 0000000..4c2ad02 --- /dev/null +++ b/templates/entry/test-entry1.html @@ -0,0 +1,2 @@ +Test Entry Title 1 +This is an example blog entry. diff --git a/templates/entry/test-entry2.html b/templates/entry/test-entry2.html new file mode 100644 index 0000000..b165eae --- /dev/null +++ b/templates/entry/test-entry2.html @@ -0,0 +1,2 @@ +Test Entry Title 2 +This is another example blog entry. diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..5baa36f --- /dev/null +++ b/templates/error.html @@ -0,0 +1,9 @@ +{% extends "template.html" %} +{% block content %} +
+
+ Error
+ {{ errorcode }} +
+
+{% endblock %} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..23c340c --- /dev/null +++ b/templates/index.html @@ -0,0 +1,15 @@ +{% extends "template.html" %} +{% block content %} +
+
+

Blog-Index


+

+ Archive
+ RSS +

+ {% autoescape off %} + {{ content_string }} + {% endautoescape %} +
+
+{% endblock %} diff --git a/templates/rss.xml b/templates/rss.xml new file mode 100644 index 0000000..527f410 --- /dev/null +++ b/templates/rss.xml @@ -0,0 +1,17 @@ + + + + + +The Latest from Marten +Updates from Marten Kante. Throw this in your RSS feeder for instant updates! +en-us +/blog/feed.xml + + +{% autoescape off %} +{{ content_string }} +{% endautoescape %} + + + diff --git a/templates/template.html b/templates/template.html new file mode 100644 index 0000000..c6ef677 --- /dev/null +++ b/templates/template.html @@ -0,0 +1,13 @@ + + + {{ title }} + + + + + + {% block content %} + {% endblock %} + + +