How to create options for Python markdown custom extension?

273 views Asked by At

I'm trying to add an option to my custom markdown extension in python3. Unfortuantely I'm getting the following error:

  File "pymodules/docmarkdown.py", line 232, in get_leaflang_markdown
    MyFencedCodeExtension(deflang = "leaf"),
  File "pymodules/docmarkdown.py", line 61, in __init__
    super(MyFencedCodeExtension,self).__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'deflang'

The constructor code of the extension is below. It follows the pattern provided by the docs.

class MyFencedCodeExtension(markdown.extensions.Extension):

    def __init__(self, **kwargs):
        self.config = { 'deflang' : [ None, "language if not specified" ] }

        super(MyFencedCodeExtension,self).__init__(**kwargs)

I'm referencing the extension when constructing the Markdown instance:

return markdown.Markdown(
    safe_mode = 'escape',
    extensions = [
        'meta',
        'toc',
        MyFencedCodeExtension(deflang = "leaf"),
        CenterExtension({}),
    ]
1

There are 1 answers

2
BoarGules On

This error message is happening on your super() call.

The superclass of MyFencedCodeExtension is markdown.extensions.Extension.

According to the error message, the superclass constructor is not expecting the keyword argument deflang.

Look at the signature of markdown.extensions.Extension.__init__ to work out what it is expecting.