Implement --use-spine

This commit is contained in:
Kovid Goyal 2007-10-13 03:22:52 +00:00
parent bea1ea2ef5
commit 79d6077250
4 changed files with 24 additions and 10 deletions

View File

@ -127,6 +127,8 @@ def option_parser(usage):
help='Set the format of the header. %a is replaced by the author and %t by the title. Default is %default') help='Set the format of the header. %a is replaced by the author and %t by the title. Default is %default')
laf.add_option('--override-css', default=None, dest='_override_css', type='string', laf.add_option('--override-css', default=None, dest='_override_css', type='string',
help='Override the CSS. Can be either a path to a CSS stylesheet or a string. If it is a string it is interpreted as CSS.') help='Override the CSS. Can be either a path to a CSS stylesheet or a string. If it is a string it is interpreted as CSS.')
laf.add_option('--use-spine', default=False, dest='use_spine', action='store_true',
help='Use the <spine> element from the OPF file to determine the order in which the HTML files are appended to the LRF. The .opf file must be in the same directory as the base HTML file.')
page = parser.add_option_group('PAGE OPTIONS') page = parser.add_option_group('PAGE OPTIONS')
profiles = profile_map.keys() profiles = profile_map.keys()

View File

@ -56,7 +56,7 @@ def update_css(ncss, ocss):
ocss[key] = ncss[key] ocss[key] = ncss[key]
def munge_paths(basepath, url): def munge_paths(basepath, url):
purl = urlparse(url,) purl = urlparse(unquote(url),)
path, fragment = purl[2], purl[5] path, fragment = purl[2], purl[5]
if not path: if not path:
path = basepath path = basepath
@ -547,15 +547,14 @@ class HTMLConverter(object):
while len(self.links) > 0: while len(self.links) > 0:
link = self.links.popleft() link = self.links.popleft()
para, text, path, fragment = link['para'], link['text'], link['path'], link['fragment'] para, text, path, fragment = link['para'], link['text'], link['path'], link['fragment']
# Needed for TOC entries due to bug in LRF ascii_text = text.encode('ascii', 'ignore') # Needed for TOC entries due to bug in SONY LRF renderer
ascii_text = text.encode('ascii', 'ignore')
if path in self.processed_files: if path in self.processed_files:
if path+fragment in self.targets.keys(): if path+fragment in self.targets.keys():
tb = get_target_block(path+fragment, self.targets) tb = get_target_block(path+fragment, self.targets)
else: else:
tb = self.tops[path] tb = self.tops[path]
if self.link_level == 0 and len(self.base_files) == 1: if self.link_level == 0 and self.use_spine:
add_toc_entry(ascii_text, tb) add_toc_entry(ascii_text, tb)
jb = JumpButton(tb) jb = JumpButton(tb)
self.book.append(jb) self.book.append(jb)
@ -571,7 +570,12 @@ class HTMLConverter(object):
return outside_links return outside_links
def create_toc(self, toc):
for (path, txt) in toc:
if path in self.tops:
ascii_text = txt.encode('ascii', 'ignore') # Bug in SONY LRF renderer
self.book.addTocEntry(ascii_text, self.tops[path])
def end_page(self): def end_page(self):
""" """
End the current page, ensuring that any further content is displayed End the current page, ensuring that any further content is displayed
@ -1608,7 +1612,10 @@ def process_file(path, options, logger=None):
re.compile(fpba[2], re.IGNORECASE)] re.compile(fpba[2], re.IGNORECASE)]
if not hasattr(options, 'anchor_ids'): if not hasattr(options, 'anchor_ids'):
options.anchor_ids = True options.anchor_ids = True
conv = HTMLConverter(book, fonts, options, logger, [path]) files = options.spine if options.use_spine else [path]
conv = HTMLConverter(book, fonts, options, logger, files)
if options.use_spine:
conv.create_toc(options.toc)
oname = options.output oname = options.output
if not oname: if not oname:
suffix = '.lrs' if options.lrs else '.lrf' suffix = '.lrs' if options.lrs else '.lrf'
@ -1627,7 +1634,7 @@ def try_opf(path, options, logger):
opf = glob.glob(os.path.join(os.path.dirname(path),'*.opf'))[0] opf = glob.glob(os.path.join(os.path.dirname(path),'*.opf'))[0]
except IndexError: except IndexError:
return return
opf = OPFReader(open(opf, 'rb')) opf = OPFReader(open(opf, 'rb'), os.path.dirname(os.path.abspath(opf)))
try: try:
title = opf.title title = opf.title
if title and not options.title: if title and not options.title:
@ -1667,8 +1674,12 @@ def try_opf(path, options, logger):
break break
except: except:
continue continue
options.spine = [i.href for i in opf.spine.items()]
options.toc = opf.toc
except Exception: except Exception:
logger.exception('Failed to process opf file') logger.exception('Failed to process opf file')
def option_parser(): def option_parser():
return lrf_option_parser('''Usage: %prog [options] mybook.html\n\n''' return lrf_option_parser('''Usage: %prog [options] mybook.html\n\n'''

View File

@ -22,7 +22,7 @@ from libprs500.ebooks.lrf.html.convert_from import process_file as html_process_
from libprs500 import isosx, __appname__, setup_cli_handlers, iswindows from libprs500 import isosx, __appname__, setup_cli_handlers, iswindows
CLIT = 'clit' CLIT = 'clit'
if isosx and hasattr(sys, 'frameworks_dir'): if isosx and hasattr(sys, 'frameworks_dir'):
CLIT = os.path.join(sys.frameworks_dir, CLIT) CLIT = os.path.join(getattr(sys, 'frameworks_dir'), CLIT)
def option_parser(): def option_parser():
return lrf_option_parser( return lrf_option_parser(
@ -78,7 +78,8 @@ def process_file(path, options, logger=None):
ext = '.lrs' if options.lrs else '.lrf' ext = '.lrs' if options.lrs else '.lrf'
options.output = os.path.abspath(os.path.basename(os.path.splitext(path)[0]) + ext) options.output = os.path.abspath(os.path.basename(os.path.splitext(path)[0]) + ext)
options.output = os.path.abspath(os.path.expanduser(options.output)) options.output = os.path.abspath(os.path.expanduser(options.output))
options.minimum_indent = 100 options.minimum_indent = 100
options.use_spine = True
html_process_file(htmlfile, options, logger=logger) html_process_file(htmlfile, options, logger=logger)
finally: finally:
shutil.rmtree(tdir) shutil.rmtree(tdir)

View File

@ -19,7 +19,7 @@ from urllib import unquote
from urlparse import urlparse from urlparse import urlparse
from libprs500.ebooks.metadata import MetaInformation from libprs500.ebooks.metadata import MetaInformation
from libprs500.ebooks.BeautifulSoup import BeautifulStoneSoup, BeautifulSoup, NavigableString from libprs500.ebooks.BeautifulSoup import BeautifulStoneSoup, BeautifulSoup
from libprs500.ebooks.lrf import entity_to_unicode from libprs500.ebooks.lrf import entity_to_unicode
class ManifestItem(object): class ManifestItem(object):