1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# seen, flask edition - A simple blog generator.
# Copyright (C) 2022 Ferass EL HAFIDI
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask import Flask
from flask import render_template
from flask import abort
from flask.wrappers import Response
from markupsafe import Markup
import glob
import markdown
import configparser
app = Flask(__name__)
@app.route('/')
def index():
"""
This is the article index function. It generates a list of articles.
"""
config = configparser.ConfigParser()
config.read('config.ini')
articles = "<div class=\"articles\">"
# Get config values such as the Blog name and the Welcome text.
try:
blog_name = config['Blog']['Name']
description = config['Blog']['Description']
footer = config['Blog']['Footer']
except KeyError:
return "<code>Missing configuration! Please configure the config.ini!</code>"
# Get a list of all articles.
for file in glob.glob("articles/*.md"):
try:
articles += "\n<a href=\"" + file.replace(".md", "") + "\">" + \
config[file.replace(".md", "").replace("articles/", "")]['Title'] + "</a>"
except KeyError:
return "<code>No config.ini entry! " + \
"Please create a config.ini entry for the %s article!</code>" % file
articles += "\n</div>"
# Return index.
return render_template('template.html', blog_name = blog_name,
welcome_text = description, footer = footer, article_index = Markup(articles))
@app.route('/articles/')
@app.route('/articles/<name>')
def article(name = None):
"""
This is the article function. It parses a markdown file, converts it to
html, and formats it.
"""
# Open the markdown file, complain if it doesn't exist.
try: article_file = open("articles/" + name + ".md", mode='r')
except OSError: abort(404)
# Convert it to HTML5.
article_text = Markup(markdown.markdown(article_file.read()))
# Close the file
article_file.close()
# Override the defaults
config = configparser.ConfigParser()
config.read('config.ini')
try:
article_title = config[name]['Title']
article_date = config[name]['Date']
article_description = config[name]['Description']
footer = config['Blog']['Footer']
except KeyError:
return "<code>Missing configuration! Please configure the config.ini!</code>"
# Finally, return the article text.
return render_template('template.html', article = True, article_title = article_title,
article_text = article_text, article_description = article_description,
article_date = article_date, footer = footer)
@app.route('/rss.xml')
def rss_feeds():
"""
This function generates RSSĀ Feeds.
"""
config = configparser.ConfigParser()
config.read('config.ini')
if config['Blog']['RSS'] != 'y': abort(404)
blog_name = config['Blog']['Name']
try:
blog_description = config['Blog']['Description']
rss_feed = "<rss version=\"2.0\">"
rss_feed += "<channel>" + "<title>" + blog_name + "</title>"
rss_feed += "<link>" + config['Blog']['URL'] + "</link>"
rss_feed += "<description>" + blog_description + "</description>"
except KeyError:
return "<code>Missing configuration! Please configure the config.ini!</code>"
# Create an RSS entry for each article.
for file in glob.glob("articles/*.md"):
article = open(file, mode='r')
rss_feed += "<item>"
rss_feed += "<title>" + \
config[file.replace("articles/", "").replace(".md", "")]['Title'] + \
"</title>"
rss_feed += "<description><![CDATA[" + markdown.markdown(article.read()) + "]]></description>"
rss_feed += "</item>"
article.close()
rss_feed += "</channel>" + "</rss>"
# Return it!
return Response(rss_feed, mimetype='application/xml')
|