From: w. <wil...@fo...> - 2013-08-27 13:38:34
|
Hi, 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. |
From: Dmitry S. <mi...@gm...> - 2013-08-27 18:54:29
|
Try this: from markdown.extensions.headerid import HeaderIdExtension ... headerId = HeaderIdExtension([('slugify', my_slugify)]) md = markdown.Markdown(extensions=[headerId]) Initially I thought that extensions would accept dicts, but they seem to only support lists of tuples. On Tue, Aug 27, 2013 at 5:07 PM, wilbur <wil...@fo...> wrote: > Hi, 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. -- Dmitry Shachnev |
From: Waylan L. <wa...@gm...> - 2013-08-29 00:51:39
|
Wilbur, As you need to pass in a reference to your slugify function, you can't use the sting based shortcut for passing in extension config settings (we would need to run the string through eval for that to work). Rather you need to use the [extension_configs] keyword which actually passes in a reference - not just a string. Of course, the extension_configs keyword is ugly to work with (it existed before I joined the project). As Dmitry points out, the best way is to create an instance of the extension with our custom settings and pass that instance in (no need for extension_configs). [extension_configs]: http://pythonhosted.org/Markdown/reference.html#extension_configs ---- \X/ /-\ `/ |_ /-\ |\| Waylan Limberg On Tue, Aug 27, 2013 at 9:07 AM, wilbur <wil...@fo...> wrote: > Hi, 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. > > > ------------------------------------------------------------------------------ > Introducing Performance Central, a new site from SourceForge and > AppDynamics. Performance Central is your source for news, insights, > analysis and resources for efficient Application Performance Management. > Visit us today! > http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk > _______________________________________________ > Python-markdown-discuss mailing list > Pyt...@li... > https://lists.sourceforge.net/lists/listinfo/python-markdown-discuss > |
From: w. <wil...@fo...> - 2013-08-29 07:31:05
|
Thanks for your help. I took Dmitry Shachnev's advice and finally got what I wanted. 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> |