#! /usr/bin/env python3 """In Hevea output, replace "??" 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 from pathlib import Path auxfile = sys.argv[1] htmlfile = sys.argv[2] # map from label to line label_line = {} label_re = re.compile(r"\\newlabel{([^}]*)}{{([0-9]*)}{([0-9]*)}}") lineref_re = re.compile(r'()(\?\?)()') with Path(auxfile).open() 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: re.Match[str]) -> str: """Replace a line. Args: match: a match for lineref_re. Returns: The match with the replacement done. """ label = match.group(2) return match.group(1) + label_line[label] + match.group(4) with Path(htmlfile).open() as htmlfile_handle: for line in htmlfile_handle: line = lineref_re.sub(replace_line, line) print(line)