From 93b706bca64112a29a75cbb8e0f1a3fea80e5e2a Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Fri, 29 Jul 2022 01:52:33 -0700 Subject: [PATCH] deduplicate footnotes --- openpower/texmunge.py | 124 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 116 insertions(+), 8 deletions(-) diff --git a/openpower/texmunge.py b/openpower/texmunge.py index c1d0ecff4..1ea0b12a6 100755 --- a/openpower/texmunge.py +++ b/openpower/texmunge.py @@ -1,14 +1,122 @@ #!/usr/bin/env python3 +from collections import defaultdict import sys +import re + + +def merge_continuation_lines(lines: "list[str]"): + nest_level = 0 + cur = [] + for line in lines: + cur.append(line) + nest_level += len(re.findall(r"(?= 0, "too many closing }" + if nest_level == 0: + yield ''.join(cur) + cur.clear() + assert nest_level == 0, "missing closing }" + + +def merge_footnotes(lines: "list[str]"): + inp_ctr = 0 + footnote_inp_ctr_to_text_map: "dict[int, str]" = {} + + def replace_footnotemark(match): + nonlocal inp_ctr + print(f"input footnote ref #{inp_ctr}") + retval = "\\footnotemark{" + str(inp_ctr) + "}" + inp_ctr += 1 + return retval + + tmpl_lines = [] # template lines + for line in merge_continuation_lines(lines): + parts = line.split(r'\footnotetext') + if len(parts) > 1: + assert len(parts) == 2 and parts[0] == '', \ + "\\footnotetext must only be at the beginning of a line" + nest_level = 0 + footnote_parts = [] + trailing_parts = [] + after_footnote = False + for part in re.split(r'(? 1: + if len(footnote_queue) == 0: + line = parts[1] + else: + line = footnote_queue.pop() + parts[1] + for footnote in footnote_queue: + retval.append(footnote + "\n") + footnote_queue.clear() + line = re.sub(r"\\footnotemark\{([0-9]+)\}", + replace_footnotemark_tmpl, line) + retval.append(line) + return retval + + +with open(sys.argv[1], "r") as f: + lines = list(f.readlines()) with open(sys.argv[2], "w") as o: - with open(sys.argv[1], "r") as f: - for line in f.readlines(): - - if sys.argv[1].endswith("comparison_table_pre.tex") and \ - line.startswith(r"\begin{itemize}"): - o.write(line) - o.write("\\itemsep -0.6em\n") - continue + if sys.argv[1].endswith("comparison_table_pre.tex"): + o.write("\\renewcommand{\\footnotesize}" + "{\\fontsize{6pt}{4pt}\\selectfont}\n") + lines = merge_footnotes(lines) + + for line in lines: + if sys.argv[1].endswith("comparison_table_pre.tex") and \ + line.startswith(r"\begin{itemize}"): o.write(line) + o.write("\\itemsep -0.6em\n") + continue + o.write(line) -- 2.30.2