Refactor ToC generation code to make it re-useable

This commit is contained in:
Kovid Goyal 2014-11-20 11:22:29 +05:30
parent 1cf54a9253
commit cf42295fc5

View File

@ -495,22 +495,7 @@ def find_inline_toc(container):
if container.parsed(name).xpath('//*[local-name()="body" and @id="calibre_generated_inline_toc"]'): if container.parsed(name).xpath('//*[local-name()="body" and @id="calibre_generated_inline_toc"]'):
return name return name
def create_inline_toc(container, title=None): def toc_to_html(toc, container, toc_name, title, lang=None):
'''
Create an inline (HTML) Table of Contents from an existing NCX table of contents.
:param title: The title for this table of contents.
'''
lang = get_book_language(container)
default_title = 'Table of Contents'
if lang:
lang = lang_as_iso639_1(lang) or lang
default_title = translate(lang, default_title)
title = title or default_title
toc = get_toc(container)
if len(toc) == 0:
return None
toc_name = find_inline_toc(container)
def process_node(html_parent, toc, level=1, indent=' ', style_level=2): def process_node(html_parent, toc, level=1, indent=' ', style_level=2):
li = html_parent.makeelement(XHTML('li')) li = html_parent.makeelement(XHTML('li'))
@ -549,7 +534,6 @@ def create_inline_toc(container, title=None):
) )
) )
name = toc_name
ul = html[1][1] ul = html[1][1]
ul.set('class', 'level1') ul.set('class', 'level1')
for child in toc: for child in toc:
@ -557,6 +541,27 @@ def create_inline_toc(container, title=None):
if lang: if lang:
html.set('lang', lang) html.set('lang', lang)
pretty_html_tree(container, html) pretty_html_tree(container, html)
return html
def create_inline_toc(container, title=None):
'''
Create an inline (HTML) Table of Contents from an existing NCX table of contents.
:param title: The title for this table of contents.
'''
lang = get_book_language(container)
default_title = 'Table of Contents'
if lang:
lang = lang_as_iso639_1(lang) or lang
default_title = translate(lang, default_title)
title = title or default_title
toc = get_toc(container)
if len(toc) == 0:
return None
toc_name = find_inline_toc(container)
name = toc_name
html = toc_to_html(toc, container, name, title, lang)
raw = serialize(html, 'text/html') raw = serialize(html, 'text/html')
if name is None: if name is None:
name, c = 'toc.xhtml', 0 name, c = 'toc.xhtml', 0