-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgen_mainpage.py
103 lines (77 loc) · 3.57 KB
/
gen_mainpage.py
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
import re
import pathlib
"""
This is a very naive parser that converts the README.md file, accepted
by GitHub, to the doc/mainpage.md file, accepted by doxygen. It is
possible that it may get some things wrong, so always a good idea to
build locally first and check in a browser.
I haven't found an alternative robust solution yet so this "hacky"
script will have to do for now.
"""
class LineFixer:
def __init__(self):
self._outside_formula = True
self._outside_code = True
def fix(self, line):
# Adjust width of youtube video image
if 'width="50%"' in line and "REBmbCANx0s" in line:
return line.replace('width="50%"', 'width="30%"')
# Check if this is the start/end of code
if ("```python" in line) or "```" in line or ("```bibtex" in line):
if self._outside_code:
# we are outside code extract (i.e. at the start), so replace with the start of code
if "python" in line:
line = "\code{.py}\n" # need \n since a space is put at the start of the code snippet otherwise
elif "bibtex" in line:
line = "\code{.bib}\n" # need \n since a space is put at the start of the code snippet otherwise
else:
# we are inside code extract (i.e. at the end), so replace with the end of code
line = "\endcode"
self._outside_code = not self._outside_code
return line
# Check if ` character appears twice in the line (i.e. inline code)
if line.count("`") == 2:
# Define a regular expression pattern to match the inline LaTeX
pattern = r"\`(.*?)\`"
# Define a function to handle the replacement
def replace_latex(match):
return r"<code>%s</code>" % match.group(1)
line = re.sub(pattern, replace_latex, line)
return line
# Check if this line corresponds to beginning/ending of a formula
if "$$" in line:
if self._outside_formula:
# we are outside the formula (i.e. at the start), so replace with the start of formula
line = line.replace("$$", r"\f[")
else:
# we are inside the formula, so replace with end of formula
line = line.replace("$$", r"\f]")
self._outside_formula = not self._outside_formula
return line
# Define a regular expression pattern to match the inline LaTeX
pattern = r"\$(.*?)\$"
# Define a function to handle the replacement
def replace_latex(match):
return r"\f$%s\f$" % match.group(1)
if "$" in line: # and ('$$' not in line):
line = re.sub(pattern, replace_latex, line)
line = line.replace("\|\|", "\|")
line = line.replace("doc/image/eu.png", "eu.png")
return line
def main():
line_fixer = LineFixer()
repo_path = pathlib.Path(__file__).parent.absolute().parent.absolute()
doc_path = repo_path / "doc"
readme_file_name = repo_path / "README.md"
mainpage_file_name = doc_path / "mainpage.md"
if mainpage_file_name.is_file():
mainpage_file_name.unlink()
print("Removed old version of doc/mainpage.md")
with open(readme_file_name, "r") as input_file:
with open(mainpage_file_name, "w") as output_file:
for line in input_file.readlines():
new_line = line_fixer.fix(line)
output_file.write(new_line)
print("Created doc/mainpage.md")
if __name__ == "__main__":
main()