PDF Output: Fix using __SECTION__ in header and footer templates resolving to the inscorrect section if a page with no sections follows a page with multiple sections. Fixes #1295236 [[Conversion HTML-> PDF] PDF header/footer uses wrong _SECTION_ when a single page contains more than one section](https://bugs.launchpad.net/calibre/+bug/1295236)

This commit is contained in:
Kovid Goyal 2014-03-22 18:17:09 +05:30
parent 1eb1a32254
commit 34c1e4c24f

View File

@ -10,6 +10,7 @@ __docformat__ = 'restructuredtext en'
import json, os import json, os
from future_builtins import map from future_builtins import map
from math import floor from math import floor
from collections import defaultdict
from PyQt4.Qt import (QObject, QPainter, Qt, QSize, QString, QTimer, from PyQt4.Qt import (QObject, QPainter, Qt, QSize, QString, QTimer,
pyqtProperty, QEventLoop, QPixmap, QRect, pyqtSlot) pyqtProperty, QEventLoop, QPixmap, QRect, pyqtSlot)
@ -310,7 +311,7 @@ class PDFWriter(QObject):
evaljs('document.getElementById("MathJax_Message").style.display="none";') evaljs('document.getElementById("MathJax_Message").style.display="none";')
def get_sections(self, anchor_map): def get_sections(self, anchor_map):
sections = {} sections = defaultdict(list)
ci = os.path.abspath(os.path.normcase(self.current_item)) ci = os.path.abspath(os.path.normcase(self.current_item))
if self.toc is not None: if self.toc is not None:
for toc in self.toc.flat(): for toc in self.toc.flat():
@ -323,8 +324,7 @@ class PDFWriter(QObject):
col = 0 col = 0
if frag and frag in anchor_map: if frag and frag in anchor_map:
col = anchor_map[frag]['column'] col = anchor_map[frag]['column']
if col not in sections: sections[col].append(toc.text or _('Untitled'))
sections[col] = toc.text or _('Untitled')
return sections return sections
@ -380,7 +380,11 @@ class PDFWriter(QObject):
mf = self.view.page().mainFrame() mf = self.view.page().mainFrame()
while True: while True:
if col in sections: if col in sections:
self.current_section = sections[col] self.current_section = sections[col][0]
elif col - 1 in sections:
# Ensure we are using the last section on the previous page as
# the section for this page, since this page has no sections
self.current_section = sections[col][-1]
self.doc.init_page() self.doc.init_page()
if self.header or self.footer: if self.header or self.footer:
evaljs('paged_display.update_header_footer(%d)'%self.current_page_num) evaljs('paged_display.update_header_footer(%d)'%self.current_page_num)