class mdfy.mdfy.Mdfier(filepath: Path | None = None, textio: TextIOBase | None = None, encoding: str = 'utf-8')[source]

Bases: object

Writes 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

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.

Parameters:
  • filepath (Union[str, Path]) – The path to the file.

  • encoding (str) – The encoding of the file.

  • create_dir_if_not_exist (bool) – If True, creates the directory if it does not exist. Defaults to True.

Returns:

An instance of the Mdfier class.

Return type:

Mdfier

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:

Mdfier

__enter__() Mdfier[source]

Returns the Mdfier instance.

Returns:

The Mdfier instance.

Return type:

Mdfier

__exit__(exc_type: Type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) None[source]

Writes the Markdown content to the file.

Parameters:
  • exc_type (type) – The type of the exception.

  • exc_value (Exception) – The exception that was raised.

  • traceback (Traceback) – The traceback of the exception.

classmethod stringify(contents: str | MdElement | Iterable[str | MdElement | Iterable[MdContents]], separator: str = '\n\n') str[source]

Converts the given Markdown content to a string.

Parameters:

content (Union[str, MdElement]) – The Markdown content to convert to a string.

write(contents: str | MdElement | Iterable[str | MdElement | Iterable[MdContents]]) None[source]

Writes the given Markdown content to the file.

Parameters:

content (Union[str, MdElement]) – The Markdown content to write to the file.