Skip to main content

A library to simplify creating markdown documents.

Project description

[TOC]

python-markdown-library

Description

pmd_lib is a library to easily generate markdown documents using code.

Usage examples

headlines

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
d += Headline('This is a level 1 headline')
d += Headline('This is also a level 1 headline', level=1)
d += Headline('This is a level 2 headline', level=2)
d += Headline('This is a level 3 headline', level=3)
d += Headline('This is a level 4 headline', level=4)
d += Headline('This is a level 5 headline', level=5)
d += Headline('This is a level 6 headline', level=6)

paragraphs

There are three ways to make paragraphs.

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()

# adding the paragraph directly to the document.
d += Text('This is a text for the first paragraph.'
          ' It even has multiple sentences that will be split into multiple lines.')
d += Text('With this method I can add more text to a paragraph.')
d += Paragraph()
d += Text('This is a text for the second paragraph.'
          ' It even has multiple sentences that will be split into multiple lines.')
d += Paragraph()

# treating a paragraph as an object and adding the entire paragraph to the document.
p1 = Paragraph()
p1 += Text('This is a text for the first paragraph.'
           ' It even has multiple sentences that will be split into multiple lines.')
p1 += Text('With this method I can also add more text to a paragraph.')
p2 = Paragraph()
p2 += Text('This is a text for the second paragraph.'
           ' It even has multiple sentences that will be split into multiple lines.')

d += p1
d += p2

d += Paragraph(['This is the third option to create paragraphs',
                'you can simply provide text, or other markdown elements like: ',
                Bold('Bold, '),
                Code('and code.')])
print(d.compile())

bold, italics, strikethrough

Bold, Italics, and Strikethrough can be nested, combined and used in line together with strings.

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
d += Paragraph([Bold('This is a bold sentence.'),
                Italics('But this also works with italics.'),
                Bold(Italics('We can even combine the two.')),
                Italics(Bold('And it is not important in which order.')),
                Strikethrough('If we want we can also create strikethrough text.'),
                Strikethrough(Bold('Surprise, you can make it bold.')),
                Strikethrough(Italics('Or you can make it italic.')),
                Strikethrough(Italics(Bold('This also works in all combinations.'))),
                Italics(Bold(Strikethrough('See, this is exactly the same.'))),
                Strikethrough(Code('You can even combine it with inline code.')),
                ])
print(d.compile())

blockquote

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
d += Blockquote("This is a blockquote.\nIt has multiple lines.")

d += Blockquote("This is a blockquote.\nIt has multiple lines."
                + Blockquote('It even has a nested quote.'
                             + Code('this is a piece of code')))
print(d.compile())

lists

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
# Add an unordered list
d += MarkdownList(["Item 1", "Item 2", "Item 3"])

# Add an ordered list
d += MarkdownList(["Item 1", "Item 2", "Item 3"], ordered=True)

# Add a task list
d += MarkdownList([("Task 1", False),
                   ("Task 2", True),
                   'Task 3'],
                  task=True)
print(d.compile())

Code

Inline code is extremely simple by using the Code(content=str) class. For Block-code there are three ways to create it. You can either provide the entire string, or build it line by line.

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
# option 1
d += CodeBlock("def hello_world():\n    print('Hello, world!')")
# option 2
d += CodeBlock(["def hello_world():",
                "    print('Hello, world!')"])
# option 3
cb = CodeBlock()
cb += "def hello_world():"
cb += "    print('Hello, world!')"
d += cb
print(d.compile())

links and images

There are two ways of creating images and links. Directly and via references.

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
# For inline links
d += Link('link text', 'https://google.com')

# For reference-style links
d += Link('link text', reference='link_ref')

# For inline images
d += Image('Image', 'placeholder.png')

# For reference-style images
d += Image('Image', reference='Image_ref')

# At the bottom of the document, you'd define the image reference like this:
d += Reference('link_ref', 'https://google.com')
d += Reference('Image_ref', 'placeholder.png')
print(d.compile())

For reference style there is also a ReferenceManager that makes everything easier.

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
reference_manager = ReferenceManager()

d += reference_manager.new_link('link text', 'https://google.com', inline=False)
d += reference_manager.new_image('image description', 'placeholder.png')

d += reference_manager
print(d.compile())

tables

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
d += Table(header=[Strikethrough('one'), 'two', 'three'],
           alignment=['<', '^', '>'],
           rows=[[1, 2, 3], [2, 3, 4]])
print(d.compile())

You can also progressively build tables.

from pmd_lib import Document
from pmd_lib.elements.common import *

d = Document()
t = Table(header=[Strikethrough('one'), 'two', 'three'],
          alignment=['<', '^', '>'],
          rows=[[1, 2, 3], [2, 3, 4]])
t += [9, 9, 9]
d += t
print(d.compile())

Contributing

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

pmd_lib-0.0.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

pmd_lib-0.0.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pmd_lib-0.0.2.tar.gz.

File metadata

  • Download URL: pmd_lib-0.0.2.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pmd_lib-0.0.2.tar.gz
Algorithm Hash digest
SHA256 dcd2062672956cb2dc3d92dc21c5a091cd07343573f5910ab16cb1f344fc4b9a
MD5 0fb388bf0519f12a57ce7718d1461956
BLAKE2b-256 333dc6e14f263170210ae180b5180666ea4ec966182cd8180ddda0c25bb42724

See more details on using hashes here.

File details

Details for the file pmd_lib-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pmd_lib-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pmd_lib-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 27856da908fde992df0b4bccc801e9cde38778f794d1bd898fbf89169055d80a
MD5 09bf8c2c1021c030fa61f78b2c663a83
BLAKE2b-256 c98e3d111f996be1a8383d6211d0503a039a946b440adda332ac60681fdafd1a

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