#!/bin/python3
import os
import sys
import tempfile
import re
from config.config import *
def generate_menu():
"""Generate menu."""
sys.stderr.write("generating menu\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)
# Generate the string who contain the links of the menu
htmly_website_page = "
"
for name in sorted(os.listdir(os.path.join(BUILDDIR, "website"))):
if name != "Documents":
htmly_website_page += "- "+name.split('.html')[0]+"
"
htmly_website_page += "
"
# 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:
if os.path.exists("build/"+html_file):
with open("build/"+html_file, 'r', encoding='utf-8') as indexmd:
lines = indexmd.readlines()
with open("build/"+html_file, 'w', encoding='utf-8') as indexmd:
for line in lines:
indexmd.write(re.sub(r'-- generate menu here --', htmly_website_page, line))
os.remove(tmppath)