Skip to content

Spacer

Spacer class for “text:s” tag.

Classes:

Name Description
Spacer

Representation of several spaces, “text:s”.

Spacer

Bases: MDSpacer, Element

Representation of several spaces, “text:s”.

This element shall be used to represent the second and all following ” ” (U+0020, SPACE) characters in a sequence of ” ” (U+0020, SPACE) characters. It’s good practice to use this element only for the second and all following SPACE characters in a sequence, though it’s not an error if the preceding character is not a white space.

Methods:

Name Description
__init__

Create a Spacer element, “text:s”, representing several spaces.

__str__

Attributes:

Name Type Description
length int

Get the number of spaces represented by the spacer.

number str | None
text str

Get the string representation of the spacer.

Source code in odfdo/spacer.py
 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
class Spacer(MDSpacer, Element):
    """Representation of several spaces, "text:s".

    This element shall be used to represent the second and all following " "
    (U+0020, SPACE) characters in a sequence of " " (U+0020, SPACE) characters.
    It's good practice to use this element only for the second and all
    following SPACE characters in a sequence, though it's not an error if
    the preceding character is not a white space.
    """

    _tag = "text:s"
    _properties: tuple[PropDef | PropDefBool, ...] = (PropDef("number", "text:c"),)

    def __init__(self, number: int | None = 1, **kwargs: Any) -> None:
        """Create a Spacer element, "text:s", representing several spaces.

        Args:
            number: The number of spaces. Defaults to 1.
        """
        super().__init__(**kwargs)
        if self._do_init:
            if number and number >= 2:
                self.number: str | None = str(number)
            else:
                self.number = None

    def __str__(self) -> str:
        return self.text

    @property
    def text(self) -> str:
        """Get the string representation of the spacer.

        Returns:
            str: A string composed of spaces, e.g., "   ".
        """
        return " " * self.length

    @text.setter
    def text(self, text: str | None) -> None:
        """Set the string value of the spacer, which updates its length.

        Args:
            text: The string to set.
        """
        if text is None:
            text = ""
        self.length = len(text)

    @property
    def length(self) -> int:
        """Get the number of spaces represented by the spacer.

        Returns:
            int: The number of spaces.
        """
        value = self._base_attrib_getter("text:c")
        if value is None:
            return 1  # minimum 1 space
        return int(value)

    @length.setter
    def length(self, value: int | None) -> None:
        """Set the number of spaces represented by the spacer.

        Args:
            value: The number of spaces to set. If None or less
                than 2, the `text:c` attribute is removed, defaulting to 1 space.
        """
        if value is None or int(value) < 2:
            self._base_attrib_setter("text:c", None)
        else:
            self._base_attrib_setter("text:c", int(value))

_properties class-attribute instance-attribute

_properties: tuple[PropDef | PropDefBool, ...] = (
    PropDef("number", "text:c"),
)

_tag class-attribute instance-attribute

_tag = 'text:s'

length property writable

length: int

Get the number of spaces represented by the spacer.

Returns:

Name Type Description
int int

The number of spaces.

number instance-attribute

number: str | None = str(number)

text property writable

text: str

Get the string representation of the spacer.

Returns:

Name Type Description
str str

A string composed of spaces, e.g., ” “.

__init__

__init__(number: int | None = 1, **kwargs: Any) -> None

Create a Spacer element, “text:s”, representing several spaces.

Parameters:

Name Type Description Default
number int | None

The number of spaces. Defaults to 1.

1
Source code in odfdo/spacer.py
47
48
49
50
51
52
53
54
55
56
57
58
def __init__(self, number: int | None = 1, **kwargs: Any) -> None:
    """Create a Spacer element, "text:s", representing several spaces.

    Args:
        number: The number of spaces. Defaults to 1.
    """
    super().__init__(**kwargs)
    if self._do_init:
        if number and number >= 2:
            self.number: str | None = str(number)
        else:
            self.number = None

__str__

__str__() -> str
Source code in odfdo/spacer.py
60
61
def __str__(self) -> str:
    return self.text