Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand Down Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
50 changes: 35 additions & 15 deletions pdf_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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}",
Expand All @@ -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("&#160;", self.styles["QuotationBodyX"]))
Expand Down Expand Up @@ -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")
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ <h1>PDF Compiler</h1>
<label class="check full"><input type="checkbox" name="quote_inline_number_footnotes"> Enable inline numbered footnotes (<code>[11]</code>) in sourced blockquotes
<small>Off by default to avoid interpreting ordinary bracketed text as a citation. With this on, <code>[11]</code> and <code>[11]:</code> work only inside <code>[!QUOTE]</code> blocks with matching local definitions. Standard <code>[^11]</code> works without this setting.</small>
</label>
<label class="check full"><input type="checkbox" name="quote_source_before_footnotes"> Place quotation source attribution before its footnotes
<small>Off by default. When enabled, the right-aligned source appears immediately below the quoted text, followed by any quotation-local footnotes. Works independently of the inline numbered footnote setting above.</small>
</label>
<div class="full">
<small>PDF outline/bookmarks are always generated from Markdown sections, even when the visible TOC is off or visible section numbering is disabled.</small>
</div>
Expand Down
95 changes: 87 additions & 8 deletions tests/test_quotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
[
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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()

Expand All @@ -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)
Expand Down
Loading