Skip to content

MDDocument

Handsdown API Index / Handsdown / MDDocument

Auto-generated documentation for handsdown.md_document module.

MDDocument

Show source in md_document.py:22

Markdown file builder.

Can be used as a context manager, on exit context is written to path.

Examples

md_doc = MDDocument(path=Path('output.md'))
md_doc.append('## New section')
md_doc.append('some content')
md_doc.title = 'My doc'
md_doc.write()

# output is indented for readability
Path('output.md').read_text()
'''# My doc

- [My doc](#my-doc)
  - [New section](#new-section)

## New section

some content
'''

with MDDocument(path=Path('output.md')) as md_document:
    md_document.title = 'My doc'
    md_doc.append_title('New section', level=2)
    md_doc.append('New line')

Arguments

  • path - Path to store document.

Signature

class MDDocument:
    def __init__(self, path: Path, encoding: str = ENCODING) -> None:
        ...

See also

MDDocument().append

Show source in md_document.py:309

Append content to the document.

Handle trimming and sectioning the content and update MDDocument().title and MDDocument().toc_section fields.

Arguments

  • content - Text to add.

Signature

def append(self, content: str) -> None:
    ...

MDDocument.get_anchor

Show source in md_document.py:127

Convert title to a GitHub-friendly anchor link.

Returns

A test of anchor link.

Signature

@classmethod
def get_anchor(cls, title: str) -> str:
    ...

Show source in md_document.py:220

Get Markdown link to a local MD document, use relative path as a link.

Arguments

  • MDDocument().path - Path to local MDDocument
  • anchor - Unescaped or escaped anchor tag

Returns

A string with Markdown link.

Signature

def get_doc_link(self, path: Path, anchor: str = "") -> str:
    ...

MDDocument.is_toc

Show source in md_document.py:139

Check if the section is Tree of Contents.

Returns

True the section is ToC.

Signature

@staticmethod
def is_toc(section: str) -> bool:
    ...

MDDocument().path

Show source in md_document.py:302

Output path of the document.

Signature

@property
def path(self) -> Path:
    ...

MDDocument().read

Show source in md_document.py:93

Read and parse content from source_path.

Arguments

  • source_path - Input file path. If not provided - MDDocument().path is used.
  • encoding - File encoding.

Signature

def read(self, path: Path) -> None:
    ...

Show source in md_document.py:178

Render Markdown link to a local MD document, use relative path as a link.

Examples

md_doc = MDDocument(path='/root/parent/doc.md')
MDDocument.render_doc_link(
    'my title',
    anchor='my-anchor',
    target_path=Path('/root/parent/doc.md'
)
'[my title](#my-anchor)'

MDDocument.render_doc_link('my title', target_path=Path('/root/parent/other.md'))
'[my title](other.md)'

MDDocument.render_doc_link('my title', anchor='my-anchor', target_path=Path('doc.md'))
'[my title](doc.md#my-anchor)'

MDDocument.render_doc_link('my title', anchor='my-anchor')
'[my title](#my-anchor)'

Arguments

  • MDDocument().title - Link text.
  • anchor - Unescaped or escaped anchor tag.
  • target_path - Target MDDocument path.

Returns

A string with Markdown link.

Signature

def render_doc_link(
    self, title: str, anchor: str = "", target_path: Optional[Path] = None
) -> str:
    ...

Show source in md_document.py:156

Render Markdown link wih escaped title.

Examples

MDDocument.render_link('my title', 'doc.md#test')
'[my title](doc.md#test)'

MDDocument.render_link('MyClass.__init__', 'my.md')
'[MyClass.__init__](doc.md#my.md)'

Arguments

Returns

A string with Markdown link.

Signature

@classmethod
def render_link(cls, title: str, link: str) -> str:
    ...

MDDocument().sections

Show source in md_document.py:295

All non-special sections of the document.

Signature

@property
def sections(self) -> List[str]:
    ...

MDDocument().source_file_name

Show source in md_document.py:86

Source cide file name.

Signature

@property
def source_file_name(self) -> str:
    ...

MDDocument().subtitle

Show source in md_document.py:271

MDDocument subtitle or an empty string.

Signature

@property
def subtitle(self) -> str:
    ...

MDDocument().subtitle

Show source in md_document.py:278

Signature

@subtitle.setter
def subtitle(self, subtitle: str) -> None:
    ...

MDDocument().title

Show source in md_document.py:259

MDDocument title or an empty string.

Signature

@property
def title(self) -> str:
    ...

MDDocument().title

Show source in md_document.py:266

Signature

@title.setter
def title(self, title: str) -> None:
    ...

MDDocument().toc_section

Show source in md_document.py:283

Document Tree of Contents section or an empty line.

Signature

@property
def toc_section(self) -> str:
    ...

MDDocument().toc_section

Show source in md_document.py:290

Signature

@toc_section.setter
def toc_section(self, toc_section: str) -> None:
    ...

MDDocument().write

Show source in md_document.py:251

Write MD content to MDDocument().path.

Signature

def write(self) -> None:
    ...