Source code for wikitextparser._comment_bold_italic
from typing import Dict, List, MutableSequence, Optional, Tuple, Union
from regex import DOTALL, MULTILINE
from ._wikitext import SubWikiText, rc
COMMENT_PATTERN = r'<!--[\s\S]*?(?>-->|\Z)'
COMMA_COMMENT = "'(?>" + COMMENT_PATTERN + ')*+'
COMMENT_COMMA = '(?>' + COMMENT_PATTERN + ")*+'"
BOLD_FULLMATCH = rc(
COMMA_COMMENT * 2 + "'(.*?)(?>'" + COMMENT_COMMA * 2 + '|$)',
MULTILINE | DOTALL,
).fullmatch
ITALIC_FULLMATCH = rc(
COMMA_COMMENT + "'(.*?)(?>'" + COMMENT_COMMA + '|$)', DOTALL
).fullmatch
ITALIC_NOEND_FULLMATCH = rc(COMMA_COMMENT + "'(.*)", DOTALL).fullmatch
[docs]
class BoldItalic(SubWikiText):
__slots__ = ()
@property
def text(self) -> str:
"""Return text value of self (without triple quotes)."""
# noinspection PyUnresolvedReferences
return self._match[1]
@text.setter
def text(self, s: str):
# noinspection PyUnresolvedReferences
b, e = self._match.span(1)
self[b:e] = s
@property
def _content_span(self) -> Tuple[int, int]:
# noinspection PyUnresolvedReferences
return self._match.span(1)
[docs]
class Bold(BoldItalic):
__slots__ = ()
@property
def _match(self):
return BOLD_FULLMATCH(self.string)
[docs]
class Italic(BoldItalic):
__slots__ = ('end_token',)
[docs]
def __init__(
self,
string: Union[str, MutableSequence[str]],
_type_to_spans: Optional[Dict[str, List[List[int]]]] = None,
_span: Optional[List[int]] = None,
_type: Optional[Union[str, int]] = None,
end_token: bool = True,
):
"""Initialize the Italic object.
:param end_token: set to True if the italic object ends with a '' token
False otherwise.
"""
super().__init__(string, _type_to_spans, _span, _type)
self.end_token = end_token
@property
def _match(self):
if self.end_token:
return ITALIC_FULLMATCH(self.string)
return ITALIC_NOEND_FULLMATCH(self.string)