ticket id
000037
status
closed
priority
???
assigned to
Waylan
Reported by: Paul Stansifer
Component:

If you emit multiple footnote-containing documents onto one HTML page, and their authors both used, e.g., a footnote labeled "[^1]", they will be assigned identical anchor tags, and the footnote links for whichever document is second on the page will not work.

I have a patch that adds a 'UNIQUE_IDS' option to the footnotes extension. When that option is enabled, each separate document (as determined by a call to 'reset()') processed by the same 'Markdown' object will have its own namespace for footnote IDs and all links will be usable.

diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py
index 6dacab7..751779d 100644
--- a/markdown/extensions/footnotes.py
+++ b/markdown/extensions/footnotes.py
@@ -38,11 +38,18 @@ class FootnoteExtension(markdown.Extension):
         """ Setup configs. """
         self.config = {'PLACE_MARKER':
                        ["///Footnotes Go Here///",
-                        "The text string that marks where the footnotes go"]}
+                        "The text string that marks where the footnotes go"],
+                       'UNIQUE_IDS':
+                       [False,
+                        "Whether to prevent footnote name collisions in "
+                        "one Markdown object across calls to reset()."]}

         for key, value in configs:
             self.config[key][0] = value
-            
+
+        # In multiple invocations, emit links that don't get tangled.
+        self.unique_prefix = 0
+
         self.reset()

     def extendMarkdown(self, md, md_globals):
@@ -66,8 +73,9 @@ class FootnoteExtension(markdown.Extension):
                                   ">amp_substitute")

     def reset(self):
-        """ Clear the footnotes on reset. """
+        """ Clear the footnotes on reset, and prepare for a distinct document. """
         self.footnotes = markdown.odict.OrderedDict()
+        self.unique_prefix += 1

     def findFootnotesPlaceholder(self, root):
         """ Return ElementTree Element that contains Footnote placeholder. """
@@ -91,11 +99,17 @@ class FootnoteExtension(markdown.Extension):

     def makeFootnoteId(self, id):
         """ Return footnote link id. """
-        return 'fn:%s' % id
+        if self.getConfig("UNIQUE_IDS"):
+            return 'fn:%d-%s' % (self.unique_prefix, id)
+        else:
+            return 'fn:%s' % id

     def makeFootnoteRefId(self, id):
         """ Return footnote back-link id. """
-        return 'fnref:%s' % id
+        if self.getConfig("UNIQUE_IDS"):
+            return 'fnref:%d-%s' % (self.unique_prefix, id)
+        else:
+            return 'fnref:%s' % id

     def makeFootnotesDiv(self, root):
         """ Return div of footnotes as et Element. """

Resolution

fixed

Powered by Sputnik | XHTML 1.1