diff --git a/README.md b/README.md index 4a366a0..20d5744 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,20 @@ _See supra note_ {ref-a}. If `ref-a` is ultimately note 7, the rendered text reads `See supra note 7.` The compiler determines numbers from real `[^key]` citations before rendering, so `{key}` does not create a footnote or change numbering and can safely appear before or after the citation. An unknown `{key}` is left visible rather than being assigned a number. The previous double-braced syntax (`{{key}}`) is no longer interpolated. References and citations inside fenced code blocks or inline code spans are shown literally and never influence footnote numbering. +### Repeated footnote citations + +Under **Sections & TOC**, choose **Repeated footnote references**, or use +`--repeated-footnotes reuse|full|supra` on the CLI. The default (`reuse`) +preserves the original numbering. The `full` mode assigns each occurrence +a new number with the full definition. The `supra` mode assigns each +occurrence a new number, uses _Id._ for consecutive citations to the same +defined key, and _See supra_ note {key}. for nonconsecutive repeats, with +the final note number linked to the first full citation. The word “note” +and the number are not italicized. Numbering depends on citation order, +not page or paragraph placement. The source Markdown, code spans, fences, +and quotation-local citations are unchanged. This option applies to PDF +output, including endnotes; Word exports retain their existing behavior. + ## Optional PDF/A-4 output Standard PDF remains the default. Choose **PDF/A-4 (ISO 19005-4:2020)** diff --git a/app.py b/app.py index 7fde2cb..4d6088b 100644 --- a/app.py +++ b/app.py @@ -469,6 +469,7 @@ def metadata_value(name: str) -> str | None: smart_quotes=truthy("smart_quotes"), quote_inline_number_footnotes=truthy("quote_inline_number_footnotes"), quote_source_before_footnotes=truthy("quote_source_before_footnotes"), + repeated_footnotes=request.form.get("repeated_footnotes", "reuse"), emoji_style=emoji_style, emoji_pack=emoji_pack, underline_links=truthy("underline_links"), diff --git a/pdf_compiler.py b/pdf_compiler.py index 7ddcb11..97d2336 100644 --- a/pdf_compiler.py +++ b/pdf_compiler.py @@ -261,7 +261,7 @@ def transform(segment: str) -> str: -def protect_inline_code(text: str) -> tuple[str, dict[str, str]]: +def protect_inline_code(text: str, *, preserve_source: bool = False) -> tuple[str, dict[str, str]]: """Protect backtick code spans and escaped backticks from Markdown parsing. Code spans use matching backtick-run lengths. Newlines become spaces; @@ -290,7 +290,7 @@ def token(markup: str) -> str: slash_count += 1 if slash_count % 2: result.append(text[cursor:index - 1]) - result.append(token("`")) + result.append(token(text[index - 1:index + 1] if preserve_source else "`")) index += 1 cursor = index continue @@ -322,7 +322,10 @@ def token(markup: str) -> str: if content.startswith(" ") and content.endswith(" ") and content.strip(" "): content = content[1:-1] result.append(text[cursor:index]) - result.append(token(f'{html.escape(content, quote=False)}')) + result.append(token( + text[index:closing_end] if preserve_source + else f'{html.escape(content, quote=False)}' + )) index = closing_end cursor = index @@ -446,6 +449,7 @@ def __init__( smart_quotes: bool = False, quote_inline_number_footnotes: bool = False, quote_source_before_footnotes: bool = False, + repeated_footnotes: str = "reuse", emoji_style: str = "twemoji", emoji_pack: Path | None = None, underline_links: bool = True, @@ -461,6 +465,9 @@ def __init__( self.smart_quotes = smart_quotes self.quote_inline_number_footnotes = quote_inline_number_footnotes self.quote_source_before_footnotes = quote_source_before_footnotes + if repeated_footnotes not in {"reuse", "full", "supra"}: + raise ValueError("Unknown repeated footnote mode.") + self.repeated_footnotes = repeated_footnotes validate_emoji_style(emoji_style, emoji_pack) self.emoji_style = emoji_style self.emoji_pack = emoji_pack @@ -486,6 +493,8 @@ def __init__( self.body_text, self.note_defs = extract_footnotes(body) self.body_text = normalize_page_breaks(self.body_text) self._prepare_signature_directives() + if self.repeated_footnotes != "reuse": + self.expand_repeated_footnotes() self.prime_note_numbers() self.styles = self._styles() @@ -999,6 +1008,75 @@ def note_number(self, key: str) -> int: def note_destination(number: int) -> str: return f"footnote-{number}" + def expand_repeated_footnotes(self) -> None: + """Give repeated document citations unique keys without changing the source file.""" + prefix = "pdfcompiler-repeat-" + reserved = self.body_text + "\n" + "\n".join(self.note_defs) + "\n" + "\n".join(self.note_defs.values()) + while prefix in reserved: + prefix += "x" + seen: set[str] = set() + previous: str | None = None + serial = 0 + + def expand(text: str, local_notes: dict[str, str]) -> str: + nonlocal previous, serial + + def replace(match: re.Match[str]) -> str: + nonlocal previous, serial + key = match.group(1) + if key in local_notes: + return match.group(0) + consecutive = key == previous + previous = key + # Do not turn an unresolved reference into an apparently valid citation. + if key not in self.note_defs or key not in seen: + seen.add(key) + return match.group(0) + serial += 1 + alias = f"{prefix}{serial}" + if self.repeated_footnotes == "full": + self.note_defs[alias] = self.note_defs[key] + elif consecutive: + self.note_defs[alias] = "_Id._" + else: + self.note_defs[alias] = f"_See supra_ note {{{key}}}." + return f"[^{alias}]" + + return self.note_ref_re.sub(replace, text) + + # Fence boundaries must be identified before shielding inline code. Preserve + # original code spans rather than replacing them with rendered HTML. + lines = self.body_text.splitlines() + index = 0 + fence_marker: str | None = None + while index < len(lines): + fence_marker, boundary = advance_code_fence(lines[index], fence_marker) + if boundary or fence_marker is not None: + index += 1 + continue + start = index + if lines[index].startswith(">"): + while index < len(lines) and lines[index].startswith(">"): + index += 1 + quote_lines = [line.lstrip(">").strip() for line in lines[start:index]] + local_notes = self.quotation_local_notes(quote_lines) if ( + quote_lines and re.fullmatch(r"\[!QUOTE\]\s*", quote_lines[0], re.IGNORECASE) + ) else {} + else: + while index < len(lines) and not lines[index].startswith(">"): + _, boundary = advance_code_fence(lines[index], None) + if boundary: + break + index += 1 + local_notes = {} + chunk = "\n".join(lines[start:index]) + shielded, spans = protect_inline_code(chunk, preserve_source=True) + expanded = expand(shielded, local_notes) + for token, original in spans.items(): + expanded = expanded.replace(token, original) + lines[start:index] = expanded.split("\n") + self.body_text = "\n".join(lines) + def prime_note_numbers(self) -> None: """Assign final note numbers from real footnote citations before rendering.""" shielded, _ = protect_inline_code(self.body_text) @@ -1560,6 +1638,10 @@ def main() -> int: action="store_true", help="Place [!SOURCE] attribution before quote-local footnotes (off by default)", ) + parser.add_argument( + "--repeated-footnotes", choices=("reuse", "full", "supra"), default="reuse", + help="Reuse note numbers, or generate full or Id./supra notes for repeated citations", + ) args = parser.parse_args() output = args.output or args.source.with_suffix(".pdf") @@ -1587,6 +1669,7 @@ def main() -> int: smart_quotes=args.smart_quotes, quote_inline_number_footnotes=args.quote_inline_number_footnotes, quote_source_before_footnotes=args.quote_source_before_footnotes, + repeated_footnotes=args.repeated_footnotes, emoji_style=args.emoji_style, emoji_pack=args.emoji_pack, **presentation, diff --git a/templates/index.html b/templates/index.html index ba9f728..fd95ea9 100644 --- a/templates/index.html +++ b/templates/index.html @@ -470,6 +470,14 @@