Skip to main content

Python library for dynamically generating HTML sanitized Markdown syntax.

Project description

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

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

python_markdown_generator-0.1.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_markdown_generator-0.1.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file python_markdown_generator-0.1.0.tar.gz.

File metadata

File hashes

Hashes for python_markdown_generator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dddda18bbeb43755a2ea0a3f4a4a448b50de9798ec8258755cc43b5c1b864a30
MD5 3db295345f5b5db5fa9f69200e54fef8
BLAKE2b-256 ee018b3410c5d6efe214723f8a162b8ffc1debd74aa59fa3f99da614a6bc391e

See more details on using hashes here.

File details

Details for the file python_markdown_generator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_markdown_generator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5baddf8bbc601a3161a12b24e1d996c3e45776c1f7615e100d6d607d5d52b756
MD5 a6d146c3555cac1dce014c2bfc2ae5df
BLAKE2b-256 5452c560f8958ef34ebd29c7bcb3a1a86be1e994e1a5c4d73b67903e930e0fba

See more details on using hashes here.

Supported by

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