From f2d9f3041e857fe08359647671edfd1d75718ff1 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 26 May 2019 13:45:44 +0200 Subject: Move generate_blog_list to external file --- generators/generators.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'generators/generators.py') diff --git a/generators/generators.py b/generators/generators.py index 9e0fb33c..1247a1c5 100644 --- a/generators/generators.py +++ b/generators/generators.py @@ -137,3 +137,61 @@ def generate_table(): indexmd.write(re.sub(r"-- end table --", "", line)) os.remove(tmppath) + + +def generate_blog_list(feed): + """"Generate blog list """ + + sys.stderr.write("generating blog list\n") + + html_fileList = [] + for root, dirs, files in os.walk(BUILDDIR): + for name in files: + if re.search(r'blog',root): + if name.endswith(".html"): + try: + html_fileList.append(os.path.join(root.split('blog/')[1], name)) + except IndexError: + html_fileList.append(name) + + # generate TOC + for html_file in html_fileList: + div_blog_list = u'
\n\n' + year = 10000 # will be larger than the latest year for quite a while + # recall that entries are in reverse chronological order + table_opened = False + for entry in feed.entries: + date = entry.updated_datetime + if date.year < year: + # close the previous table if there is one + if table_opened: + div_blog_list += u'\n' + # write a new

tag with the smaller year + year = date.year + div_blog_list += u'\n

.:{0}:.

\n\n'.format(year) + div_blog_list += u'\n' + table_opened = True + + # write a new table row entry in Markdown, in the format: + # + # + # + # + # + monthday = date.strftime("%b %d") + div_blog_list += (u'' + '\n' % + (date.isoformat(), monthday, entry.relpath, entry.title_text)) + if table_opened: + div_blog_list += u'
[Blah blah](/blog/2015-05-04-blah-blah.html)
%s
\n' + div_blog_list += u'
' + + fd, tmppath = tempfile.mkstemp() + os.close(fd) + with open(tmppath, 'w', encoding='utf-8') as tmpfile: + if os.path.exists("build/blog/index.html"): + with open("build/blog/index.html", 'r', encoding='utf-8') as indexmd: + lines = indexmd.readlines() + with open("build/blog/index.html", 'w', encoding='utf-8') as indexmd: + for line in lines: + indexmd.write(re.sub(r'{% generate blog_list here %}', div_blog_list, line)) -- cgit v1.2.1