From 64da66626e7d9d809495c07d310969e44482d0f9 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 24 Nov 2019 21:40:10 +0100 Subject: Add right menu language switch --- generators/generators.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'generators') 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 = "" + 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) -- cgit v1.2.1