View Single Post
Old 03-09-2011, 11:41 AM   #13
siebert
Developer
siebert has a complete set of Star Wars action figures.siebert has a complete set of Star Wars action figures.siebert has a complete set of Star Wars action figures.
 
Posts: 136
Karma: 280
Join Date: Nov 2010
Device: Kindle 3 (Keyboard) 3G / iPad 3 WiFi / Nexus 4 (Android)
Quote:
Originally Posted by kovidgoyal View Post
Then feel free to submit a patch
Ok, here it is:

Code:
diff --git a/src/calibre/ebooks/oeb/transforms/structure.py b/src/calibre/ebooks/oeb/transforms/structure.py
index 0db9b15..90de41d 100644
--- a/src/calibre/ebooks/oeb/transforms/structure.py
+++ b/src/calibre/ebooks/oeb/transforms/structure.py
@@ -95,10 +95,8 @@ class DetectStructure(object):
                     self.log.exception('Failed to mark chapter')
 
     def create_level_based_toc(self):
-        if self.opts.level1_toc is None:
-            return
-        for item in self.oeb.spine:
-            self.add_leveled_toc_items(item)
+        if self.opts.level1_toc is not None:
+            self.add_leveled_toc_items()
 
     def create_toc_from_chapters(self):
         counter = self.oeb.toc.next_play_order()
@@ -145,14 +143,15 @@ class DetectStructure(object):
         return text, href
 
 
-    def add_leveled_toc_items(self, item):
-        level1 = XPath(self.opts.level1_toc)(item.data)
+    def add_leveled_toc_items(self):
         level1_order = []
-        document = item
-
+        added = {}
+        added2 = {}
         counter = 1
-        if level1:
-            added = {}
+        for item in self.oeb.spine:
+            level1 = XPath(self.opts.level1_toc)(item.data)
+            document = item
+
             for elem in level1:
                 text, _href = self.elem_to_link(document, elem, counter)
                 counter += 1
@@ -163,14 +162,18 @@ class DetectStructure(object):
                     added[elem] = node
                     #node.add(_('Top'), _href)
             if self.opts.level2_toc is not None:
-                added2 = {}
                 level2 = list(XPath(self.opts.level2_toc)(document.data))
                 for elem in level2:
                     level1 = None
                     for item in document.data.iterdescendants():
                         if item in added.keys():
                             level1 = added[item]
-                        elif item == elem and level1 is not None:
+                        elif item == elem:
+                            if level1 is None:
+                                if added == {}:
+                                    continue
+                                else:
+                                    level1 = added[added.keys()[-1]]
                             text, _href = self.elem_to_link(document, elem, counter)
                             counter += 1
                             if text:
@@ -183,11 +186,15 @@ class DetectStructure(object):
                         for item in document.data.iterdescendants():
                             if item in added2.keys():
                                 level2 = added2[item]
-                            elif item == elem and level2 is not None:
+                            elif item == elem:
+                                if level2 is None:
+                                    if added2 == {}:
+                                        continue
+                                    else:
+                                        level2 = added2[added2.keys()[-1]]
                                 text, _href = \
                                         self.elem_to_link(document, elem, counter)
                                 counter += 1
                                 if text:
                                     level2.add(text, _href,
-                                    play_order=self.oeb.toc.next_play_order())
-
+                                        play_order=self.oeb.toc.next_play_order())
Ciao,
Steffen
siebert is offline   Reply With Quote