From 5c1e0dda4e2d3d04eaa70525d5d3f3020d542b64 Mon Sep 17 00:00:00 2001
From: neodarz <neodarz@neodarz.net>
Date: Fri, 15 Nov 2019 07:54:41 +0100
Subject: Add multi language support for index

---
 generators/generators.py | 123 ++++++++++++++++++++++++++---------------------
 1 file changed, 68 insertions(+), 55 deletions(-)

(limited to 'generators')

diff --git a/generators/generators.py b/generators/generators.py
index 3122f9c8..c6cca082 100644
--- a/generators/generators.py
+++ b/generators/generators.py
@@ -304,63 +304,70 @@ def generate_notes_list():
 
 def generate_index(feed):
     """Generate index.html from index.md and a TOC."""
+    for language in LANGUAGES:
+        if language != DEFAULTLANG:
+            indexmdl = re.sub(r'index.md$', "index-{}.md".format(language), INDEXMD)
+            indexhtmll = re.sub(r'index.html$', "index-{}.html".format(language), INDEXHTML)
+        else:
+            indexmdl = INDEXMD
+            indexhtmll = INDEXHTML
+
+        sys.stderr.write("generating index-{}.html\n".format(language))
+
+        # 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
 
-    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>'
+            # 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>')
+        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)
+        # 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(indexmdl):
+                with open(indexmdl, '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", indexhtmll,
+        ]
+        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."""
@@ -369,7 +376,7 @@ def generate_sitemap(feed):
     sitemap.append(utils.make_sitemap_url_element(BLOG_HOME, feed.updated, "daily", 1.0))
     # other top level pages
     for name in os.listdir(BUILDDIR):
-        if (not name.endswith(".html") or name == "index.html" or
+        if (not name.endswith(".html") or re.match("index.*\.html", name) or
             re.match("google[a-z0-9]+\.html", name)):  # exclude Google's site ownership verification file
             continue
         link = urllib.parse.urljoin(BLOG_HOME, name)
@@ -799,7 +806,13 @@ def generate_blog(fresh=False, report_total_errors=True):
                  max(fundamental_mtime, os.path.getmtime(srcpath)))):
                 # new post or modified post
                 anything_modified = True
-                if srcpath == INDEXMD:
+                indexmdlist = []
+                for language in LANGUAGES:
+                    if language != DEFAULTLANG:
+                        indexmdlist.append("index-{}.md".format(language))
+                    else:
+                        indexmdlist.append("index.md")
+                if srcpath in indexmdlist:
                     continue # index will be processed separately
                 if extension in ["css", "js",  "asc", "html", "jpg", "png", "svg", "ico", "txt",
                                  "eot", "ttf", "woff", "woff2"]:
-- 
cgit v1.2.1