Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension Api doc #946

Merged
merged 30 commits into from May 7, 2020
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ae50e13
Fix broken links
merriam Apr 20, 2020
8562747
Rewrite opening paragraph to have more links.
merriam Apr 20, 2020
cf2c0c6
Update Preprocessors section
merriam Apr 21, 2020
1d7f8ea
Update Postprocessors section
merriam Apr 21, 2020
4cb4bb7
Reorder the sections
merriam Apr 21, 2020
ef2b88d
Fix levels of headings
merriam Apr 21, 2020
7c77280
Add encodings to spelling
merriam Apr 21, 2020
296e79b
Update Inline Processors
merriam Apr 21, 2020
58af237
typos
merriam Apr 21, 2020
f1f14d7
Add 'metadata'
merriam Apr 21, 2020
73efc95
Rewrite BlockProcessors section. Still need to add code.
merriam Apr 22, 2020
ad3d681
Finish block processors section, with example
merriam Apr 23, 2020
dc3493a
Remove priority from tables; fix up headings
merriam Apr 23, 2020
44c3053
Move Patterns sections. Fix #729
merriam Apr 23, 2020
e3773fb
Block Processing; reorder the example.
merriam Apr 23, 2020
13fc3d6
Changes to Inline Processor section
merriam Apr 23, 2020
b83a193
Update Tree Processor section
merriam Apr 23, 2020
b827edb
Add note to 3.2 release notes, as requested on a different patch.
merriam Apr 25, 2020
31cadc1
NoRender example now has no pass statement
merriam May 2, 2020
0a56849
Non-substantive changes; internal paragraph breaks.
merriam May 2, 2020
efcdc2e
Remove line numbers from github links
merriam May 2, 2020
368f617
Two typos in same sentence.
merriam May 2, 2020
78f1de9
Clean up awkward or incorrect wording.
waylan May 7, 2020
33e2d2e
Whitespace and hardwrap cleanup.
waylan May 7, 2020
d40e7c6
TOC cleanup
waylan May 7, 2020
7cb6e86
Cleanup examples
waylan May 7, 2020
88f0b77
Fix #949
waylan May 7, 2020
2787534
Spellcheck fix
waylan May 7, 2020
dccfb12
Correct Release note
waylan May 7, 2020
04d22c8
Merge branch 'master' into api_doc
waylan May 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 32 additions & 13 deletions docs/extensions/api.md
Expand Up @@ -2,16 +2,18 @@ title: Extensions API

# Writing Extensions for Python-Markdown


Python-Markdown includes an API for extension writers to plug their own
custom functionality and syntax into the parser. An extension will patch
into one or more stages of the parser:

* [*Preprocessors*](#preprocessors) alter the source before it is passed to the parser.
* [*Block Processors*](#blockprocessors) work with blocks of text separated by blank lines.
* [*Inline Patterns*](#inlinepatterns) work with inline elements, such as `*strong*`.
* [*Tree Processors*](#treeprocessors) modify the constructed ElementTree
* [*Inline Patterns*](#inlinepatterns) are common tree processors for inline elements, such as `*strong*`.
* [*Postprocessors*](#postprocessors) munge of the output of the parser just before it is returned.


The parser loads text, applies the preprocessors, creates an
[ElementTree][ElementTree] added to by the block processors and
inline patterns, renders the ElementTree as Unicode text, and then
Expand All @@ -26,34 +28,51 @@ other issues on the [bug tracker].

## Preprocessors {: #preprocessors }

Preprocessors munge the source text before it is passed into the Markdown
parser. This is an excellent place to clean up bad syntax, extract things the
parser may otherwise choke on and perhaps even store it for later retrieval.
Preprocessors munge the source text before it is passed to the Markdown
parser. This is an excellent place to clean up bad encodings or to extract
waylan marked this conversation as resolved.
Show resolved Hide resolved
portions for later processing that the parser may otherwise choke on.

Preprocessors should inherit from `markdown.preprocessors.Preprocessor` and
implement a `run` method with one argument `lines`. The `run` method of
each Preprocessor will be passed the entire source text as a list of Unicode
strings. Each string will contain one line of text. The `run` method should
return either that list, or an altered list of Unicode strings.

implement a `run` method, which takes a single parameter `lines`. This parameter is
the entire source text stored as a list of Unicode string, one per line. `run` should
waylan marked this conversation as resolved.
Show resolved Hide resolved
return its processed list of of Unicode strings, one per line.
waylan marked this conversation as resolved.
Show resolved Hide resolved

A pseudo example:
A simple example:

```python
from markdown.preprocessors import Preprocessor
import re

class MyPreprocessor(Preprocessor):
class NoRender(Preprocessor):
""" Skip any line with words 'NO RENDER' in it. """
def run(self, lines):
new_lines = []
for line in lines:
m = MYREGEX.match(line)
m = re.search("NO RENDER", line)
if m:
# do stuff
pass # skipping this line
waylan marked this conversation as resolved.
Show resolved Hide resolved
else:
new_lines.append(line)
return new_lines
```

Some preprocessors in the Markdown source tree include:


| Name | kind | Priority | Description |
| ----------------------------|-----------|----|-----------------------------------------------
| [NormalizeWhiteSpace][c1] | built-in | 30 | Normalizes whitespace by expanding tabs, fixing `\r` line endings, etc. |
| [HtmlBlockPreprocessor][c2] | built-in | 20 | Removes html blocks from the text and stores them for later processing |
| [ReferencePreprocessor][c3] | built-in | 10 | Removes reference definitions from text and stores for later processing |
| [MetaPreprocessor][c4] | extension | 27 | Strips and records meta data at top of documents |
| [FootnotesPreprocessor][c5] | extension | 15 | Removes footnote blocks from the text and stores them for later processing |
waylan marked this conversation as resolved.
Show resolved Hide resolved

[c1]: https://github.com/Python-Markdown/markdown/blob/master/markdown/preprocessors.py#L62
[c2]: https://github.com/Python-Markdown/markdown/blob/master/markdown/preprocessors.py#L74
[c3]: https://github.com/Python-Markdown/markdown/blob/master/markdown/preprocessors.py#L339
[c4]: https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/meta.py#L32
[c5]: https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/footnotes.py#L205

## Inline Patterns {: #inlinepatterns }

### Legacy
Expand Down