From a8707d65df2679b73351c27fbd4f28e5f8a4714c Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Mon, 1 Jul 2024 09:55:38 -0400 Subject: [PATCH] Create LoI file if it doesn't exist --- se/commands/build_loi.py | 3 +- se/data/templates/loi.xhtml | 14 +++ se/se_epub.py | 14 ++- .../test-2/golden/src/epub/content.opf | 119 ++++++++++++++++++ .../golden/src/epub/text/chapter-1.xhtml | 17 +++ .../golden/src/epub/text/chapter-2.xhtml | 14 +++ .../golden/src/epub/text/chapter-3.xhtml | 14 +++ .../test-2/golden/src/epub/text/loi.xhtml | 20 +++ .../test-2/golden/src/epub/toc.xhtml | 24 ++++ .../build-loi/test-2/in/src/epub/content.opf | 119 ++++++++++++++++++ .../test-2/in/src/epub/text/chapter-1.xhtml | 17 +++ .../build-loi/test-2/in/src/epub/toc.xhtml | 24 ++++ 12 files changed, 396 insertions(+), 3 deletions(-) create mode 100644 se/data/templates/loi.xhtml create mode 100644 tests/draft_commands/build-loi/test-2/golden/src/epub/content.opf create mode 100644 tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-1.xhtml create mode 100644 tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-2.xhtml create mode 100644 tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-3.xhtml create mode 100644 tests/draft_commands/build-loi/test-2/golden/src/epub/text/loi.xhtml create mode 100644 tests/draft_commands/build-loi/test-2/golden/src/epub/toc.xhtml create mode 100644 tests/draft_commands/build-loi/test-2/in/src/epub/content.opf create mode 100644 tests/draft_commands/build-loi/test-2/in/src/epub/text/chapter-1.xhtml create mode 100644 tests/draft_commands/build-loi/test-2/in/src/epub/toc.xhtml diff --git a/se/commands/build_loi.py b/se/commands/build_loi.py index 2ebaa221..3e3a4628 100644 --- a/se/commands/build_loi.py +++ b/se/commands/build_loi.py @@ -34,7 +34,8 @@ def build_loi(plain_output: bool) -> int: if args.stdout: print(xhtml) else: - with open(se_epub.loi_path, "w", encoding="utf-8") as file: + loi_path = se_epub.loi_path or (se_epub.content_path / "text/loi.xhtml") + with open(loi_path, "w", encoding="utf-8") as file: file.write(xhtml) except se.SeException as ex: diff --git a/se/data/templates/loi.xhtml b/se/data/templates/loi.xhtml new file mode 100644 index 00000000..2351443b --- /dev/null +++ b/se/data/templates/loi.xhtml @@ -0,0 +1,14 @@ + + + + List of Illustrations + + + + + + + diff --git a/se/se_epub.py b/se/se_epub.py index 149258f4..b8e55ae0 100644 --- a/se/se_epub.py +++ b/se/se_epub.py @@ -9,6 +9,7 @@ import datetime import os from pathlib import Path +import importlib.resources from typing import Dict, List, Optional, Tuple, Union import git @@ -917,12 +918,21 @@ def shift_illustrations(self, target_illustration_number: int, step: int = 1) -> def generate_loi(self) -> str: """ - Updates the given LoI DOM based on all
elements that contain an + Generate an LoI DOM based on all
elements that contain an . Text from the
, if any, is preferred over that from the 's alt attribute. """ - loi_dom = self.get_dom(self.loi_path) + loi_dom = None + if not self.loi_path: + with importlib.resources.files("se.data.templates").joinpath("loi.xhtml").open("r", encoding="utf-8") as file: + loi_dom = se.easy_xml.EasyXmlTree(file.read()) + + language = self.metadata_dom.xpath("/package/metadata/dc:language/text()")[0] + loi_dom.xpath("/html")[0].set_attr("xml:lang", language) + else: + loi_dom = self.get_dom(self.loi_path) + ols = loi_dom.xpath("/html/body/nav/ol") if len(ols) != 1: raise se.InvalidSeEbookException(f"LoI contains unexpected number of [html]
    [/]: [path][link=file://{self.loi_path}]{self.loi_path}[/][/].") diff --git a/tests/draft_commands/build-loi/test-2/golden/src/epub/content.opf b/tests/draft_commands/build-loi/test-2/golden/src/epub/content.opf new file mode 100644 index 00000000..bf450769 --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/golden/src/epub/content.opf @@ -0,0 +1,119 @@ + + + + url:https://standardebooks.org/ebooks/samuel-pepys/the-diary + 2019-08-21T18:40:45Z + 2019-08-21T18:40:45Z + The source text and artwork in this ebook are believed to be in the United States public domain; that is, they are believed to be free of copyright restrictions in the United States. They may still be copyrighted in other countries, so users located outside of the United States must check their local laws before using this ebook. The creators of, and contributors to, this ebook dedicate their contributions to the worldwide public domain via the terms in the [CC0 1.0 Universal Public Domain Dedication](https://creativecommons.org/publicdomain/zero/1.0/). + Standard Ebooks + Standard Ebooks + https://standardebooks.org + bkd + mdc + pbl + The League of Moveable Type + League of Moveable Type, The + https://www.theleagueofmoveabletype.com + tyd + + Standard Ebooks + textual + textual + readingOrder + structuralNavigation + tableOfContents + none + This publication conforms to WCAG 2.2 Level AA. + + The Diary + Diary, The + Pepys, Samuel, 1633-1703 -- Diaries + Cabinet officers -- Great Britain -- Diaries + Diarists -- Great Britain -- Diaries + Great Britain -- Social life and customs -- 17th century -- Sources + Great Britain -- History -- Charles II, 1660-1685 -- Sources + LCSH + Unknown + LCSH + sh2008100004 + LCSH + Unknown + LCSH + sh2007100274 + LCSH + sh2008115250 + Autobiography + Nonfiction + The diary of a man living in London in the 17th century. + + <p>Pepys’ <i>Diary</i> is an incredibly frank decade-long snapshot of the life of an up and coming naval administrator in mid-17th century London. In it he describes everything from battles against the Dutch and the intrigues of court, down to the plays he saw, his marital infidelities, and the quality of the meat provided for his supper. His observations have proved invaluable in establishing an accurate record of the daily life of the people of London of that period.</p> + <p>Pepys eventually stopped writing his diary due to progressively worse eyesight, a condition he feared. He did consider employing an amanuensis to transcribe future entries for him, but worried that the content he wanted written would be too personal. Luckily for Pepys, his eyesight difficulties never progressed to blindness and he was able to go on to become both a Member of Parliament and the President of the Royal Society.</p> + <p>After Pepys’ death he left his large library of books and manuscripts first to his nephew, which was then passed on to Magdalene College, Cambridge, where it survives to this day. The diary, originally written in a shorthand, was included in this trove and was eventually deciphered in the early 19th century, and published by Lord Baybrooke in 1825. This early release censored large amounts of the text, and it was only in the 1970s that an uncensored version was published. Presented here is the 1893 edition, which restores the majority of the originally censored content but omits “a few passages which cannot possibly be printed.” The rich collection of endnotes serve to further illustrate the lives of the people Pepys meets and the state of England’s internal politics and international relations at the time.</p> + + en-GB + https://www.gutenberg.org/ebooks/4200 + https://archive.org/details/diaryofsamuelpep01pepy + + • Volumes 9 and 10 (appendix and Pepysiana) of the original series have been omitted from the production due to time constraints. + • Each diary entry has a time element to start. The datetime attribute is ISO8601 compliant (Gregorian), but the actual dates Pepys uses are Julian, so they don’t appear to match. This is correct. + • This date is copied into the `entry-x` id attribute for each diary entry. Unfortunately, this causes linting to fail with leading 0 errors, so we remove those for the id. + + 1209949 + 70.57 + https://en.wikipedia.org/wiki/Samuel_Pepys#The_diary + https://github.com/standardebooks/samuel-pepys_the-diary + Samuel Pepys + Pepys, Samuel + https://en.wikipedia.org/wiki/Samuel_Pepys + http://id.loc.gov/authorities/names/n79018796 + aut + John Hayls + Hayls, John + https://en.wikipedia.org/wiki/John_Hayls + http://id.loc.gov/authorities/names/nr94041870 + art + Henry B. Wheatley + Wheatley, Henry B. + Henry Benjamin Wheatley + https://en.wikipedia.org/wiki/Henry_B._Wheatley + http://id.loc.gov/authorities/names/n87860971 + aft + ann + aui + edt + trc + Richard Griffin + 0 + Griffin, Richard + https://en.wikipedia.org/wiki/Richard_Griffin,_3rd_Baron_Braybrooke + http://id.loc.gov/authorities/names/n50014973 + ann + cns + ill + David Widger + Widger, David + http://id.loc.gov/authorities/names/no2011017869 + trc + Robin Whittleton + Whittleton, Robin + https://www.robinwhittleton.com + bkp + blw + cov + mrk + pfr + trc + tyg + David Grigg + Grigg, David + https://rightword.com.au/david.php + pfr + + + + + + + + + diff --git a/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-1.xhtml b/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-1.xhtml new file mode 100644 index 00000000..25ba590c --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-1.xhtml @@ -0,0 +1,17 @@ + + + + I + + + + +
    +

    I

    +
    + +
    Caption.
    +
    +
    + + diff --git a/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-2.xhtml b/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-2.xhtml new file mode 100644 index 00000000..1a38cf14 --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-2.xhtml @@ -0,0 +1,14 @@ + + + + II + + + + +
    +

    II

    +

    Here is a figure:

    +
    + + diff --git a/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-3.xhtml b/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-3.xhtml new file mode 100644 index 00000000..d320972b --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/golden/src/epub/text/chapter-3.xhtml @@ -0,0 +1,14 @@ + + + + III + + + + +
    +

    III

    + +
    + + diff --git a/tests/draft_commands/build-loi/test-2/golden/src/epub/text/loi.xhtml b/tests/draft_commands/build-loi/test-2/golden/src/epub/text/loi.xhtml new file mode 100644 index 00000000..08be0ef4 --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/golden/src/epub/text/loi.xhtml @@ -0,0 +1,20 @@ + + + + List of Illustrations + + + + + + + diff --git a/tests/draft_commands/build-loi/test-2/golden/src/epub/toc.xhtml b/tests/draft_commands/build-loi/test-2/golden/src/epub/toc.xhtml new file mode 100644 index 00000000..7f47198a --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/golden/src/epub/toc.xhtml @@ -0,0 +1,24 @@ + + + + Table of Contents + + + + + + diff --git a/tests/draft_commands/build-loi/test-2/in/src/epub/content.opf b/tests/draft_commands/build-loi/test-2/in/src/epub/content.opf new file mode 100644 index 00000000..bf450769 --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/in/src/epub/content.opf @@ -0,0 +1,119 @@ + + + + url:https://standardebooks.org/ebooks/samuel-pepys/the-diary + 2019-08-21T18:40:45Z + 2019-08-21T18:40:45Z + The source text and artwork in this ebook are believed to be in the United States public domain; that is, they are believed to be free of copyright restrictions in the United States. They may still be copyrighted in other countries, so users located outside of the United States must check their local laws before using this ebook. The creators of, and contributors to, this ebook dedicate their contributions to the worldwide public domain via the terms in the [CC0 1.0 Universal Public Domain Dedication](https://creativecommons.org/publicdomain/zero/1.0/). + Standard Ebooks + Standard Ebooks + https://standardebooks.org + bkd + mdc + pbl + The League of Moveable Type + League of Moveable Type, The + https://www.theleagueofmoveabletype.com + tyd + + Standard Ebooks + textual + textual + readingOrder + structuralNavigation + tableOfContents + none + This publication conforms to WCAG 2.2 Level AA. + + The Diary + Diary, The + Pepys, Samuel, 1633-1703 -- Diaries + Cabinet officers -- Great Britain -- Diaries + Diarists -- Great Britain -- Diaries + Great Britain -- Social life and customs -- 17th century -- Sources + Great Britain -- History -- Charles II, 1660-1685 -- Sources + LCSH + Unknown + LCSH + sh2008100004 + LCSH + Unknown + LCSH + sh2007100274 + LCSH + sh2008115250 + Autobiography + Nonfiction + The diary of a man living in London in the 17th century. + + <p>Pepys’ <i>Diary</i> is an incredibly frank decade-long snapshot of the life of an up and coming naval administrator in mid-17th century London. In it he describes everything from battles against the Dutch and the intrigues of court, down to the plays he saw, his marital infidelities, and the quality of the meat provided for his supper. His observations have proved invaluable in establishing an accurate record of the daily life of the people of London of that period.</p> + <p>Pepys eventually stopped writing his diary due to progressively worse eyesight, a condition he feared. He did consider employing an amanuensis to transcribe future entries for him, but worried that the content he wanted written would be too personal. Luckily for Pepys, his eyesight difficulties never progressed to blindness and he was able to go on to become both a Member of Parliament and the President of the Royal Society.</p> + <p>After Pepys’ death he left his large library of books and manuscripts first to his nephew, which was then passed on to Magdalene College, Cambridge, where it survives to this day. The diary, originally written in a shorthand, was included in this trove and was eventually deciphered in the early 19th century, and published by Lord Baybrooke in 1825. This early release censored large amounts of the text, and it was only in the 1970s that an uncensored version was published. Presented here is the 1893 edition, which restores the majority of the originally censored content but omits “a few passages which cannot possibly be printed.” The rich collection of endnotes serve to further illustrate the lives of the people Pepys meets and the state of England’s internal politics and international relations at the time.</p> + + en-GB + https://www.gutenberg.org/ebooks/4200 + https://archive.org/details/diaryofsamuelpep01pepy + + • Volumes 9 and 10 (appendix and Pepysiana) of the original series have been omitted from the production due to time constraints. + • Each diary entry has a time element to start. The datetime attribute is ISO8601 compliant (Gregorian), but the actual dates Pepys uses are Julian, so they don’t appear to match. This is correct. + • This date is copied into the `entry-x` id attribute for each diary entry. Unfortunately, this causes linting to fail with leading 0 errors, so we remove those for the id. + + 1209949 + 70.57 + https://en.wikipedia.org/wiki/Samuel_Pepys#The_diary + https://github.com/standardebooks/samuel-pepys_the-diary + Samuel Pepys + Pepys, Samuel + https://en.wikipedia.org/wiki/Samuel_Pepys + http://id.loc.gov/authorities/names/n79018796 + aut + John Hayls + Hayls, John + https://en.wikipedia.org/wiki/John_Hayls + http://id.loc.gov/authorities/names/nr94041870 + art + Henry B. Wheatley + Wheatley, Henry B. + Henry Benjamin Wheatley + https://en.wikipedia.org/wiki/Henry_B._Wheatley + http://id.loc.gov/authorities/names/n87860971 + aft + ann + aui + edt + trc + Richard Griffin + 0 + Griffin, Richard + https://en.wikipedia.org/wiki/Richard_Griffin,_3rd_Baron_Braybrooke + http://id.loc.gov/authorities/names/n50014973 + ann + cns + ill + David Widger + Widger, David + http://id.loc.gov/authorities/names/no2011017869 + trc + Robin Whittleton + Whittleton, Robin + https://www.robinwhittleton.com + bkp + blw + cov + mrk + pfr + trc + tyg + David Grigg + Grigg, David + https://rightword.com.au/david.php + pfr + + + + + + + + + diff --git a/tests/draft_commands/build-loi/test-2/in/src/epub/text/chapter-1.xhtml b/tests/draft_commands/build-loi/test-2/in/src/epub/text/chapter-1.xhtml new file mode 100644 index 00000000..25ba590c --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/in/src/epub/text/chapter-1.xhtml @@ -0,0 +1,17 @@ + + + + I + + + + +
    +

    I

    +
    + +
    Caption.
    +
    +
    + + diff --git a/tests/draft_commands/build-loi/test-2/in/src/epub/toc.xhtml b/tests/draft_commands/build-loi/test-2/in/src/epub/toc.xhtml new file mode 100644 index 00000000..7f47198a --- /dev/null +++ b/tests/draft_commands/build-loi/test-2/in/src/epub/toc.xhtml @@ -0,0 +1,24 @@ + + + + Table of Contents + + + + + +