Python Markdown extension to include local or remote files
Project description
Include extension for Python Markdown. It lets you include local or remote (downloadable) files into your markdown at your desired places.
This project is motivated by markdown-include and provides the same functionalities with some extras.
You should not use markdown-include along with this extension, choose either one, not both.
Syntax
- With explicit encoding:
{! file_path_or_url | encoding !}
- Without explicit encoding:
{! file_path_or_url !}
Install
Install from Pypi:
pip install mdx_include
Usage
text = r"""
some text {! some_file !} some more text {! some_more_file | utf-8 !}
Escaping will give you the exact literal \{! some_file !}
If you escape, then the backslash will be removed.
If you want the backslash too, then provide two more: \\\{! some_file !}
"""
md = markdown.Markdown(extensions=['mdx_include'])
html = md.convert(text)
print(html)
Configuration
Config param | Default | Details |
---|---|---|
base_path |
. |
The base path from which relative paths are normalized. |
encoding |
utf-8 |
The file encoding. |
allow_local |
True |
Whether to allow including local files. |
allow_remote |
True |
Whether to allow including remote files. |
truncate_on_failure |
True |
Whether to truncate the matched include syntax on failure. False value for both allow_local and allow_remote is treated as a failure. |
Example with configuration
configs = {
'mdx_include': {
'base_path': 'mdx_include/test/',
'encoding': 'utf-8',
'allow_local': True,
'allow_remote': True,
'truncate_on_failure': False,
},
}
text = r"""
some text {! some_file !} some more text {! some_more_file | utf-8 !}
Escaping will give you the exact literal \{! some_file !}
If you escape, then the backslash will be removed.
If you want the backslash too, then provide two more: \\\{! some_file !}
"""
md = markdown.Markdown(extensions=['mdx_include'], extension_configs=configs)
html = md.convert(text)
print(html)
Examples
The following markdown:
Including a gist:
```python
{! https://gist.github.com/drgarcia1986/3cce1d134c3c3eeb01bd/raw/73951574d6b62a18b4c342235006ff89d299f879/django_hello.py !}
```
Writing the syntax literally: \{! file_path !}
You just escape it with a backslash.
\\\{! file_path !} -> this one will show the backslash before the syntax in HTML
will produce (with fenced code block enabled):
<p>Including a gist:</p>
<pre><code class="python"># -*- coding: utf-8 -*-
# Settings
from django.conf import settings
settings.configure(
DEBUG=True,
SECRET_KEY='secretfoobar',
ROOT_URLCONF=__name__,
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
)
# Views
from django.http import HttpResponse
from django.conf.urls import url
def index(request):
return HttpResponse('<h1>Hello Word</h1>')
# Routes
urlpatterns = (
url(r'^$', index),
)
# RunServer
if __name__ == '__main__':
from django.core.management import execute_from_command_line
import sys
execute_from_command_line(sys.argv)
</code></pre>
<p>Writing the syntax literally: {! file_path !}</p>
<p>You just escape it with a backslash.</p>
<p>\{! file_path !} -> this one will show the backslash before the syntax in HTML</p>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mdx_include-1.0.1.tar.gz
(5.4 kB
view details)
Built Distribution
File details
Details for the file mdx_include-1.0.1.tar.gz
.
File metadata
- Download URL: mdx_include-1.0.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f23224ae52212990d8fdc732f9867706725398d669ce9786b7822ff38df53fc4 |
|
MD5 | ac59da940924b16a234712e412e6597d |
|
BLAKE2b-256 | 63339cf2a17ad1492fb68932b4760c23ed2f817ef811671fad44285ecd4a044e |
File details
Details for the file mdx_include-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: mdx_include-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5008d12e560c21eee00fe2a90d2c2fd8eb2dd0b7ab37d9db145af67f410893c6 |
|
MD5 | 1cf3f9bb8e0538d5b08293341e9e732c |
|
BLAKE2b-256 | 1d835ee43308f8781de367476d69d4f3da8bff789e151d88f3fa3faa14e09315 |