Skip to content

Instantly share code, notes, and snippets.

@fmannhardt
Forked from breuderink/siks2tex.py
Created July 27, 2017 22:12
Show Gist options
  • Save fmannhardt/8fb552ea5a5b635d690960024b8aba07 to your computer and use it in GitHub Desktop.
Save fmannhardt/8fb552ea5a5b635d690960024b8aba07 to your computer and use it in GitHub Desktop.
Converter for SIKS dissertation list to LaTeX
#!/usr/bin/env python
'''
Quick and dirty hack to get the SIKS dissertation list in LaTeX.
Original code taken from https://gist.github.com/breuderink/1253855
'''
import argparse, re, operator, itertools, sys
def tex_escape(text):
"""
:param text: a plain text message
:return: the message escaped to appear correctly in LaTeX
"""
conv = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}',
'~': r'\textasciitilde{}',
'^': r'\^{}',
'\\': r'\textbackslash{}',
'<': r'\textless ',
'>': r'\textgreater ',
}
regex = re.compile('|'.join(re.escape(unicode(key)) for key in sorted(conv.keys(), key = lambda item: - len(item))))
return regex.sub(lambda match: conv[match.group()], text)
parser = argparse.ArgumentParser(
description='Convert SIKS dissertation list to LaTeX.')
parser.add_argument('txt')
parser.add_argument('tex')
parser.add_argument('-n', '--output-lines', type=int, default=10000)
parser.add_argument('-y', '--min-year', type=int, default=2011)
args = parser.parse_args()
dissertations = []
with open(args.txt, 'r') as f:
for line in f:
match = re.search(r'\d{4}-\d+', line)
if match:
titleMatch = re.search(r'(\d{4}-\d+)\s+(\S.+)', line)
if titleMatch:
diss_id = match.group(0)
author = titleMatch.group(2)
else:
diss_id = match.group(0)
author = f.next().strip()
title = f.next().strip()
dissertations.append((diss_id, author, tex_escape(title)))
sorted_diss = sorted(dissertations, key=operator.itemgetter(0), reverse=False)
with open(args.tex, 'w') as f:
diss_cmd = r'\newcommand{\SIKSdiss}[3]{{\textbf{#1}}\hspace*{1ex}#2, {\textit{#3}.}\\}'
f.write('%s\n\n' % diss_cmd)
year_cmd = r'\newcommand{\SIKSyear}[1]{{\textbf{#1}}\\}'
f.write('%s\n\n' % year_cmd)
oldYear = 0
print args.min_year
for diss in sorted_diss[:args.output_lines]:
match = re.search(r'(\d{4})-(\d+)', diss[0])
year = int(match.group(1))
if year >= args.min_year:
if (year > oldYear):
oldYear = year
f.write('\\SIKSyear{%s}\n' % (year))
number = match.group(2)
f.write('\\SIKSdiss{%s}{%s}{%s}\n' % (number, diss[1], diss[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment