mdfy package¶
Subpackages¶
Submodules¶
Module contents¶
- class mdfy.MdCode(code: str, inline: bool = False, syntax: str = '')[source]¶
Bases:
MdElementRepresents Markdown code or code block.
Examples
>>> code = MdCode("print('Hello World!')", inline=True) >>> print(code) `print('Hello World!')` >>> code = MdCode("print('Hello World!')", inline=False, syntax="python") >>> print(code) ```python print('Hello World!') ```
- class mdfy.MdHeader(content: str, level: int = 1)[source]¶
Bases:
MdElementRepresents a Markdown header.
Examples
>>> from mdfy.elements import MdHeader >>> >>> header = MdHeader("This is a header") >>> print(header) # This is a header >>> >>> header = MdHeader("This is a header", level=2) >>> print(header) ## This is a header
- class mdfy.MdHorizontal(content: str = '***')[source]¶
Bases:
MdElementRepresents a Markdown horizontal rule.
Examples
>>> from mdfy.elements import MdHorizontal >>> >>> horizontal = MdHorizontal() >>> print(horizontal) *** >>> >>> horizontal = MdHorizontal("---") >>> print(horizontal) ---
- class mdfy.MdImage(src: str, alt: str = '')[source]¶
Bases:
MdElementRepresents a Markdown image.
Examples
>>> from mdfy.elements import MdImage >>> >>> image = MdImage("https://example.com/image.png") >>> print(image)  >>> >>> image = MdImage("https://example.com/image.png", alt="Example image") >>> print(image) 
- class mdfy.MdLink(url: str, text: str = '', title: str | None = None)[source]¶
Bases:
MdElementRepresents a Markdown link.
Examples
>>> from mdfy.elements import MdLink >>> >>> link = MdLink("https://www.example.com", "example") >>> print(link) [example](https://www.example.com) >>> >>> link = MdLink("https://www.example.com", "example", title="example") >>> print(link) [example](https://www.example.com "example")
- class mdfy.MdList(items: list[str | MdList], depth: int = 0, indent: int = 4, numbered: bool = False)[source]¶
Bases:
MdElementRepresents a Markdown list.
- items¶
List of items, where each item can be a string or another MdList.
Examples
>>> from mdfy.elements import MdList >>> >>> list = MdList(["item 1", "item 2"]) >>> print(list) - item 1 - item 2 >>> >>> list = MdList(["item 1", "item 2"], numbered=True) >>> print(list) 1. item 1 1. item 2 >>> >>> list = MdList([ ... "item 1", ... [ ... "item 2.1", ... "item 2.2" ... ] ... ]) >>> print(list) - item 1 - item 2.1 - item 2.2
- class mdfy.MdQuote(content: str | MdElement)[source]¶
Bases:
MdElementRepresents a Markdown blockquote.
Examples
>>> from mdfy.elements import MdQuote >>> >>> quote = MdQuote("This is a quote.") >>> print(quote) > This is a quote. >>> >>> quote = MdQuote("This is a quote.\nThis is another line.") >>> print(quote) > This is a quote. > This is another line.
- class mdfy.MdTable(data: Dict[str, Any] | list[Dict[str, Any]], header: list[str] | None = None, row_labels: list[str] | None = None, transpose: bool = False, precision: None | int = None)[source]¶
Bases:
MdElementConverter for dict or list to markdown table.
- Parameters:
header (list[str], optional) – Custom header labels. If not provided, dictionary keys will be used.
row_labels (list[str], optional) – Custom row labels. If not provided, no row labels will be shown.
transpose (bool, optional) – If True, transpose the table. Defaults to False.
precision (Optional[int]) – Number of decimal places for floats. If None, values are not formatted.
Examples
>>> data = { ... "Name": "John Doe", ... "Age": 30, ... "Occupation": "Software Engineer", ... } >>> table = MdTable(data) >>> print(table) | Name | Age | Occupation | | --- | --- | --- | | John Doe | 30 | Software Engineer | >>> # Custom headers >>> table = MdTable(data, header=["Full Name", "Years", "Job"]) >>> print(table) | Full Name | Years | Job | | --- | --- | --- | | John Doe | 30 | Software Engineer | >>> # With row labels >>> data = [ ... {"Name": "John Doe", "Age": 30}, ... {"Name": "Jane Doe", "Age": 25} ... ] >>> table = MdTable(data, row_labels=["Person 1", "Person 2"]) >>> print(table) | | Name | Age | | --- | --- | --- | | Person 1 | John Doe | 30 | | Person 2 | Jane Doe | 25 | >>> # Transposed table >>> print(MdTable(data, transpose=True)) | | | | | --- | --- | --- | | Name | John Doe | Jane Doe | | Age | 30 | 25 |
- class mdfy.MdTableOfContents(contents: list[str | MdElement] | None = None, render_all: bool = False)[source]¶
Bases:
MdControlElementRepresents a table of contents in Markdown.
This element can generate a table of contents from a list of markdown elements. If contents are provided during initialization, it will generate the table of contents when stringified. Otherwise, it will return just the title.
- contents¶
The markdown contents to generate TOC from.
- Type:
Optional[ContentType]
- render(contents: list[str | MdElement] | None = None, index: int | None = None) str[source]¶
Generates a table of contents from a list of elements.
- Parameters:
elements (list[ContentElementType]) – The list of elements to generate TOC from.
toc (MdTableOfContents) – The table of contents element with configuration.
- Returns:
The generated table of contents.
- Return type:
- class mdfy.MdText(content: str, formatter: MdFormatter | None = None, no_style: bool = False)[source]¶
Bases:
MdElementMdElementt class to handle the text and styling of text.
- formatter¶
The formatter to apply styling to the content.
- Type:
MdFormatter
Examples
>>> # If you have installed mdfy[styled-text] >>> from mdfy import MdText >>> try: >>> import lark >>> except ModuleNotFoundError: >>> print("Please install mdfy[styled-text] to use MdText with styles.") >>> exit(0) >>> >>> text = MdText("This is [bold:bold] text.") >>> print(text) This is **bold** text. >>> MdText("[This is underline text:underline].").to_str() <u>This is underline text</u>. >>> MdText("[This is [nested:bold] style text:underline].").to_str() <u>This is **nested** style text</u>. >>> MdText("You can use aliases e.g. [st:st] [bd:bo].").to_str() You can use aliases e.g. ***st*** **bd**.
Note
- Available style patterns:
strong: bold text (e.g. ***strong***)
bold: bold text (e.g. **bold**)
italic: italic text (e.g. *italic*)
not: strike-through text (e.g. ~~strike-through~~)
underline: underlined text (e.g. <u>underlined</u>)
code: inline code (e.g. code)
- Also, the following aliases are available for the style patterns:
strong: st
bold: bo, bd
italic: it
not: no, nt
underline: un, ul
code: cd, quote
- class mdfy.Mdfier(filepath: Path | None = None, textio: TextIOBase | None = None, encoding: str = 'utf-8')[source]¶
Bases:
objectWrites Markdown content to a file.
Examples
>>> from mdfy import Mdfier, MdHeader, MdQuote, MdText >>> # Writing Markdown content to a file >>> mdfier = Mdfier("/tmp/quote.md") >>> mdfier.write([ ... MdHeader("Hello, world!", 1), ... MdQuote("This is a quote.") ... ]) >>> >>> with open("/tmp/quote.md") as file: ... print(file.read()) ... # Hello, world! > This is a quote.
from mdfy import MdHeader, MdQuote, MdText >>> mdfier = Mdfier(“/tmp/nest.md”) >>> # Nested content will be flattened >>> mdfier.write([ … MdHeader(“Hello, world!”, 1), … [ … MdText(f”{i} * {i} = {i * i}”) … for i in range(1, 3) … ] … ]) >>> with open(“/tmp/nest.md”) as file: … print(file.read()) … # Hello, world! 1 * 1 = 1 2 * 2 = 4
- __exit__(exc_type: Type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) None[source]¶
Writes the Markdown content to the file.
- classmethod from_file(file_object: TextIOBase) Mdfier[source]¶
Creates an instance of the Mdfier class to write Markdown content to a file object.
- Parameters:
file_object (TextIOBase) – The file object or TextIOBase object to write to.
- Returns:
An instance of the Mdfier class.
- Return type:
- classmethod from_filepath(filepath: str | Path, encoding: str = 'utf-8', create_dir_if_not_exist: bool = True) Mdfier[source]¶
Creates an instance of the Mdfier class to write Markdown content to a file.
- mdfy.code(code: str, inline: bool = False, syntax: str = '') MdCode[source]¶
Creates a code element.
- mdfy.horizontal(content: str = '***') MdHorizontal[source]¶
Creates a horizontal rule element.
- mdfy.link(url: str, text: str = '', title: str | None = None) MdLink[source]¶
Creates a link element.
- mdfy.list(items: list[str | MdList], depth: int = 0, indent: int = 4, numbered: bool = False) MdList¶
Creates a list item element.
- mdfy.table(data: dict[str, Any] | list[dict[str, Any]], header: list[str] | None = None, row_labels: list[str] | None = None, transpose: bool = False, precision: None | int = None) MdTable[source]¶
Creates a table element.