style: simplify _fixup_mrkdwn and trim docstring in SlackChannel
This commit is contained in:
@@ -241,22 +241,11 @@ class SlackChannel(BaseChannel):
|
|||||||
if not text:
|
if not text:
|
||||||
return ""
|
return ""
|
||||||
text = cls._TABLE_RE.sub(cls._convert_table, text)
|
text = cls._TABLE_RE.sub(cls._convert_table, text)
|
||||||
text = slackify_markdown(text)
|
return cls._fixup_mrkdwn(slackify_markdown(text))
|
||||||
text = cls._fixup_mrkdwn(text)
|
|
||||||
return text
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _fixup_mrkdwn(cls, text: str) -> str:
|
def _fixup_mrkdwn(cls, text: str) -> str:
|
||||||
"""Fix markdown artifacts that slackify_markdown misses.
|
"""Fix markdown artifacts that slackify_markdown misses."""
|
||||||
|
|
||||||
The slackify_markdown library uses markdown-it which requires certain
|
|
||||||
boundary conditions for emphasis. Patterns like ``**key:**value``
|
|
||||||
(closing ``**`` immediately followed by non-space) are not recognised
|
|
||||||
as bold and pass through as literal asterisks. This method catches
|
|
||||||
those leftovers, stray ``## headers``, and over-escaped ``&`` in
|
|
||||||
bare URLs, while leaving code spans untouched.
|
|
||||||
"""
|
|
||||||
# Protect code blocks from further processing
|
|
||||||
code_blocks: list[str] = []
|
code_blocks: list[str] = []
|
||||||
|
|
||||||
def _save_code(m: re.Match) -> str:
|
def _save_code(m: re.Match) -> str:
|
||||||
@@ -265,19 +254,12 @@ class SlackChannel(BaseChannel):
|
|||||||
|
|
||||||
text = cls._CODE_FENCE_RE.sub(_save_code, text)
|
text = cls._CODE_FENCE_RE.sub(_save_code, text)
|
||||||
text = cls._INLINE_CODE_RE.sub(_save_code, text)
|
text = cls._INLINE_CODE_RE.sub(_save_code, text)
|
||||||
|
|
||||||
text = cls._LEFTOVER_BOLD_RE.sub(r"*\1*", text)
|
text = cls._LEFTOVER_BOLD_RE.sub(r"*\1*", text)
|
||||||
text = cls._LEFTOVER_HEADER_RE.sub(r"*\1*", text)
|
text = cls._LEFTOVER_HEADER_RE.sub(r"*\1*", text)
|
||||||
|
text = cls._BARE_URL_RE.sub(lambda m: m.group(0).replace("&", "&"), text)
|
||||||
|
|
||||||
# Fix & in bare URLs that the library over-escaped
|
|
||||||
text = cls._BARE_URL_RE.sub(
|
|
||||||
lambda m: m.group(0).replace("&", "&"), text
|
|
||||||
)
|
|
||||||
|
|
||||||
# Restore code blocks
|
|
||||||
for i, block in enumerate(code_blocks):
|
for i, block in enumerate(code_blocks):
|
||||||
text = text.replace(f"\x00CB{i}\x00", block)
|
text = text.replace(f"\x00CB{i}\x00", block)
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user