From a380a7a284bc12dbde704a74dfb476dc9aa76759 Mon Sep 17 00:00:00 2001 From: John Schember Date: Thu, 28 Jan 2010 19:08:39 -0500 Subject: [PATCH] Fix bug #4411: Include chapter headins when generating toc from pml files. --- src/calibre/ebooks/pdb/ereader/writer.py | 4 ++- src/calibre/ebooks/pml/pmlconverter.py | 43 +++++++++++++++++++----- src/calibre/ebooks/pml/pmlml.py | 2 +- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/calibre/ebooks/pdb/ereader/writer.py b/src/calibre/ebooks/pdb/ereader/writer.py index a379899af5..a6ee16db15 100644 --- a/src/calibre/ebooks/pdb/ereader/writer.py +++ b/src/calibre/ebooks/pdb/ereader/writer.py @@ -42,7 +42,9 @@ class Writer(FormatWriter): pml = unicode(pmlmlizer.extract_content(oeb_book, self.opts)).encode('cp1252', 'replace') text, text_sizes = self._text(pml) - chapter_index = self._index_item(r'(?s)\\C(?P\d)="(?P.+?)"', pml) + chapter_index = self._index_item(r'(?s)\\C(?P[0-4)="(?P.+?)"', pml) + chapter_index += self.index_item(r'(?s)\\X(?P[0-4])(?P.+?)\\X[0-4]', pml) + chapter_index += self.index_item(r'(?s)\\x(?P.+?)\\x', pml) link_index = self._index_item(r'(?s)\\Q="(?P.+?)"', pml) images = self._images(oeb_book.manifest, pmlmlizer.image_hrefs) metadata = [self._metadata(metadata)] diff --git a/src/calibre/ebooks/pml/pmlconverter.py b/src/calibre/ebooks/pml/pmlconverter.py index aa2ff117a4..356e2679ee 100644 --- a/src/calibre/ebooks/pml/pmlconverter.py +++ b/src/calibre/ebooks/pml/pmlconverter.py @@ -171,6 +171,9 @@ class PML_HTMLizer(object): # &. It will display as & pml = pml.replace('&', '&') + pml = re.sub(r'(?<=\\x)(?P.*?)(?=\\x)', lambda match: '="%s"%s' % (self.strip_pml(match.group('text')), match.group('text')), pml) + pml = re.sub(r'(?<=\\X[0-4])(?P.*?)(?=\\X[0-4])', lambda match: '="%s"%s' % (self.strip_pml(match.group('text')), match.group('text')), pml) + pml = re.sub(r'\\a(?P\d{3})', lambda match: '&#%s;' % match.group('num'), pml) pml = re.sub(r'\\U(?P[0-9a-f]{4})', lambda match: '%s' % my_unichr(int(match.group('num'), 16)), pml) @@ -178,6 +181,19 @@ class PML_HTMLizer(object): return pml + def strip_pml(self, pml): + pml = re.sub(r'\\.\d=""', '', pml) + pml = re.sub(r'\\.=""', '', pml) + pml = re.sub(r'\\.\d', '', pml) + pml = re.sub(r'\\.', '', pml) + pml = re.sub(r'\\a\d\d\d', '', pml) + pml = re.sub(r'\\U\d\d\d\d', '', pml) + pml.replace('\r\n', ' ') + pml.replace('\n', ' ') + pml.replace('\r', ' ') + + return pml + def cleanup_html(self, html): old = html html = self.cleanup_html_remove_redundant(html) @@ -503,9 +519,9 @@ class PML_HTMLizer(object): if c == '\\': c = line.read(1) - if c in 'xqcrtTiIuobBlk': + if c in 'qcrtTiIuobBlk': text = self.process_code(c, line) - elif c in 'FSX': + elif c in 'FS': l = line.read(1) if '%s%s' % (c, l) == 'Fn': text = self.process_code('Fn', line, 'fn') @@ -515,8 +531,24 @@ class PML_HTMLizer(object): text = self.process_code('SB', line) elif '%s%s' % (c, l) == 'Sd': text = self.process_code('Sd', line, 'sb') + elif c in 'xXC': + # The PML was modified eariler so x and X put the text + # inside of ="" so we don't have do special processing + # for C. + t = '' + if c in 'XC': + level = line.read(1) + id = 'pml_toc-%s' % len(self.toc) + value = self.code_value(line) + if c == 'x': + t = self.process_code(c, line) + elif c == 'X': + t = self.process_code('%s%s' % (c, level), line) + if not value or value == '': + text = t else: - text = self.process_code('%s%s' % (c, l), line) + self.toc.add_item(os.path.basename(self.file_name), id, value) + text = '%s' % (id, t) elif c == 'm': empty = False src = self.code_value(line) @@ -528,11 +560,6 @@ class PML_HTMLizer(object): elif c == 'p': empty = False text = '

' - elif c == 'C': - line.read(1) - id = 'pml_toc-%s' % len(self.toc) - self.toc.add_item(os.path.basename(self.file_name), id, self.code_value(line)) - text = '' % id elif c == 'n': pass elif c == 'w': diff --git a/src/calibre/ebooks/pml/pmlml.py b/src/calibre/ebooks/pml/pmlml.py index 7427a77c2f..d57ed136f6 100644 --- a/src/calibre/ebooks/pml/pmlml.py +++ b/src/calibre/ebooks/pml/pmlml.py @@ -233,7 +233,7 @@ class PMLMLizer(object): w += '="50%"' text.append(w) toc_id = elem.attrib.get('id', None) - if toc_id: + if toc_id and tag not in ('h1', 'h2','h3','h4','h5','h6',): if self.toc.get(page.href, None): toc_title = self.toc[page.href].get(toc_id, None) if toc_title: