Skip to content

list

List

Bases: MdObj

Markdown list.

Source code in mkreports/md/list.py
 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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@register_md("List")
class List(MdObj):
    """
    Markdown list.
    """

    marker: Literal["-", "*", "+", "1"]
    list: MdSeq

    def __init__(
        self,
        items: Union[str, Iterable[Union[MdObj, str]]] = (),
        marker: Literal["-", "*", "+", "1"] = "-",
    ):
        """
        Initialize the list as a markdown object.

        Args:
            items (Union[str, Iterable[Union[MdObj, str]]]): List of items in the list.
            marker (Literal["-", "*", "+", "1"]): Marker to use for the list.
        """
        super().__init__()
        self.list = MdSeq(items)
        self.marker = marker

        # create the markdown output for every item; indent it appropriately
        # and then put it all together.

        # create the body
        # now we need to attach the right element at the beginning
        if self.marker == "1":
            prefix = [f"{i}. " for i in range(self.num_items)]
        else:
            prefix = [f"{self.marker} "] * self.num_items

        md_list = [
            _indent_hanging(elem.body.text, hanging=prefix)
            for elem, prefix in zip(self.list.items, prefix)
        ]

        self._body = SpacedText("\n".join(md_list), (2, 2))
        self._back = self.list.back
        self._settings = self.list.settings

    def append(self, item: Union[Text, MdObj]) -> "List":
        """
        Append an item. This returns a new list, does not append to the old.

        Args:
            item (Union[Text, MdObj]): The item to append to the list.

        Returns:
            List: A list object with the new item appended.
        """
        if isinstance(item, (str, SpacedText)):
            item = Raw(item)
        return List(self.list.items + (item,), marker=self.marker)

    def extend(self, items: Sequence[Union[Text, MdObj]]) -> "List":
        """
        Extend the list by additional items.

        The old list will not be updated. A new one will be created.

        Args:
            items (Sequence[Union[Text, MdObj]]): The items with which to extend the list.

        Returns:
            List: A new list object

        """
        items = tuple(
            [
                Raw(item) if isinstance(item, (str, SpacedText)) else item
                for item in items
            ]
        )
        return List(self.list.items + items, marker=self.marker)

    @property
    def num_items(self) -> int:
        """
        Number of items in the list.

        Returns:
            int: Number of items in the list.
        """
        return len(self.list)

__init__(items=(), marker='-')

Initialize the list as a markdown object.

Parameters:

Name Type Description Default
items Union[str, Iterable[Union[MdObj, str]]]

List of items in the list.

()
marker Literal['-', '*', '+', '1']

Marker to use for the list.

'-'
Source code in mkreports/md/list.py
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
def __init__(
    self,
    items: Union[str, Iterable[Union[MdObj, str]]] = (),
    marker: Literal["-", "*", "+", "1"] = "-",
):
    """
    Initialize the list as a markdown object.

    Args:
        items (Union[str, Iterable[Union[MdObj, str]]]): List of items in the list.
        marker (Literal["-", "*", "+", "1"]): Marker to use for the list.
    """
    super().__init__()
    self.list = MdSeq(items)
    self.marker = marker

    # create the markdown output for every item; indent it appropriately
    # and then put it all together.

    # create the body
    # now we need to attach the right element at the beginning
    if self.marker == "1":
        prefix = [f"{i}. " for i in range(self.num_items)]
    else:
        prefix = [f"{self.marker} "] * self.num_items

    md_list = [
        _indent_hanging(elem.body.text, hanging=prefix)
        for elem, prefix in zip(self.list.items, prefix)
    ]

    self._body = SpacedText("\n".join(md_list), (2, 2))
    self._back = self.list.back
    self._settings = self.list.settings

append(item)

Append an item. This returns a new list, does not append to the old.

Parameters:

Name Type Description Default
item Union[Text, MdObj]

The item to append to the list.

required

Returns:

Name Type Description
List 'List'

A list object with the new item appended.

Source code in mkreports/md/list.py
63
64
65
66
67
68
69
70
71
72
73
74
75
def append(self, item: Union[Text, MdObj]) -> "List":
    """
    Append an item. This returns a new list, does not append to the old.

    Args:
        item (Union[Text, MdObj]): The item to append to the list.

    Returns:
        List: A list object with the new item appended.
    """
    if isinstance(item, (str, SpacedText)):
        item = Raw(item)
    return List(self.list.items + (item,), marker=self.marker)

extend(items)

Extend the list by additional items.

The old list will not be updated. A new one will be created.

Parameters:

Name Type Description Default
items Sequence[Union[Text, MdObj]]

The items with which to extend the list.

required

Returns:

Name Type Description
List 'List'

A new list object

Source code in mkreports/md/list.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def extend(self, items: Sequence[Union[Text, MdObj]]) -> "List":
    """
    Extend the list by additional items.

    The old list will not be updated. A new one will be created.

    Args:
        items (Sequence[Union[Text, MdObj]]): The items with which to extend the list.

    Returns:
        List: A new list object

    """
    items = tuple(
        [
            Raw(item) if isinstance(item, (str, SpacedText)) else item
            for item in items
        ]
    )
    return List(self.list.items + items, marker=self.marker)

num_items()

Number of items in the list.

Returns:

Name Type Description
int int

Number of items in the list.

Source code in mkreports/md/list.py
 98
 99
100
101
102
103
104
105
106
@property
def num_items(self) -> int:
    """
    Number of items in the list.

    Returns:
        int: Number of items in the list.
    """
    return len(self.list)