| 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 |
"’": "'", |
| 45 |
"«": "\\\\guillemotleft{}", |
| 46 |
"»": "\\\\guillemotright{}", |
| 47 |
"×": r"\\times{}" |
| 48 |
} |
| 49 |
for code, repl in toReplace.items(): |
| 50 |
t=re.sub(code, repl, t) |
| 51 |
return t |
| 52 |
|
| 53 |
def filterSphinxIncludeGraphics(t, verbose=False): |
| 54 |
if verbose: |
| 55 |
sys.stderr.write("remove empty lines between SphinxIncludeGraphics\n") |
| 56 |
pattern=re.compile(r"\\noindent\\sphinxincludegraphics.*") |
| 57 |
lines=t.split("\n") |
| 58 |
new=[lines[0], lines[1]] # always keep the two first lines |
| 59 |
for i in range(2, len(lines)): |
| 60 |
if pattern.match(new[-2]) and new[-1]=="" and pattern.match(lines[i]): |
| 61 |
new[-1]=lines[i] # this drops the empty line |
| 62 |
else: |
| 63 |
new.append(lines[i]) |
| 64 |
return "\n".join(new) |
| 65 |
|
| 66 |
def filterCurlyBraces(t,verbose=False): |
| 67 |
""" |
| 68 |
remove .png in image names, replace by .pdf |
| 69 |
""" |
| 70 |
if verbose: |
| 71 |
sys.stderr.write("removing extra curly braces around image filenames\n") |
| 72 |
return re.sub(r"(\\noindent\\sphinxincludegraphics.*){{(.*)}\.pdf}", r"\1{\2.pdf}", t) |
| 73 |
|
| 74 |
def filterGreek(t,verbose=False): |
| 75 |
""" |
| 76 |
remove .png in image names, replace by .pdf |
| 77 |
""" |
| 78 |
if verbose: |
| 79 |
sys.stderr.write("replacing UTF-8 greek characters\n") |
| 80 |
t = re.sub(r"π", r"$\\pi$", t) |
| 81 |
t = re.sub(r"θ", r"$\\theta$", t) |
| 82 |
t = re.sub(r"(\S+)Ω", r"$\1\\Omega$", t) |
| 83 |
return re.sub(r"Ω", r"$\\Omega$", t) |
| 84 |
|
| 85 |
def filterSphinx(t,verbose=False): |
| 86 |
""" |
| 87 |
remove .png in image names, replace by .pdf |
| 88 |
""" |
| 89 |
if verbose: |
| 90 |
sys.stderr.write("customizing some directives\n") |
| 91 |
t = re.sub(r"\\usepackage{sphinx}", r"\\usepackage{sphinx}"+"\n"+r"\\usepackage{pdfpages}", t) |
| 92 |
return re.sub(r"\\sphinxmaketitle", r"\\includepdf{coverpage.pdf}"+"\n"+r"\\includepdf{preface.pdf}", t) |
| 93 |
|
| 94 |
def l10n(t, lang): |
| 95 |
""" |
| 96 |
make localization changes in the source text t |
| 97 |
""" |
| 98 |
babel = { |
| 99 |
"en": "english", |
| 100 |
"fr": "french", |
| 101 |
"es": "spanish", |
| 102 |
} |
| 103 |
if lang not in babel: |
| 104 |
return t |
| 105 |
return re.sub(r"\\usepackage({babel})", r"\\usepackage[{name}]\1".format(name=babel[lang]), t) |
| 106 |
|
| 107 |
filters=( |
| 108 |
filterSVG, |
| 109 |
filterPNG, |
| 110 |
filterJPG, |
| 111 |
filterNastyUnicode, |
| 112 |
filterSphinxIncludeGraphics, |
| 113 |
filterCurlyBraces, |
| 114 |
filterGreek, |
| 115 |
filterSphinx, |
| 116 |
) |
| 117 |
|
| 118 |
if __name__=="__main__": |
| 119 |
buildDir=sys.argv[1] |
| 120 |
texFile=sys.argv[2] |
| 121 |
t="" |
| 122 |
with open(buildDir+"/"+texFile, encoding="utf-8") as infile: |
| 123 |
t=infile.read() |
| 124 |
for f in filters: |
| 125 |
t=f(t, verbose=True) |
| 126 |
|
| 127 |
lang = os.path.abspath(buildDir).split("/")[-3] |
| 128 |
t = l10n(t,lang) |
| 129 |
|
| 130 |
with open(buildDir+"/"+texFile+".tmp","w", encoding="utf-8") as outfile: |
| 131 |
outfile.write(t) |
| 132 |
|
| 133 |
os.rename(buildDir+"/"+texFile+".tmp", buildDir+"/"+texFile) |