mirror of
https://github.com/venthur/blag.git
synced 2025-11-25 20:52:43 +00:00
restructured archive and feed code
This commit is contained in:
53
blag/blag.py
53
blag/blag.py
@@ -89,14 +89,30 @@ def build(args):
|
|||||||
path = os.path.relpath(f'{root}/{dirname}', start=args.input_dir)
|
path = os.path.relpath(f'{root}/{dirname}', start=args.input_dir)
|
||||||
os.makedirs(f'{args.output_dir}/{path}', exist_ok=True)
|
os.makedirs(f'{args.output_dir}/{path}', exist_ok=True)
|
||||||
|
|
||||||
process_markdown(
|
env = Environment(
|
||||||
convertibles, args.input_dir,
|
loader=ChoiceLoader([
|
||||||
|
FileSystemLoader([args.template_dir]),
|
||||||
|
PackageLoader('blag', 'templates'),
|
||||||
|
])
|
||||||
|
)
|
||||||
|
page_template = env.get_template('page.html')
|
||||||
|
article_template = env.get_template('article.html')
|
||||||
|
archive_template = env.get_template('archive.html')
|
||||||
|
|
||||||
|
articles, pages = process_markdown(
|
||||||
|
convertibles,
|
||||||
|
args.input_dir,
|
||||||
args.output_dir,
|
args.output_dir,
|
||||||
args.template_dir
|
page_template,
|
||||||
|
article_template,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
generate_feed(articles, args.output_dir)
|
||||||
|
generate_archive(articles, archive_template, args.output_dir)
|
||||||
|
|
||||||
def process_markdown(convertibles, input_dir, output_dir, template_dir):
|
|
||||||
|
def process_markdown(convertibles, input_dir, output_dir,
|
||||||
|
page_template, article_template):
|
||||||
"""Process markdown files.
|
"""Process markdown files.
|
||||||
|
|
||||||
This method processes the convertibles, converts them to html and
|
This method processes the convertibles, converts them to html and
|
||||||
@@ -111,20 +127,14 @@ def process_markdown(convertibles, input_dir, output_dir, template_dir):
|
|||||||
relative paths to markdown- (src) html- (dest) files
|
relative paths to markdown- (src) html- (dest) files
|
||||||
input_dir : str
|
input_dir : str
|
||||||
output_dir : str
|
output_dir : str
|
||||||
template_dir : str
|
page_template, archive_template : jinja2 template
|
||||||
|
templats for pages and articles
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
articles, pages : List[Tuple[str, Dict]]
|
articles, pages : List[Tuple[str, Dict]]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
env = Environment(
|
|
||||||
loader=ChoiceLoader([
|
|
||||||
FileSystemLoader([template_dir]),
|
|
||||||
PackageLoader('blag', 'templates'),
|
|
||||||
])
|
|
||||||
)
|
|
||||||
|
|
||||||
md = markdown_factory()
|
md = markdown_factory()
|
||||||
|
|
||||||
articles = []
|
articles = []
|
||||||
@@ -143,18 +153,19 @@ def process_markdown(convertibles, input_dir, output_dir, template_dir):
|
|||||||
# everything else are just pages
|
# everything else are just pages
|
||||||
if meta and 'date' in meta:
|
if meta and 'date' in meta:
|
||||||
articles.append((dst, context))
|
articles.append((dst, context))
|
||||||
template = env.get_template('article.html')
|
result = article_template.render(context)
|
||||||
else:
|
else:
|
||||||
pages.append((dst, content))
|
pages.append((dst, content))
|
||||||
template = env.get_template('page.html')
|
result = page_template.render(context)
|
||||||
result = template.render(context)
|
|
||||||
with open(f'{output_dir}/{dst}', 'w') as fh_dest:
|
with open(f'{output_dir}/{dst}', 'w') as fh_dest:
|
||||||
fh_dest.write(result)
|
fh_dest.write(result)
|
||||||
|
|
||||||
# sort articles by date, descending
|
# sort articles by date, descending
|
||||||
articles = sorted(articles, key=lambda x: x[1]['date'], reverse=True)
|
articles = sorted(articles, key=lambda x: x[1]['date'], reverse=True)
|
||||||
|
return articles, pages
|
||||||
|
|
||||||
# generate feed
|
|
||||||
|
def generate_feed(articles, output_dir):
|
||||||
feed = feedgenerator.Atom1Feed(
|
feed = feedgenerator.Atom1Feed(
|
||||||
link='https://venthur.de',
|
link='https://venthur.de',
|
||||||
title='my title',
|
title='my title',
|
||||||
@@ -170,23 +181,21 @@ def process_markdown(convertibles, input_dir, output_dir, template_dir):
|
|||||||
pubdate=context['date'],
|
pubdate=context['date'],
|
||||||
)
|
)
|
||||||
|
|
||||||
with open('atom.xml', 'w') as fh:
|
with open(f'{output_dir}/atom.xml', 'w') as fh:
|
||||||
feed.write(fh, encoding='utf8')
|
feed.write(fh, encoding='utf8')
|
||||||
|
|
||||||
# generate archive
|
|
||||||
|
def generate_archive(articles, template, output_dir):
|
||||||
archive = []
|
archive = []
|
||||||
for dst, context in articles:
|
for dst, context in articles:
|
||||||
entry = context.copy()
|
entry = context.copy()
|
||||||
entry['dst'] = dst
|
entry['dst'] = dst
|
||||||
archive.append(entry)
|
archive.append(entry)
|
||||||
|
|
||||||
template = env.get_template('archive.html')
|
|
||||||
result = template.render(dict(archive=archive))
|
result = template.render(dict(archive=archive))
|
||||||
with open('build/index.html', 'w') as fh:
|
with open(f'{output_dir}/index.html', 'w') as fh:
|
||||||
fh.write(result)
|
fh.write(result)
|
||||||
|
|
||||||
return articles, pages
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
<nav>
|
<nav>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="/">Blog</a></li>
|
<li><a href="/">Blog</a></li>
|
||||||
|
<li><a href="/atom.xml">Atom Feed</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
Reference in New Issue
Block a user