From c53307a4d555c04e97739fefe0cafc2e97d55328 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Sun, 30 Oct 2011 17:28:56 -0400 Subject: Fixed #49. Don't crash on poorly/randomly ordered header levels. Maybe someday we will better support any input (patches welcome), but we should never crash on poorly formatted input text. With this fix, we catch the exception and skip over it. The TOC up to the point (and perhaps after) still gets rendered. The incomplete TOC should be the clue to the document author that s/he has a formatting error in the document. --- markdown/extensions/toc.py | 88 ++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index fb930e2..f00a249 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -66,49 +66,53 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor): marker_found = True if header_rgx.match(c.tag): - tag_level = int(c.tag[-1]) - - while tag_level < level: - list_stack.pop() - level -= 1 - - if tag_level > level: - newlist = etree.Element("ul") - if last_li: - last_li.append(newlist) - else: - list_stack[-1].append(newlist) - list_stack.append(newlist) - if level == 0: - level = tag_level + try: + tag_level = int(c.tag[-1]) + + while tag_level < level: + list_stack.pop() + level -= 1 + + if tag_level > level: + newlist = etree.Element("ul") + if last_li: + last_li.append(newlist) + else: + list_stack[-1].append(newlist) + list_stack.append(newlist) + if level == 0: + level = tag_level + else: + level += 1 + + # Do not override pre-existing ids + if not "id" in c.attrib: + id = unique(self.config["slugify"](text, '-'), used_ids) + c.attrib["id"] = id else: - level += 1 - - # Do not override pre-existing ids - if not "id" in c.attrib: - id = unique(self.config["slugify"](text, '-'), used_ids) - c.attrib["id"] = id - else: - id = c.attrib["id"] - - # List item link, to be inserted into the toc div - last_li = etree.Element("li") - link = etree.SubElement(last_li, "a") - link.text = text - link.attrib["href"] = '#' + id - - if self.config["anchorlink"] in [1, '1', True, 'True', 'true']: - anchor = etree.Element("a") - anchor.text = c.text - anchor.attrib["href"] = "#" + id - anchor.attrib["class"] = "toclink" - c.text = "" - for elem in c.getchildren(): - anchor.append(elem) - c.remove(elem) - c.append(anchor) - - list_stack[-1].append(last_li) + id = c.attrib["id"] + + # List item link, to be inserted into the toc div + last_li = etree.Element("li") + link = etree.SubElement(last_li, "a") + link.text = text + link.attrib["href"] = '#' + id + + if self.config["anchorlink"] in [1, '1', True, 'True', 'true']: + anchor = etree.Element("a") + anchor.text = c.text + anchor.attrib["href"] = "#" + id + anchor.attrib["class"] = "toclink" + c.text = "" + for elem in c.getchildren(): + anchor.append(elem) + c.remove(elem) + c.append(anchor) + + list_stack[-1].append(last_li) + except IndexError: + # We have bad ordering of headers. Just move on. + pass if not marker_found: # searialize and attach to markdown instance. prettify = self.markdown.treeprocessors.get('prettify') -- cgit v1.2.3