Eclectic Media Git archived_em.space_site / 433f520
Added blog markdown rendering engine Ariana Giroux 6 months ago
2 changed file(s) with 51 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
0 from flask import Flask
0 import os
1 from flask import Flask, abort
12
3 from utilities import sort_blog
24 from utilities.renderer import render, render_markdown
35
46 app = Flask(__name__)
1416 )
1517
1618
17 @app.route('/open-source-difference')
18 def open_source_difference():
19 return render('widgets/content.jinja',
20 context={'body':
21 render_markdown('content/open-source-difference.md')}
22 )
19 @app.route('/blog/', methods=['GET'])
20 def blog():
21 return render('blog.jinja',
22 context={'items': [item for item in sort_blog()]})
23
24
25 @app.route('/blog/<article>', methods=['GET'])
26 def article(article):
27 article = f'content/blog/{article}.md'
28 if os.access(article, os.F_OK):
29 return render('widgets/content.jinja',
30 context={'body':
31 render_markdown(article)}
32 )
33 else:
34 abort(404)
2335
2436
2537 @app.route('/an-eclectic-background')
0 import os
1 from .renderer import render_markdown
2
3
4 def __yield_files():
5 for path, subdirs, files in os.walk('content/blog/'):
6 for name in reversed(sorted(files, key=lambda name:
7 os.path.getmtime(os.path.join(path, name)))):
8 yield name, os.path.join(path, name)
9
10
11 def __process_blog_names():
12 for name, path in __yield_files():
13 name = name.replace('-', ' ')
14 name = name.replace('.md', '')
15 name = name.title()
16
17 link = name.lower().replace(' ', '-')
18
19 yield name, link, path
20
21
22 def __get_blog_text(path):
23 text = render_markdown(path)
24 for line in text.split('\n'):
25 if not line.count('h1') and line.count('<p>'):
26 return line.replace('<p>', '').replace('</p>', '')
27
28
29 def sort_blog():
30 for name, link, path in __process_blog_names():
31 yield name, link, path, os.path.getmtime(path), __get_blog_text(path)