| 1 |
title: Command Line |
| 2 |
|
| 3 |
Using Python-Markdown on the Command Line |
| 4 |
========================================= |
| 5 |
|
| 6 |
While Python-Markdown is primarily a python library, a command line script is |
| 7 |
included as well. While there are many other command line implementations |
| 8 |
of Markdown, you may not have them installed, or you may prefer to use |
| 9 |
Python-Markdown's various extensions. |
| 10 |
|
| 11 |
Generally, you will want to have the Markdown library fully installed on your |
| 12 |
system to run the command line script. See the |
| 13 |
[Installation instructions](install.md) for details. |
| 14 |
|
| 15 |
## Basic Usage |
| 16 |
|
| 17 |
Python-Markdown's command line script takes advantage of Python's `-m` flag. |
| 18 |
Therefore, assuming the python executable is on your system path, use the |
| 19 |
following format: |
| 20 |
|
| 21 |
```bash |
| 22 |
python -m markdown [options] [args] |
| 23 |
``` |
| 24 |
|
| 25 |
That will run the module as a script with the options and arguments provided. |
| 26 |
|
| 27 |
At its most basic usage, one would simply pass in a file name as the only argument: |
| 28 |
|
| 29 |
```bash |
| 30 |
python -m markdown input_file.txt |
| 31 |
``` |
| 32 |
|
| 33 |
Use the `--help` option for a list of all available options and arguments: |
| 34 |
|
| 35 |
```bash |
| 36 |
python -m markdown --help |
| 37 |
``` |
| 38 |
|
| 39 |
!!! warning |
| 40 |
|
| 41 |
The Python-Markdown library does ***not*** sanitize its HTML output. If |
| 42 |
you are processing Markdown input from an untrusted source, it is your |
| 43 |
responsibility to ensure that it is properly sanitized. For more |
| 44 |
information see [Sanitizing HTML Output](sanitization.md). |
| 45 |
|
| 46 |
## Piping Input and Output |
| 47 |
|
| 48 |
Piping input and output (on `STDIN` and `STDOUT`) is fully supported. |
| 49 |
For example: |
| 50 |
|
| 51 |
```bash |
| 52 |
echo "Some **Markdown** text." | python -m markdown > output.html |
| 53 |
``` |
| 54 |
|
| 55 |
The above command would generate a file named `output.html` with the following content: |
| 56 |
```html |
| 57 |
<p>Some <strong>Markdown</strong> Text.</p> |
| 58 |
``` |
| 59 |
|
| 60 |
As Python-Markdown only ever outputs HTML fragments (no `<html>`, `<head>`, |
| 61 |
and `<body>` tags), it is generally expected that the command line interface |
| 62 |
will always be used to pipe output to a templating engine. In the event that |
| 63 |
no additional content is needed and the output only needs to be wrapped in |
| 64 |
otherwise empty `<html>`, `<head>`, and `<body>` tags, |
| 65 |
[JustHTML](https://emilstenstrom.github.io/justhtml/) can do that with with |
| 66 |
a single command: |
| 67 |
|
| 68 |
```bash |
| 69 |
echo "Some **Markdown** text." | python -m markdown | justhtml - --fragment > output.html |
| 70 |
``` |
| 71 |
|
| 72 |
The above command would generate a file named `output.html` with the following content: |
| 73 |
|
| 74 |
```html |
| 75 |
<html> |
| 76 |
<head></head> |
| 77 |
<body> |
| 78 |
<p>Some <strong>Markdown</strong> Text.</p> |
| 79 |
</body> |
| 80 |
</html> |
| 81 |
``` |
| 82 |
|
| 83 |
If you don't need or want JustHTML's HTML sanitation, you can disable it with the |
| 84 |
`--unsafe` flag, although that is not recommended. See JustHTML's |
| 85 |
[Command Line Interface](https://emilstenstrom.github.io/justhtml/cli.html) |
| 86 |
documentation for details. |
| 87 |
|
| 88 |
## Using Extensions |
| 89 |
|
| 90 |
To load a Python-Markdown extension from the command line use the `-x` |
| 91 |
(or `--extension`) option. The extension module must be on your `PYTHONPATH` |
| 92 |
(see the [Extension API](extensions/api.md) for details). The extension can |
| 93 |
then be invoked by the name assigned to an entry point or using Python's dot |
| 94 |
notation to point to an extension |
| 95 |
|
| 96 |
For example, to load an extension with the assigned entry point name `myext`, |
| 97 |
run the following command: |
| 98 |
|
| 99 |
```bash |
| 100 |
python -m markdown -x myext input.txt |
| 101 |
``` |
| 102 |
|
| 103 |
And to load an extension with Python's dot notation: |
| 104 |
|
| 105 |
```bash |
| 106 |
python -m markdown -x path.to.module:MyExtClass input.txt |
| 107 |
``` |
| 108 |
|
| 109 |
To load multiple extensions, specify an `-x` option for each extension: |
| 110 |
|
| 111 |
```bash |
| 112 |
python -m markdown -x myext -x path.to.module:MyExtClass input.txt |
| 113 |
``` |
| 114 |
|
| 115 |
If the extension supports configuration options (see the documentation for the |
| 116 |
extension you are using to determine what settings it supports, if any), you |
| 117 |
can pass them in as well: |
| 118 |
|
| 119 |
```bash |
| 120 |
python -m markdown -x myext -c config.yml input.txt |
| 121 |
``` |
| 122 |
|
| 123 |
The `-c` (or `--extension_configs`) option accepts a file name. The file must be |
| 124 |
in either the [YAML] or [JSON] format and contain YAML or JSON data that would |
| 125 |
map to a Python Dictionary in the format required by the |
| 126 |
[`extension_configs`][ec] keyword of the `markdown.Markdown` class. Therefore, |
| 127 |
the file `config.yaml` referenced in the above example might look like this: |
| 128 |
|
| 129 |
```yaml |
| 130 |
myext: |
| 131 |
option1: 'value1' |
| 132 |
option2: True |
| 133 |
``` |
| 134 |
|
| 135 |
Similarly, a JSON configuration file might look like this: |
| 136 |
|
| 137 |
```json |
| 138 |
{ |
| 139 |
"myext": |
| 140 |
{ |
| 141 |
"option1": "value1", |
| 142 |
"option2": "value2" |
| 143 |
} |
| 144 |
} |
| 145 |
``` |
| 146 |
|
| 147 |
Note that while the `--extension_configs` option does specify the |
| 148 |
`myext` extension, you still need to load the extension with the `-x` option, |
| 149 |
or the configuration for that extension will be ignored. Further, if an |
| 150 |
extension requires a value that cannot be parsed in JSON (for example a |
| 151 |
reference to a function), one has to use a YAML configuration file. |
| 152 |
|
| 153 |
The `--extension_configs` option will only support YAML configuration files if |
| 154 |
[PyYAML] is installed on your system. JSON should work with no additional |
| 155 |
dependencies. The format of your configuration file is automatically detected. |
| 156 |
|
| 157 |
[ec]: reference.md#extension_configs |
| 158 |
[YAML]: https://yaml.org/ |
| 159 |
[JSON]: https://json.org/ |
| 160 |
[PyYAML]: https://pyyaml.org/ |
| 161 |
[2.5 release notes]: change_log/release-2.5.md |
| 162 |
|
| 163 |
## Using the `markdown_py` Command |
| 164 |
|
| 165 |
If you don't want to call the python executable directly (using the `-m` flag), |
| 166 |
follow the instructions below to use a wrapper script: |
| 167 |
|
| 168 |
### Setup `markdown_py` |
| 169 |
|
| 170 |
Upon installation, the `markdown_py` script will have been copied to |
| 171 |
your Python "Scripts" directory. Different systems require different methods to |
| 172 |
ensure that any files in the Python "Scripts" directory are on your system |
| 173 |
path. |
| 174 |
|
| 175 |
* **Windows**: |
| 176 |
|
| 177 |
Assuming a default install of Python on Windows, your "Scripts" directory |
| 178 |
is most likely something like `C:\\Python37\Scripts`. Verify the location |
| 179 |
of your "Scripts" directory and add it to you system path. |
| 180 |
|
| 181 |
Calling `markdown_py` from the command line will call the wrapper batch |
| 182 |
file `markdown_py.bat` in the `"Scripts"` directory created during install. |
| 183 |
|
| 184 |
* __*nix__ (Linux, OSX, BSD, Unix, etc.): |
| 185 |
|
| 186 |
As each \*nix distribution is different and we can't possibly document all |
| 187 |
of them here, we'll provide a few helpful pointers: |
| 188 |
|
| 189 |
* Some systems will automatically install the script on your path. Try it |
| 190 |
and see if it works. Just run `markdown_py` from the command line. |
| 191 |
|
| 192 |
* Other systems may maintain a separate "Scripts" ("bin") directory which |
| 193 |
you need to add to your path. Find it (check with your distribution) and |
| 194 |
either add it to your path or make a symbolic link to it from your path. |
| 195 |
|
| 196 |
* If you are sure `markdown_py` is on your path, but it still is not being |
| 197 |
found, check the permissions of the file and make sure it is executable. |
| 198 |
|
| 199 |
As an alternative, you could just `cd` into the directory which contains |
| 200 |
the source distribution, and run it from there. However, remember that your |
| 201 |
markdown text files will not likely be in that directory, so it is much |
| 202 |
more convenient to have `markdown_py` on your path. |
| 203 |
|
| 204 |
!!!Note |
| 205 |
Python-Markdown uses `"markdown_py"` as a script name because the Perl |
| 206 |
implementation has already taken the more obvious name "markdown". |
| 207 |
Additionally, the default Python configuration on some systems would cause a |
| 208 |
script named `"markdown.py"` to fail by importing itself rather than the |
| 209 |
markdown library. Therefore, the script has been named `"markdown_py"` as a |
| 210 |
compromise. If you prefer a different name for the script on your system, it |
| 211 |
is suggested that you create a symbolic link to `markdown_py` with your |
| 212 |
preferred name. |
| 213 |
|
| 214 |
### Using `markdown_py` |
| 215 |
|
| 216 |
To use `markdown_py` from the command line, run it as |
| 217 |
|
| 218 |
```bash |
| 219 |
markdown_py input_file.txt |
| 220 |
``` |
| 221 |
|
| 222 |
or |
| 223 |
|
| 224 |
```bash |
| 225 |
markdown_py input_file.txt > output_file.html |
| 226 |
``` |
| 227 |
|
| 228 |
For a complete list of options, run |
| 229 |
|
| 230 |
```bash |
| 231 |
markdown_py --help |
| 232 |
``` |