GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — page lists are now attached to each EpubHtml item as well as to the … · 06linux/ebooklib@fe88934

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • AUTHORS.txt
  • ebooklib
    • epub.py
    • plugins
      • booktype.py
    • utils.py
  • samples/07_pagebreaks
    • README.md
    • create.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎AUTHORS.txt‎

Copy file name to clipboardExpand all lines: AUTHORS.txt
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ Olivier Le Thanh Duong <[email protected]>
2121
Michael Storm <[email protected]>
2222
Sergey Markelov
2323
Laurent Laporte <[email protected]>
24-
Deborah Kaplan
24+
Deborah Kaplan <[email protected]>
Collapse file

‎ebooklib/epub.py‎

Copy file name to clipboardExpand all lines: ebooklib/epub.py
+49-50Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import ebooklib
3333

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
3535

3636

3737
# Version of EPUB library
@@ -254,6 +254,7 @@ def __init__(self, uid=None, file_name='', media_type='', content=None, title=''
254254

255255
self.links = []
256256
self.properties = []
257+
self.pages = []
257258

258259
def is_chapter(self):
259260
"""
@@ -317,32 +318,6 @@ def get_links_of_type(self, link_type):
317318
"""
318319
return (link for link in self.links if link.get('type', '') == link_type)
319320

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-
346321
def add_item(self, item):
347322
"""
348323
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):
733708
if isinstance(item, EpubHtml):
734709
item.id = 'chapter_%d' % self._id_html
735710
self._id_html += 1
711+
# If there's a page list, append it to the book's page list
712+
self.pages += item.pages
736713
elif isinstance(item, EpubImage):
737714
item.id = 'image_%d' % self._id_image
738715
self._id_image += 1
@@ -1204,33 +1181,39 @@ def _create_section(itm, items):
12041181
a_item.text = _title
12051182

12061183
# 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+
)
12211202

1222-
pages_ol = etree.SubElement(pagelist_nav, 'ol')
1203+
pages_ol = etree.SubElement(pagelist_nav, 'ol')
12231204

1224-
for filename, pageref, label in self.book.pages:
1225-
li_item = etree.SubElement(pages_ol, 'li')
12261205

1227-
_href = u'{}#{}'.format(filename, pageref)
1228-
_title = label
12291206

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
12341217

12351218
tree_str = etree.tostring(nav_xml, pretty_print=True, encoding='utf-8', xml_declaration=True)
12361219

@@ -1598,8 +1581,24 @@ def parse_list(list_node):
15981581
if navtype == 'toc':
15991582
self.book.toc = parse_list(nav_node.find('ol'))
16001583
elif nav_node is not None:
1584+
# generate the pages list if there is one
16011585
self.book.pages = parse_list(nav_node.find('ol'))
16021586

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+
16031602
def _load_spine(self):
16041603
spine = self.container.find('{%s}%s' % (NAMESPACES['OPF'], 'spine'))
16051604

Collapse file

‎ebooklib/plugins/booktype.py‎

Copy file name to clipboardExpand all lines: ebooklib/plugins/booktype.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def html_before_write(self, book, chapter):
5151
if _link.get('href', '').find('InsertNoteID') != -1:
5252
_ln = _link.get('href', '')
5353
i = _ln.find('#')
54-
_link.set('href', _ln[i:]);
54+
_link.set('href', _ln[i:])
5555

5656
continue
5757

@@ -106,9 +106,9 @@ def html_before_write(self, book, chapter):
106106

107107
footnote_text = body.xpath('//li[@id="%s"]' % footnote_id)[0]
108108

109-
a.attrib['epub:type'] = 'noteref'
109+
a.attrib['{%s}type' % epub.NAMESPACES['EPUB']] = 'noteref'
110110
ftn = etree.SubElement(body, 'aside', {'id': footnote_id})
111-
ftn.attrib['epub:type'] = 'footnote'
111+
ftn.attrib['{%s}type' % epub.NAMESPACES['EPUB']] = 'footnote'
112112
ftn_p = etree.SubElement(ftn, 'p')
113113
ftn_p.text = footnote_text.text
114114

Collapse file

‎ebooklib/utils.py‎

Copy file name to clipboardExpand all lines: ebooklib/utils.py
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,41 @@ def guess_type(extenstion):
5858
mimetype_initialised = True
5959

6060
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

‎samples/07_pagebreaks/README.md‎

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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

‎samples/07_pagebreaks/create.py‎

Copy file name to clipboard
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.