text stringlengths 0 14.1k |
|---|
# |
# Permission to use, copy, modify, and/or distribute this software for any |
# purpose with or without fee is hereby granted, provided that the above |
# copyright notice and this permission notice appear in all copies. |
# |
# THE SOFTWARE IS PROVIDED ""AS IS"" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
# PERFORMANCE OF THIS SOFTWARE. |
# The epub format specification is available at http://idpf.org/epub/201 |
'''Contains the EpubBuilder class to build epub2.0.1 files with the getebook |
module.''' |
import html |
import re |
import datetime |
import getebook |
import os.path |
import re |
import zipfile |
__all__ = ['EpubBuilder', 'EpubTOC', 'Author'] |
def _normalize(name): |
'''Transform ""Firstname [Middlenames] Lastname"" into |
""Lastname, Firstname [Middlenames]"".''' |
split = name.split() |
if len(split) == 1: |
return name |
return split[-1] + ', ' + ' '.join(name[0:-1]) |
def _make_starttag(tag, attrs): |
'Write a starttag.' |
out = '<' + tag |
for key in attrs: |
out += ' {}=""{}""'.format(key, html.escape(attrs[key])) |
out += '>' |
return out |
def _make_xml_elem(tag, text, attr = []): |
'Write a flat xml element.' |
out = ' <' + tag |
for (key, val) in attr: |
out += ' {}=""{}""'.format(key, val) |
if text: |
out += '>{}</{}>\n'.format(text, tag) |
else: |
out += ' />\n' |
return out |
class EpubTOC(getebook.TOC): |
'Table of contents.' |
_head = (( |
'<?xml version=""1.0"" encoding=""UTF-8""?>\n' |
'<ncx xmlns=""http://www.daisy.org/z3986/2005/ncx/"" version=""2005-1"" xml:lang=""en-US"">\n' |
' <head>\n' |
' <meta name=""dtb:uid"" content=""{}"" />\n' |
' <meta name=""dtb:depth"" content=""{}"" />\n' |
' <meta name=""dtb:totalPageCount"" content=""0"" />\n' |
' <meta name=""dtb:maxPageNumber"" content=""0"" />\n' |
' </head>\n' |
' <docTitle>\n' |
' <text>{}</text>\n' |
' </docTitle>\n' |
)) |
_doc_author = (( |
' <docAuthor>\n' |
' <text>{}</text>\n' |
' </docAuthor>\n' |
)) |
_navp = (( |
'{0}<navPoint id=""nav{1}"">\n' |
'{0} <navLabel>\n' |
'{0} <text>{2}</text>\n' |
'{0} </navLabel>\n' |
'{0} <content src=""{3}"" />\n' |
)) |
def _navp_xml(self, entry, indent_lvl): |
'Write xml for an entry and all its subentries.' |
xml = self._navp.format(' '*indent_lvl, str(entry.no), entry.text, |
entry.target) |
for sub in entry.entries: |
xml += self._navp_xml(sub, indent_lvl+1) |
xml += ' '*indent_lvl + '</navPoint>\n' |
return xml |
def write_xml(self, uid, title, authors): |
'Write the xml code for the table of contents.' |
xml = self._head.format(uid, self.max_depth, title) |
for aut in authors: |
xml += self._doc_author.format(aut) |
xml += ' <navMap>\n' |
for entry in self.entries: |
xml += self._navp_xml(entry, 2) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.