Skip to content

How to set extras correctly #627

@sr-verde

Description

@sr-verde

Hey, I have this code that works with python-markdown2 <= 2.5.0:

    # Load data from markdown file
    try:
        with open(filepath, "r") as markdown_file:
            html_content = markdown(
                markdown_file.read(),
                extras={
                    "tables": None,
                    "metadata": None,
                    "fenced-code-blocks": None,
                    "toc": None,
                    "middle-word-em": None,
                    "footnotes": None,
                    "breaks": {"on_newline": False, "on_backslash": True},
                },
            )
    except FileNotFoundError:
        print("Fatal error: Couldn’t find input file.")
        exit(1)

But with v2.5.0 that breaks:

  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/bin/my-project", line 6, in <module>
    sys.exit(convert())
             ~~~~~~~^^
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/click/core.py", line 1161, in __call__
    return self.main(*args, **kwargs)
           ~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/click/core.py", line 1082, in main
    rv = self.invoke(ctx)
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/click/core.py", line 1443, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/click/core.py", line 788, in invoke
    return __callback(*args, **kwargs)
  File "/home/user/git/identeco/my-project/myproject/converter.py", line 95, in convert
    create_pdf(filepath, output, toc, template, verbose)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/git/identeco/my-project/myproject/converter.py", line 39, in create_pdf
    html_content = markdown(
        markdown_file.read(),
    ...<8 lines>...
        },
    )
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/markdown2.py", line 210, in markdown
    use_file_vars=use_file_vars, cli=cli).convert(text)
                                          ~~~~~~~^^^^^^
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/markdown2.py", line 453, in convert
    self.reset()
    ~~~~~~~~~~^^
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/markdown2.py", line 402, in reset
    self._setup_extras()
    ~~~~~~~~~~~~~~~~~~^^
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/markdown2.py", line 422, in _setup_extras
    self.extra_classes[name] = klass(self, (self.extras.get(name, {})))
                               ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.cache/pypoetry/virtualenvs/myproject-1ZPfj6IR-py3.13/lib/python3.13/site-packages/markdown2.py", line 3124, in __init__
    options.setdefault('allowed', True)
    ^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'setdefault'

I am not sure whether this is a bug or just a documentation issue but the extas wiki page states to set markdowner = Markdown(extras={"tables": None, "html-classes":classesDict}). The example in the wiki still works fine but it doesn’t work with the middle-word-em: None. Instead, I have to set middle-word-em: {}. So, this may be a problem in the middle-word-em plugin or just in the documentation.

TL;DR: Should that work:

markdown(
                markdown_file.read(),
                extras={
                    "tables": None,
                    "metadata": None,
                    "fenced-code-blocks": None,
                    "toc": None,
                    "middle-word-em": None,
                    "footnotes": None,
                    "breaks": {"on_newline": False, "on_backslash": True},
                },
            )

or should one set

            markdown(
                markdown_file.read(),
                extras={
                    "tables": {},
                    "metadata": {},
                    "fenced-code-blocks": {},
                    "toc": {},
                    "middle-word-em": {},
                    "footnotes": {},
                    "breaks": {"on_newline": False, "on_backslash": True},
                },
            )

In the latter case, the wiki should be updated.

Thanks for your work on this library! ✌️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions