- class mdfy.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
- 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.
- 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:
- __exit__(exc_type: Type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) None[source]¶
Writes the Markdown content to the file.