pbdl-book/fixup-latex.py

163 lines
5.1 KiB
Python
Raw Normal View History

2021-04-26 08:12:34 +02:00
import sys, os, re
# fix jupyter book latex output
#filter_mem = re.compile(r".+\|\s+(\d+)MiB\s+/\s+(\d+)MiB\s+\|")#')
2021-06-29 18:02:09 +02:00
#ft2 = re.compile(r"tst")
#ft3 = re.compile(r"")
#fte = re.compile(r"👋")
2021-04-26 08:12:34 +02:00
2021-06-30 19:35:14 +02:00
# TODO check, remove full "name": "stderr" {} block? (grep stderr *ipynb) ???
2021-04-26 08:12:34 +02:00
# TODO , replace phi symbol w text in phiflow
2021-06-29 18:02:09 +02:00
inf = "book-in.tex"
outf = "book-in2.tex"
print("Start fixup latex, "+inf+" -> "+outf+" \n\n")
2021-04-26 08:12:34 +02:00
2021-06-29 18:02:09 +02:00
reSkip = [] ; reSCnt = []
reSkip.append( re.compile(r"catcode") ) ; reSCnt.append( 1 )
reSkip.append( re.compile(r"sepackage{fontspec}") ) ; reSCnt.append( 1 )
reSkip.append( re.compile(r"defaultfontfeatures") ) ; reSCnt.append( 1 )
reSkip.append( re.compile(r"polyglossia") ) ; reSCnt.append( 1 )
reSkip.append( re.compile(r"setmainlanguage{english}") ) ; reSCnt.append( 1 )
reSkip.append( re.compile(r"addto.captionsenglish") ) ; reSCnt.append( 1 )
reSkip.append( re.compile(r"set....font{Free") ) ; reSCnt.append( 7 )
reSkip.append( re.compile(r"ucharclasses") ) ; reSCnt.append( 1 )
reSkip.append( re.compile(r"unicode-math") ) ; reSCnt.append( 1 )
2021-04-26 08:12:34 +02:00
2021-06-29 18:02:09 +02:00
#reSkip.append( re.compile(r"") )
#reSkip.append( re.compile(r"") )
#reSkip.append( re.compile(r"") )
2021-04-26 08:12:34 +02:00
2021-06-30 19:35:14 +02:00
# ugly, manually fix citations in captions one by one
2021-08-06 20:02:59 +02:00
recs = []; rect = []
recs.append( re.compile(r"example prediction from ....hyperlink.cite.references:id6..WKA.20...." ) )
rect.append( 'example prediction from {[}\\\\protect\\\\hyperlink{cite.references:id6}{WKA+20}{]}' ) # note, quad \ needed!
2021-06-30 19:35:14 +02:00
2021-08-06 20:02:59 +02:00
recs.append( re.compile(r"parametrized GAN {\[}.hyperlink{cite.references:id2}{CTS.21}{\]}" ) )
rect.append( "parametrized GAN {[}\\\\protect\\\\hyperlink{cite.references:id2}{CTS+21}{]}" )
2021-06-30 19:35:14 +02:00
2021-08-06 20:02:59 +02:00
recs.append( re.compile(r"approach using continuous convolutions {\[}.hyperlink{cite.references:id12}{UPTK19}{\]}" ) )
rect.append( "approach using continuous convolutions {[}\\\\protect\\\\hyperlink{cite.references:id12}{UPTK19}{]}" )
2021-06-30 19:35:14 +02:00
2021-08-06 20:02:59 +02:00
# fixup title , cumbersome...
# fix backslashes... saves at least typing a few of them! still needs manual \ -> \\ , could be done better
tt =( 'hrule\n' +
'\\vspace{5cm}\n' +
'\\centering{\n' +
'\\sphinxstylestrong{\\Huge \\textsf{Physics-based Deep Learning}} \\vspace{0.2cm} \n' +
'\\sphinxstylestrong{\\LARGE \\textsf{\\url{http://physicsbaseddeeplearning.org}}} \\vspace{2cm} \n' +
'\\noindent\sphinxincludegraphics[height=220\\sphinxpxdimen]{{teaser}.jpg} \\vspace{2cm} \n' +
'\\centering{ \\textsf{\\large N.\@{} Thuerey, P.\@{} Holl, M.\@{} Mueller, P.\@{} Schnell, F.\@{} Trost, K.\@{} Um}} \n' )
#print(tt);
recBST1 = re.compile(r"\\")
recBST1t = '\\\\\\\\'
tt = recBST1.sub( recBST1t, tt ) # replace all
#print(tt); exit(1)
# insert instead of sphinx version
recs.append( re.compile(r"sphinxmaketitle") )
rect.append( tt )
# remove authors
recs.append( re.compile(r"author{.*}") )
rect.append( 'author{}' )
# center date
recs.append( re.compile(r"date{(.*)}") )
rect.append( r'date{\\centering{\1}}' )
# ---
2021-04-26 08:12:34 +02:00
2021-08-06 20:20:31 +02:00
# only do replacements via recs for book.tex , via applyRecs=True
def parseF(inf,outf,reSkip,reSCnt,applyRecs=False):
2021-06-29 18:02:09 +02:00
print("Fixup, "+inf+" -> "+outf+" ")
with open(outf, 'w') as fout:
with open(inf, 'r') as f:
c = 0
skip = 0
skipTot = 0
for line in iter(f.readline, ''):
# skip lines?
if skip==0:
for r in range(len(reSkip)):
t = reSkip[r].search(str(line))
if t is not None:
#print(format(c)+" skip due to '" + format(t) +"', RE #"+format(r)+" , skip "+format(reSCnt[r]) ) # debug
skip = reSCnt[r]
skipTot += reSCnt[r]
if skip>0:
skip = skip-1
fout.write("% SKIP due to RE #"+format(r)+" , L"+format(reSCnt[r]) +" "+line)
#print("S "+line[:-1]) # debug
else:
2021-08-06 20:21:51 +02:00
if applyRecs:
# fix captions and apply other latex replacements
for i in range(len(recs)):
line = recs[i].sub( rect[i], line ) # replace all
2021-06-30 19:35:14 +02:00
2021-06-29 18:02:09 +02:00
fout.write(line)
#print(line[:-1]) # debug
c = c+1
# line = re.sub('', '\'', str(line))
# line = re.sub('[abz]', '.', str(line))
# t = ft3.search(str(line))
# if t is not None:
# print("H " + format(t) +" "+ format(t.group(0)) )
# t = fte.search(str(line))
# if t is not None:
# print("E " + format(t) + format(t.group(0)) )
print("Fixup -> "+outf+" done, skips: "+format(skipTot) +" \n")
2021-08-06 20:20:31 +02:00
parseF(inf,outf,reSkip,reSCnt,applyRecs=True)
2021-06-29 18:02:09 +02:00
2021-08-06 20:02:59 +02:00
#exit(1); print("debug exit!"); exit(1)
2021-06-30 17:03:07 +02:00
#---
2021-06-29 18:02:09 +02:00
inf = "sphinxmessages-in.sty"
outf = "sphinxmessages.sty"
reSkip = [] ; reSCnt = []
reSkip.append( re.compile(r"addto.captionsenglish") ) ; reSCnt.append( 1 )
parseF(inf,outf,reSkip,reSCnt)
2021-04-26 08:12:34 +02:00
2021-06-30 17:03:07 +02:00
#---
inf = "book-in.aux"
outf = "book.aux"
2021-07-14 18:16:27 +02:00
# remove selectlang eng statements from book aux
2021-06-30 17:03:07 +02:00
reSkip = [] ; reSCnt = []
reSkip.append( re.compile(r"selectlanguage...english") ) ; reSCnt.append( 1 )
parseF(inf,outf,reSkip,reSCnt)
#---
2021-07-14 18:16:27 +02:00
# same, selectlanguage for toc
2021-06-30 17:03:07 +02:00
inf = "book-in.toc"
outf = "book.toc"
parseF(inf,outf,reSkip,reSCnt)
2021-07-14 18:16:27 +02:00
#---
inf = "sphinxmanual-in.cls"
outf = "sphinxmanual.cls"
# remove openright option from style
reSkip = [] ; reSCnt = []
reSkip.append( re.compile(r"PassOptionsToClass.openright...sphinxdocclass") ) ; reSCnt.append( 1 )
parseF(inf,outf,reSkip,reSCnt)