mawenbao
mawenbao

Reputation: 279

python markdown headerid extension custom slugify function error

I tried to parse my markdown files with python-markdown's headerid extension, with my custom slugify function:

import markdown

def my_slugify(value, sep):
    return "100"

md = markdown.Markdown(extensions=['headerid(slugify=my_slugify)'])
print md.convert("#Head 1")

But I got this error:

Traceback (most recent call last):
File "a.py", line 7, in <module>
    print md.convert("#Head 1")
File "/usr/local/lib/python2.7/dist-packages/markdown/__init__.py", line 296, in convert
    newRoot = treeprocessor.run(root)
File "/usr/local/lib/python2.7/dist-packages/markdown/extensions/headerid.py", line 139, in run
  id = slugify(''.join(itertext(elem)), sep)
TypeError: 'unicode' object is not callable

I looked into headerid's source code. It seems that the headerid extension just use the unicode object as a callable object:

id = slugify(''.join(itertext(elem)), sep)

So my question is how can I pass my custom slugify function to headerid? Besides, my python version is 2.7.3 and python-markdown is 2.3.1. Thanks in advance.

Upvotes: 3

Views: 600

Answers (2)

mawenbao
mawenbao

Reputation: 279

I checked the documentation here and read the headerid codes again, and found the solution:

import markdown
from markdown.extensions.headerid import HeaderIdExtension

def my_slugify(value, sep):
    return "100"

md = markdown.Markdown(extensions=[HeaderIdExtension(configs=[('slugify', my_slugify)])])
print md.convert("#Head 1")

which gives the correct output:

<h1 id="100">Head 1</h1>

Upvotes: 0

user1593705
user1593705

Reputation:

I installed markdown and could do this :

import markdown

def my_slugify(value, sep):
    return "100"

extension_config = {'headerid':('slugify','my_slugify')}

md = markdown.Markdown(extensions=extension_config)
print md.convert("#Head 1")

which prints

<h1 id="head-1">Head 1</h1>

this is in the official doc

I hope this could help

Upvotes: 1

Related Questions