stream_consumer_directives.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Hold-back check for attachments still being streamed: ``MEDIA:<path>``, ``[[…]]`` flags and
  2. markdown/HTML image tags (the display cleaner hides each one only once it is complete)."""
  3. from __future__ import annotations
  4. import re
  5. from gateway.platforms.base import HTML_IMAGE_TAG_PATTERN, MD_IMAGE_TAG_PATTERN, BasePlatformAdapter
  6. # A delivery directive still being typed on the open (last) line: "MEDIA:" the cleaner has not
  7. # stripped yet whose remainder can still become a path (MEDIA_TAG_CLEANUP_RE's openers: a quote,
  8. # ~/, /, a drive letter), or a prefix of "MEDIA" not glued to a preceding letter or digit. A
  9. # complete tag has no left boundary, so a prefix after punctuation ("图片:MED") is held too.
  10. # Prose such as "social media: Twitter" is not a path, so it never holds the line back.
  11. _PENDING_MEDIA_RE = re.compile(
  12. r"""MEDIA:\s*(?:$|[`"'~/\\]|[A-Za-z](?::|$))"""
  13. r"""|(?<![A-Za-z0-9])[`"'*_]{0,3}M(?:E(?:D(?:I(?:A)?)?)?)?$""",
  14. re.IGNORECASE)
  15. _PENDING_FLAG_RE = re.compile(r"\[\[?[A-Za-z_]*\]?$")
  16. _STREAM_FLAGS = ("[[audio_as_voice]]", "[[as_document]]")
  17. # The end of the text is still a prefix of an image tag ``extract_images`` could hide: ``!``,
  18. # ``![alt`` (the alt may span lines), ``![alt](https://ho``, ``<img src="…``, a closing ``</img``.
  19. # The extractor has no left boundary, so "chart:!" waits for the next delta too; only a "!" right
  20. # after a word, closing bracket/quote or another "!"/"?"/"." streams on, because that is how prose
  21. # ends a sentence ("world!"), and holding it would keep every exclamation off the live draft.
  22. # Anything that leaves the tag's grammar (whitespace in the URL, ``<img alt``) ends the hold: that
  23. # text is never extracted, so it stays visible either way.
  24. _PENDING_IMAGE_RE = re.compile(
  25. r"""!\[[^\]]*(?:\](?:\([^)\s]*)?)?\Z|(?<![\w)\]"'”’!?.*_~`])!\Z"""
  26. r"""|<(?:i(?:m(?:g(?:\s+(?:s(?:r(?:c(?:=["']?[^\s"'<>]*["']?\s*/?)?)?)?)?)?)?)?)?\Z"""
  27. r"""|</(?:i(?:m(?:g)?)?)?\Z""")
  28. def strip_image_tags_for_display(text: str) -> str:
  29. """Hide the image tags ``extract_images`` delivers as attachments post-stream.
  30. Each tag goes with up to two line breaks after it, and nothing else is trimmed
  31. or collapsed: an interim frame must stay a prefix of the next one, so text in front of the tag
  32. (already shown) is never rewritten.
  33. """
  34. if "![" not in text and "<img" not in text:
  35. return text
  36. images, _ = BasePlatformAdapter.extract_images(text)
  37. if not images:
  38. return text
  39. urls = {url for url, _ in images}
  40. for pattern, url_group in ((MD_IMAGE_TAG_PATTERN, 2), (HTML_IMAGE_TAG_PATTERN, 1)):
  41. text = re.sub(f"(?:{pattern})(?:\\r?\\n){{0,2}}",
  42. lambda m, g=url_group: "" if m.group(g) in urls else m.group(0), text)
  43. return text
  44. def has_pending_media_directive(cleaned: str) -> bool:
  45. """True while the end of cleaned streamed text may still become a hidden attachment.
  46. Complete deliverable tags are already gone from *cleaned*, so a ``MEDIA:`` or image tag left
  47. there is unfinished or undeliverable; the final (non-interim) flush still shows the latter.
  48. Showing the unfinished part and hiding it later would break prefix-stable draft frames.
  49. Directives live on one line; an image tag's alt text may span several.
  50. """
  51. line = cleaned.rsplit("\n", 1)[-1]
  52. if _PENDING_MEDIA_RE.search(line) or _PENDING_IMAGE_RE.search(cleaned):
  53. return True
  54. flag = _PENDING_FLAG_RE.search(line)
  55. return bool(flag) and any(f.startswith(flag.group(0)) for f in _STREAM_FLAGS)