aboutsummaryrefslogtreecommitdiff
path: root/generators
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-11-24 21:40:10 +0100
committerneodarz <neodarz@neodarz.net>2019-11-24 21:40:10 +0100
commit64da66626e7d9d809495c07d310969e44482d0f9 (patch)
treee84da1f5ee7972ee9c4db1a5aa4a30c8bd922528 /generators
parent5c1e0dda4e2d3d04eaa70525d5d3f3020d542b64 (diff)
downloadmy_new_personal_website-64da66626e7d9d809495c07d310969e44482d0f9.tar.xz
my_new_personal_website-64da66626e7d9d809495c07d310969e44482d0f9.zip
Add right menu language switch
Diffstat (limited to 'generators')
-rw-r--r--generators/generators.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/generators/generators.py b/generators/generators.py
index c6cca082..c7a45e4b 100644
--- a/generators/generators.py
+++ b/generators/generators.py
@@ -26,6 +26,66 @@ from config.config import *
from utils import utils
+def generate_lang_switch():
+ """Generate language switcher"""
+
+ sys.stderr.write("generating langugage switcher\n")
+
+ fd, tmppath = tempfile.mkstemp()
+ os.close(fd)
+
+ # Put in a list the pages where the menu will be written
+ html_fileList = []
+ for root, dirs, files in os.walk(BUILDDIR):
+ for name in files:
+ if name.endswith(".html"):
+ try:
+ html_fileList.append(os.path.join(root.split('build/')[1], name))
+ except IndexError:
+ html_fileList.append(name)
+
+ # Writing the menu in all pages contained in the variable in place of the -- generate menu here --
+ for html_file in html_fileList:
+ with open(tmppath, 'w', encoding='utf-8') as tmpfile:
+ trad_files = []
+ html_file_name = re.sub(r"(.*)-[a-z]{2}(\.html)", r"\1\2",html_file)
+ for language in LANGUAGES:
+ if language != DEFAULTLANG:
+ trad_file = "{}-{}.html".format(
+ html_file_name.rsplit('.', 1)[0],
+ language)
+ if os.path.exists("build/"+trad_file):
+ trad_files.append({"file":trad_file, "lang":language})
+ else:
+ trad_file = "{}.html".format(html_file_name.rsplit('.', 1)[0])
+ if os.path.exists("build/"+trad_file):
+ trad_files.append({"file":trad_file, "lang":language})
+ if len(trad_files) > 1 and os.path.exists("build/"+html_file):
+ with open("build/"+html_file, 'r', encoding='utf-8') as f:
+ htmly_website_page = "<nav class='lang_switch'><div class='lia-border'><ul>"
+ for trad_file in trad_files:
+ if trad_file["file"] == html_file:
+ htmly_website_page += "<a href='{}' class='current lia'><li><span class='left-lia'></span><span class='center-lia'>{}</span><span class='right-lia'></span></li></a>".format(
+ trad_file["file"],
+ trad_file["lang"])
+ else:
+ htmly_website_page += "<a href='{}' class='lia'><li><span class='left-lia'></span><span class='center-lia'>{}</span><span class='right-lia'></span></li></a>".format(
+ trad_file["file"],
+ trad_file["lang"])
+ htmly_website_page += "</ul></div></nav>"
+ lines = f.readlines()
+ with open("build/"+html_file, 'w', encoding='utf-8') as f:
+ for line in lines:
+ f.write(re.sub(r'-- generate LANGSWITCH --', htmly_website_page, line))
+ else:
+ with open("build/"+html_file, 'r', encoding='utf-8') as f:
+ lines = f.readlines()
+ with open("build/"+html_file, 'w', encoding='utf-8') as f:
+ for line in lines:
+ f.write(re.sub(r'-- generate LANGSWITCH --', "", line))
+
+ os.remove(tmppath)
+
def generate_menu():
"""Generate menu."""
@@ -673,6 +733,7 @@ def generate_index_and_feed():
rss.items.sort(key=lambda item: item.timestamp, reverse=True)
generate_index(feed)
+ generate_lang_switch()
generate_menu()
generate_table()
generate_blog_list(feed)