Skip to main content

Python Markdown Generator

Python library for generating HTML sanitised Markdown documents.

It aims to bring a modular approach for dynamically building pure (with some exceptions) Markdown documents with pure Python so that they could consist of multiple reusable components and include some automated tasks such as creating a table of contents.

CircleCI codecov License

But why?

In most cases, Markdown is finally converted into HTML markup language, and there are already multiple libraries for dynamically generating HTML output. Markdown was created initially as a lightweight, user-friendly, user-written text formatting markup language to produce rich text format by using a plain text editor, and the existence of this kind of library might be against the original purpose of the language.

However, there are a few rare use cases where you might want the machine to generate Markdown instead of direct HTML.

  • A machine can ideally generate some documents, but they might be edited by some users later on.
  • There might be a need to generate some Markdown templates, which are finally filled by the end-user. Still, there might be easier ways to create this kind of template.
  • Also, there are some external systems whereof HTML rendering is not supported. Still, you can input Markdown into them: this allows the integration of something automatically into these systems with Markdown.
  • Markdown syntax is simple and not a fully-featured language, resulting in a cleaner and simpler (less buggy!) result. The resulted file itself is lightweight.

The original need for this kind of library came from generating different types of reports automatically into Git repositories with Python. This library was created during the CinCan project to dynamically make malware analysis reports deployed and rendered directly into the Git -repositories. At the time of starting making this library, no complete existing similar library was found. If there is some, let's take this as a programming exercise...

This is not yet-another-Markdown-to-HTML conversion tool.

Quick Install

Python 3.7+ is required.

You can install the latest version from the GitHub by using pip:

pip3 install git+https://github.com/Nicceboy/python-markdown-generator

Quick usage

After installation, the library can be just imported, and we are ready to rock. Method names should be self-descriptive.

The library supports all the syntax currently from the standard Markdown and some partial functionality of GitHub and GitLab syntax.

:TODO Better example, included in wiki maybe, documentation on the way for methods.

from markdowngenerator import MarkdownGenerator

def main():
    with MarkdownGenerator(
        # By setting enable_write as False, content of the file is written
        # into buffer at first, instead of writing directly into the file
        # This enables for example the generation of table of contents
        filename="example.md", enable_write=False
    ) as doc:
        doc.addHeader(1, "Hello there!")
        doc.writeTextLine(f'{doc.addBoldedText("This is just a test.")}')
        doc.addHeader(2, "Second level header.")
        table = [
            {"Column1": "col1row1 data", "Column2": "col2row1 data"},
            {"Column1": "col1row2 data", "Column2": "col2row2 data"},
        ]

        doc.addTable(dictionary_list=table)
        doc.writeTextLine("Ending the document....")

if __name__ == "__main__":
    main()

Which should generate following output:

# Hello there!  
**This is just a test.**  
  
### Table of Contents  
  * [Hello there!](#hello-there)
    * [Second level header.](#second-level-header)
  
## Second level header.  
  
| Column1 | Column2 |  
|:---:|:---:|  
| col1row1 data | col2row1 data |  
| col1row2 data | col2row2 data |  

Ending the document....

Licence

Copyright © 2019-2022 Niklas Saari under the MIT Licence

Metadata

Release files for python-markdown-generator 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-markdown-generator 0.1.0
File Size Uploaded
python_markdown_generator-0.1.0.tar.gz 16.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-markdown-generator 0.1.0
File Interpreter ABI Platform
python_markdown_generator-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 35.3 kB

Release files / python_markdown_generator-0.1.0.tar.gz

Download URL python_markdown_generator-0.1.0.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
dddda18bbeb43755a2ea0a3f4a4a448b50de9798ec8258755cc43b5c1b864a30
BLAKE2b-256 checksum
How to use checksums
ee018b3410c5d6efe214723f8a162b8ffc1debd74aa59fa3f99da614a6bc391e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / python_markdown_generator-0.1.0-py3-none-any.whl

Download URL python_markdown_generator-0.1.0-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5baddf8bbc601a3161a12b24e1d996c3e45776c1f7615e100d6d607d5d52b756
BLAKE2b-256 checksum
How to use checksums
5452c560f8958ef34ebd29c7bcb3a1a86be1e994e1a5c4d73b67903e930e0fba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page