1
0
flewkey.com/scripts/gen_blog.py

35 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import markdown
from template import template, escape_braces
from pymdownx.superfences import SuperFencesCodeExtension
def gen_list(files, ext, limit=-1, mini=False):
from template import template
md = markdown.Markdown(extensions = ["meta", "extra", "toc", SuperFencesCodeExtension(preserve_tabs=True)])
items = ""
i = 0
for post in files:
with open("posts/"+post, "r", encoding="utf-8") as file_in:
html = md.convert(file_in.read())
if "visibility" not in md.Meta or md.Meta["visibility"] == "hidden":
md.Meta["template"] = "post_preview" if not mini else "post_preview_mini"
items += template(escape_braces(html), "blog/"+post[:-3]+ext, ext, md.Meta)
i += 1
if limit >= 0 and i >= limit:
break;
return items
def main():
meta = {"template": "post_list"}
ext = "."+sys.argv[2].split(".")[-1]
files = sorted(os.listdir(sys.argv[1]), reverse=True)
output = template(gen_list(files, ext), sys.argv[2], ext, meta)
with open(sys.argv[2], "w+", encoding="utf-8") as file_out:
file_out.write(output)
if __name__ == "__main__":
main()