Antonio  Valentino avatar Antonio Valentino committed 85ab2b5

Avoid duplicate keywords (patch by Luke Campagnola)

Comments (0)

Files changed (1)

sphinx/builders/qthelp.py

                 new_sections.append(section)
         sections = u'\n'.encode('utf-8').join(new_sections)
 
-        # keywords
-        keywords = []
+        # keywords from index entries
+        keyword_items = []  # list of (name, id, ref) items to be added to keyword list
         index = self.env.create_index(self, group_entries=False)
         for (key, group) in index:
             for title, (refs, subitems) in group:
-                keywords.extend(self.build_keywords(title, refs, subitems))
+                keyword_items.extend(self.build_keywords(title, refs, subitems))
+                
+        index_ids = dict([item[1:] for item in keyword_items])
 
         # ID keywords from xref labels
-        item_template = ' ' * 12 + '<keyword id="%s" ref="%s"/>'
         for domain in self.env.domains.itervalues():
             for name, dispname, _, docname, anchor, _ in domain.get_objects():
                 if not anchor:
                     continue
-                uri = self.get_target_uri(docname) + '#' + anchor
-                item = item_template % (escape(name), escape(uri))
-                item.encode('ascii', 'xmlcharrefreplace')
-                keywords.append(item)
+                uri = escape(self.get_target_uri(docname) + '#' + anchor)
+                id_ = escape(name)
+                
+                if id_ not in index_ids:  ## Do not override any previously-existing IDs
+                    keyword_items.append((None, id_, uri))
+                    index_ids[id_] = uri
+                #else:
+                    #if index_ids[id_] == uri:
+                        #print "Skipping ID", id_, " (same reference)"
+                    #else:
+                        #print "Skipping ID", id_
+                        #print "        keep: ", index_ids[id_]
+                        #print "      ignore: ", uri
+                    
 
+        # generate keyword text
+        keywords = []
+        item_template = ' ' * 12 + '<keyword %s%sref="%s"/>'
+        for name, id_, ref in keyword_items:
+            if name is None:
+                name = ""
+            else:
+                name = 'name="%s" ' % name
+            if id_ is None:
+                id_ = ""
+            else:
+                id_ = 'id="%s" ' % id_
+            keywords.append(item_template % (name, id_, ref))
+        
         keywords = '\n'.join(keywords)
 
         # files
         else:
             id_ = None
 
-        if id:
-            item = ' ' * 12 + '<keyword name="%s" id="%s" ref="%s"/>' % (
-                name, id_, ref[1])
-        else:
-            item = ' ' * 12 + '<keyword name="%s" ref="%s"/>' % (name, ref[1])
-        item.encode('ascii', 'xmlcharrefreplace')
-        return item
+        return (name, id_, ref[1])
 
     def build_keywords(self, title, refs, subitems):
         keywords = []
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.