diff options
Diffstat (limited to 'generators/generators.py')
-rw-r--r-- | generators/generators.py | 58 |
1 files changed, 58 insertions, 0 deletions
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 --", "</tbody></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'<div class="blog-index" id="toc">\n</table>\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'</table>\n' + # write a new <h2 class="blog-index-year-title"> tag with the smaller year + year = date.year + div_blog_list += u'\n<h2 class="blog-index-year-title" id="{0}"><span class="left-h2">.:</span><span class="title-h2">{0}</span><span class="right-h2">:.</span></h2>\n\n'.format(year) + div_blog_list += u'<table class="blog-index-yearly-index">\n' + table_opened = True + + # write a new table row entry in Markdown, in the format: + # + # <tr> + # <td class="blog-index-post-date"><time class="date" datetime="2015-05-05T00:06:04-0700">May 5</time></td> + # <td class="blog-index-post-title">[Blah blah](/blog/2015-05-04-blah-blah.html)</td> + # </tr> + monthday = date.strftime("%b %d") + div_blog_list += (u'<tr><td class="blog-index-post-date"><time class="date" datetime="%s">%s</time></td>' + '<td class="blog-index-post-title"><a href="%s">%s</a></td></tr>\n' % + (date.isoformat(), monthday, entry.relpath, entry.title_text)) + if table_opened: + div_blog_list += u'</table>\n' + div_blog_list += u'</div>' + + 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)) |