Simple markdown generator for Python.
Reason this release was yanked:
deprecated
Project description
t-python-markdown
Simple markdown generator for Python.
Note: This package does not validate markdown.
Installation
pip install t-python-markdown
Example
from t_python_markdown import Document, Header, Paragraph, Sentence, Bold, Table, UnorderedList
import time
import requests
j = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json").json()
bpi = j["bpi"]
front_matter = {
"title": j["chartName"],
"authors": ["A.U.Thor"],
"date": time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
}
doc = Document(front_matter)
doc >> Header(j["chartName"], 1)
doc >> Paragraph([j["disclaimer"]])
al = [("--:" if isinstance(_, (int, float)) else ":-:" if _.startswith("&") else ":--") for _ in bpi[list(bpi.keys())[0]].values()]
t = Table([_.replace("_", " ").title() for _ in bpi[list(bpi.keys())[0]].keys()], alignment=al)
doc >> t
ul = UnorderedList()
doc >> Paragraph("Bitcoin Price Index")
doc >> ul
for k, v in bpi.items():
t >> [_ for _ in bpi[k].values()]
ul >> Sentence([Bold(k), bpi[k]["description"]])
# Write markdown to file
doc.write("example.md")
Saved as example.py
then running python example.py
results in:
---
title: Bitcoin
authors:
- A.U.Thor
date: '2023-02-25T14:17:02Z'
...
# Bitcoin
This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org
| **Code** | **Symbol** | **Rate** | **Description** | **Rate Float** |
| :-- | :-: | :-- | :-- | --: |
| USD | $ | 23,007.6135 | United States Dollar | 23007.6135 |
| GBP | £ | 19,224.9778 | British Pound Sterling | 19224.9778 |
| EUR | € | 22,412.7746 | Euro | 22412.7746 |
Bitcoin Price Index
- **USD** United States Dollar.
- **GBP** British Pound Sterling.
- **EUR** Euro.
Usage
Document
Document
takes one optional argument, front matter, a dictionary, and is output as YAML.
doc = Document(front_matter)
doc.write("example.md")
Header
Header
takes two arguments, a title and a level. The level relates to the header tags <h1>
- <h6>
:
header = Header("Header Title", level)
doc >> header
Paragraph
Paragraph
takes a list of sentences. A sentence could be a simple string or a Sentence
(see below).
p = Paragraph(["A sentence.", Sentence(["The quick", "brown fox"])])
doc >> p
Sentence
Sentence
takes three arguments, an array of strings or other markdown objects, a separator (defaults to space) and an end character (defaults to a full stop).
s = Sentence(["The quick", "brown fox"], end="!")
doc >> s
Horizontal Rule
HorizontalRule
takes no arguments and produces a ---
in the output.
hr = HorizontalRule()
doc >> hr
Link
Link
takes three arguments, a title, a url and an optional alternate title.
l = Link("Title", "http://localhost/")
doc >> l
Image
Image
takes two arguments, a title and a url (path to image).
i = Image("Picture", "/images/nice_piccie.png")
doc >> i
Bold
Bold
takes one argument, the text to be bolded.
bt = Bold("Bold Text")
doc >> bt
Italic
Italic
takes one argument, the text to be italicised.
it = Italic("Italic Text")
doc >> it
Bold/Italic
BoldItalic
takes one argument, the text to be bolded and italicised.
bit = BoldItalic("Bold/Italic Text")
doc >> bit
Strikethrough
Strikethrough
takes one argument, the strikethrough text.
st = Strikethrough("Strikethrough Text")
doc >> st
Code
Code
takes one argument, the text to be output as code.
c = Code("find / -name README.md")
doc >> c
Code Block
CodeBlock
takes one argument, the text to output as code in a block.
cb = CodeBlock("ls\nfind / -name README.md")
doc >> cb
Unordered List
UnorderedList
takes one optional argument, a list of items. The resultant object can have further items added.
ul = UnorderedList()
ul >> "List entry 1"
ul >> "List entry 2"
doc >> ul
Ordered List
OrderedList
takes one optional argument, a list of items. The resultant object can have further items added.
ol = OrderedList()
ol >> "List entry 1"
ol >> "List entry 2"
doc >> ol
Table
Table
takes two arguments, a list of column headers and a list of column alignments. The resultant object is then used to add rows, each row being a list of columns. The number of column alignments is adjusted to match the number of columns. If the number of alignments is too small, the last alignment is repeated to fill the missing ones. If no alignment is provided, it defaults to centred (":-:"
).
t = Table(["Id", "Description"], alignment=[":--"])
t >> ["1", "One"]
t >> ["2", "Two"]
doc >> t
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for t_python_markdown-1.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 158d27863af1be0a1a3b99777b02e2caf114cc8df8d6ac20c976a70f89a5100e |
|
MD5 | 9ec34fe2dd319e2f2b1195b331ee4beb |
|
BLAKE2b-256 | 4d5bb4ca8792795aa584d88381922b279b586409b11943cf115af938118d30f4 |