#! /bin/python3 # Combine tous les fichiers markdown de la documentation en un seul fichier # Usage : # python3 ./mkdocs-combine.py > cassiopee_doc-fr.md # Pour la conversion au format tex : # pandoc cassiopee_doc-fr.md -o cassiopee_doc-fr.md.tex outputDir = 'docs-fr_pdf' filename = 'cassiopee_docs-fr' def readConfig(sYAML): f = open(sYAML, 'r') with f: try: import yaml dMkdocsYaml = yaml.load(f, Loader=yaml.SafeLoader) except yaml.YAMLError as e: import sys sys.exit("ERROR on YAML loading {}: {}".format(sYAML, str(e))) return dMkdocsYaml def getMdHeader(title, level): return "#" * level + " " + title + "\n" def shiftMdHeaders(mdContent, level): import re if level == 0: return mdContent lMd = mdContent.splitlines() for i, item in enumerate(lMd): if len(item) > 0 : if item[0] == "#": lMd[i] = ("#" * level) + item return "\n".join(lMd) def explore(docs_dir, nav, output = '', level = 0): """ @param docs_dir path to the mkdocs content files @param dictionnary with the structure to explore @param output markdown files content already merged """ import os, re if type(nav) is str: nav = [nav] for d in nav: if type(d) is str: filepath = os.path.join(docs_dir, d) f = open(filepath, 'r') path = os.path.join('..', os.path.dirname(filepath)) s = f.read() + "\n" # Modification of image and links paths s = re.sub(r'(\!\[.+\]\()(.+)(\))', r'\1'+path+r'/\2\3', s) s = re.sub(r'(\\\()(.+?)(\\\))', r'$\2$', s) s = shiftMdHeaders(s, level) output += "\n" + s elif type(d) is dict: level += 1 for key, value in d.items(): if type(value) is list: output += "\n" + getMdHeader(key, level) + "\n" output = explore(docs_dir, value, output, level) level -= 1 return output if __name__ == '__main__': # Read config dMkdocsYaml = readConfig('mkdocs.yml') # Create string with merged MarkDown s = explore(dMkdocsYaml['docs_dir'], dMkdocsYaml['nav']) # Save the md file import os os.makedirs(outputDir, exist_ok=True) OutputPath = os.path.join(outputDir, filename) with open('{}.md'.format(OutputPath), 'w') as f: f.writelines(s) # Convert to tex format os.system( 'pandoc {0}.md -f markdown -t latex -s -o {0}.tex'.format(OutputPath) ) # Remove header of tex file bContent = False ls = [] with open('{}.tex'.format(OutputPath), 'r') as f: for line in f: if line.strip() == '\\end{document}': bContent = False exit if bContent: ls.append(line) if line.strip() == '\\begin{document}': bContent = True for i, line in enumerate(ls): ls[i] = line.replace('\\includegraphics', '\\includegraphics[max size={\\textwidth}{0.9\\textheight}]') with open('{}.tex'.format(OutputPath), 'w') as f: f.writelines(ls)