#! /usr/bin/env python3 """Given a file produced by Hevea that contains "??" in place of line references, replace those by actual line numbers, read from an aux file. Invoke like: hevea-add-verbatim-linenos mydoc.aux mydoc.html > mydoc-fixed.html """ # This is a bit of a hack. The problem is that Hevea doesn't support the # fancyvrb LaTeX package. import re import sys auxfile = sys.argv[1] htmlfile = sys.argv[2] # map from label to line label_line = {} label_re = re.compile("\\\\newlabel{([^}]*)}{{([0-9]*)}{([0-9]*)}}") lineref_re = re.compile('()(\\?\\?)()') with open(auxfile) as auxfile_handle: for line in auxfile_handle: m = label_re.match(line) if m: label_line[m.group(1)] = m.group(2) # print label_line def replace_line(match): """argument match is a match for lineref_re""" label = match.group(2) return match.group(1) + label_line[label] + match.group(4) with open(htmlfile) as htmlfile_handle: for line in htmlfile_handle: line = lineref_re.sub(replace_line, line) print(line)