diff --git a/se/commands/build_loi.py b/se/commands/build_loi.py new file mode 100644 index 00000000..c0560b14 --- /dev/null +++ b/se/commands/build_loi.py @@ -0,0 +1,33 @@ +""" +This module implements the `se build-loi` command. +""" + +import argparse + +import se +from se.se_epub import SeEpub + + +def build_loi(plain_output: bool) -> int: + """ + Entry point for `se build-loi` + """ + + parser = argparse.ArgumentParser(description="Update the LoI file based on all
elements that contain an .") + parser.add_argument("-a", "--prefer-alt-text", dest="prefer_alt_text", nargs="+", help="prefer alt text over
for these
IDs") + parser.add_argument("-d", "--default-link-text", dest="default_link_text", metavar="DEFAULT-LINK-TEXT", type=str, default="TODO", help="link text to use if
or alt text is absent or empty") + parser.add_argument("directory", metavar="DIRECTORY", help="a Standard Ebooks source directory") + args = parser.parse_args() + + return_code = 0 + + try: + se_epub = SeEpub(args.directory) + prefer_alt_text = set(args.prefer_alt_text or []) + se_epub.generate_loi(prefer_alt_text=prefer_alt_text, default_link_text=args.default_link_text) + + except se.SeException as ex: + se.print_error(ex, plain_output=plain_output) + return_code = ex.code + + return return_code diff --git a/se/se_epub.py b/se/se_epub.py index 06857dce..aa4d8b80 100644 --- a/se/se_epub.py +++ b/se/se_epub.py @@ -8,7 +8,7 @@ import datetime import os from pathlib import Path -from typing import Dict, List, Optional, Tuple, Union +from typing import Dict, List, Optional, Tuple, Union, Set import git from lxml import etree @@ -913,6 +913,53 @@ def shift_illustrations(self, target_illustration_number: int, step: int = 1) -> with open(file_path, "w", encoding="utf-8") as file: file.write(dom.to_string()) + def generate_loi(self, prefer_alt_text: Set[str], default_link_text: str) -> None: + """ + Updates the LoI file based on all
elements that contain an + . Text from the
, if any, is preferred over that from + the 's alt attribute, unless prefer_alt_text contains the +
's ID. If the resulting text is empty, default_link_text is used. + """ + + figures = [] + for file_path in self.spine_file_paths: + dom = self.get_dom(file_path) + + for node in dom.xpath("/html/body//figure[@id and img]"): + figcaption = node.xpath("./figcaption") + figure_id = node.get_attr('id') + text = "" + if figcaption and figure_id not in prefer_alt_text: + text = figcaption[0].inner_text() + else: + text = node.xpath("./img")[0].get_attr("alt") + + figures.append((f"{file_path.name}#{figure_id}", text)) + + dom = self.get_dom(self.loi_path) + + ols = dom.xpath("/html/body/nav/ol") + if len(ols) != 1: + raise se.InvalidSeEbookException(f"LoI file contains unexpected number of [html]
    [/]: [path][link=file://{self.loi_path}]{self.loi_path}[/][/].") + + etree.strip_elements(ols[0].lxml_element, "li") + + for (href, text) in figures: + a = se.easy_xml.EasyXmlElement("") + a.set_text(text or default_link_text) + a.set_attr("href", href) + p = se.easy_xml.EasyXmlElement("

    ") + p.append(a) + li = se.easy_xml.EasyXmlElement("

  1. ") + li.append(p) + ols[0].append(li) + + try: + with open(self.loi_path, "w", encoding="utf-8") as file: + file.write(se.formatting.format_xhtml(dom.to_string())) + except Exception as ex: + raise se.InvalidSeEbookException(f"Couldn’t open LoI file: [path][link=file://{self.loi_path}]{self.loi_path}[/][/].") from ex + def set_release_timestamp(self) -> None: """ If this ebook has not yet been released, set the first release timestamp in the metadata file. diff --git a/tests/draft_commands/build-loi/test-1/build-loi-command b/tests/draft_commands/build-loi/test-1/build-loi-command new file mode 100644 index 00000000..d85398bd --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/build-loi-command @@ -0,0 +1 @@ +build-loi -a 'f-8' -d 'placeholder text' diff --git a/tests/draft_commands/build-loi/test-1/golden/src/epub/content.opf b/tests/draft_commands/build-loi/test-1/golden/src/epub/content.opf new file mode 100644 index 00000000..65934188 --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/golden/src/epub/content.opf @@ -0,0 +1,125 @@ + + + + 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-1/golden/src/epub/text/chapter-1.xhtml b/tests/draft_commands/build-loi/test-1/golden/src/epub/text/chapter-1.xhtml new file mode 100644 index 00000000..fbe417d2 --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/golden/src/epub/text/chapter-1.xhtml @@ -0,0 +1,48 @@ + + + + I + + + + +
    +

    I

    + + +
    + +
    + + +
    + +
    +
    + +
    +
    + +
    +
    +
    + a b +
    +
    + +
    C D
    +
    +
    + e +
    F
    +
    + +
    +
    + g +
    H
    +
    +
    +
    + + diff --git a/tests/draft_commands/build-loi/test-1/golden/src/epub/text/chapter-2.xhtml b/tests/draft_commands/build-loi/test-1/golden/src/epub/text/chapter-2.xhtml new file mode 100644 index 00000000..4224085d --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/golden/src/epub/text/chapter-2.xhtml @@ -0,0 +1,16 @@ + + + + II + + + + +
    +

    II

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

    III

    + +
    + + diff --git a/tests/draft_commands/build-loi/test-1/golden/src/epub/text/loi.xhtml b/tests/draft_commands/build-loi/test-1/golden/src/epub/text/loi.xhtml new file mode 100644 index 00000000..c85e114b --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/golden/src/epub/text/loi.xhtml @@ -0,0 +1,55 @@ + + + + List of Illustrations + + + + +
    + + diff --git a/tests/draft_commands/build-loi/test-1/golden/src/epub/toc.xhtml b/tests/draft_commands/build-loi/test-1/golden/src/epub/toc.xhtml new file mode 100644 index 00000000..7f47198a --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/golden/src/epub/toc.xhtml @@ -0,0 +1,24 @@ + + + + Table of Contents + + + + + + diff --git a/tests/draft_commands/build-loi/test-1/in/src/epub/content.opf b/tests/draft_commands/build-loi/test-1/in/src/epub/content.opf new file mode 100644 index 00000000..65934188 --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/in/src/epub/content.opf @@ -0,0 +1,125 @@ + + + + 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-1/in/src/epub/text/chapter-1.xhtml b/tests/draft_commands/build-loi/test-1/in/src/epub/text/chapter-1.xhtml new file mode 100644 index 00000000..fbe417d2 --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/in/src/epub/text/chapter-1.xhtml @@ -0,0 +1,48 @@ + + + + I + + + + +
    +

    I

    + + +
    + +
    + + +
    + +
    +
    + +
    +
    + +
    +
    +
    + a b +
    +
    + +
    C D
    +
    +
    + e +
    F
    +
    + +
    +
    + g +
    H
    +
    +
    +
    + + diff --git a/tests/draft_commands/build-loi/test-1/in/src/epub/text/chapter-2.xhtml b/tests/draft_commands/build-loi/test-1/in/src/epub/text/chapter-2.xhtml new file mode 100644 index 00000000..4224085d --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/in/src/epub/text/chapter-2.xhtml @@ -0,0 +1,16 @@ + + + + II + + + + +
    +

    II

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

    III

    + +
    + + diff --git a/tests/draft_commands/build-loi/test-1/in/src/epub/text/loi.xhtml b/tests/draft_commands/build-loi/test-1/in/src/epub/text/loi.xhtml new file mode 100644 index 00000000..68a3b6cc --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/in/src/epub/text/loi.xhtml @@ -0,0 +1,16 @@ + + + + List of Illustrations + + + + + + + diff --git a/tests/draft_commands/build-loi/test-1/in/src/epub/toc.xhtml b/tests/draft_commands/build-loi/test-1/in/src/epub/toc.xhtml new file mode 100644 index 00000000..7f47198a --- /dev/null +++ b/tests/draft_commands/build-loi/test-1/in/src/epub/toc.xhtml @@ -0,0 +1,24 @@ + + + + Table of Contents + + + + + + diff --git a/tests/stdout_commands/help/test-1/golden/help-test-1-out.txt b/tests/stdout_commands/help/test-1/golden/help-test-1-out.txt index 99d4b879..bdda74c9 100644 --- a/tests/stdout_commands/help/test-1/golden/help-test-1-out.txt +++ b/tests/stdout_commands/help/test-1/golden/help-test-1-out.txt @@ -3,6 +3,7 @@ british2american build build-ids build-images +build-loi build-manifest build-spine build-title diff --git a/tests/stdout_commands/help/test-2/golden/help-test-2-out.txt b/tests/stdout_commands/help/test-2/golden/help-test-2-out.txt index c6db58a5..cd8ca109 100644 --- a/tests/stdout_commands/help/test-2/golden/help-test-2-out.txt +++ b/tests/stdout_commands/help/test-2/golden/help-test-2-out.txt @@ -4,9 +4,9 @@ The entry point for the Standard Ebooks toolset. positional arguments: COMMAND one of: british2american build build-ids build-images build- - manifest build-spine build-title build-toc clean compare- - versions create-draft css-select dec2roman extract-ebook - find-mismatched-dashes find-mismatched-diacritics find- + loi build-manifest build-spine build-title build-toc clean + compare-versions create-draft css-select dec2roman extract- + ebook find-mismatched-dashes find-mismatched-diacritics find- unusual-characters help hyphenate interactive-replace lint make-url-safe modernize-spelling prepare-release recompose- epub renumber-endnotes roman2dec semanticate shift-endnotes