Peano
Loading...
Searching...
No Matches
convert-jupyter-notebooks.py
Go to the documentation of this file.
2# Extremely primitive Python script which takes the markdown generated from a
3# Jupyter notebook and adds all the stuff such that it is automatically
4# embedded into the generated webpage of Peano.
5#
6# See page page_tutorials_overview
7#
8import os
9import os.path
10import subprocess
11
12from enum import Enum
13
14
15class Mode(Enum):
16 Text = (1,)
17 RawData = (2,)
18 EquationSystem = 3
19
20
21current_mode = Mode.Text
22
23
25 """!
26 currently_in_raw_data_box: Jupyter's markdown export does not preserve raw
27 data cells. But we use them quite frequently. So we follow the convention
28 that raw data cells should be indented. And then we automatically
29 filter those lines out, and add the markup box around them.
30 """
31 global current_mode
32
33 if "\\begin{equation" in line and current_mode == Mode.Text:
34 current_mode = Mode.EquationSystem
35 line = """ @f{eqnarray*}{
36"""
37 elif "\\end{equation" in line:
38 current_mode = Mode.Text
39 line = """ @f}
40 """
41 # elif line.startswith( " " ) and current_mode==Mode.Text:
42 # current_mode = Mode.RawData
43 # line = """
44 #
45 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46 # """ + line
47 # elif not line.startswith( " " ) and current_mode==Mode.RawData:
48 # current_mode = Mode.Text
49 # line = """
50 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51 #
52 # """ + line
53 elif current_mode == Mode.Text:
54 line = line.replace("$$", "\n @f$ ")
55 # This replacement messes up any dollar sign in raw data cells (which I cannot detect atm)
56 # line = line.replace( "$", "@f$ ")
57 return line
58
59
61 """!
62 Takes the markdown produced by nbconvert into something that plays along
63 nicely with our Doxygen.
64
65 filename: File without extension
66 """
67 ouput_file_name = filename + ".dox"
68 output_file = open(ouput_file_name, "w")
69
70 output_file.write("/**\n")
71
72 subpages = []
73
74 have_created_header = False
75 with open(filename + ".md") as file:
76 for line in file:
77 if line.startswith("#") and not have_created_header:
78 line = line.replace("#", "")
79 path = filename.replace("./", "").replace("/", "_").replace(".", "_")
80 output_file.write("@page " + path + " " + line)
81 output_file.write(
82 """
83<div style="color:Black ; background-color: #fcc ; padding: 10px; border: 1px solid green;">
84This documentation is autogenerated from {}.ipynb. Once you clone the Peano repository, please work against this notebook or study
85Peano's @ref page_tutorials_overview "generic description how to generate web documentation from Jupyter notebooks".
86</div>
87 """.format(
88 filename
89 )
90 )
91 have_created_header = True
92 elif "@subpage" in line:
93 new_subpage = line.split("@subpage")[1].strip().split(" ")[0]
94 subpages.append(new_subpage)
95 output_file.write(line.replace("@subpage", "@ref"))
96 elif "\\subpage" in line:
97 new_subpage = line.split("\\subpage")[1].strip().split(" ")[0]
98 subpages.append(new_subpage)
99 output_file.write(line.replace("\\subpage", "@ref"))
100 else:
101 output_file.write(postprocess_markdown_line(line))
102
103 subpages = list(dict.fromkeys(subpages))
104 for subpage in subpages:
105 if subpage.endswith("."):
106 subpage = subpage[0:-1]
107 output_file.write(
108 """
109@subpage {}
110 """.format(
111 subpage
112 )
113 )
114 print("Add subpage {}".format(subpage))
115 output_file.write("*/ \n ")
116
117
118for dirpath, dirnames, filenames in os.walk("."):
119 for filename in [f for f in filenames if f.endswith(".ipynb")]:
120 notebook_filename = os.path.join(dirpath, filename)
121 if ".ipynb" in dirpath:
122 print("Ignore {}".format(notebook_filename))
123 else:
124 print("Convert {} into markdown".format(notebook_filename))
125 subprocess.run(
126 ["jupyter", "nbconvert", "--to", "markdown", notebook_filename]
127 )
128 name_without_extension = notebook_filename.replace(".ipynb", "")
129 print("Postprocess markdown file {}.md".format(name_without_extension))
130 postprocess_notebook_markdown(name_without_extension)
131 print("Delete markdown file")
132 subprocess.run(["rm", name_without_extension + ".md"])
postprocess_markdown_line(line)
currently_in_raw_data_box: Jupyter's markdown export does not preserve raw data cells.
postprocess_notebook_markdown(str filename)
Takes the markdown produced by nbconvert into something that plays along nicely with our Doxygen.