diff options
author | Waylan Limberg <waylan@gmail.com> | 2011-10-30 17:28:56 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2011-10-30 17:28:56 -0400 |
commit | c53307a4d555c04e97739fefe0cafc2e97d55328 (patch) | |
tree | b1161ea088aa28f1de63528d791d8fc5db75054e | |
parent | e8cdb0b4e56666a69a61ae1e56d32cb05c3404b5 (diff) | |
download | markdown-c53307a4d555c04e97739fefe0cafc2e97d55328.tar.gz markdown-c53307a4d555c04e97739fefe0cafc2e97d55328.tar.bz2 markdown-c53307a4d555c04e97739fefe0cafc2e97d55328.zip |
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.
-rw-r--r-- | markdown/extensions/toc.py | 88 |
1 files 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') |