diff --git a/src/libprs500/ebooks/lrf/__init__.py b/src/libprs500/ebooks/lrf/__init__.py index dfba327321..9378517c7a 100644 --- a/src/libprs500/ebooks/lrf/__init__.py +++ b/src/libprs500/ebooks/lrf/__init__.py @@ -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') 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.') + laf.add_option('--use-spine', default=False, dest='use_spine', action='store_true', + help='Use the 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') profiles = profile_map.keys() diff --git a/src/libprs500/ebooks/lrf/html/convert_from.py b/src/libprs500/ebooks/lrf/html/convert_from.py index f45276c127..63f3eca074 100644 --- a/src/libprs500/ebooks/lrf/html/convert_from.py +++ b/src/libprs500/ebooks/lrf/html/convert_from.py @@ -56,7 +56,7 @@ def update_css(ncss, ocss): ocss[key] = ncss[key] def munge_paths(basepath, url): - purl = urlparse(url,) + purl = urlparse(unquote(url),) path, fragment = purl[2], purl[5] if not path: path = basepath @@ -547,15 +547,14 @@ class HTMLConverter(object): while len(self.links) > 0: link = self.links.popleft() 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') + ascii_text = text.encode('ascii', 'ignore') # Needed for TOC entries due to bug in SONY LRF renderer if path in self.processed_files: if path+fragment in self.targets.keys(): tb = get_target_block(path+fragment, self.targets) else: 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) jb = JumpButton(tb) self.book.append(jb) @@ -571,7 +570,12 @@ class HTMLConverter(object): 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): """ 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)] if not hasattr(options, 'anchor_ids'): 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 if not oname: 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] except IndexError: return - opf = OPFReader(open(opf, 'rb')) + opf = OPFReader(open(opf, 'rb'), os.path.dirname(os.path.abspath(opf))) try: title = opf.title if title and not options.title: @@ -1667,8 +1674,12 @@ def try_opf(path, options, logger): break except: continue + options.spine = [i.href for i in opf.spine.items()] + options.toc = opf.toc except Exception: logger.exception('Failed to process opf file') + + def option_parser(): return lrf_option_parser('''Usage: %prog [options] mybook.html\n\n''' diff --git a/src/libprs500/ebooks/lrf/lit/convert_from.py b/src/libprs500/ebooks/lrf/lit/convert_from.py index e7126ae461..c39aa2161a 100644 --- a/src/libprs500/ebooks/lrf/lit/convert_from.py +++ b/src/libprs500/ebooks/lrf/lit/convert_from.py @@ -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 CLIT = 'clit' 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(): return lrf_option_parser( @@ -78,7 +78,8 @@ def process_file(path, options, logger=None): 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.expanduser(options.output)) - options.minimum_indent = 100 + options.minimum_indent = 100 + options.use_spine = True html_process_file(htmlfile, options, logger=logger) finally: shutil.rmtree(tdir) diff --git a/src/libprs500/ebooks/metadata/opf.py b/src/libprs500/ebooks/metadata/opf.py index fec4457baf..0b65b94962 100644 --- a/src/libprs500/ebooks/metadata/opf.py +++ b/src/libprs500/ebooks/metadata/opf.py @@ -19,7 +19,7 @@ from urllib import unquote from urlparse import urlparse 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 class ManifestItem(object):