Generate external TOC entries from links in root HTML file.

This commit is contained in:
Kovid Goyal 2007-05-02 21:30:17 +00:00
parent 21af20d036
commit 1a748bc574
3 changed files with 19 additions and 9 deletions

View File

@ -33,7 +33,7 @@ You may have to adjust the GROUP and the location of the rules file to
suit your distribution.
"""
__version__ = "0.3.18"
__version__ = "0.3.19"
__docformat__ = "epytext"
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>"

View File

@ -31,7 +31,8 @@ from libprs500.lrf.html.BeautifulSoup import BeautifulSoup, Comment, Tag, \
NavigableString, Declaration, ProcessingInstruction
from libprs500.lrf.pylrs.pylrs import Paragraph, CR, Italic, ImageStream, TextBlock, \
ImageBlock, JumpButton, CharButton, BlockStyle,\
Page, Bold, Space, Plot, TextStyle, Image, BlockSpace
Page, Bold, Space, Plot, TextStyle, Image, BlockSpace,\
TableOfContents
from libprs500.lrf.pylrs.pylrs import Span as _Span
from libprs500.lrf import ConversionError, option_parser, Book
from libprs500 import extract
@ -241,7 +242,8 @@ class HTMLConverter(object):
def __init__(self, book, path, width=575, height=747,
font_delta=0, verbose=False, cover=None,
max_link_levels=sys.maxint, link_level=0):
max_link_levels=sys.maxint, link_level=0,
is_root=True):
'''
Convert HTML file at C{path} and add it to C{book}. After creating
the object, you must call L{self.process_links} on it to create the links and
@ -265,6 +267,8 @@ class HTMLConverter(object):
@type max_link_levels: C{int}
@param link_level: Current link level
@type link_level: C{int}
@param is_root: True iff this object is converting the root HTML file
@type is_root: C{bool}
'''
self.page_width = width #: The width of the page
self.page_height = height #: The height of the page
@ -280,6 +284,7 @@ class HTMLConverter(object):
self.memory = [] #: Used to ensure that duplicate CSS unhandled erros are not reported
self.in_ol = False #: Flag indicating we're in an <ol> element
self.book = book #: The Book object representing a BBeB book
self.is_root = is_root #: Are we converting the root HTML file
path = os.path.abspath(path)
os.chdir(os.path.dirname(path))
self.file_name = os.path.basename(path)
@ -420,6 +425,8 @@ class HTMLConverter(object):
if not path or os.path.basename(path) == self.file_name:
if fragment in self.targets.keys():
tb = self.targets[fragment]
if self.is_root:
self.book.addTocEntry(self.get_text(tag), tb)
sys.stdout.flush()
jb = JumpButton(tb)
self.book.append(jb)
@ -437,7 +444,8 @@ class HTMLConverter(object):
self.files[path] = HTMLConverter(self.book, path,
font_delta=self.font_delta, verbose=self.verbose,
link_level=self.link_level+1,
max_link_levels=self.max_link_levels)
max_link_levels=self.max_link_levels,
is_root = False)
HTMLConverter.processed_files[path] = self.files[path]
except Exception, err:
print >>sys.stderr, 'Unable to process', path, err
@ -453,6 +461,8 @@ class HTMLConverter(object):
tb = conv.targets[fragment]
else:
tb = conv.top
if self.is_root:
self.book.addTocEntry(self.get_text(tag), tb)
jb = JumpButton(tb)
self.book.append(jb)
cb = CharButton(jb, text=self.get_text(tag))

View File

@ -622,8 +622,8 @@ class TableOfContents(object):
def addTocEntry(self, tocLabel, textBlock):
if not isinstance(textBlock, TextBlock) and not isinstance(textBlock, ImageBlock):
raise LrsError, "TOC destination must be a TextBlock or an ImageBlock"
if not isinstance(textBlock, (TextBlock, ImageBlock, BlockSpace)):
raise LrsError, "TOC destination must be a TextBlock, ImageBlock or BlockSpace"
if textBlock.parent is None or not isinstance(textBlock.parent, Page):
raise LrsError, "TOC text block must be already appended to a page"