Skip to content

String formatting

Warning

This module has currently not much functionality. The tests need to continue passing for the rimscode website though, all else, go have at it!

StringFmt class to format strings for different types of output.

StringFmt

Class to format strings for different types of output.

This is a simple string formatter and takes for now LaTeX strings and turns them into HTML strings. Only sub and superscript, and a few symbols are currently included.

Source code in src/rttools/string_formatting.py
class StringFmt:
    """Class to format strings for different types of output.

    This is a simple string formatter and takes for now LaTeX strings and turns them
    into HTML strings. Only sub and superscript, and a few symbols are currently
    included.
    """

    class Type(Enum):
        """Enum for the different types of string formatting."""

        latex = 0

    def __init__(self, string: str, string_type: Type):
        """Initialize the class with a string and a type."""
        if string_type != self.Type.latex:
            raise NotImplementedError(
                "String formatting is currently only implemented for LaTeX strings."
            )

        self._string = string
        self._string_type = string_type

    @property
    def html(self):
        string = self._string

        if not string:
            return None

        # replace unescaped $ with nothing
        string = re.sub(r"(?<!\\)\$", "", string)

        # replace superscript in between { } (unescaped) with <sup>...</sup>
        string = re.sub(r"(?<!\\)\^\{((.*?)|\w)\}", r"<sup>\1</sup>", string)

        # replace single character superscript without { } and unescaped with <sup>...</sup>
        string = re.sub(r"(?<!\\)\^(\w)", r"<sup>\1</sup>", string)

        # replace subscript in between { } (unescaped) with <sub>...</sub>
        string = re.sub(r"(?<!\\)_\{((.*?)|\w)\}", r"<sub>\1</sub>", string)

        # replace single character subscript without { } and unescaped with <sub>...</sub>
        string = re.sub(r"(?<!\\)_(\w)", r"<sub>\1</sub>", string)

        # replace {\\circ} with °
        string = string.replace(r"{\circ}", "°")

        # replace \, with a space
        string = string.replace(r"\,", " ")

        # replace \times with ×
        string = string.replace(r"\times", "×")

        return string

    @property
    def latex(self) -> str:
        """Return the string formatted for LaTeX."""
        return self._string

latex property

Return the string formatted for LaTeX.

Type

Bases: Enum

Enum for the different types of string formatting.

Source code in src/rttools/string_formatting.py
class Type(Enum):
    """Enum for the different types of string formatting."""

    latex = 0

__init__(string, string_type)

Initialize the class with a string and a type.

Source code in src/rttools/string_formatting.py
def __init__(self, string: str, string_type: Type):
    """Initialize the class with a string and a type."""
    if string_type != self.Type.latex:
        raise NotImplementedError(
            "String formatting is currently only implemented for LaTeX strings."
        )

    self._string = string
    self._string_type = string_type