text.py

Text utilities for mdpo.

mdpo.text.min_not_max_chars_in_a_row(char, text, default=1)

Returns the minimum possible of characters not found in a row for a string.

For example, given the string "c cc cccc" and the character "c", returns the minimum number of characters in a row that are not found, so 3 in this case.

This function is used in the source code to compute the string that wraps markdown code spans. Given the code span "code that contains 3 \`\`\` and 2 \`\` backticks" and the character "`", this function returns 1.

Parameters
  • char (str) – Character to search.

  • text (str) – Text inside which find the character repeated in a row.

Returns

Minimum number possible of characters not found in a row.

Return type

int

mdpo.text.parse_escaped_pair(value, separator=':')

Escapes a pair key-value separated by a character.

The separator can be escaped using the character ‘'.

Parameters
  • value (str) – String to be converted to a pair key-value.

  • separator (str) – Separator to use for the split.

Raises

ValueError – The value doesn’t contains an unescaped valid separator.

Returns

Parsed key-value pair.

Return type

tuple

mdpo.text.parse_escaped_pairs(pairs, separator=':')

Escapes multiples pairs key-value separated by a character.

The separator can be escaped using the character ‘'.

Parameters
  • pairs (list) – List of texts to parse.

  • separator (str) – Separator to use for the splits.

Raises
  • ValueError – A value doesn’t contains an unescaped valid separator.

  • KeyError – Repeated keys in pairs.

Returns

Key-value pairs.

Return type

dict

mdpo.text.parse_strint_0_inf(value)

Parse a string to a integer accepting infinte values.

Converts an integer passed as string in an integer or math.inf if the passed value is "0" or math.inf.

Parameters

value (str) – Value to parse.

mdpo.text.parse_wrapwidth_argument(value)

Parse the argument -w/--wrapwidth passed to CLIs.

Parameters

value (str) – Wrapwidth value.

mdpo.text.removeprefix(text, prefix)

Removes a prefix from a string.

If the string starts with the prefix string, return string[len(prefix):]. Otherwise, returns the original string. This function has been added in Python3.9 as the builtin str.removeprefix, but is defined here to support previous versions of the language.

Parameters
  • text (str) – Value whose prefix will be removed.

  • prefix (str) – Prefix to remove.

Returns

Text without the prefix, if that prefix exists in the text.

Return type

str

mdpo.text.removesuffix(text, suffix)

Removes a suffix from a string.

If the string ends with the suffix string and the suffix is not empty, returns string[:-len(suffix)]. Otherwise, returns the original string. This function has been added in Python3.9 as the builtin str.removesuffix, but is defined here to support previous versions of the language.

Parameters
  • text (str) – Value whose suffix will be removed.

  • suffix (str) – Suffix to remove.

Returns

Text without the suffix, if that suffix exists in the text.

Return type

str