GeezerVison is showing github.com — page lists are now attached to each EpubHtml item as well as to the … · 06linux/ebooklib@fe88934
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: AUTHORS.txt +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | | 22 | 22 | Sergey Markelov | 23 | 23 | | 24 | | -Deborah Kaplan | | 24 | + |
Collapse file Copy file name to clipboardExpand all lines: ebooklib/epub.py +49-50Lines changed: 49 additions & 50 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 31 | 31 |
| 32 | 32 | import ebooklib | 33 | 33 |
| 34 | | -from ebooklib.utils import parse_string, parse_html_string, guess_type | | 34 | +from ebooklib.utils import parse_string, parse_html_string, guess_type, get_pages_for_items | 35 | 35 |
| 36 | 36 |
| 37 | 37 | # Version of EPUB library | @@ -254,6 +254,7 @@ def __init__(self, uid=None, file_name='', media_type='', content=None, title='' | 254 | 254 |
| 255 | 255 | self.links = [] | 256 | 256 | self.properties = [] | | 257 | + self.pages = [] | 257 | 258 |
| 258 | 259 | def is_chapter(self): | 259 | 260 | """ | @@ -317,32 +318,6 @@ def get_links_of_type(self, link_type): | 317 | 318 | """ | 318 | 319 | return (link for link in self.links if link.get('type', '') == link_type) | 319 | 320 |
| 320 | | - def add_pageref(self, pageref, label=None): | 321 | | - """ | 322 | | - Create a pagebreak element with optional visible content. | 323 | | - Append to the current end of the EpubHtml content. | 324 | | - Save for the page-list in the nav. | 325 | | - """ | 326 | | - | 327 | | - pageref_attributes = { | 328 | | - '{%s}type' % NAMESPACES['EPUB']: 'pagebreak', | 329 | | - 'title': u'{}'.format(pageref), | 330 | | - 'id': u'{}'.format(pageref), | 331 | | - } | 332 | | - | 333 | | - pageref_elem = etree.Element('span', pageref_attributes) | 334 | | - | 335 | | - if label: | 336 | | - pageref_elem.text = label | 337 | | - | 338 | | - # Append a tuple to the pages list: The filename of this EpubHtml | 339 | | - # object, the id to reference, and a label to be used in the page-list. | 340 | | - self.book.pages.append((self.file_name, pageref, label or pageref)) | 341 | | - | 342 | | - # Append this pageref to the HTML content | 343 | | - self.content += etree.tostring(pageref_elem, encoding='unicode') | 344 | | - | 345 | | - | 346 | 321 | def add_item(self, item): | 347 | 322 | """ | 348 | 323 | Add other item to this document. It will create additional links according to the item type. | @@ -733,6 +708,8 @@ def add_item(self, item): | 733 | 708 | if isinstance(item, EpubHtml): | 734 | 709 | item.id = 'chapter_%d' % self._id_html | 735 | 710 | self._id_html += 1 | | 711 | + # If there's a page list, append it to the book's page list | | 712 | + self.pages += item.pages | 736 | 713 | elif isinstance(item, EpubImage): | 737 | 714 | item.id = 'image_%d' % self._id_image | 738 | 715 | self._id_image += 1 | @@ -1204,33 +1181,39 @@ def _create_section(itm, items): | 1204 | 1181 | a_item.text = _title | 1205 | 1182 |
| 1206 | 1183 | # PAGE-LIST | 1207 | | - if len(self.book.pages) > 0 and self.options.get('epub3_pages'): | 1208 | | - pagelist_nav = etree.SubElement( | 1209 | | - body, | 1210 | | - 'nav', | 1211 | | - { | 1212 | | - '{%s}type' % NAMESPACES['EPUB']: 'page-list', | 1213 | | - 'id': 'pages', | 1214 | | - 'hidden': 'hidden', | 1215 | | - } | 1216 | | - ) | 1217 | | - pagelist_content_title = etree.SubElement(pagelist_nav, 'h2') | 1218 | | - pagelist_content_title.text = self.options.get( | 1219 | | - 'pages_title', 'Pages' | 1220 | | - ) | | 1184 | + if self.options.get('epub3_pages'): | | 1185 | + inserted_pages = get_pages_for_items([item for item in self.book.get_items_of_type(ebooklib.ITEM_DOCUMENT) \ | | 1186 | + if not isinstance(item, EpubNav)]) | | 1187 | + | | 1188 | + if len(inserted_pages) > 0: | | 1189 | + pagelist_nav = etree.SubElement( | | 1190 | + body, | | 1191 | + 'nav', | | 1192 | + { | | 1193 | + '{%s}type' % NAMESPACES['EPUB']: 'page-list', | | 1194 | + 'id': 'pages', | | 1195 | + 'hidden': 'hidden', | | 1196 | + } | | 1197 | + ) | | 1198 | + pagelist_content_title = etree.SubElement(pagelist_nav, 'h2') | | 1199 | + pagelist_content_title.text = self.options.get( | | 1200 | + 'pages_title', 'Pages' | | 1201 | + ) | 1221 | 1202 |
| 1222 | | - pages_ol = etree.SubElement(pagelist_nav, 'ol') | | 1203 | + pages_ol = etree.SubElement(pagelist_nav, 'ol') | 1223 | 1204 |
| 1224 | | - for filename, pageref, label in self.book.pages: | 1225 | | - li_item = etree.SubElement(pages_ol, 'li') | 1226 | 1205 |
| 1227 | | - _href = u'{}#{}'.format(filename, pageref) | 1228 | | - _title = label | 1229 | 1206 |
| 1230 | | - a_item = etree.SubElement(li_item, 'a', { | 1231 | | - 'href': os.path.relpath(_href, nav_dir_name), | 1232 | | - }) | 1233 | | - a_item.text = _title | | 1207 | + for filename, pageref, label in inserted_pages: | | 1208 | + li_item = etree.SubElement(pages_ol, 'li') | | 1209 | + | | 1210 | + _href = u'{}#{}'.format(filename, pageref) | | 1211 | + _title = label | | 1212 | + | | 1213 | + a_item = etree.SubElement(li_item, 'a', { | | 1214 | + 'href': os.path.relpath(_href, nav_dir_name), | | 1215 | + }) | | 1216 | + a_item.text = _title | 1234 | 1217 |
| 1235 | 1218 | tree_str = etree.tostring(nav_xml, pretty_print=True, encoding='utf-8', xml_declaration=True) | 1236 | 1219 |
| @@ -1598,8 +1581,24 @@ def parse_list(list_node): | 1598 | 1581 | if navtype == 'toc': | 1599 | 1582 | self.book.toc = parse_list(nav_node.find('ol')) | 1600 | 1583 | elif nav_node is not None: | | 1584 | + # generate the pages list if there is one | 1601 | 1585 | self.book.pages = parse_list(nav_node.find('ol')) | 1602 | 1586 |
| | 1587 | + # generate the per-file pages lists | | 1588 | + # because of the order of parsing the files, this can't be done | | 1589 | + # when building the EpubHtml objects | | 1590 | + htmlfiles = dict() | | 1591 | + for htmlfile in self.book.items: | | 1592 | + if isinstance(htmlfile, EpubHtml): | | 1593 | + htmlfiles[htmlfile.file_name] = htmlfile | | 1594 | + for page in self.book.pages: | | 1595 | + try: | | 1596 | + (filename, idref) = page.href.split('#') | | 1597 | + except ValueError: | | 1598 | + filename = page.href | | 1599 | + if filename in htmlfiles: | | 1600 | + htmlfiles[filename].pages.append(page) | | 1601 | + | 1603 | 1602 | def _load_spine(self): | 1604 | 1603 | spine = self.container.find('{%s}%s' % (NAMESPACES['OPF'], 'spine')) | 1605 | 1604 |
| |
Collapse file Copy file name to clipboardExpand all lines: ebooklib/plugins/booktype.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,7 +51,7 @@ def html_before_write(self, book, chapter): | 51 | 51 | if _link.get('href', '').find('InsertNoteID') != -1: | 52 | 52 | _ln = _link.get('href', '') | 53 | 53 | i = _ln.find('#') | 54 | | - _link.set('href', _ln[i:]); | | 54 | + _link.set('href', _ln[i:]) | 55 | 55 |
| 56 | 56 | continue | 57 | 57 |
| @@ -106,9 +106,9 @@ def html_before_write(self, book, chapter): | 106 | 106 |
| 107 | 107 | footnote_text = body.xpath('//li[@id="%s"]' % footnote_id)[0] | 108 | 108 |
| 109 | | - a.attrib['epub:type'] = 'noteref' | | 109 | + a.attrib['{%s}type' % epub.NAMESPACES['EPUB']] = 'noteref' | 110 | 110 | ftn = etree.SubElement(body, 'aside', {'id': footnote_id}) | 111 | | - ftn.attrib['epub:type'] = 'footnote' | | 111 | + ftn.attrib['{%s}type' % epub.NAMESPACES['EPUB']] = 'footnote' | 112 | 112 | ftn_p = etree.SubElement(ftn, 'p') | 113 | 113 | ftn_p.text = footnote_text.text | 114 | 114 |
| |
Collapse file Copy file name to clipboardExpand all lines: ebooklib/utils.py +38Lines changed: 38 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,3 +58,41 @@ def guess_type(extenstion): | 58 | 58 | mimetype_initialised = True | 59 | 59 |
| 60 | 60 | return mimetypes.guess_type(extenstion) | | 61 | + | | 62 | + | | 63 | +def create_pagebreak(pageref, label=None, html=True): | | 64 | + from ebooklib.epub import NAMESPACES | | 65 | + | | 66 | + pageref_attributes = { | | 67 | + '{%s}type' % NAMESPACES['EPUB']: 'pagebreak', | | 68 | + 'title': u'{}'.format(pageref), | | 69 | + 'id': u'{}'.format(pageref), | | 70 | + } | | 71 | + | | 72 | + pageref_elem = etree.Element('span', pageref_attributes, nsmap={'epub': NAMESPACES['EPUB']}) | | 73 | + | | 74 | + if label: | | 75 | + pageref_elem.text = label | | 76 | + | | 77 | + if html: | | 78 | + return etree.tostring(pageref_elem, encoding='unicode') | | 79 | + | | 80 | + return pageref_elem | | 81 | + | | 82 | + | | 83 | +def get_pages(item): | | 84 | + body = parse_html_string(item.get_body_content()) | | 85 | + pages = [] | | 86 | + | | 87 | + for elem in body.iter(): | | 88 | + if 'epub:type' in elem.attrib: | | 89 | + pages.append((item.get_name(), elem.get('id'), elem.text or elem.get('id'))) | | 90 | + | | 91 | + return pages | | 92 | + | | 93 | + | | 94 | +def get_pages_for_items(items): | | 95 | + | | 96 | + pages_from_docs = [get_pages(item) for item in items] | | 97 | + | | 98 | + return [item for pages in pages_from_docs for item in pages] |
Collapse file Copy file name to clipboard +8Lines changed: 8 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +Advanced create | | 2 | +=============== | | 3 | + | | 4 | +Create simple EPUB3 with both visible and invisible pagebreaks | | 5 | + | | 6 | +## Start | | 7 | + | | 8 | + python create.py |
Collapse file Copy file name to clipboard +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# coding=utf-8 | | 2 | + | | 3 | +from ebooklib import epub | | 4 | +from ebooklib.plugins import standard | | 5 | +from ebooklib.utils import create_pagebreak | | 6 | + | | 7 | + | | 8 | +if __name__ == '__main__': | | 9 | + book = epub.EpubBook() | | 10 | + | | 11 | + # add metadata | | 12 | + book.set_identifier('sample123456') | | 13 | + book.set_title('Sample book') | | 14 | + book.set_language('en') | | 15 | + | | 16 | + book.add_author('Aleksandar Erkalovic') | | 17 | + | | 18 | + # build the chapter HTML and add the page break | | 19 | + c1 = epub.EpubHtml(title='Introduction', file_name='intro.xhtml', lang='en') | | 20 | + c1.content = u'<h1>Introduction</h1><p><span id="page1" epub:type="pagebreak">1</span>This chapter has a visible page number.</p><p><span id="page2" epub:type="pagebreak">2</span>Something else now.</p>' | | 21 | + | | 22 | + c2 = epub.EpubHtml(title='Chapter the Second', file_name='chap02.xhtml', lang='en') | | 23 | + c2.content = u'<html><head></head><body><h1>Chapter the Second</h1><p>This chapter has two page breaks, both with invisible page numbers.</p>' | | 24 | + | | 25 | + # Add invisible page numbers that match the printed text, for accessibility | | 26 | + c2.content += create_pagebreak("2") | | 27 | + | | 28 | + # You can add more content after the page break | | 29 | + c2.content += u'<p>This is the second page in the second chapter, after the invisible page break.</p>' | | 30 | + | | 31 | + # Add invisible page numbers that match the printed text, for accessibility | | 32 | + c2.content += create_pagebreak("3", label="Page 3") | | 33 | + | | 34 | + # close the chapter | | 35 | + c2.content += u'</body></html>' | | 36 | + | | 37 | + # add chapters to the book | | 38 | + book.add_item(c1) | | 39 | + book.add_item(c2) | | 40 | + | | 41 | + # create table of contents | | 42 | + # - add manual link | | 43 | + # - add section | | 44 | + # - add auto created links to chapters | | 45 | + | | 46 | + book.toc = ((c1, c2, )) | | 47 | + | | 48 | + # add navigation files | | 49 | + book.add_item(epub.EpubNcx()) | | 50 | + book.add_item(epub.EpubNav()) | | 51 | + | | 52 | + # create spine | | 53 | + book.spine = ['nav', c1, c2, ] | | 54 | + | | 55 | + # create epub file | | 56 | + opts = {'plugins': [standard.SyntaxPlugin()]} | | 57 | + epub.write_epub('test.epub', book, opts) | | 58 | + |
|