From a0f77c7bc4fb75f4d1a1035720f1402a54fd3985 Mon Sep 17 00:00:00 2001 From: TiynGER Date: Sun, 24 May 2020 17:33:42 +0200 Subject: [PATCH] markdown-support added --- app.py | 79 ++----------------------- content.py | 104 +++++++++++++++++++++++++++++++++ requirements.txt | 1 + templates/entry/test-entry3.md | 10 ++++ templates/rss.xml | 7 +-- 5 files changed, 123 insertions(+), 78 deletions(-) create mode 100644 content.py create mode 100644 templates/entry/test-entry3.md diff --git a/app.py b/app.py index d181e88..9a9923d 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,10 @@ 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 + +import content as con_gen app = Flask(__name__) -website = 'localhost:5000' @app.errorhandler(404) def page_not_found(e): @@ -18,91 +14,26 @@ def page_not_found(e): @app.route('/') @app.route('/index.html') def index(): - content = gen_index_string() - print('content is: ', content) + content = con_gen.gen_index_string() return render_template('index.html', title='Blog', content_string=content) @app.route('/archive') @app.route('/archive.html') def blog_archive(): - content = gen_arch_string() + content = con_gen.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() + content = con_gen.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/content.py b/content.py new file mode 100644 index 0000000..651b1f0 --- /dev/null +++ b/content.py @@ -0,0 +1,104 @@ +import datetime +from datetime import datetime +import markdown +import os +from os import path +import pathlib + +WEBSITE = 'localhost:5000' + +ENTRY_DIR = 'templates/entry' + + +def gen_arch_string(): + path_ex = ENTRY_DIR + 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 reversed(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 = ENTRY_DIR + 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 reversed(contents): + filename = pathlib.PurePath(file) + purefile = filename + 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' + if file.endswith('.html'): + for line in text: + content_string += line + content_string += '
' + if file.endswith('.md'): + content_string += gen_md_content(file) + content_string += '' + \ + datetime.fromtimestamp(os.path.getctime( + file)).strftime('%Y-%m-%d') + '' + content_string += '
' + return content_string + + +def gen_md_content(path_ex): + content_string = '' + if path.exists(path_ex): + filename = path_ex.split('.', 1) + fileend = filename[len(filename) - 1] + markdown_file = open(path_ex, "r") + depth = 2 + header = '#' + for i in range(depth): + header += '#' + header += ' ' + markdown_text = markdown_file.read().replace('# ', header) + content_string = markdown.markdown( + markdown_text, extensions=["fenced_code", "tables"] + ) + return content_string + + +def get_rss_string(): + path_ex = ENTRY_DIR + 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 reversed(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 diff --git a/requirements.txt b/requirements.txt index 46a48dd..5ab13d0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ Flask==1.1.2 +Markdown==3.1.1 diff --git a/templates/entry/test-entry3.md b/templates/entry/test-entry3.md new file mode 100644 index 0000000..440dae5 --- /dev/null +++ b/templates/entry/test-entry3.md @@ -0,0 +1,10 @@ +Test Entry Title 3 +This is a markdown file + +- list entry +- list entry +- list entry + +# md-header + +more content diff --git a/templates/rss.xml b/templates/rss.xml index 527f410..a4936cc 100644 --- a/templates/rss.xml +++ b/templates/rss.xml @@ -1,12 +1,11 @@ - -The Latest from Marten -Updates from Marten Kante. Throw this in your RSS feeder for instant updates! + Blog Title + A short description of the blog. en-us -/blog/feed.xml +localhost:5000/feed.xml {% autoescape off %}