Skip to content

Commit ce878e5

Browse files
authored
Merge pull request #38 from mwtoews/ruff-format
Use ruff format
2 parents 5bde5f4 + 22a93e9 commit ce878e5

25 files changed

+1287
-830
lines changed

docs/pestutilslib/docx_to_md.py

Lines changed: 84 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import sys
22
import os
33

4+
45
def processFile(inFile, outFile):
5-
mdFile = open(inFile, 'r')
6+
mdFile = open(inFile, "r")
67
toc = []
7-
levels = [0,0,0,0,0 ]
8-
newFile = open(outFile, 'w')
8+
levels = [0, 0, 0, 0, 0]
9+
newFile = open(outFile, "w")
910
tempFile = []
1011
tocLoc = 0
1112
partOfToc = False
12-
13+
1314
for line in mdFile:
14-
if partOfToc and line != '\n':
15+
if partOfToc and line != "\n":
1516
continue
1617
else:
1718
partOfToc = False
18-
if 'Table of Contents' in line:
19+
if "Table of Contents" in line:
1920
tocLoc = len(tempFile) + 1
2021
partOfToc = True
2122
line += "\n"
22-
elif line[0] == '#':
23+
elif line[0] == "#":
2324
secId = buildToc(line, toc, levels)
24-
line = addSectionTag(cleanLine(line), secId) + '\n'
25+
line = addSectionTag(cleanLine(line), secId) + "\n"
2526
tempFile.append(line)
2627

27-
2828
for line in toc:
2929
tempFile.insert(tocLoc, line)
3030
tocLoc += 1
@@ -36,58 +36,75 @@ def processFile(inFile, outFile):
3636
mdFile.close()
3737
newFile.close()
3838

39+
3940
def addSectionTag(line, secId):
40-
startIndex = line.find(' ')
41-
line = line[:startIndex + 1] + '<a id=\'' + secId + '\' />' + line[startIndex + 1:]
41+
startIndex = line.find(" ")
42+
line = line[: startIndex + 1] + "<a id='" + secId + "' />" + line[startIndex + 1 :]
4243
return line
4344

45+
4446
def buildToc(line, toc, levels):
4547
line = cleanLine(line)
46-
secId = 's'
47-
if line[:4] == '####':
48-
49-
#raise UserWarning('Header levels greater than 3 not supported')
48+
secId = "s"
49+
if line[:4] == "####":
50+
# raise UserWarning('Header levels greater than 3 not supported')
5051
levels[4] += 1
51-
secId += str(levels[1]) + '-' + str(levels[2]) + '-' + str(levels[3]) + '-' + str(levels[4])
52-
toc.append(' - [' + line[5:] + '](#' + secId + ')\n')
53-
elif line[:3] == '###':
52+
secId += (
53+
str(levels[1])
54+
+ "-"
55+
+ str(levels[2])
56+
+ "-"
57+
+ str(levels[3])
58+
+ "-"
59+
+ str(levels[4])
60+
)
61+
toc.append(" - [" + line[5:] + "](#" + secId + ")\n")
62+
elif line[:3] == "###":
5463
levels[3] += 1
55-
secId += str(levels[1]) + '-' + str(levels[2]) + '-' + str(levels[3])
56-
toc.append(' - [' + line[4:] + '](#' + secId + ')\n')
57-
elif line[:2] == '##':
64+
secId += str(levels[1]) + "-" + str(levels[2]) + "-" + str(levels[3])
65+
toc.append(" - [" + line[4:] + "](#" + secId + ")\n")
66+
elif line[:2] == "##":
5867
levels[2] += 1
5968
levels[3] = 0
60-
secId += str(levels[1]) + '-' + str(levels[2])
61-
toc.append(' - [' + line[3:] + '](#' + secId + ')\n')
62-
elif line[:1] == '#':
69+
secId += str(levels[1]) + "-" + str(levels[2])
70+
toc.append(" - [" + line[3:] + "](#" + secId + ")\n")
71+
elif line[:1] == "#":
6372
levels[1] += 1
6473
levels[3] = levels[2] = 0
6574
secId += str(levels[1])
66-
toc.append('- [' + line[2:] + '](#' + secId + ')\n')
75+
toc.append("- [" + line[2:] + "](#" + secId + ")\n")
6776
return secId
6877

78+
6979
def cleanLine(text):
7080
text = stripNewline(text)
7181
text = removeAnchors(text)
7282
return text
7383

84+
7485
def stripNewline(text):
75-
return text.replace('\n', '')
86+
return text.replace("\n", "")
87+
7688

7789
def removeAnchors(text):
78-
while ('<' in text and '>' in text):
79-
leftTag = text.index('<')
80-
rightTag = text.index('>')
81-
text = text[0:leftTag] + text[rightTag + 1:]
90+
while "<" in text and ">" in text:
91+
leftTag = text.index("<")
92+
rightTag = text.index(">")
93+
text = text[0:leftTag] + text[rightTag + 1 :]
8294
return text
8395

84-
def clean(docx_file,inFile,outFile,run_pandoc=True):
96+
97+
def clean(docx_file, inFile, outFile, run_pandoc=True):
8598
if run_pandoc:
86-
os.system("pandoc -t gfm --wrap=none --extract-media . -o file.md {0} --mathjax".format(docx_file))
87-
num_str = [str(i) for i in range(1,11)]
88-
lines = open(inFile,'r').readlines()
99+
os.system(
100+
"pandoc -t gfm --wrap=none --extract-media . -o file.md {0} --mathjax".format(
101+
docx_file
102+
)
103+
)
104+
num_str = [str(i) for i in range(1, 11)]
105+
lines = open(inFile, "r").readlines()
89106

