Skip to content

MarkdownInjector

MarkdownInjector

Finds anchors in a markdown document and exposes the anchors attribute, which has sub attributes of all the valid anchors found in the document.

Exceptions:

Type Description
ValueError

Can not find pair of anchors for each anchor defined.

anchors: Anchors property readonly

Returns anchors found as class attributes.

Access to these anchors is done by way of standard dot object notation.

document.anchors.anchorname

These anchor names are sanitised from strings using sanitise_attribute utility.

Returns:

Type Description
Anchors

Class with MarkdownAnchor classes as attributes per anchor.

render(self, trailing_whitespace=False)

Renders the final document with modifications.

Parameters:

Name Type Description Default
trailing_whitespace bool

Add whitespace to end of the document. Defaults to False.

False

Returns:

Type Description
str

Rendered document.

MarkdownAnchor

This class is only interacted with on the anchors property of the MarkdownInjector class.

This class represents the document object between two anchor points.

end: int property readonly

End index of the tags.

Note: Indexes are zero indexed, so line numbers are indexes + 1.

Returns:

Type Description
int

List index value of the closing tag.

indent: int property readonly

Indent level in spaces.

Returns:

Type Description
int

Count of whitespace before tags.

start: int property readonly

Start index of the tags.

Note: Indexes are zero indexed, so line numbers are indexes + 1.

Returns:

Type Description
int

List index value of the opening tag.

value: str property writable

Text between the anchor comments.

To read the value simply assign the value of this property:

text_between_anchors = document.anchors.anchorname.value

To set the value, thereby overwriting the lines in the document between the anchors:

document.anchors.anchorname.value = "This text will replace the lines"

Returns:

Type Description
str

Raw text between the anchors.

Back to top