tdoxybib.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch) (HTM) git clone git://src.adamsgaard.dk/pism (DIR) Log (DIR) Files (DIR) Refs (DIR) LICENSE --- tdoxybib.py (2593B) --- 1 #!/usr/bin/env python3 2 import re 3 from os import popen, system 4 5 input = "ice-bib" 6 bbl = "texput.bbl" 7 output = "references.md" 8 9 header = """References {#references} 10 ========== 11 12 @par Notes 13 This large list collects all references which the PISM authors have found 14 convenient. There is no claim that all of these references get direct use, 15 or even mention, in the PISM project files.<br><br><hr> 16 """ 17 18 # dummy LaTeX document that \cites everything and uses a special BibTeX style file: 19 latexdummy = """\\documentclass{article} 20 \\begin{document} 21 \\cite{*}\\bibliography{%s}\\bibliographystyle{doxybib} 22 \\end{document} 23 """ % input 24 25 # Remove an old .bbl so that LaTeX does not choke on it: 26 system("rm -f %s" % bbl) 27 28 # Run LaTeX: 29 f = popen("latex", 'w') 30 f.write(latexdummy) 31 f.close() 32 33 # Run BibTeX: 34 system("bibtex texput") 35 36 # Read all the lines from a .bbl generated by BibTeX 37 f = open(bbl) 38 lines = f.readlines() 39 f.close() 40 body = "".join(lines[:]) 41 42 # NB! The order of substitutions is important. 43 subs = [(r"%\n", r""), # lines wrapped by BibTeX 44 (r"\\href{([^}]*)}{([^}]*)}", r'[\2](\1)'), # hyperref href command 45 (r"\\url{([^}]*)}", r'[\1](\1)'), # hyperref url command 46 (r"\\\w*{([^}]*)}", r" \1 "), # ignore other LaTeX commands 47 (r"[}{]", r""), # curly braces 48 (r"\$\\sim\$", r"~"), # LaTeX \sim used to represent ~ 49 (r"---", r"—"), # em-dash 50 (r"--", r"–"), # en-dash 51 (r"([^/])~", r"\1 "), # tildes that are not in URLs 52 (r'\\"([a-zA-Z])', r"&\1uml;"), # umlaut 53 (r"\\'([a-zA-Z])", r"&\1grave;"), # grave 54 (r'\\`([a-zA-Z])', r"&\1acute;"), # acute 55 (r'\\^([a-zA-Z])', r"&\1circ;"), # circumflex 56 (r'``', r'"'), # opening quotes 57 (r"''", r'"'), # closing quotes 58 (r"\\,", r""), # \, LaTeX math spacing command 59 (r"\\ae", r"æ"), # ae ligature 60 (r"\\tt", r"\\c"), # \tt (in the 'siple' entry) 61 ] 62 63 for (regex, substitution) in subs: 64 body = re.compile(regex).sub(substitution, body) 65 66 f = open(output, 'w') 67 f.write(header) 68 f.write(body) 69 f.close()