From f52d62656705841316d1d653644d874b4db9ff37 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Thu, 13 Dec 2012 14:47:34 -0500 Subject: Fixed #165. Switched the order of treeprocessors when attr_list and headerid extensions are used togeather. While this means headerid may alter IDs defined in attr_lists for uniqueness, automaticaly generated ids will not contain unparsed attr_lists. This is the lesser of two evils - and actually generates a more valid output (all IDs will be unique) --- markdown/extensions/attr_list.py | 8 +------- markdown/extensions/headerid.py | 10 +++++++--- tests/test_extensions.py | 12 ++++++++++++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/markdown/extensions/attr_list.py b/markdown/extensions/attr_list.py index 36f3e3a..8734e9f 100644 --- a/markdown/extensions/attr_list.py +++ b/markdown/extensions/attr_list.py @@ -74,7 +74,6 @@ class AttrListTreeprocessor(markdown.treeprocessors.Treeprocessor): def run(self, doc): for elem in doc.getiterator(): - #import pdb; pdb.set_trace() if isBlockLevel(elem.tag): # Block level: check for attrs on last line of text RE = self.BLOCK_RE @@ -131,12 +130,7 @@ class AttrListTreeprocessor(markdown.treeprocessors.Treeprocessor): class AttrListExtension(markdown.extensions.Extension): def extendMarkdown(self, md, md_globals): - if 'headerid' in md.treeprocessors.keys(): - # insert after 'headerid' treeprocessor - md.treeprocessors.add('attr_list', AttrListTreeprocessor(md), '>headerid') - else: - # insert after 'inline' treeprocessor - md.treeprocessors.add('attr_list', AttrListTreeprocessor(md), '>inline') + md.treeprocessors.add('attr_list', AttrListTreeprocessor(md), '>inline') def makeExtension(configs={}): diff --git a/markdown/extensions/headerid.py b/markdown/extensions/headerid.py index 2d18c15..1d158f9 100644 --- a/markdown/extensions/headerid.py +++ b/markdown/extensions/headerid.py @@ -133,7 +133,7 @@ class HeaderIdTreeprocessor(markdown.treeprocessors.Treeprocessor): if elem.tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']: if force_id: if "id" in elem.attrib: - id = elem.id + id = elem.get('id') else: id = slugify(u''.join(itertext(elem)), sep) elem.set('id', unique(id, self.IDs)) @@ -183,8 +183,12 @@ class HeaderIdExtension (markdown.Extension): self.processor = HeaderIdTreeprocessor() self.processor.md = md self.processor.config = self.getConfigs() - # Replace existing hasheader in place. - md.treeprocessors.add('headerid', self.processor, '>inline') + if 'attr_list' in md.treeprocessors.keys(): + # insert after attr_list treeprocessor + md.treeprocessors.add('headerid', self.processor, '>attr_list') + else: + # insert after 'inline' treeprocessor. + md.treeprocessors.add('headerid', self.processor, '>inline') def reset(self): self.processor.IDs = [] diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 315e7ef..b10414e 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -171,6 +171,18 @@ header_forceid: Off self.assertEqual(markdown.markdown(text, ['headerid', 'meta']), '

A Header

') + def testHeaderIdWithAttr_List(self): + """ Test HeaderIDs with Attr_List extension. """ + + text = '# Header1 {: #foo }\n# Header2 {: .bar }' + self.assertEqual(markdown.markdown(text, ['headerid', 'attr_list']), + '

Header1

\n' + '

Header2

') + # Switch order extensions are loaded - should be no change in behavior. + self.assertEqual(markdown.markdown(text, ['attr_list', 'headerid']), + '

Header1

\n' + '

Header2

') + class TestMetaData(unittest.TestCase): """ Test MetaData extension. """ -- cgit v1.2.3