Skip to content

builder

A Markdown document builder with line and span writing modes.

MarkdownBuilder dataclass

A Markdown document builder with line and span writing modes.

Source code in src/pymarkdown_builder/builder.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@dataclass
class MarkdownBuilder:
    """A Markdown document builder with line and span writing modes."""

    document: str = field(default="")
    """Content of the builder."""

    def write_lines(self, *lines: str) -> Self:
        """Joins the lines with double line breaks and appends to the document.

        Args:
            *lines (str): Unpacked iterable of lines to be appended.

        Returns:
            The builder instance.
        """
        joined_lines = "\n\n".join(lines)

        if self.document != "":
            self.document += "\n\n"

        self.document += joined_lines

        return self

    def write_spans(self, *spans: str) -> Self:
        """Joins the spans and appends to the document.

        Args:
            *spans (str): Unpacked iterable of spans to be appended.

        Returns:
            The builder instance.
        """
        joined_spans = "".join(spans)

        self.document += joined_spans

        return self

    def line_break(self) -> Self:
        """Appends a line break to the document.

        Returns:
            The builder instance.
        """
        self.document += "\n\n"

        return self

    def __str__(self) -> str:
        """Returns the content of the builder."""
        return self.document

    lines = write_lines
    spans = write_spans
    br = line_break

document: str = field(default='') class-attribute instance-attribute

Content of the builder.

__str__()

Returns the content of the builder.

Source code in src/pymarkdown_builder/builder.py
63
64
65
def __str__(self) -> str:
    """Returns the content of the builder."""
    return self.document

line_break()

Appends a line break to the document.

Returns:

Type Description
Self

The builder instance.

Source code in src/pymarkdown_builder/builder.py
53
54
55
56
57
58
59
60
61
def line_break(self) -> Self:
    """Appends a line break to the document.

    Returns:
        The builder instance.
    """
    self.document += "\n\n"

    return self

write_lines(*lines)

Joins the lines with double line breaks and appends to the document.

Parameters:

Name Type Description Default
*lines str

Unpacked iterable of lines to be appended.

()

Returns:

Type Description
Self

The builder instance.

Source code in src/pymarkdown_builder/builder.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def write_lines(self, *lines: str) -> Self:
    """Joins the lines with double line breaks and appends to the document.

    Args:
        *lines (str): Unpacked iterable of lines to be appended.

    Returns:
        The builder instance.
    """
    joined_lines = "\n\n".join(lines)

    if self.document != "":
        self.document += "\n\n"

    self.document += joined_lines

    return self

write_spans(*spans)

Joins the spans and appends to the document.

Parameters:

Name Type Description Default
*spans str

Unpacked iterable of spans to be appended.

()

Returns:

Type Description
Self

The builder instance.

Source code in src/pymarkdown_builder/builder.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def write_spans(self, *spans: str) -> Self:
    """Joins the spans and appends to the document.

    Args:
        *spans (str): Unpacked iterable of spans to be appended.

    Returns:
        The builder instance.
    """
    joined_spans = "".join(spans)

    self.document += joined_spans

    return self