"""
pymdown.headeranchor
An extension for Python Markdown.
Github header anchors

MIT license.

Copyright (c) 2014 Isaac Muse <isaacmuse@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from __future__ import unicode_literals
from markdown import Extension
from markdown.treeprocessors import Treeprocessor
from markdown.extensions.headerid import slugify, stashedHTML2text, itertext, unique

LINK = (
    '<a '
    'name="user-content-%(id)s" '
    'href="#%(id)s" class="headeranchor-link" '
    'aria-hidden="true">'
    '<span class="headeranchor"></span>'
    '</a>'
)


class HeaderAnchorTreeprocessor(Treeprocessor):
    def run(self, root):
        """ Add header anchors """

        # Get a list of id attributes
        used_ids = set()
        for tag in root.getiterator():
            if "id" in tag.attrib:
                used_ids.add(tag.attrib["id"])

        for tag in root.getiterator():
            if tag.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
                if "id" in tag.attrib:
                    id = tag.get('id')
                else:
                    id = stashedHTML2text(''.join(itertext(tag)), self.md)
                    id = unique(slugify(id, self.config.get('sep')), used_ids)
                    tag.set('id', id)
                tag.text = self.markdown.htmlStash.store(
                    LINK % {"id": id},
                    safe=True
                ) + tag.text if tag.text is not None else ''
        return root


class HeaderAnchorExtension(Extension):
    def __init__(self, *args, **kwargs):
        self.config = {
            'sep': ['-', "Separator to use when creating header ids - Default: '-'"]
        }
        self.configured = False
        super(HeaderAnchorExtension, self).__init__(*args, **kwargs)

    def extendMarkdown(self, md, md_globals):
        """Add HeaderAnchorTreeprocessor to Markdown instance"""

        self.processor = HeaderAnchorTreeprocessor(md)
        self.processor.config = self.getConfigs()
        self.processor.md = md
        md.treeprocessors.add("header-anchor", self.processor, "_end")
        md.registerExtension(self)

    def reset(self):
        """ Ensure this is always after toc """
        if 'toc' in self.processor.md.treeprocessors.keys() and not self.configured:
            self.configured = True
            del self.processor.md.treeprocessors["header-anchor"]
            self.processor.md.treeprocessors.add("header-anchor", self.processor, ">toc")


def makeExtension(*args, **kwargs):
    return HeaderAnchorExtension(*args, **kwargs)

@@ -2163,23 +2163,38 @@
  unique(
+self.config%5B'
 slugify
+'%5D
 (id, sel
@@ -2205,19 +2205,15 @@
 nfig
-.get(
+%5B
 'sep'
-)
+%5D
 ), u
@@ -2658,16 +2658,82 @@
 t: '-'%22%5D
+,%0A            'slugify': %5Bslugify, 'Callable to generate anchors'%5D
 %0A       
@@ -2954,32 +2954,48 @@
 %22%0A%0A        self.
+md = md%0A        
 processor = Head
@@ -3020,37 +3020,32 @@
 sor(md)%0A        
-self.
 processor.config
@@ -3065,37 +3065,32 @@
 nfigs()%0A        
-self.
 processor.md = m
@@ -3130,37 +3130,32 @@
 header-anchor%22, 
-self.
 processor, %22_end
@@ -3273,16 +3273,40 @@
      if 
+not self.configured and 
 'toc' in
@@ -3307,34 +3307,24 @@
 oc' in self.
-processor.
 md.treeproce
@@ -3339,32 +3339,8 @@
 ys()
- and not self.configured
 :%0A  
@@ -3388,17 +3388,8 @@
     
-del self.
 proc
@@ -3385,32 +3385,39 @@
        processor
+ = self
 .md.treeprocesso
@@ -3448,21 +3448,32 @@
         
+del 
 self.
+md.tree
 processo
@@ -3473,16 +3473,51 @@
 rocessor
+s%5B%22header-anchor%22%5D%0A            self
 .md.tree
@@ -3548,21 +3548,16 @@
 nchor%22, 
-self.
 processo

Allow slugify to be set in headeranchor extension