Skip to content

html#

This module defines a formatter for HTML.

HTML_ENTITY_MAP: dict #

A dictionary mapping unicode characters to their equivalent HTML entities.

HtmlFormatter #

A formatter for HTML.

Inherits BaseFormatter.

color(text, color) staticmethod #

Applies color to a string of text.

Parameters:

Name Type Description Default
text str

The subject text.

required
color Tuple[int, int, int]

The RGB value for the color.

required

Returns:

Type Description
str

The colored text.

Source code in picharsso/format/html.py
22
23
24
@staticmethod
def color(text, color):
    return f'<span style="color : rgb{tuple(color)};">{text}</span>'

translate(text_matrix) staticmethod #

Applies translatations to text_matrix.

Parameters:

Name Type Description Default
text_matrix numpy.ndarray

The subject text matrix, with shape = (<height>, <width>), and dtype = str.

required

Returns:

Type Description
numpy.ndarray

The translated text_matrix.

Source code in picharsso/format/html.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@staticmethod
def translate(text_matrix):
    unique_chars = np.unique(text_matrix)

    # Change datatype to accomodate strings of varying length
    text_matrix = text_matrix.astype(
        f"<U{len(max(name2codepoint.keys(), key=len))}"
    )

    # Change each character to its equivalent entity
    for char in unique_chars:
        if char in HTML_ENTITY_MAP:
            text_matrix[text_matrix == char] = f"&{HTML_ENTITY_MAP[char]}"

    return text_matrix

unify(text_matrix) staticmethod #

Formats a text_matrix into a single string.

Parameters:

Name Type Description Default
text_matrix numpy.ndarray

The subject text matrix, with shape = (<height>, <width>), and dtype = str.

required

Returns:

Type Description
str

The formatted string of text art.

Source code in picharsso/format/html.py
42
43
44
45
46
@staticmethod
def unify(text_matrix):
    return "<div>{}</div>".format(
        "\n".join([f"<div>{''.join(row)}</div>" for row in text_matrix])
    )