You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just as directives make it easy to add custom blocks without needing distinct syntax for each block type, roles make it easy to add custom inline spans without needing distinct syntax for each. (Similarly, for a Mistune plugin writer, role plugins are like directive plugins - easier to write than writing a standalone plugin from scratch.)
The combination of directives and roles brings Markdown's expressive power on par with reStructuredText.
@lepture if you're interested in merging this, I can flesh it out into a full PR (docstrings, tests). In the meantime, if anyone wants to use it, you can have it under the same license as Mistune. (If there's no response for a while or lepture doesn't want to include this in-package, I'm open to publishing this separately on PyPI.)
Note
Many parts of roles.py are very similar to code in Mistune. We could get good code reuse if this was merged, and it would help keep the behavior consistent. For examples: Role.parse_method is just a couple name replacements away from BaseDirective.parse_method, Role.parse_tokens is the same as the recursive inline parsing done by built-in plugins like strikethrough, and the unique parts of InlineParser.parse_codespan are almost identical to Role.parse_content.
Example 1: Substitution/Templating
This lets you inject variables into your Markdown, like a template, and optionally you can have the injected value be parsed as Markdown.
[click to expand `Substitute` role implementation]
I wrote a Mistune plug-in to add support for MyST Markdown's "role" syntax:
[click to expand
roles.py
]Just as directives make it easy to add custom blocks without needing distinct syntax for each block type, roles make it easy to add custom inline spans without needing distinct syntax for each. (Similarly, for a Mistune plugin writer, role plugins are like directive plugins - easier to write than writing a standalone plugin from scratch.)
The combination of directives and roles brings Markdown's expressive power on par with reStructuredText.
@lepture if you're interested in merging this, I can flesh it out into a full PR (docstrings, tests). In the meantime, if anyone wants to use it, you can have it under the same license as Mistune. (If there's no response for a while or lepture doesn't want to include this in-package, I'm open to publishing this separately on PyPI.)
Note
Many parts of
roles.py
are very similar to code in Mistune. We could get good code reuse if this was merged, and it would help keep the behavior consistent. For examples:Role.parse_method
is just a couple name replacements away fromBaseDirective.parse_method
,Role.parse_tokens
is the same as the recursive inline parsing done by built-in plugins like strikethrough, and the unique parts ofInlineParser.parse_codespan
are almost identical toRole.parse_content
.Example 1: Substitution/Templating
This lets you inject variables into your Markdown, like a template, and optionally you can have the injected value be parsed as Markdown.
[click to expand `Substitute` role implementation]
You'd enable this similarly to a Directive plugin:
And then the Markdown looks like this:
Which is as-if you wrote:
Example 2: Nicer Link Shorthands
I have some roles which expand to links nicely. So that
[me@example.com](mailto:me@example.com)
I can just write{email}`me@example.com`
,[+1 234 567 89](tel:+1-234-567-89)
I can write{phone}`+1-234-567-89`
,[click to expand `Phone` role implementation]
The text was updated successfully, but these errors were encountered: