formatter

class mautrix.util.formatter.MatrixParser

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

classmethod 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')
classmethod blockquote_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

classmethod color_to_fstring(node, ctx, color)
Parameters
Return type

mautrix.util.formatter.parser.T

classmethod custom_node_to_fstring(node, ctx)
Parameters
Return type

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

e

alias of mautrix.util.formatter.EntityType

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

alias of mautrix.util.formatter.MarkdownString

classmethod header_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

Parameters
Return type

mautrix.util.formatter.parser.T

classmethod list_bullet(depth)
Parameters

depth (int) –

Return type

str

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

mautrix.util.formatter.parser.T

mention_regex: Pattern = re.compile('https://matrix.to/#/(@.+:.+)')
classmethod node_to_fstring(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

classmethod node_to_fstrings(node, ctx)
Parameters
Return type

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

classmethod node_to_tagged_fstrings(node, ctx)
Parameters
Return type

List[Tuple[mautrix.util.formatter.parser.T, str]]

classmethod parse(data)
Parameters

data (str) –

Return type

mautrix.util.formatter.parser.T

classmethod parse_node(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

read_html()
Parameters

data (str) –

Return type

mautrix.util.formatter.html_reader_htmlparser.HTMLNode

classmethod room_pill_to_fstring(msg, room_alias)
Parameters
Return type

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

room_regex: Pattern = re.compile('https://matrix.to/#/(#.+:.+)')
classmethod tag_aware_parse_node(node, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

classmethod text_to_fstring(text, ctx)
Parameters
Return type

mautrix.util.formatter.parser.T

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

  • url (str) –

Return type

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

classmethod user_pill_to_fstring(msg, user_id)
Parameters
Return type

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

class mautrix.util.formatter.RecursionContext

Bases: object

__init__(strip_linebreaks=True, ul_depth=0)
Parameters
  • strip_linebreaks (bool) –

  • ul_depth (int) –

enter_code_block()
Return type

mautrix.util.formatter.RecursionContext

enter_list()
Return type

mautrix.util.formatter.RecursionContext

strip_linebreaks: bool
ul_depth: int
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.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
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

Optional[mautrix.util.formatter.AbstractEntity]

abstract copy()
Return type

mautrix.util.formatter.AbstractEntity

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

Optional[mautrix.util.formatter.SemiAbstractEntity]

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: mautrix.util.formatter.EntityType
offset: int
length: int
extra_info: Dict[str, Any]
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[mautrix.util.formatter.entity_string.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[mautrix.util.formatter.entity_string.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.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
mautrix.util.formatter.parse_html(input_html)
Parameters

input_html (str) –

Return type

str