Repository navigation
Description
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
waylan commentedon Feb 10, 2020
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_strongonly handled triple asterisks (***strongem***or***em*strong**, but not***strong**em*which was under the namestrong_em). For <3.2 you would probably want to deregister all of the patterns defined here:markdown/markdown/inlinepatterns.py
Lines 94 to 99 in 58b4d64
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 thederegistermethod includes astrictargument. Set it toFalseand it will not raise an error when attempting to deregister a non-existent pattern. Like this:Of course, you may need to deregister a few more depending on what you want to accomplish.
arikrupnik commentedon Feb 10, 2020
Waylan, thank you for your fast and informative response!
Yes, this is exactly what happened! This works as expected in 3.1:
Am I correct assuming that only the specific
em_strongandem_strong2processors changed, but the general API for constructingSimpleTagInlineProcessors remains the same?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()?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.
waylan commentedon Feb 10, 2020
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.
No, that has been stable for years.
arikrupnik commentedon Feb 11, 2020
Thank you! Your support is outstanding.