aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-05-26 15:46:29 +0200
committerneodarz <neodarz@neodarz.net>2019-05-26 15:46:29 +0200
commit7e3060cc02aacecd13ba220cf69d1b8ae2517e09 (patch)
treef902fc845dcb707e610f5bac44e25c755fc6ddc6
parent60e70f9e2b4b14ea5533531f7a30510866601b44 (diff)
downloadmy_new_personal_website-7e3060cc02aacecd13ba220cf69d1b8ae2517e09.tar.xz
my_new_personal_website-7e3060cc02aacecd13ba220cf69d1b8ae2517e09.zip
Move generate_index to external file
-rw-r--r--generators/generators.py64
-rwxr-xr-xpyblog63
2 files changed, 65 insertions, 62 deletions
diff --git a/generators/generators.py b/generators/generators.py
index 1792fa57..06a34743 100644
--- a/generators/generators.py
+++ b/generators/generators.py
@@ -8,6 +8,9 @@ import re
import bs4
import dateutil
+import io
+import subprocess
+
from config.config import *
@@ -284,3 +287,64 @@ def generate_notes_list():
with open("build/notes/index.html", 'w', encoding='utf-8') as indexmd:
for line in lines:
indexmd.write(re.sub(r'{% generate notes_list here %}', div_notes_list, line))
+
+
+def generate_index(feed):
+ """Generate index.html from index.md and a TOC."""
+
+ sys.stderr.write("generating index.html\n")
+
+ # generate TOC
+ tocbuff = io.StringIO()
+ tocbuff.write('<div class="blog-index" id="toc">')
+ 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:
+ tocbuff.write(u'</table>\n')
+ # write a new <h2 class="blog-index-year-title"> tag with the smaller year
+ year = date.year
+ tocbuff.write(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))
+ tocbuff.write(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")
+ tocbuff.write(u'<tr><td class="blog-index-post-date"><time class="date" datetime="%s">%s</time></td>'
+ '<td class="blog-index-post-title">[%s](%s)</td></tr>\n' %
+ (date.isoformat(), monthday, entry.title_text, entry.relpath))
+ if table_opened:
+ tocbuff.write(u'</table>\n')
+ tocbuff.write('</div>')
+
+ # create tempfile with index.md and the TOC concatenated, and generate index.html from that
+ # pylint: disable=invalid-name
+ fd, tmppath = tempfile.mkstemp()
+ os.close(fd)
+ with open(tmppath, 'w', encoding='utf-8') as tmpfile:
+ if os.path.exists(INDEXMD):
+ with open(INDEXMD, 'r', encoding='utf-8') as indexmd:
+ tmpfile.write(u"%s\n\n<hr>\n\n" % indexmd.read())
+ tmpfile.write("%s\n" % tocbuff.getvalue())
+ tocbuff.close()
+
+ pandoc_args = [
+ "pandoc", tmppath,
+ "--template", HTMLTEMPLATE,
+ "--highlight-style=pygments",
+ "-o", INDEXHTML,
+ ]
+ try:
+ subprocess.check_call(pandoc_args)
+ except subprocess.CalledProcessError:
+ sys.stderr.write("error: failed to generate index.html\n")
+ os.remove(tmppath)
diff --git a/pyblog b/pyblog
index d1e83ba0..c398b932 100755
--- a/pyblog
+++ b/pyblog
@@ -51,67 +51,6 @@ from config.config import *
from generators import generators
-def generate_index(feed):
- """Generate index.html from index.md and a TOC."""
-
- sys.stderr.write("generating index.html\n")
-
- # generate TOC
- tocbuff = io.StringIO()
- tocbuff.write('<div class="blog-index" id="toc">')
- 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:
- tocbuff.write(u'</table>\n')
- # write a new <h2 class="blog-index-year-title"> tag with the smaller year
- year = date.year
- tocbuff.write(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))
- tocbuff.write(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")
- tocbuff.write(u'<tr><td class="blog-index-post-date"><time class="date" datetime="%s">%s</time></td>'
- '<td class="blog-index-post-title">[%s](%s)</td></tr>\n' %
- (date.isoformat(), monthday, entry.title_text, entry.relpath))
- if table_opened:
- tocbuff.write(u'</table>\n')
- tocbuff.write('</div>')
-
- # create tempfile with index.md and the TOC concatenated, and generate index.html from that
- # pylint: disable=invalid-name
- fd, tmppath = tempfile.mkstemp()
- os.close(fd)
- with open(tmppath, 'w', encoding='utf-8') as tmpfile:
- if os.path.exists(INDEXMD):
- with open(INDEXMD, 'r', encoding='utf-8') as indexmd:
- tmpfile.write(u"%s\n\n<hr>\n\n" % indexmd.read())
- tmpfile.write("%s\n" % tocbuff.getvalue())
- tocbuff.close()
-
- pandoc_args = [
- "pandoc", tmppath,
- "--template", HTMLTEMPLATE,
- "--highlight-style=pygments",
- "-o", INDEXHTML,
- ]
- try:
- subprocess.check_call(pandoc_args)
- except subprocess.CalledProcessError:
- sys.stderr.write("error: failed to generate index.html\n")
- os.remove(tmppath)
-
-
def generate_sitemap(feed):
"""Generate sitemap.xml."""
sitemap = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
@@ -414,7 +353,7 @@ def generate_index_and_feed():
feed.entries.sort(key=lambda entry: entry.updated_datetime, reverse=True)
rss.items.sort(key=lambda item: item.timestamp, reverse=True)
- generate_index(feed)
+ generators.generate_index(feed)
generators.generate_menu()
generators.generate_table()
generators.generate_blog_list(feed)