diff --git a/README.md b/README.md index c98ed5a..4a366a0 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,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) ``` @@ -287,7 +288,7 @@ 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. 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 2bd9e80..7fde2cb 100644 --- a/app.py +++ b/app.py @@ -468,6 +468,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"), emoji_style=emoji_style, emoji_pack=emoji_pack, underline_links=truthy("underline_links"), diff --git a/pdf_compiler.py b/pdf_compiler.py index ee7359b..7ddcb11 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, emoji_style: str = "twemoji", emoji_pack: Path | None = None, underline_links: bool = True, @@ -459,6 +460,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 validate_emoji_style(emoji_style, emoji_pack) self.emoji_style = emoji_style self.emoji_pack = emoji_pack @@ -803,11 +805,35 @@ def quotation_flowables(self, quote_lines: list[str]) -> list[RefParagraph]: ) flowables.append(RefParagraph(body_text, body_style, body_refs)) + 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) + 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) + ) + + 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 or bool(source) + 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}", @@ -827,20 +853,8 @@ 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 source and not self.quote_source_before_footnotes: + append_source(has_following=False) if not flowables: flowables.append(RefParagraph(" ", self.styles["QuotationBodyX"])) @@ -1541,6 +1555,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") @@ -1567,6 +1586,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, emoji_style=args.emoji_style, emoji_pack=args.emoji_pack, **presentation, diff --git a/templates/index.html b/templates/index.html index f7da588..2c3eb43 100644 --- a/templates/index.html +++ b/templates/index.html @@ -466,6 +466,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 7b0524e..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( [ @@ -97,7 +102,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( @@ -108,13 +113,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( @@ -129,6 +135,7 @@ def test_source_footnotes_render_inside_quote_before_attribution(self): "[^main]: Document footnote." ), quote_inline_number_footnotes=True, + quote_source_before_footnotes=True, ) _pages, footnotes, _continuations = renderer.build() @@ -139,11 +146,83 @@ 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_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)