"""Hold-back check for attachments still being streamed: ``MEDIA:``, ``[[…]]`` flags and markdown/HTML image tags (the display cleaner hides each one only once it is complete).""" from __future__ import annotations import re from gateway.platforms.base import HTML_IMAGE_TAG_PATTERN, MD_IMAGE_TAG_PATTERN, BasePlatformAdapter # A delivery directive still being typed on the open (last) line: "MEDIA:" the cleaner has not # stripped yet whose remainder can still become a path (MEDIA_TAG_CLEANUP_RE's openers: a quote, # ~/, /, a drive letter), or a prefix of "MEDIA" not glued to a preceding letter or digit. A # complete tag has no left boundary, so a prefix after punctuation ("图片:MED") is held too. # Prose such as "social media: Twitter" is not a path, so it never holds the line back. _PENDING_MEDIA_RE = re.compile( r"""MEDIA:\s*(?:$|[`"'~/\\]|[A-Za-z](?::|$))""" r"""|(?]*["']?\s*/?)?)?)?)?)?)?)?)?\Z""" r"""| str: """Hide the image tags ``extract_images`` delivers as attachments post-stream. Each tag goes with up to two line breaks after it, and nothing else is trimmed or collapsed: an interim frame must stay a prefix of the next one, so text in front of the tag (already shown) is never rewritten. """ if "![" not in text and " bool: """True while the end of cleaned streamed text may still become a hidden attachment. Complete deliverable tags are already gone from *cleaned*, so a ``MEDIA:`` or image tag left there is unfinished or undeliverable; the final (non-interim) flush still shows the latter. Showing the unfinished part and hiding it later would break prefix-stable draft frames. Directives live on one line; an image tag's alt text may span several. """ line = cleaned.rsplit("\n", 1)[-1] if _PENDING_MEDIA_RE.search(line) or _PENDING_IMAGE_RE.search(cleaned): return True flag = _PENDING_FLAG_RE.search(line) return bool(flag) and any(f.startswith(flag.group(0)) for f in _STREAM_FLAGS)