| 1 |
.. _parser-api: |
| 2 |
|
| 3 |
Parser API |
| 4 |
========== |
| 5 |
|
| 6 |
`The docutils documentation describes`__ parsers as follows: |
| 7 |
|
| 8 |
The Parser analyzes the input document and creates a node tree |
| 9 |
representation. |
| 10 |
|
| 11 |
__ https://docutils.sourceforge.io/docs/dev/hacking.html#parsing-the-document |
| 12 |
|
| 13 |
In Sphinx, the parser modules works as same as docutils. The parsers are |
| 14 |
registered to Sphinx by extensions using Application APIs; |
| 15 |
:meth:`.Sphinx.add_source_suffix` and :meth:`.Sphinx.add_source_parser`. |
| 16 |
|
| 17 |
The *source suffix* is a mapping from file suffix to file type. For example, |
| 18 |
``.rst`` file is mapped to ``'restructuredtext'`` type. Sphinx uses the |
| 19 |
file type to looking for parsers from registered list. On searching, |
| 20 |
Sphinx refers to the ``Parser.supported`` attribute and picks up a parser |
| 21 |
which contains the file type in the attribute. |
| 22 |
|
| 23 |
The users can override the source suffix mappings using |
| 24 |
:confval:`source_suffix` like following:: |
| 25 |
|
| 26 |
# a mapping from file suffix to file types |
| 27 |
source_suffix = { |
| 28 |
'.rst': 'restructuredtext', |
| 29 |
'.md': 'markdown', |
| 30 |
} |
| 31 |
|
| 32 |
You should indicate file types your parser supports. This will allow users |
| 33 |
to configure their settings appropriately. |
| 34 |
|
| 35 |
.. module:: sphinx.parsers |
| 36 |
|
| 37 |
.. autoclass:: Parser |
| 38 |
:members: |