You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

69 lines
2.2 KiB

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 18 10:40:57 2022
@author: solid
"""
import re
import os
content = open('python-core.tex', 'r').read()
#split the file by \subsection, \section, \subsubsection tokens
splitted = content.split('\section')
splitted = ['\section\n' + s for s in splitted ] ## add splitter again
splitted2 = [s for s in splitted if '{python}' in s ]
for i, s in enumerate(splitted2):
## find and record section name
lines = s.split('\n')
header = lines[1].replace('{','')
header = header[: header.index('}')]
print("HEADER=== " + header)
## find and record section name
directoryPath = "examples/" + str(i).zfill(2) + "-" + header
os.makedirs(directoryPath,exist_ok = True)
for j, k in enumerate( s.split('\subsection') ):
try:
subHeader = k.split('\n')[0]
subHeader = subHeader.replace('{','')
subHeader = subHeader[ : subHeader.index("}")]
print(subHeader)
except:
subHeader = "intro"
z = '\section\n' + k
z = z.replace('\section','###################\n###################\n###################')
z = z.replace('\subsubsection','###################')
# z = k.replace('\subsection','###\n####')
# print(z)
##### annotate python block intervals
inPyBlock = [0 for f in range(len(z.split('\n')) ) ]
for l,line in enumerate(z.split('\n')):
if '\\begin{python}' in line:
inPyBlock[l:] = [1 for xxx in inPyBlock[l:]]
if '\\end{python}' in line:
inPyBlock[l+1:] = [0 for xxx in inPyBlock[l+1:]]
procLines = []
for l,line in enumerate(z.split('\n')):
if inPyBlock[l]:
lineProc = line
else:
lineProc = '#' + line
lineProc = lineProc.replace('\\begin{python}','')
lineProc = lineProc.replace('\\end{python}','')
procLines.append(lineProc)
zz = '\n'.join(procLines)
with open('%s/%s-%s.py' % (directoryPath,str(j).zfill(2),subHeader) , 'w+') as fp:
fp.write(zz)