From b54bb471cf929c91d19413d14ddcaa2d24b2797b Mon Sep 17 00:00:00 2001 From: John Wooten Date: Sat, 26 Sep 2026 17:52:57 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=96=8A=EF=B8=8F=20Fix=20quote-local?= =?UTF-8?q?=20footnotes=20rendering=20in=20README=20and=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- pdf_compiler.py | 37 +++++++++++++++++++++---------------- tests/test_quotations.py | 13 +++++++------ 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 8994359..c090d2c 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,6 @@ To preserve footnotes from the quoted source, define them inside the quotation. > [!SOURCE] Author, *Work* ``` -Quote-local footnotes render inside the quotation after its text and before the right-aligned source attribution. A footnote whose definition is outside the quotation remains an ordinary document footnote. +Quote-local footnotes render inside the quotation beneath the right-aligned source attribution. A footnote whose definition is outside the quotation remains an ordinary document footnote. The rendering engine, presentation settings, and validation layer are kept separate so document generation does not alter source content. diff --git a/pdf_compiler.py b/pdf_compiler.py index 84b9307..8306b16 100644 --- a/pdf_compiler.py +++ b/pdf_compiler.py @@ -631,11 +631,31 @@ def quotation_flowables(self, quote_lines: list[str]) -> list[RefParagraph]: ) flowables.append(RefParagraph(body_text, body_style, body_refs)) + if source: + rendered_source, source_refs = self.quotation_inline(source, local_notes) + source_style = self.styles["QuotationSourceX"] + has_preceding = bool(flowables) + has_following = bool(local_note_items) + if has_preceding or has_following: + source_style = ParagraphStyle( + "QuotationSourceJoinedX", + parent=source_style, + quoteRoundTop=not has_preceding, + quoteRoundBottom=not has_following, + quotePaddingTop=0 if has_preceding else 6, + quotePaddingBottom=0 if has_following else 6, + spaceBefore=0, + spaceAfter=0, + ) + flowables.append( + RefParagraph(f"— {rendered_source}", source_style, source_refs) + ) + for index, (key, note_text) in enumerate(local_note_items): rendered_note, note_refs = self.quotation_inline(note_text, local_notes) note_style = self.styles["QuotationFootnoteX"] has_preceding = bool(flowables) - has_following = index < len(local_note_items) - 1 or bool(source) + has_following = index < len(local_note_items) - 1 if has_preceding or has_following: note_style = ParagraphStyle( f"QuotationFootnoteJoinedX{index}", @@ -655,21 +675,6 @@ def quotation_flowables(self, quote_lines: list[str]) -> list[RefParagraph]: ) ) - if source: - rendered_source, source_refs = self.quotation_inline(source, local_notes) - source_style = self.styles["QuotationSourceX"] - if flowables: - source_style = ParagraphStyle( - "QuotationSourceJoinedX", - parent=source_style, - quoteRoundTop=False, - quotePaddingTop=0, - spaceBefore=0, - ) - flowables.append( - RefParagraph(f"— {rendered_source}", source_style, source_refs) - ) - if not flowables: flowables.append(RefParagraph(" ", self.styles["QuotationBodyX"])) return flowables diff --git a/tests/test_quotations.py b/tests/test_quotations.py index ae24ff7..f631d95 100644 --- a/tests/test_quotations.py +++ b/tests/test_quotations.py @@ -92,7 +92,7 @@ def test_source_footnotes_keep_original_discontinuous_numbers(self): self.assertEqual(renderer.order, ["main"]) self.assertEqual(len(flowables), 4) - body, note_four, note_seventeen, source = flowables + body, source, note_four, note_seventeen = flowables self.assertEqual(body.getPlainText(), "First passage.4 Later passage.17") self.assertEqual(note_four.getPlainText(), "4. Original fourth note.") self.assertEqual( @@ -103,13 +103,14 @@ def test_source_footnotes_keep_original_discontinuous_numbers(self): self.assertEqual(body.footnote_refs, []) self.assertEqual(source.footnote_refs, []) self.assertFalse(body.style.quoteRoundBottom) + self.assertFalse(source.style.quoteRoundTop) + self.assertFalse(source.style.quoteRoundBottom) self.assertFalse(note_four.style.quoteRoundTop) self.assertFalse(note_four.style.quoteRoundBottom) self.assertFalse(note_seventeen.style.quoteRoundTop) - self.assertFalse(note_seventeen.style.quoteRoundBottom) - self.assertFalse(source.style.quoteRoundTop) + self.assertTrue(note_seventeen.style.quoteRoundBottom) - def test_source_footnotes_render_inside_quote_before_attribution(self): + def test_source_footnotes_render_inside_quote_beneath_attribution(self): with TemporaryDirectory() as temp: root = Path(temp) renderer = self._renderer( @@ -133,9 +134,9 @@ def test_source_footnotes_render_inside_quote_before_attribution(self): note_four = page.search_for("4. Original fourth note.")[0] note_seventeen = page.search_for("17. Original seventeenth note.")[0] source = page.search_for("— Ada")[0] - self.assertLess(body.y0, note_four.y0) + self.assertLess(body.y0, source.y0) + self.assertLess(source.y0, note_four.y0) self.assertLess(note_four.y0, note_seventeen.y0) - self.assertLess(note_seventeen.y0, source.y0) def test_source_marker_is_literal_in_an_ordinary_blockquote(self): with TemporaryDirectory() as temp: From 32d91c580fa70e2582a522944c028264e91d8f53 Mon Sep 17 00:00:00 2001 From: John Wooten <60260750+JFWooten4@users.noreply.github.com> Date: Fri, 13 Nov 2026 03:59:38 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A7=20Make=20quotation=20source=20?= =?UTF-8?q?placement=20opt-in?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep quote-local footnotes above source attribution by default. Add an independent web toggle and CLI flag for putting attribution before local notes; preserve rounded quote styling, document footnote numbering, and both quotation citation syntaxes. Cover default and opt-in ordering in rendering and web regression tests. Co-authored-by: ChatGPT 6 --- README.md | 3 +- app.py | 1 + pdf_compiler.py | 21 ++++++++-- templates/index.html | 3 ++ tests/test_quotations.py | 82 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 104 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8ca39d1..964bc63 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,7 @@ Useful options: --start-heading TEXT compile only content after an exact Markdown heading --smart-quotes use typographic quotes in rendered text --quote-inline-number-footnotes opt in to [11] notation inside [!QUOTE] blocks +--quote-source-before-footnotes put a quotation's source above its local footnotes --pdfa4 export verified PDF/A-4 (veraPDF required) ``` @@ -283,7 +284,7 @@ To preserve footnotes from the quoted source, define them inside the quotation. > [!SOURCE] Author, *Work* ``` -Quote-local footnotes render inside the quotation beneath the right-aligned source attribution. Each `[!QUOTE]` has its own labels, so different quotations can each use footnote 17 with different definitions. An ordinary document footnote outside the quotation still uses `[^key]` and receives the document's sequential numbering. +By default, quote-local footnotes render inside the quotation after its text and before the right-aligned source attribution. To place the source attribution immediately after the quotation text with its local footnotes underneath, check **Place quotation source attribution before its footnotes** under **Sections & TOC**, or use `--quote-source-before-footnotes` on the CLI. This optional layout works with both caret-prefixed and inline numbered quotation footnotes, and does not change ordinary document footnotes. Each `[!QUOTE]` has its own labels, so different quotations can each use footnote 17 with different definitions. An ordinary document footnote outside the quotation still uses `[^key]` and receives the document's sequential numbering. **Optional inline numbered quotation footnotes:** Check **Enable inline numbered footnotes ([11]) in sourced blockquotes** under **Sections & TOC**, or pass `--quote-inline-number-footnotes` on the CLI. This allows copied source text of the form `[11]` and `[11]:` *only inside* a `[!QUOTE]` block, provided a matching local definition exists: diff --git a/app.py b/app.py index 9efeb15..7718ee3 100644 --- a/app.py +++ b/app.py @@ -394,6 +394,7 @@ def metadata_value(name: str) -> str | None: keywords=metadata_value("keywords"), smart_quotes=truthy("smart_quotes"), quote_inline_number_footnotes=truthy("quote_inline_number_footnotes"), + quote_source_before_footnotes=truthy("quote_source_before_footnotes"), underline_links=truthy("underline_links"), hide_url_scheme=truthy("hide_url_scheme"), link_color=link_color, diff --git a/pdf_compiler.py b/pdf_compiler.py index 9c454f3..b6805b4 100644 --- a/pdf_compiler.py +++ b/pdf_compiler.py @@ -445,6 +445,7 @@ def __init__( start_heading: str | None = None, smart_quotes: bool = False, quote_inline_number_footnotes: bool = False, + quote_source_before_footnotes: bool = False, underline_links: bool = True, link_color: str = LINK_COLOR, ): @@ -457,6 +458,7 @@ def __init__( self.author = author or "" self.smart_quotes = smart_quotes self.quote_inline_number_footnotes = quote_inline_number_footnotes + self.quote_source_before_footnotes = quote_source_before_footnotes self.underline_links = underline_links if not re.fullmatch(r"#[0-9a-fA-F]{6}", link_color): raise ValueError("Link color must be a six-digit hex color, such as #2E732E.") @@ -798,11 +800,10 @@ def quotation_flowables(self, quote_lines: list[str]) -> list[RefParagraph]: ) flowables.append(RefParagraph(body_text, body_style, body_refs)) - if source: + def append_source(has_following: bool) -> None: rendered_source, source_refs = self.quotation_inline(source, local_notes) source_style = self.styles["QuotationSourceX"] has_preceding = bool(flowables) - has_following = bool(local_note_items) if has_preceding or has_following: source_style = ParagraphStyle( "QuotationSourceJoinedX", @@ -818,11 +819,16 @@ def quotation_flowables(self, quote_lines: list[str]) -> list[RefParagraph]: RefParagraph(f"— {rendered_source}", source_style, source_refs) ) + if source and self.quote_source_before_footnotes: + append_source(has_following=bool(local_note_items)) + for index, (key, note_text) in enumerate(local_note_items): rendered_note, note_refs = self.quotation_inline(note_text, local_notes) note_style = self.styles["QuotationFootnoteX"] has_preceding = bool(flowables) - has_following = index < len(local_note_items) - 1 + has_following = index < len(local_note_items) - 1 or bool( + source and not self.quote_source_before_footnotes + ) if has_preceding or has_following: note_style = ParagraphStyle( f"QuotationFootnoteJoinedX{index}", @@ -842,6 +848,9 @@ def quotation_flowables(self, quote_lines: list[str]) -> list[RefParagraph]: ) ) + if source and not self.quote_source_before_footnotes: + append_source(has_following=False) + if not flowables: flowables.append(RefParagraph(" ", self.styles["QuotationBodyX"])) return flowables @@ -1536,6 +1545,11 @@ def main() -> int: action="store_true", help="Interpret [11] citations and [11]: definitions inside [!QUOTE] blocks (off by default)", ) + parser.add_argument( + "--quote-source-before-footnotes", + action="store_true", + help="Place [!SOURCE] attribution before quote-local footnotes (off by default)", + ) args = parser.parse_args() output = args.output or args.source.with_suffix(".pdf") @@ -1562,6 +1576,7 @@ def main() -> int: start_heading=args.start_heading, smart_quotes=args.smart_quotes, quote_inline_number_footnotes=args.quote_inline_number_footnotes, + quote_source_before_footnotes=args.quote_source_before_footnotes, **presentation, ) from pdfa4_export import render_pdf diff --git a/templates/index.html b/templates/index.html index ccabbc2..9368fa5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -440,6 +440,9 @@

PDF Compiler

+
PDF outline/bookmarks are always generated from Markdown sections, even when the visible TOC is off or visible section numbering is disabled.
diff --git a/tests/test_quotations.py b/tests/test_quotations.py index 4f3e150..109779e 100644 --- a/tests/test_quotations.py +++ b/tests/test_quotations.py @@ -12,11 +12,15 @@ class QuotationTests(unittest.TestCase): def _renderer(self, root: Path, text: str, *, typography=None, - quote_inline_number_footnotes=False): + quote_inline_number_footnotes=False, + quote_source_before_footnotes=False): source = root / "source.md" source.write_text(text, encoding="utf-8") renderer_class = TypographyPdfRenderer if typography else PdfRenderer - kwargs = {"quote_inline_number_footnotes": quote_inline_number_footnotes} + kwargs = { + "quote_inline_number_footnotes": quote_inline_number_footnotes, + "quote_source_before_footnotes": quote_source_before_footnotes, + } if typography: kwargs["typography"] = typography return renderer_class(source, root / "output.pdf", **kwargs) @@ -84,6 +88,7 @@ def test_source_footnotes_keep_original_discontinuous_numbers(self): "[^main]: Document footnote." ), quote_inline_number_footnotes=True, + quote_source_before_footnotes=True, ) flowables = renderer.blockquote_flowables( [ @@ -130,6 +135,7 @@ def test_source_footnotes_render_inside_quote_beneath_attribution(self): "[^main]: Document footnote." ), quote_inline_number_footnotes=True, + quote_source_before_footnotes=True, ) _pages, footnotes, _continuations = renderer.build() @@ -145,6 +151,78 @@ def test_source_footnotes_render_inside_quote_beneath_attribution(self): self.assertLess(note_four.y0, note_seventeen.y0) + def test_quote_local_source_defaults_to_after_footnotes(self): + with TemporaryDirectory() as temp: + root = Path(temp) + markdown = ( + "> [!QUOTE]\n" + "> Passage.[^7]\n" + "> [^7]: Seventh source note.\n" + "> [!SOURCE] Historian" + ) + lines = [ + "[!QUOTE]", "Passage.[^7]", "[^7]: Seventh source note.", + "[!SOURCE] Historian", + ] + default = self._renderer(root, markdown) + self.assertFalse(default.quote_source_before_footnotes) + body, note, source = default.blockquote_flowables(lines) + self.assertEqual(body.getPlainText(), "Passage.7") + self.assertEqual(note.getPlainText(), "7. Seventh source note.") + self.assertEqual(source.getPlainText(), "— Historian") + self.assertFalse(note.style.quoteRoundBottom) + self.assertTrue(source.style.quoteRoundBottom) + + enabled = self._renderer( + root, markdown, quote_source_before_footnotes=True, + ) + body, source, note = enabled.blockquote_flowables(lines) + self.assertEqual(body.getPlainText(), "Passage.7") + self.assertEqual(source.getPlainText(), "— Historian") + self.assertEqual(note.getPlainText(), "7. Seventh source note.") + self.assertFalse(source.style.quoteRoundBottom) + self.assertTrue(note.style.quoteRoundBottom) + + def test_source_order_toggle_preserves_quotes_without_local_notes(self): + with TemporaryDirectory() as temp: + root = Path(temp) + markdown = "> [!QUOTE]\n> Passage.\n> [!SOURCE] Historian" + lines = ["[!QUOTE]", "Passage.", "[!SOURCE] Historian"] + for enabled in (False, True): + renderer = self._renderer( + root, markdown, quote_source_before_footnotes=enabled, + ) + body, source = renderer.blockquote_flowables(lines) + self.assertEqual(body.getPlainText(), "Passage.") + self.assertEqual(source.getPlainText(), "— Historian") + self.assertTrue(source.style.quoteRoundBottom) + + def test_web_source_order_opt_in_changes_rendered_pdf(self): + from app import app + + markdown = ( + "> [!QUOTE]\n" + "> Passage.[^7]\n" + "> [^7]: Seventh source note.\n" + "> [!SOURCE] Historian" + ) + for enabled in (False, True): + data = {"markdown": markdown} + if enabled: + data["quote_source_before_footnotes"] = "1" + response = app.test_client().post("/render", data=data) + self.assertEqual(response.status_code, 200, response.data[:500]) + with pymupdf.open(stream=response.data, filetype="pdf") as pdf: + page = pdf[0] + body = page.search_for("Passage.")[0] + source = page.search_for("— Historian")[0] + note = page.search_for("7. Seventh source note.")[0] + self.assertLess(body.y0, min(note.y0, source.y0)) + if enabled: + self.assertLess(source.y0, note.y0) + else: + self.assertLess(note.y0, source.y0) + def test_quote_local_numbers_can_repeat_in_separate_quotes(self): with TemporaryDirectory() as temp: root = Path(temp)