formatter

class mautrix.util.formatter.AbstractEntity

Bases: abc.ABC

__init__(type, offset, length, extra_info)
Parameters
Return type

None

abstract adjust_offset(offset, max_length=- 1)
Parameters
  • offset (int) –

  • max_length (int) –

Return type

mautrix.util.formatter.AbstractEntity | None

abstract copy()
Return type

mautrix.util.formatter.AbstractEntity

class mautrix.util.formatter.EntityString

Bases: Generic[mautrix.util.formatter.entity_string.TEntity, mautrix.util.formatter.entity_string.TEntityType], mautrix.util.formatter.FormattedString

__init__(text='', entities=None)
Parameters
  • text (str) –

  • entities (Optional[list[~TEntity]]) –

Return type

None

append(*args)

Append strings to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
Returns

A FormattedString that is a concatenation of this string and the given strings.

Return type

mautrix.util.formatter.EntityString

property entities: list[~TEntity]
entity_class

alias of mautrix.util.formatter.SimpleEntity

format(entity_type, offset=None, length=None, **kwargs)

Apply formatting to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
  • entity_type (mautrix.util.formatter.entity_string.TEntityType) – The type of formatting to apply to this string.

  • **kwargs – Additional metadata required by the formatting type.

  • offset (Optional[int]) –

  • length (Optional[int]) –

Returns

A FormattedString with the given formatting applied.

Return type

mautrix.util.formatter.EntityString

classmethod join(items, separator=' ')

Join a list of FormattedStrings with the given separator.

Parameters
Returns

A FormattedString that is a combination of the given strings with the given separator between each one.

Return type

mautrix.util.formatter.EntityString

prepend(*args)

Prepend strings to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
Returns

A FormattedString that is a concatenation of the given strings and this string.

Return type

mautrix.util.formatter.EntityString

split(separator, max_items=- 1)

Split this FormattedString by the given separator.

Parameters
  • separator – The separator to split by.

  • max_items (int) – The maximum number of items to return. If the limit is reached, the remaining string will be returned as one even if it contains the separator.

Returns

The split strings.

Return type

list[mautrix.util.formatter.EntityString]

trim()

Trim surrounding whitespace from this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Returns

A FormattedString without surrounding whitespace.

Return type

mautrix.util.formatter.EntityString

text: str
class mautrix.util.formatter.SemiAbstractEntity

Bases: mautrix.util.formatter.AbstractEntity, abc.ABC

adjust_offset(offset, max_length=- 1)
Parameters
  • offset (int) –

  • max_length (int) –

Return type

mautrix.util.formatter.SemiAbstractEntity | None

offset: int
length: int
class mautrix.util.formatter.SimpleEntity

Bases: mautrix.util.formatter.SemiAbstractEntity

__init__(type, offset, length, extra_info=NOTHING)

Method generated by attrs for class SimpleEntity.

Parameters
Return type

None

copy()
Return type

mautrix.util.formatter.SimpleEntity

type: EntityType
offset: int
length: int
extra_info: dict[str, Any]
class mautrix.util.formatter.EntityType

Bases: enum.Enum

EntityType is a Matrix formatting entity type.

BOLD = 1
ITALIC = 2
STRIKETHROUGH = 3
UNDERLINE = 4
URL = 5
EMAIL = 6
USER_MENTION = 7
ROOM_MENTION = 8
PREFORMATTED = 9
INLINE_CODE = 10
BLOCKQUOTE = 11
HEADER = 12
COLOR = 13
SPOILER = 14
class mautrix.util.formatter.FormattedString

Bases: abc.ABC

FormattedString is an abstract HTML parsing target.

abstract append(*args)

Append strings to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
Returns

A FormattedString that is a concatenation of this string and the given strings.

Return type

mautrix.util.formatter.FormattedString

classmethod concat(*args)

Concatenate many FormattedStrings.

Parameters
Returns

A FormattedString that is a concatenation of the given strings.

Return type

mautrix.util.formatter.FormattedString

abstract format(entity_type, **kwargs)

Apply formatting to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
  • entity_type (mautrix.util.formatter.EntityType) – The type of formatting to apply to this string.

  • **kwargs – Additional metadata required by the formatting type.

Returns

A FormattedString with the given formatting applied.

Return type

mautrix.util.formatter.FormattedString

abstract classmethod join(items, separator=' ')

Join a list of FormattedStrings with the given separator.

Parameters
Returns

A FormattedString that is a combination of the given strings with the given separator between each one.

Return type

mautrix.util.formatter.FormattedString

abstract prepend(*args)

Prepend strings to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
Returns

A FormattedString that is a concatenation of the given strings and this string.

Return type

mautrix.util.formatter.FormattedString

abstract split(separator, max_items=- 1)

Split this FormattedString by the given separator.

Parameters
  • separator – The separator to split by.

  • max_items (int) – The maximum number of items to return. If the limit is reached, the remaining string will be returned as one even if it contains the separator.

Returns

The split strings.

Return type

list[mautrix.util.formatter.FormattedString]

abstract trim()

