Skip to main content

Markdown Meta Extension for using Python functions and classes from within documents.

Project description

pipeline status

Markdown Meta Extension

The Markdown Meta Extension, a Python-Markdown extension, enables users to use Python functions and classes from within Markdown documents and assign results to variables.

This software has been created by Martin Schorfmann (@schorfma) for his bachelor thesis at the Universität Bremen with the following title:

Markdown Meta Extension: Providing programmable Content in Markdown Documents

Getting Started

Prerequisites

You need to have a working installation of

  • Python3.7.0

Installation

Install from PyPI

Execute the following command to install the Markdown Meta Extension from PyPI.

$ pip install markdown-meta-extension

Install from the GitLab repository

Execute the following command to install the Markdown Meta Extension from this Git repository.

$ pip install git+https://gitlab.com/markdown-meta-extension/markdown-meta-extension.git

Install from downloaded source

Execute the following command in the main directory of the downloaded repository for manual installation of Markdown Meta Extension.

$ pip install .

Usage

Usage as Python-Markdown extension

The extension name for use with Python-Markdown is markdown_meta_extension.

The extension can be used with Python-Markdown as follows:

import markdown

parser = markdown.Markdown(extensions=["markdown_meta_extension"])
# Convert a Markdown Meta Extension string
parser.convert("""
---
now: datetime:datetime.now
---

{{ now }}
""")

Usage of the CLI command

  1. Navigate to the directory the document is in.
  2. Install requirements of custom Python modules, if needed.
  3. Convert the document using the command markdown-meta-extension
$ cd ./example
$ pip install -r requirements.txt
$ markdown-meta-extension ./document.md --yes
Output path document.html is assumed.
3 callables were imported.
19 calls were made.
File document.html will be overwritten.
Output written to file document.html

Available options

$ markdown-meta-extension --help
Usage: markdown-meta-extension [OPTIONS] INPUT_PATH

  CLI command for converting an input Markdown file to an output HTML file.

  [...]

Options:
  -o, --output PATH  The optional path for the HTML output.
  -q, --quiet        Mute all command line output.
  -v, --verbose      Display additional command line output and file contents.
  -y, --yes          Confirm overwriting existing output file.
  --help             Show this message and exit.

Tutorial

Import Front Matter

The Markdown document needs to start with a YAML front matter, in which the import statements are assigned to variables:

---
Date: datetime:date                     # Import of class from an installed package
now: datetime:datetime.now              # Import of a class method
echo: ./echo_module.py:echo_function    # Import of a function from a file system module
demo: ./jinja_template.html:demo_macro  # Import of a Jinja macro from a Jinja HTML Template
---

The import statements use a : to separate the top-level package or file name from the remaining import statement.

For relative import paths the current working directory (where the command is executed) is the base for the relative paths.

The possible import types are the following:

  • Installed Python package
    • Any defined variable or callable
  • Python module in file system
    • Any defined variable or callable
  • Jinja HTML template
    • Jinja Macro defined in template

Outsourcing Common Imports

import:
- ../base_imports.yaml
- ./more_specific_imports.yaml
echo: ./echo_module.py:echo_function

It is possible to outsource commonly used imports into dedicated YAML files. The outsourced imports are imported in order, potentially overwriting duplicate keys with the import defined the later file or the front matter itself.

Call Blocks

Call blocks are used to invoke a callable with potential parameters. Any import key or variable can be either directly invoked or subscripted to invoke a method or attribute. The parameters can be either a YAML list or mapping. If no parameters are needed they can be omitted entirely.

If no assignment to a variable is made, the result gets displayed unless the invocation returns an empty result. When assigning the result to a variable, a Force Display Flag (!) is needed to display the result.

{{! variable_name = ClassName.class_method_name
argument: value
}}

Calling a function without parameters

{{ now }}

Calling a function with parameters

Block with list parameters:

{{ echo
- My Message
}}
Inline with list parameters:

{{ echo [My Message] }}
Block with mapping parameters:

{{ echo
message: My Message
}}
Inline with mapping parameters:

{{ echo {message: My Message} }}

Instantiating a class

{{! board = ChessBoard }}
{{ board.move
move: a2a4
}}

Nesting

Call blocks can be nested if needed. It is important, that they are explicitly marked as strings in YAML

{{ sum
- 42
- "{{ my_number }}"
}}

Variable Blocks

Variable blocks allow for defining multiple variables in one block.

{{
number: 42
quote: |
  First Line.
  Second Line.
pi: "{{ get_pi }}"
budget: |
  {{ money
  value: 12.34
  currency: EUR
  }}
}}

Standalone Documents

Standalone documents are used to insert the result document in a Jinja template together with additional meta data variables.

---
meta-data:
  template: ./template.html
  title: The Title
  author: The Author
today: datetime:date.today
# ...

Outsourced Meta Data Imports

The outsourcing of meta data follows the same rules like the outsourcing of imports.

meta-data-import:
- ../base-information.yaml
- ./external-data.yaml
meta-data:
  title: Another Title
today: datetime:date.today
# ...

Behavior

If an object has an __html__() method, it is used as a representation. The string representation __str__()is used as a fallback.

The syntax to access attributes and methods can also be used to access the __getitem__() builtin method.

Some data types have a default display behavior. For example lists of items are displayed as an HTML unordered list when used in a block, and as comma-separated values when used inline.

Examples

Seven examples have been implemented in the subdirectories of the directory examples

Used Libraries

Without these libraries the development of the Markdown Meta Extension would not have been possible. Thanks to their authors and contributors!

Libraries used in Tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown-meta-extension-0.5.2.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

markdown_meta_extension-0.5.2-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file markdown-meta-extension-0.5.2.tar.gz.

File metadata

  • Download URL: markdown-meta-extension-0.5.2.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for markdown-meta-extension-0.5.2.tar.gz
Algorithm Hash digest
SHA256 7e26f2570c89311f99a5ce342d5da6925aeb1ab30d6589084f3b4494265fb730
MD5 777ab0a34c5fac7b04ef95345b074fef
BLAKE2b-256 4aa316133010f0c43aad252c04f9acbacde6d4e5543e69057240dc524fcfafba

See more details on using hashes here.

File details

Details for the file markdown_meta_extension-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: markdown_meta_extension-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for markdown_meta_extension-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 61e638ab31e719fc2dc69e245e6f1b3b3eb354f20335408c26a5cf7412d917e1
MD5 c00c2f8ebc7ccf659cb1dafcb14fc7d2
BLAKE2b-256 74e79bb7494aa1b38d8db5fd8121ea0e5a06d7416c156523d130b0f51aa4f571

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page