Skip to main content

A library to creates markdown documents programatically

Project description

Markdown Sausage Machine

A Python3 library to creates markdown documents programatically.

Usage

You can integrate and use the markdown sausage machine via two interfaces:

  1. SausageMachine: A class that lets you call programatically on its instantiated self to create a markdown document.

  2. SausageMan: A class that lets you import a Python dict of content to create a markdown document.

More details on each approach is shown below.

SausageMachine

SausageMachine can be instantiated and controlled programatically to produce a markdown document.

import logging
from pathlib import Path

from markdownsausagemachine.contents import OrderedList, Paragraph, UnorderedList
from markdownsausagemachine.sausage_machine import SausageMachine

my_sausage_machine = SausageMachine()
index_doc = my_sausage_machine.add_document("index")
index_doc.set_header("Index")
new_section = index_doc.add_section("my-sausage-machine")
new_section.set_header("My Sausage Machine")
new_section.add_content(
    Paragraph("P1: How does a sausage machine become a reality")
)
new_section.add_content(
    Paragraph("P2: Going back to the start (a brief history of the world)")
)
new_section.add_content(
    UnorderedList(["A brief intro", "A confusing middle", "A well deserved end"])
)
new_section.add_content(Paragraph("P3: An ordered history of the world"))
new_section.add_content(
    OrderedList(["A brief intro", "A confusing middle", "A well deserved end"])
)

supp_doc = my_sausage_machine.add_document("supplementary-words")
supp_doc.set_header("Supplementary Words")
new_section = supp_doc.add_section("my-sausage-machine")
new_section.add_content(
    Paragraph("P1: Cat Dog Sheep\n\nDog Sheep Cat\n\nSheep Dog Cat")
)
new_section.add_content(Paragraph("P2: Up Down Left Right"))
new_section.add_content(Paragraph("P3: Music Dance Sunshine"))

output_dir = Path("./example1/output")
output_dir.mkdir(parents=True, exist_ok=True)
my_sausage_machine.output_markdown_documents(output_dir)
logger.info("Done! Output created in: %s", output_dir)

SausageMan

SausageMan can be instantiated and wraps a SausageMachine instance, and takes a dictionary as input to produce a markdown document. Said dictionary is of an expected format that is not yet fully documented but the example below provides a rough guideline.

import logging
from pathlib import Path

from markdownsausagemachine.sausage_man import SausageMan

logger = logging.getLogger(__name__)
logging.basicConfig(encoding="utf-8", level=logging.DEBUG)

example_mystery_meat = {
    "schema": "0.1",
    "documents": {
        "index": {
            "header": "Top-Level Header (Index)",
            "lede": "The root of the problems.",
            "sections": [
                {
                    "header": "A Section Header",
                    "contents": [
                        {
                            "type": "paragraph",
                            "text": [
                                "This is a standard paragraph section.",
                                "Multiple lines can be passed in as an array.",
                                "Allowing nicely formatted mystery meat.",
                            ],
                        },
                        {
                            "type": "unordered_list",
                            "items": [
                                "Item 1: Hotdog",
                                "Item 2: Cabbage",
                                "Item 3: Broccoli",
                            ],
                        },
                        {
                            "type": "subsection",
                            "header": "A subsection",
                            "contents": [
                                {
                                    "type": "paragraph",
                                    "text": ["A paragraph in a subsection."],
                                }
                            ],
                        },
                    ],
                }
            ],
        },
        "supplementary": {
            "header": "Top-Level Header (Supp)",
            "lede": "Additional problems.",
            "sections": [
                {
                    "header": "A Section Header",
                    "contents": [
                        {
                            "type": "paragraph",
                            "text": [
                                "This is a standard paragraph section.",
                            ],
                        },
                        {
                            "type": "ordered_list",
                            "items": [
                                "Item 1: Random",
                                "Item 2: Items",
                                "Item 3: Are not hotdogs",
                            ],
                        },
                    ],
                }
            ],
        },
    },
}

my_sausage_man = SausageMan()
my_sausage_man.give_ingredients(example_mystery_meat)

output_dir = Path("./example2/output")
output_dir.mkdir(parents=True, exist_ok=True)
my_sausage_man.get_markdown_files(output_dir)
logger.info("Done! Output created in: %s", output_dir)

SausageMan also supports enforcing "promises" around the digested content. This allows you to enforce things such as required documents, required contents, etc.

import logging

from markdownsausagemachine.sausage_man import IngredientPromise, SausageMan

logger = logging.getLogger(__name__)
logging.basicConfig(encoding="utf-8", level=logging.DEBUG)

def check_has_index_doc(ingredients: dict[str, Any]) -> bool:
    """Return True if promise met else False"""
    documents = ingredients.get("documents", {})
    return "index" in documents

promises = [
    IngredientPromise("An index document is present.", check_has_index_doc),
]

my_sausage_man = SausageMan()
my_sausage_man.give_ingredients(example_mystery_meat)
my_sausage_man.give_promises(promises)
promises_met = my_sausage_man.check_promises()
if not promises_met:
    logger.error("Quality not met! Ingredients are not good!")

Developers

The following is information for developers working with extending/enhancing this library.

Unit Tests

You can run the unit tests like so:

pdm install --dev
pdm unit-test

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

markdownsausagemachine-0.2.1.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

markdownsausagemachine-0.2.1-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file markdownsausagemachine-0.2.1.tar.gz.

File metadata

  • Download URL: markdownsausagemachine-0.2.1.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.22.3 CPython/3.13.2 Linux/6.8.0-1021-azure

File hashes

Hashes for markdownsausagemachine-0.2.1.tar.gz
Algorithm Hash digest
SHA256 26eed7a5719dd46647c8bde7016a1ee9df06be0a3ed897afadc7bfa80c0320e9
MD5 8a0ee9435cb905dd86201a362cdf80eb
BLAKE2b-256 ee188b23b862db9dd004b3038bb1881cf34002a9eaa0ec49f321f2e6192dc0f6

See more details on using hashes here.

File details

Details for the file markdownsausagemachine-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for markdownsausagemachine-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e394223774f7de4621083c948b68ecf43b8ab1ca744b657f69a6b4771ffe1f7c
MD5 4dec613e24d0493a3a270edb36c96c24
BLAKE2b-256 11b2fe9b1445a56e12a2076ea74d2864569c58de917a9e94b3cadc0d64bc8454

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