| 1 |
#!/usr/bin/python3 |
| 2 |
import os, sys, re |
| 3 |
|
| 4 |
def filterSVG(t,verbose=False): |
| 5 |
""" |
| 6 |
remove .svg in image names, replace by .pdf |
| 7 |
""" |
| 8 |
if verbose: |
| 9 |
sys.stderr.write("replacing .svg extensions by .pdf for included graphics\n") |
| 10 |
return re.sub(r"\.svg", ".pdf", t) |
| 11 |
|
| 12 |
def filterPNG(t,verbose=False): |
| 13 |
""" |
| 14 |
remove .png in image names, replace by .pdf |
| 15 |
""" |
| 16 |
if verbose: |
| 17 |
sys.stderr.write("replacing .png extensions by .pdf for included graphics\n") |
| 18 |
return re.sub(r"\.png", ".pdf", t) |
| 19 |
|
| 20 |
def filterJPG(t,verbose=False): |
| 21 |
""" |
| 22 |
remove .jpg in image names, replace by .pdf |
| 23 |
""" |
| 24 |
if verbose: |
| 25 |
sys.stderr.write("replacing .jpg extensions by .pdf for included graphics\n") |
| 26 |
return re.sub(r"\.jpg", ".pdf", t) |
| 27 |
|
| 28 |
def filterNastyUnicode(t,verbose=False): |
| 29 |
""" |
| 30 |
remove problematic Unicode characters, like dots, thin spaces, |
| 31 |
special minus sign |
| 32 |
""" |
| 33 |
if verbose: |
| 34 |
sys.stderr.write("removing nasty unicode chars\n") |
| 35 |
toReplace={ |
| 36 |
"\u2005": " ", |
| 37 |
"\u2003": " ", |
| 38 |
"\u200a": " ", |
| 39 |
"\u22ef": r"\\dots", |
| 40 |
"\u2212": "-", |
| 41 |
"↑": "", |
| 42 |
"↓": "", |
| 43 |
} |
| 44 |
for code, repl in toReplace.items(): |
| 45 |
t=re.sub(code, repl, t) |
| 46 |
return t |
| 47 |
|
| 48 |
def filterSphinxIncludeGraphics(t, verbose=False): |
| 49 |
if verbose: |
| 50 |
sys.stderr.write("remove empty lines between SphinxIncludeGraphics\n") |
| 51 |
pattern=re.compile(r"\\noindent\\sphinxincludegraphics.*") |
| 52 |
lines=t.split("\n") |
| 53 |
new=[lines[0], lines[1]] # always keep the two first lines |
| 54 |
for i in range(2, len(lines)): |
| 55 |
if pattern.match(new[-2]) and new[-1]=="" and pattern.match(lines[i]): |
| 56 |
new[-1]=lines[i] # this drops the empty line |
| 57 |
else: |
| 58 |
new.append(lines[i]) |
| 59 |
return "\n".join(new) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
filters=( |
| 64 |
filterSVG, |
| 65 |
filterPNG, |
| 66 |
filterJPG, |
| 67 |
filterNastyUnicode, |
| 68 |
filterSphinxIncludeGraphics |
| 69 |
) |
| 70 |
|
| 71 |
if __name__=="__main__": |
| 72 |
buildDir=sys.argv[1] |
| 73 |
texFile=sys.argv[2] |
| 74 |
t="" |
| 75 |
with open(buildDir+"/"+texFile, encoding="utf-8") as infile: |
| 76 |
t=infile.read() |
| 77 |
for f in filters: |
| 78 |
t=f(t, verbose=True) |
| 79 |
|
| 80 |
with open(buildDir+"/"+texFile+".tmp","w", encoding="utf-8") as outfile: |
| 81 |
outfile.write(t) |
| 82 |
|
| 83 |
os.rename(buildDir+"/"+texFile+".tmp", buildDir+"/"+texFile) |