mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Generate external TOC entries from links in root HTML file.
This commit is contained in:
parent
21af20d036
commit
1a748bc574
@ -33,7 +33,7 @@ You may have to adjust the GROUP and the location of the rules file to
|
|||||||
suit your distribution.
|
suit your distribution.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = "0.3.18"
|
__version__ = "0.3.19"
|
||||||
__docformat__ = "epytext"
|
__docformat__ = "epytext"
|
||||||
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>"
|
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>"
|
||||||
|
|
||||||
|
@ -31,7 +31,8 @@ from libprs500.lrf.html.BeautifulSoup import BeautifulSoup, Comment, Tag, \
|
|||||||
NavigableString, Declaration, ProcessingInstruction
|
NavigableString, Declaration, ProcessingInstruction
|
||||||
from libprs500.lrf.pylrs.pylrs import Paragraph, CR, Italic, ImageStream, TextBlock, \
|
from libprs500.lrf.pylrs.pylrs import Paragraph, CR, Italic, ImageStream, TextBlock, \
|
||||||
ImageBlock, JumpButton, CharButton, BlockStyle,\
|
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.pylrs.pylrs import Span as _Span
|
||||||
from libprs500.lrf import ConversionError, option_parser, Book
|
from libprs500.lrf import ConversionError, option_parser, Book
|
||||||
from libprs500 import extract
|
from libprs500 import extract
|
||||||
@ -241,7 +242,8 @@ class HTMLConverter(object):
|
|||||||
|
|
||||||
def __init__(self, book, path, width=575, height=747,
|
def __init__(self, book, path, width=575, height=747,
|
||||||
font_delta=0, verbose=False, cover=None,
|
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
|
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
|
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}
|
@type max_link_levels: C{int}
|
||||||
@param link_level: Current link level
|
@param link_level: Current link level
|
||||||
@type link_level: C{int}
|
@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_width = width #: The width of the page
|
||||||
self.page_height = height #: The height 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.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.in_ol = False #: Flag indicating we're in an <ol> element
|
||||||
self.book = book #: The Book object representing a BBeB book
|
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)
|
path = os.path.abspath(path)
|
||||||
os.chdir(os.path.dirname(path))
|
os.chdir(os.path.dirname(path))
|
||||||
self.file_name = os.path.basename(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 not path or os.path.basename(path) == self.file_name:
|
||||||
if fragment in self.targets.keys():
|
if fragment in self.targets.keys():
|
||||||
tb = self.targets[fragment]
|
tb = self.targets[fragment]
|
||||||
|
if self.is_root:
|
||||||
|
self.book.addTocEntry(self.get_text(tag), tb)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
jb = JumpButton(tb)
|
jb = JumpButton(tb)
|
||||||
self.book.append(jb)
|
self.book.append(jb)
|
||||||
@ -437,7 +444,8 @@ class HTMLConverter(object):
|
|||||||
self.files[path] = HTMLConverter(self.book, path,
|
self.files[path] = HTMLConverter(self.book, path,
|
||||||
font_delta=self.font_delta, verbose=self.verbose,
|
font_delta=self.font_delta, verbose=self.verbose,
|
||||||
link_level=self.link_level+1,
|
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]
|
HTMLConverter.processed_files[path] = self.files[path]
|
||||||
except Exception, err:
|
except Exception, err:
|
||||||
print >>sys.stderr, 'Unable to process', path, err
|
print >>sys.stderr, 'Unable to process', path, err
|
||||||
@ -453,6 +461,8 @@ class HTMLConverter(object):
|
|||||||
tb = conv.targets[fragment]
|
tb = conv.targets[fragment]
|
||||||
else:
|
else:
|
||||||
tb = conv.top
|
tb = conv.top
|
||||||
|
if self.is_root:
|
||||||
|
self.book.addTocEntry(self.get_text(tag), tb)
|
||||||
jb = JumpButton(tb)
|
jb = JumpButton(tb)
|
||||||
self.book.append(jb)
|
self.book.append(jb)
|
||||||
cb = CharButton(jb, text=self.get_text(tag))
|
cb = CharButton(jb, text=self.get_text(tag))
|
||||||
|
@ -622,8 +622,8 @@ class TableOfContents(object):
|
|||||||
|
|
||||||
|
|
||||||
def addTocEntry(self, tocLabel, textBlock):
|
def addTocEntry(self, tocLabel, textBlock):
|
||||||
if not isinstance(textBlock, TextBlock) and not isinstance(textBlock, ImageBlock):
|
if not isinstance(textBlock, (TextBlock, ImageBlock, BlockSpace)):
|
||||||
raise LrsError, "TOC destination must be a TextBlock or an ImageBlock"
|
raise LrsError, "TOC destination must be a TextBlock, ImageBlock or BlockSpace"
|
||||||
|
|
||||||
if textBlock.parent is None or not isinstance(textBlock.parent, Page):
|
if textBlock.parent is None or not isinstance(textBlock.parent, Page):
|
||||||
raise LrsError, "TOC text block must be already appended to a page"
|
raise LrsError, "TOC text block must be already appended to a page"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user