Trim surrounding whitespace from this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Returns

A FormattedString without surrounding whitespace.

Return type

mautrix.util.formatter.FormattedString

class mautrix.util.formatter.HTMLNode

Bases: list

__init__(tag, attrs)
Parameters
Return type

None

tag: str
text: str
tail: str
attrib: dict[str, str]
mautrix.util.formatter.read_html(data)
Parameters

data (str) –

Return type

mautrix.util.formatter.HTMLNode

class mautrix.util.formatter.MarkdownString

Bases: mautrix.util.formatter.FormattedString

__init__(text='')
Parameters

text (str) –

Return type

None

append(*args)

Append strings to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
Returns

A FormattedString that is a concatenation of this string and the given strings.

Return type

mautrix.util.formatter.MarkdownString

format(entity_type, **kwargs)

Apply formatting to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
  • entity_type (mautrix.util.formatter.EntityType) – The type of formatting to apply to this string.

  • **kwargs – Additional metadata required by the formatting type.

Returns

A FormattedString with the given formatting applied.

Return type

mautrix.util.formatter.MarkdownString

classmethod join(items, separator=' ')

Join a list of FormattedStrings with the given separator.

Parameters
Returns

A FormattedString that is a combination of the given strings with the given separator between each one.

Return type

mautrix.util.formatter.MarkdownString

prepend(*args)

Prepend strings to this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Parameters
Returns

A FormattedString that is a concatenation of the given strings and this string.

Return type

mautrix.util.formatter.MarkdownString

split(separator, max_items=- 1)

Split this FormattedString by the given separator.

Parameters
  • separator – The separator to split by.

  • max_items (int) – The maximum number of items to return. If the limit is reached, the remaining string will be returned as one even if it contains the separator.

Returns

The split strings.

Return type

List[mautrix.util.formatter.MarkdownString]

trim()

Trim surrounding whitespace from this FormattedString.

This method may mutate the source object, but it is not required to do so. Make sure to always use the return value when mutating and to duplicate strings if you don’t want the original to change.

Returns

A FormattedString without surrounding whitespace.

Return type

mautrix.util.formatter.MarkdownString

text: str
class mautrix.util.formatter.MatrixParser

Bases: Generic[mautrix.util.formatter.parser.T]

async basic_format_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

block_tags: tuple[str, ...] = ('p', 'pre', 'blockquote', 'ol', 'ul', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'hr', 'table')
async blockquote_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

async color_to_fstring(msg, color)
Parameters
  • msg (mautrix.util.formatter.parser.T) –

  • color (str) –

Return type

mautrix.util.formatter.parser.T

async custom_node_to_fstring(node, ctx)
Parameters
Return type

Optional[mautrix.util.formatter.parser.T]

e

alias of mautrix.util.formatter.EntityType

Parameters
Return type

Optional[mautrix.util.formatter.parser.T]

exclude_plaintext_attrib: str = 'data-mautrix-exclude-plaintext'
fs

alias of mautrix.util.formatter.MarkdownString

async header_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

async hr_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

async img_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

Parameters
Return type

mautrix.util.formatter.parser.T

list_bullet(depth)
Parameters

depth (int) –

Return type

str

list_bullets: tuple[str, ...] = ('●', '○', '■', '‣')
async list_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

async node_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

async node_to_fstrings(node, ctx)
Parameters
Return type

list[~T]

async node_to_tagged_fstrings(node, ctx)
Parameters
Return type

list[tuple[~T, str]]

async parse(data)
Parameters

data (str) –

Return type

mautrix.util.formatter.parser.T

async parse_node(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

static read_html(data)
Parameters

data (str) –

Return type

mautrix.util.formatter.HTMLNode

Parameters
Return type

Optional[mautrix.util.formatter.parser.T]

async room_pill_to_fstring(msg, room_alias)
Parameters
Return type

Optional[mautrix.util.formatter.parser.T]

async spoiler_to_fstring(msg, reason)
Parameters
  • msg (mautrix.util.formatter.parser.T) –

  • reason (str) –

Return type

mautrix.util.formatter.parser.T

async tag_aware_parse_node(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

async text_to_fstring(text, ctx, strip_leading_whitespace=False)
Parameters
Return type

mautrix.util.formatter.parser.T

async url_to_fstring(msg, url)
Parameters
  • msg (mautrix.util.formatter.parser.T) –

  • url (str) –

Return type

Optional[mautrix.util.formatter.parser.T]

async user_pill_to_fstring(msg, user_id)
Parameters
Return type

Optional[mautrix.util.formatter.parser.T]

class mautrix.util.formatter.RecursionContext

Bases: object

__init__(preserve_whitespace=False, ul_depth=0)
Parameters
  • preserve_whitespace (bool) –

  • ul_depth (int) –

Return type

None

enter_code_block()
Return type

mautrix.util.formatter.RecursionContext

enter_list()
Return type

mautrix.util.formatter.RecursionContext

preserve_whitespace: bool
ul_depth: int
async mautrix.util.formatter.parse_html(input_html)
Parameters

input_html (str) –

Return type

str