markdown wikidiscuss the markdown format Extending PythonMarkdownThe functionality of PythonMarkdown can be extended without changing the code, by inserting additional pre-processors, post-processors or inline patterns into Markdown's pipeline. Pre-processors operate on lines of source text and are run in the beginning. It is sufficient to write a class with a run() method that takes a list of text lines as a parameter and returns the same or a new list. An instance of the preprocessor can then be inserted into the markdown pipeline. Preprocessor classes must extend markdown.Preprocessor.
Post-processors operate on a NanoDom tree and run at the very end. They need to implement a "run" method that takes a pointer to a NanoDom document. Postprocessor classes must extend markdown.Postprocessor.
Finally, inline patterns can be added. Each inline pattern includes a regular expression pattern and a method for turning a match object into a DOM fragment
Preprocessors, patterns and postprocessors need to then be wrapped
together into an extension, which should be implemented as a class
that extends markdown.Extension and should over-ride the
If the extension uses any parameters that the use may want to change, they should be stored in self.config in the following format:
When stored this way the config parameters can be over-riden from the command line or at the time Markdown is instantiated:
Note that parameters should always be assumed to be set to string values, and should be converted at the run time, e.g.
Each extension should ideally be placed in its own module starting
with
See the implementation of footnote support (mdxfootnotes.py) for an example of using all three in combination to provide reasonably complex additional functionality. (I use a pre-processor to collect footnote definitions, an inline pattern to handle uses of footnotes inside the text and a post-processor to attach the HTML of the actual footnotes at the end of the document) The RSS extension (mdxrss.py, also included), which generates an RSS file from markdown source is a simpler extension but provides an example of extending markdown to generate arbitrary XML rather than HTML. Other extensions:
|
Internal linksExternal Links |