An error occurred while loading the file. Please try again.
-
Dorchies David authored6d011e73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /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)