diff options
author | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-02-12 14:35:15 +0300 |
---|---|---|
committer | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-02-12 14:35:15 +0300 |
commit | 46a8b83899dd321edf511c0483f9c479ce2c1bc4 (patch) | |
tree | e5debc03beecbd10e7d1bf78c889c8d54e8c4523 /contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/html.py | |
parent | b56bbcc9f63bf31991a8aa118555ce0c12875a74 (diff) | |
download | ydb-46a8b83899dd321edf511c0483f9c479ce2c1bc4.tar.gz |
intermediate changes
ref:7c971b97c72bbbcbf889118d39017bd14f99365a
Diffstat (limited to 'contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/html.py')
-rw-r--r-- | contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/html.py | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/html.py b/contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/html.py index 06c6020f54..0af2b18b57 100644 --- a/contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/html.py +++ b/contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/html.py @@ -1,4 +1,5 @@ import xml.dom.minidom as minidom +from string import Formatter from typing import Any, List, Tuple, Union from .base import FormattedText, StyleAndTextTuples @@ -29,7 +30,7 @@ class HTML: def __init__(self, value: str) -> None: self.value = value - document = minidom.parseString("<html-root>%s</html-root>" % (value,)) + document = minidom.parseString(f"<html-root>{value}</html-root>") result: StyleAndTextTuples = [] name_stack: List[str] = [] @@ -97,7 +98,7 @@ class HTML: self.formatted_text = FormattedText(result) def __repr__(self) -> str: - return "HTML(%r)" % (self.value,) + return f"HTML({self.value!r})" def __pt_formatted_text__(self) -> StyleAndTextTuples: return self.formatted_text @@ -107,13 +108,9 @@ class HTML: Like `str.format`, but make sure that the arguments are properly escaped. """ - # Escape all the arguments. - escaped_args = [html_escape(a) for a in args] - escaped_kwargs = {k: html_escape(v) for k, v in kwargs.items()} + return HTML(FORMATTER.vformat(self.value, args, kwargs)) - return HTML(self.value.format(*escaped_args, **escaped_kwargs)) - - def __mod__(self, value: Union[object, Tuple[object, ...]]) -> "HTML": + def __mod__(self, value: object) -> "HTML": """ HTML('<b>%s</b>') % value """ @@ -124,11 +121,16 @@ class HTML: return HTML(self.value % value) +class HTMLFormatter(Formatter): + def format_field(self, value: object, format_spec: str) -> str: + return html_escape(format(value, format_spec)) + + def html_escape(text: object) -> str: # The string interpolation functions also take integers and other types. # Convert to string first. if not isinstance(text, str): - text = "{}".format(text) + text = f"{text}" return ( text.replace("&", "&") @@ -136,3 +138,6 @@ def html_escape(text: object) -> str: .replace(">", ">") .replace('"', """) ) + + +FORMATTER = HTMLFormatter() |