Skip to content

Extension API #909

Closed
Closed

Description

@arikrupnik

I'm updating a package (https://github.com/arikrupnik/fountainhead) to work with the newer MD Extension API, and I'm failing to recreate the behavior that worked in v2. I read #179 and #811 and I'm still missing something very obvious.

The language I'm processing if similar to MD in spirit, but is different enough that I want to remove all inline processors and then rebuild that registry from scratch. In v2, I achieved this with md.inlinePatterns.clear() in extendMarkdown(self, md, md_globals)

In v3.1.1, I tried replacing the inlinePatterns registry altogether, and deregistering specific patterns,. Neither of which has any effect; default patterns continue to trigger, e.g., interpreting * as <em>:

class BlankSlate(markdown.extensions.Extension):
    def extendMarkdown(self, md):
        md.inlinePatterns = markdown.util.Registry()

class NoEm(markdown.extensions.Extension):
    def extendMarkdown(self, md):
        md.inlinePatterns.deregister("em_strong")

def test_blank_slate():
    m = markdown.Markdown(extensions=[BlankSlate()])
    assert m.convert("text with *asterisks* and without") == \
        "<p>text with *asterisks* and without</p>"

def test_no_em():
    m = markdown.Markdown(extensions=[NoEm()])
    assert m.convert("text with *asterisks* and without") == \
        "<p>text with *asterisks* and without</p>"

pytest output:

E         - <p>text with <em>asterisks</em> and without</p>
E         ?              ^^^^         ^^^^^
E         + <p>text with *asterisks* and without</p>
E         ?              ^         ^

I've also tried the nuclear option that #811 discusses (deriving from markdown.Markdown and overriding build_parser). It works, but seems a bit heavy-handed.

There must be something very very simple I'm overlooking. I'm wotking with v3.1.1 from ubuntu pyton-markdown:

Package: python-markdown
Architecture: all
Version: 3.1.1-2

Activity

  1. waylan commented on Feb 10, 2020

    @waylan
    Member

    I've also tried the nuclear option that #811 discusses (deriving from markdown.Markdown and overriding build_parser).

    This is the only option I would try. This is also why we don't offer a "deregister all" or "clear" option. You should be subclassing instead. As an added bonus, you would not need to deal with the changes described below.

    You report that you are using Markdown version 3.1. We just released 3.2 the other day, which significantly simplified the inline processors (see #805). I suspect you are browsing the source here on GitHub for version 3.2, but are trying to work with 3.1 code. Prior to 3.2 em_strong only handled triple asterisks (***strongem*** or ***em*strong**, but not ***strong**em* which was under the name strong_em). For <3.2 you would probably want to deregister all of the patterns defined here:

    inlinePatterns.register(DoubleTagInlineProcessor(EM_STRONG_RE, 'strong,em'), 'em_strong', 60)
    inlinePatterns.register(DoubleTagInlineProcessor(STRONG_EM_RE, 'em,strong'), 'strong_em', 50)
    inlinePatterns.register(SimpleTagInlineProcessor(STRONG_RE, 'strong'), 'strong', 40)
    inlinePatterns.register(SimpleTagInlineProcessor(EMPHASIS_RE, 'em'), 'emphasis', 30)
    inlinePatterns.register(SimpleTagInlineProcessor(SMART_STRONG_RE, 'strong'), 'strong2', 20)
    inlinePatterns.register(SimpleTagInlineProcessor(SMART_EMPHASIS_RE, 'em'), 'emphasis2', 10)

    Needless to say, 3.2 it is much simpler as you only need to deregister one pattern to remove support for asterisk based syntax and a second (em_strong2) for underscores. Fortunately, your code can work in both versions as the deregister method includes a strict argument. Set it to False and it will not raise an error when attempting to deregister a non-existent pattern. Like this:

    md.inlinePatterns.deregister("em_strong")  # this will succeed in >=3.0
    md.inlinePatterns.deregister("em_strong2", strict=False) # this will fail silently in <3.2
    md.inlinePatterns.deregister("strong_em", strict=False)  # this will fail silently in >=3.2

    Of course, you may need to deregister a few more depending on what you want to accomplish.

  2. arikrupnik commented on Feb 10, 2020

    @arikrupnik
    Author

    Waylan, thank you for your fast and informative response!

    I suspect you are browsing the source here on GitHub for version 3.2, but are trying to work with 3.1 code.

    Yes, this is exactly what happened! This works as expected in 3.1:

        def extendMarkdown(self, md):
            md.inlinePatterns.deregister("emphasis")

    We just released 3.2 the other day, which significantly simplified the inline processors (see #805).

    Am I correct assuming that only the specific em_strong and em_strong2 processors changed, but the general API for constructing SimpleTagInlineProcessors remains the same?

    (deriving from markdown.Markdown and overriding build_parser).

    This is the only option I would try.

    That's what I'm going to go with then. Anything I need to be aware of in terms of version changes in build_parser()?

    Of course, you may need to deregister a few more depending on what you want to accomplish.

    I more or less have to start from scratch with inlines. Fountain uses underscores for underline and other oddities, plus I'm outputting XML rather than XHTML, so all the element names are different from default MD.

  3. waylan commented on Feb 10, 2020

    @waylan
    Member

    Am I correct assuming that only the specific em_strong and em_strong2 processors changed, but the general API for constructing SimpleTagInlineProcessors remains the same?

    Yes. The changes are summarized well in the release notes. We simply condensed 5 inline processors into 2 and made no other changes. Changes in version 3.0 allowed this to happen. We just didn't get to it until now.

    Anything I need to be aware of in terms of version changes in build_parser()?

    No, that has been stable for years.

  4. added
    support
    on Feb 10, 2020
  5. arikrupnik commented on Feb 11, 2020

    @arikrupnik
    Author

    Thank you! Your support is outstanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    support

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions