| 1 |
title: Library Reference |
| 2 |
|
| 3 |
# Using Markdown as a Python Library |
| 4 |
|
| 5 |
First and foremost, Python-Markdown is intended to be a python library module |
| 6 |
used by various projects to convert Markdown syntax into HTML. |
| 7 |
|
| 8 |
## The Basics |
| 9 |
|
| 10 |
To use markdown as a module: |
| 11 |
|
| 12 |
```python |
| 13 |
import markdown |
| 14 |
html = markdown.markdown(your_text_string) |
| 15 |
``` |
| 16 |
|
| 17 |
## The Details |
| 18 |
|
| 19 |
Python-Markdown provides two public functions ([`markdown.markdown`](#markdown) |
| 20 |
and [`markdown.markdownFromFile`](#markdownFromFile)) both of which wrap the |
| 21 |
public class [`markdown.Markdown`](#Markdown). If you're processing one |
| 22 |
document at a time, these functions will serve your needs. However, if you need |
| 23 |
to process multiple documents, it may be advantageous to create a single |
| 24 |
instance of the `markdown.Markdown` class and pass multiple documents through |
| 25 |
it. If you do use a single instance though, make sure to call the `reset` |
| 26 |
method appropriately ([see below](#convert)). |
| 27 |
|
| 28 |
### `markdown.markdown(text [, **kwargs])` {: #markdown data-toc-label='markdown.markdown' } |
| 29 |
|
| 30 |
!!! warning |
| 31 |
|
| 32 |
The Python-Markdown library does ***not*** sanitize its HTML output. If |
| 33 |
you are processing Markdown input from an untrusted source, it is your |
| 34 |
responsibility to ensure that it is properly sanitized. For more |
| 35 |
information see [Sanitizing HTML Output]. |
| 36 |
|
| 37 |
[Sanitizing HTML Output]: sanitization.md |
| 38 |
|
| 39 |
The following options are available on the `markdown.markdown` function: |
| 40 |
|
| 41 |
__text__{: #text } |
| 42 |
|
| 43 |
: The source Unicode string. (required) |
| 44 |
|
| 45 |
!!! note "Important" |
| 46 |
Python-Markdown expects a **Unicode** string as input (some simple ASCII binary strings *may* work only by |
| 47 |
coincidence) and returns output as a Unicode string. Do not pass binary strings to it! If your input is |
| 48 |
encoded, (e.g. as UTF-8), it is your responsibility to decode it. For example: |
| 49 |
|
| 50 |
:::python |
| 51 |
with open("some_file.txt", "r", encoding="utf-8") as input_file: |
| 52 |
text = input_file.read() |
| 53 |
html = markdown.markdown(text) |
| 54 |
|
| 55 |
If you want to write the output to disk, you *must* encode it yourself: |
| 56 |
|
| 57 |
:::python |
| 58 |
with open("some_file.html", "w", encoding="utf-8", errors="xmlcharrefreplace") as output_file: |
| 59 |
output_file.write(html) |
| 60 |
|
| 61 |
__extensions__{: #extensions } |
| 62 |
|
| 63 |
: A list of extensions. |
| 64 |
|
| 65 |
Python-Markdown provides an [API](extensions/api.md) for third parties to |
| 66 |
write extensions to the parser adding their own additions or changes to the |
| 67 |
syntax. A few commonly used extensions are shipped with the markdown |
| 68 |
library. See the [extension documentation](extensions/index.md) for a |
| 69 |
list of available extensions. |
| 70 |
|
| 71 |
The list of extensions may contain instances of extensions and/or strings |
| 72 |
of extension names. |
| 73 |
|
| 74 |
:::python |
| 75 |
extensions=[MyExtClass(), 'myext', 'path.to.my.ext:MyExtClass'] |
| 76 |
|
| 77 |
!!! note |
| 78 |
The preferred method is to pass in an instance of an extension. Strings |
| 79 |
should only be used when it is impossible to import the Extension Class |
| 80 |
directly (from the command line or in a template). |
| 81 |
|
| 82 |
When passing in extension instances, each class instance must be a subclass |
| 83 |
of `markdown.extensions.Extension` and any configuration options should be |
| 84 |
defined when initiating the class instance rather than using the |
| 85 |
[`extension_configs`](#extension_configs) keyword. For example: |
| 86 |
|
| 87 |
:::python |
| 88 |
from markdown.extensions import Extension |
| 89 |
class MyExtClass(Extension): |
| 90 |
# define your extension here... |
| 91 |
|
| 92 |
markdown.markdown(text, extensions=[MyExtClass(option='value')]) |
| 93 |
|
| 94 |
If an extension name is provided as a string, the string must either be the |
| 95 |
registered entry point of any installed extension or the importable path |
| 96 |
using Python's dot notation. |
| 97 |
|
| 98 |
See the documentation specific to an extension for the string name assigned |
| 99 |
to an extension as an entry point. Simply include the defined name as |
| 100 |
a string in the list of extensions. For example, if an extension has the |
| 101 |
name `myext` assigned to it and the extension is properly installed, then |
| 102 |
do the following: |
| 103 |
|
| 104 |
:::python |
| 105 |
markdown.markdown(text, extensions=['myext']) |
| 106 |
|
| 107 |
If an extension does not have a registered entry point, Python's dot |
| 108 |
notation may be used instead. The extension must be installed as a |
| 109 |
Python module on your PYTHONPATH. Generally, a class should be specified in |
| 110 |
the name. The class must be at the end of the name and be separated by a |
| 111 |
colon from the module. |
| 112 |
|
| 113 |
Therefore, if you were to import the class like this: |
| 114 |
|
| 115 |
:::python |
| 116 |
from path.to.module import MyExtClass |
| 117 |
|
| 118 |
Then load the extension as follows: |
| 119 |
|
| 120 |
:::python |
| 121 |
markdown.markdown(text, extensions=['path.to.module:MyExtClass']) |
| 122 |
|
| 123 |
If only one extension is defined within a module and the module includes a |
| 124 |
`makeExtension` function which returns an instance of the extension, then |
| 125 |
the class name is not necessary. For example, in that case one could do |
| 126 |
`extensions=['path.to.module']`. Check the documentation for a specific |
| 127 |
extension to determine if it supports this feature. |
| 128 |
|
| 129 |
When loading an extension by name (as a string), you can only pass in |
| 130 |
configuration settings to the extension by using the |
| 131 |
[`extension_configs`](#extension_configs) keyword. |
| 132 |
|
| 133 |
!!! seealso "See Also" |
| 134 |
See the documentation of the [Extension API](extensions/api.md) for |
| 135 |
assistance in creating extensions. |
| 136 |
|
| 137 |
__extension_configs__{: #extension_configs } |
| 138 |
|
| 139 |
: A dictionary of configuration settings for extensions. |
| 140 |
|
| 141 |
Any configuration settings will only be passed to extensions loaded by name |
| 142 |
(as a string). When loading extensions as class instances, pass the |
| 143 |
configuration settings directly to the class when initializing it. |
| 144 |
|
| 145 |
!!! Note |
| 146 |
The preferred method is to pass in an instance of an extension, which |
| 147 |
does not require use of the `extension_configs` keyword at all. |
| 148 |
See the [extensions](#extensions) keyword for details. |
| 149 |
|
| 150 |
The dictionary of configuration settings must be in the following format: |
| 151 |
|
| 152 |
:::python |
| 153 |
extension_configs = { |
| 154 |
'extension_name_1': { |
| 155 |
'option_1': 'value_1', |
| 156 |
'option_2': 'value_2' |
| 157 |
}, |
| 158 |
'extension_name_2': { |
| 159 |
'option_1': 'value_1' |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
When specifying the extension name, be sure to use the exact same |
| 164 |
string as is used in the [extensions](#extensions) keyword to load the |
| 165 |
extension. Otherwise, the configuration settings will not be applied to |
| 166 |
the extension. In other words, you cannot use the entry point in on |
| 167 |
place and Python dot notation in the other. While both may be valid for |
| 168 |
a given extension, they will not be recognized as being the same |
| 169 |
extension by Markdown. |
| 170 |
|
| 171 |
See the documentation specific to the extension you are using for help in |
| 172 |
specifying configuration settings for that extension. |
| 173 |
|
| 174 |
__output_format__{: #output_format }: |
| 175 |
|
| 176 |
: Format of output. |
| 177 |
|
| 178 |
Supported formats are: |
| 179 |
|
| 180 |
* `"xhtml"`: Outputs XHTML style tags. **Default**. |
| 181 |
* `"html"`: Outputs HTML style tags. |
| 182 |
|
| 183 |
The values can be in either lowercase or uppercase. |
| 184 |
|
| 185 |
__tab_length__{: #tab_length }: |
| 186 |
|
| 187 |
: Length of tabs in the source. Default: 4 |
| 188 |
|
| 189 |
### `markdown.markdownFromFile (**kwargs)` {: #markdownFromFile data-toc-label='markdown.markdownFromFile' } |
| 190 |
|
| 191 |
!!! warning |
| 192 |
|
| 193 |
The Python-Markdown library does ***not*** sanitize its HTML output. As |
| 194 |
`markdown.markdownFromFile` writes directly to the file system, there is |
| 195 |
no easy way to sanitize the output from Python code. Therefore, it is |
| 196 |
recommended that the `markdown.markdownFromFile` function not be used on |
| 197 |
input from an untrusted source. For more information see [Sanitizing HTML |
| 198 |
Output]. |
| 199 |
|
| 200 |
With a few exceptions, `markdown.markdownFromFile` accepts the same options as |
| 201 |
`markdown.markdown`. It does **not** accept a `text` (or Unicode) string. |
| 202 |
Instead, it accepts the following required options: |
| 203 |
|
| 204 |
__input__{: #input } (required) |
| 205 |
|
| 206 |
: The source text file. |
| 207 |
|
| 208 |
`input` may be set to one of three options: |
| 209 |
|
| 210 |
* a string which contains a path to a readable file on the file system, |
| 211 |
* a readable file-like object, |
| 212 |
* or `None` (default) which will read from `stdin`. |
| 213 |
|
| 214 |
__output__{: #output } |
| 215 |
|
| 216 |
: The target which output is written to. |
| 217 |
|
| 218 |
`output` may be set to one of three options: |
| 219 |
|
| 220 |
* a string which contains a path to a writable file on the file system, |
| 221 |
* a writable file-like object, |
| 222 |
* or `None` (default) which will write to `stdout`. |
| 223 |
|
| 224 |
__encoding__{: #encoding } |
| 225 |
|
| 226 |
: The encoding of the source text file. |
| 227 |
|
| 228 |
Defaults to `"utf-8"`. The same encoding will always be used for input and output. |
| 229 |
The `xmlcharrefreplace` error handler is used when encoding the output. |
| 230 |
|
| 231 |
!!! Note |
| 232 |
This is the only place that decoding and encoding of Unicode |
| 233 |
takes place in Python-Markdown. If this rather naive solution does not |
| 234 |
meet your specific needs, it is suggested that you write your own code |
| 235 |
to handle your encoding/decoding needs. |
| 236 |
|
| 237 |
### `markdown.Markdown([**kwargs])` {: #Markdown data-toc-label='markdown.Markdown' } |
| 238 |
|
| 239 |
The same options are available when initializing the `markdown.Markdown` class |
| 240 |
as on the [`markdown.markdown`](#markdown) function, except that the class does |
| 241 |
**not** accept a source text string on initialization. Rather, the source text |
| 242 |
string must be passed to one of two instance methods. |
| 243 |
|
| 244 |
!!! warning |
| 245 |
|
| 246 |
Instances of the `markdown.Markdown` class are only thread safe within |
| 247 |
the thread they were created in. A single instance should not be accessed |
| 248 |
from multiple threads. |
| 249 |
|
| 250 |
#### `Markdown.convert(source)` {: #convert data-toc-label='Markdown.convert' } |
| 251 |
|
| 252 |
!!! warning |
| 253 |
|
| 254 |
The Python-Markdown library does ***not*** sanitize its HTML output. If |
| 255 |
you are processing Markdown input from an untrusted source, it is your |
| 256 |
responsibility to ensure that it is properly sanitized. For more |
| 257 |
information see [Sanitizing HTML Output]. |
| 258 |
|
| 259 |
The `source` text must meet the same requirements as the [`text`](#text) |
| 260 |
argument of the [`markdown.markdown`](#markdown) function. |
| 261 |
|
| 262 |
You should also use this method if you want to process multiple strings |
| 263 |
without creating a new instance of the class for each string. |
| 264 |
|
| 265 |
```python |
| 266 |
md = markdown.Markdown() |
| 267 |
html1 = md.convert(text1) |
| 268 |
html2 = md.convert(text2) |
| 269 |
``` |
| 270 |
|
| 271 |
Depending on which options and/or extensions are being used, the parser may |
| 272 |
need its state reset between each call to `convert`. |
| 273 |
|
| 274 |
```python |
| 275 |
html1 = md.convert(text1) |
| 276 |
md.reset() |
| 277 |
html2 = md.convert(text2) |
| 278 |
``` |
| 279 |
|
| 280 |
To make this easier, you can also chain calls to `reset` together: |
| 281 |
|
| 282 |
```python |
| 283 |
html3 = md.reset().convert(text3) |
| 284 |
``` |
| 285 |
|
| 286 |
#### `Markdown.convertFile(**kwargs)` {: #convertFile data-toc-label='Markdown.convertFile' } |
| 287 |
|
| 288 |
!!! warning |
| 289 |
|
| 290 |
The Python-Markdown library does ***not*** sanitize its HTML output. As |
| 291 |
`Markdown.convertFile` writes directly to the file system, there is no |
| 292 |
easy way to sanitize the output from Python code. Therefore, it is |
| 293 |
recommended that the `Markdown.convertFile` method not be used on input |
| 294 |
from an untrusted source. For more information see [Sanitizing HTML |
| 295 |
Output]. |
| 296 |
|
| 297 |
The arguments of this method are identical to the arguments of the same |
| 298 |
name on the `markdown.markdownFromFile` function ([`input`](#input), |
| 299 |
[`output`](#output), and [`encoding`](#encoding)). As with the |
| 300 |
[`convert`](#convert) method, this method should be used to |
| 301 |
process multiple files without creating a new instance of the class for |
| 302 |
each document. State may need to be `reset` between each call to |
| 303 |
`convertFile` as is the case with `convert`. |