mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
[SNBOutput] Improve TOC handling. If an spice is not referenced in TOC, it will be appended to the last TOC item.
This commit is contained in:
parent
e72c3ce0f8
commit
081c5385f2
@ -131,6 +131,10 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
'Creating a default TOC')
|
'Creating a default TOC')
|
||||||
first = iter(oeb_book.spine).next()
|
first = iter(oeb_book.spine).next()
|
||||||
oeb_book.toc.add(_('Start'), first.href)
|
oeb_book.toc.add(_('Start'), first.href)
|
||||||
|
else:
|
||||||
|
first = iter(oeb_book.spine).next()
|
||||||
|
if oeb_book.toc[0].href != first.href:
|
||||||
|
oeb_book.toc.add(_('Start'), first.href)
|
||||||
|
|
||||||
for tocitem in oeb_book.toc:
|
for tocitem in oeb_book.toc:
|
||||||
if tocitem.href.find('#') != -1:
|
if tocitem.href.find('#') != -1:
|
||||||
@ -166,22 +170,49 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
tocInfoFile.close()
|
tocInfoFile.close()
|
||||||
|
|
||||||
# Output Files
|
# Output Files
|
||||||
|
oldTree = None
|
||||||
|
mergeLast = False
|
||||||
|
lastName = None
|
||||||
for item in s:
|
for item in s:
|
||||||
from calibre.ebooks.oeb.base import OEB_DOCS, OEB_IMAGES, PNG_MIME
|
from calibre.ebooks.oeb.base import OEB_DOCS, OEB_IMAGES, PNG_MIME
|
||||||
if m.hrefs[item.href].media_type in OEB_DOCS:
|
if m.hrefs[item.href].media_type in OEB_DOCS:
|
||||||
if not item.href in outputFiles:
|
if not item.href in outputFiles:
|
||||||
log.debug('Skipping %s because unused in TOC.' % item.href)
|
log.debug('File %s is unused in TOC. Continue in last chapter' % item.href)
|
||||||
continue
|
mergeLast = True
|
||||||
|
else:
|
||||||
|
log.debug('Output the modified chapter again: %s' % lastName)
|
||||||
|
if oldTree != None and mergeLast:
|
||||||
|
outputFile = open(os.path.join(snbcDir, lastName), 'wb')
|
||||||
|
outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
||||||
|
outputFile.close()
|
||||||
|
mergeLast = False
|
||||||
|
|
||||||
log.debug('Converting %s to snbc...' % item.href)
|
log.debug('Converting %s to snbc...' % item.href)
|
||||||
snbwriter = SNBMLizer(log)
|
snbwriter = SNBMLizer(log)
|
||||||
|
snbcTrees = None
|
||||||
|
if not mergeLast:
|
||||||
snbcTrees = snbwriter.extract_content(oeb_book, item, outputFiles[item.href], opts)
|
snbcTrees = snbwriter.extract_content(oeb_book, item, outputFiles[item.href], opts)
|
||||||
for subName in snbcTrees:
|
for subName in snbcTrees:
|
||||||
postfix = ''
|
postfix = ''
|
||||||
if subName != '':
|
if subName != '':
|
||||||
postfix = '_' + subName
|
postfix = '_' + subName
|
||||||
outputFile = open(os.path.join(snbcDir, ProcessFileName(item.href + postfix + ".snbc")), 'wb')
|
lastName = ProcessFileName(item.href + postfix + ".snbc")
|
||||||
outputFile.write(etree.tostring(snbcTrees[subName], pretty_print=True, encoding='utf-8'))
|
oldTree = snbcTrees[subName]
|
||||||
|
outputFile = open(os.path.join(snbcDir, lastName), 'wb')
|
||||||
|
outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
||||||
outputFile.close()
|
outputFile.close()
|
||||||
|
else:
|
||||||
|
log.debug('Merge %s with last TOC item...' % item.href)
|
||||||
|
snbwriter.merge_content(oldTree, oeb_book, item, [('', _("Start"))], opts)
|
||||||
|
|
||||||
|
# Output the last one if needed
|
||||||
|
log.debug('Output the last modified chapter again: %s' % lastName)
|
||||||
|
if oldTree != None and mergeLast:
|
||||||
|
outputFile = open(os.path.join(snbcDir, lastName), 'wb')
|
||||||
|
outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
||||||
|
outputFile.close()
|
||||||
|
mergeLast = False
|
||||||
|
|
||||||
for item in m:
|
for item in m:
|
||||||
if m.hrefs[item.href].media_type in OEB_IMAGES:
|
if m.hrefs[item.href].media_type in OEB_IMAGES:
|
||||||
log.debug('Converting image: %s ...' % item.href)
|
log.debug('Converting image: %s ...' % item.href)
|
||||||
|
@ -70,6 +70,14 @@ class SNBMLizer(object):
|
|||||||
self.subitems = subitems
|
self.subitems = subitems
|
||||||
return self.mlize();
|
return self.mlize();
|
||||||
|
|
||||||
|
def merge_content(self, old_tree, oeb_book, item, subitems, opts):
|
||||||
|
newTrees = self.extract_content(oeb_book, item, subitems, opts)
|
||||||
|
body = old_tree.find(".//body")
|
||||||
|
if body != None:
|
||||||
|
for subName in newTrees:
|
||||||
|
newbody = newTrees[subName].find(".//body")
|
||||||
|
for entity in newbody:
|
||||||
|
body.append(entity)
|
||||||
|
|
||||||
def mlize(self):
|
def mlize(self):
|
||||||
output = [ u'' ]
|
output = [ u'' ]
|
||||||
@ -91,11 +99,12 @@ class SNBMLizer(object):
|
|||||||
line = line.strip(' \t\n\r')
|
line = line.strip(' \t\n\r')
|
||||||
if len(line) != 0:
|
if len(line) != 0:
|
||||||
if line.find(CALIBRE_SNB_IMG_TAG) == 0:
|
if line.find(CALIBRE_SNB_IMG_TAG) == 0:
|
||||||
etree.SubElement(trees[subitem], "img").text = line[len(CALIBRE_SNB_IMG_TAG):]
|
etree.SubElement(trees[subitem].find(".//body"), "img").text = line[len(CALIBRE_SNB_IMG_TAG):]
|
||||||
elif line.find(CALIBRE_SNB_BM_TAG) == 0:
|
elif line.find(CALIBRE_SNB_BM_TAG) == 0:
|
||||||
subitem = line[len(CALIBRE_SNB_BM_TAG):]
|
subitem = line[len(CALIBRE_SNB_BM_TAG):]
|
||||||
else:
|
else:
|
||||||
etree.SubElement(trees[subitem], "text").text = etree.CDATA(unicode(u'\u3000\u3000' + line))
|
etree.SubElement(trees[subitem].find(".//body"), "text").text = \
|
||||||
|
etree.CDATA(unicode(u'\u3000\u3000' + line))
|
||||||
return trees
|
return trees
|
||||||
|
|
||||||
def remove_newlines(self, text):
|
def remove_newlines(self, text):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user