90-
#notoc_lines = []
107+
# notoc_lines = []
91108
i = 0
92109
# while i < len(lines):
93110
# line = lines[i]
@@ -104,31 +121,40 @@ def clean(docx_file,inFile,outFile,run_pandoc=True):
104121
# notoc_lines = []
105122
i = 0
106123
while i < len(lines):
107-
#if lines[i].strip().startswith("----"):
124+
# if lines[i].strip().startswith("----"):
108125
# lines[i-1] = "## " + lines[i-1]
109126
# lines[i] = "\n"
110127
if "<img" in lines[i]:
111-
lines[i] = lines[i].replace("#","")
128+
lines[i] = lines[i].replace("#", "")
112129
if "<p>" in lines[i]:
113-
lines[i] = lines[i].replace("<p>","").replace("</p>","<br>").replace("#","~")
130+
lines[i] = (
131+
lines[i].replace("<p>", "").replace("</p>", "<br>").replace("#", "~")
132+
)
114133
if "blockquote" in lines[i]:
115-
lines[i] = lines[i].replace("<blockquote>","").replace("</blockquote>","")
134+
lines[i] = lines[i].replace("<blockquote>", "").replace("</blockquote>", "")
116135
if lines[i].strip().startswith("$") and not "bmatrix" in lines[i].lower():
117136
label = lines[i].split()[-1]
118-
eq_str = lines[i].replace("$$","$").split('$')[1]
119-
eq_str = r"{0}".format(eq_str).replace("\\\\","\\").replace(" \\ "," ").replace("\\_","_")
120-
math_str_pre = "<img src=\"https://latex.codecogs.com/svg.latex?\Large&space;{0}".format(eq_str)
121-
math_str_post = "\" title=\"\Large {0}\" /> {1} <br>".format(eq_str,label)
137+
eq_str = lines[i].replace("$$", "$").split("$")[1]
138+
eq_str = (
139+
r"{0}".format(eq_str)
140+
.replace("\\\\", "\\")
141+
.replace(" \\ ", " ")
142+
.replace("\\_", "_")
143+
)
144+
math_str_pre = '<img src="https://latex.codecogs.com/svg.latex?\Large&space;{0}'.format(
145+
eq_str
146+
)
147+
math_str_post = '" title="\Large {0}" /> {1} <br>'.format(eq_str, label)
122148
lines[i] = math_str_pre + " " + math_str_post
123-
if lines[i].strip().startswith("<table>"): # and "pcf" in lines[i].lower():
124-
lines[i] = "<div style=\"text-align: left\">" + lines[i] + "</div>"
149+
if lines[i].strip().startswith("<table>"): # and "pcf" in lines[i].lower():
150+
lines[i] = '<div style="text-align: left">' + lines[i] + "</div>"
125151
if "comment" in lines[i].lower():
126-
lines[i] = lines[i].replace("~","#")
152+
lines[i] = lines[i].replace("~", "#")
127153
elif lines[i].strip().startswith("[") and "]" in lines[i]:
128-
raw = lines[i].strip().split(']')
129-
rraw = " ".join(raw[0].split()[:-1])+"]"
154+
raw = lines[i].strip().split("]")
155+
rraw = " ".join(raw[0].split()[:-1]) + "]"
130156
new_line = rraw + "".join(raw[1:]) + "\n"
131-
print(lines[i],new_line)
157+
print(lines[i], new_line)
132158
lines[i] = new_line
133159
# elif "bmatrix" in lines[i].lower():
134160
# eq_str = lines[i]
@@ -146,20 +172,18 @@ def clean(docx_file,inFile,outFile,run_pandoc=True):
146172

147173
i += 1
148174

149-
#lines[0] = "# Table of Contents\n"
150-
with open(outFile,'w') as f:
151-
175+
# lines[0] = "# Table of Contents\n"
176+
with open(outFile, "w") as f:
152177
for line in lines:
153-
#if line.lower().strip().startswith("[introduction"):
178+
# if line.lower().strip().startswith("[introduction"):
154179
# f.write("\n# Table of Contents\n\n")
155180
f.write(line)
156181

157182

158183
if __name__ == "__main__":
159-
160-
clean("new_function_documentation.docx","file.md","fortran_library_documentation.md",True)
161-
162-
163-
164-
165-
184+
clean(
185+
"new_function_documentation.docx",
186+
"file.md",
187+
"fortran_library_documentation.md",
188+
True,
189+
)

examples/clear_output_all.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import os
2+
23
dirs = ["."]
34
notebook_count = 0
45
for d in dirs:
5-
nb_files = [os.path.join(d,f) for f in os.listdir(d) if f.lower().endswith(".ipynb")]
6+
nb_files = [
7+
os.path.join(d, f) for f in os.listdir(d) if f.lower().endswith(".ipynb")
8+
]
69
for nb_file in nb_files:
7-
print("clearing",nb_file)
8-
os.system("jupyter nbconvert --ClearOutputPreprocessor.enabled=True --ClearMetadataPreprocessor.enabled=True --inplace {0}".format(nb_file))
10+
print("clearing", nb_file)
11+
os.system(
12+
"jupyter nbconvert --ClearOutputPreprocessor.enabled=True --ClearMetadataPreprocessor.enabled=True --inplace {0}".format(
13+
nb_file
14+
)
15+
)
916
notebook_count += 1
10-
print(notebook_count," notebooks cleared")
17+
print(notebook_count, " notebooks cleared")

0 commit comments

Comments
 (0)