Refactor up the embedded image recognition code from ActivityPub AttachImagesMixin to the base RawContentMixin so Matrix can use that too to upload images pre sending.

test
Jason Robinson 2020-12-29 23:12:06 +02:00
rodzic 4566b252a1
commit 82ac0ce3cf
2 zmienionych plików z 22 dodań i 10 usunięć

Wyświetl plik

@ -1,5 +1,4 @@
import logging import logging
import re
import uuid import uuid
from typing import Dict, List from typing import Dict, List
@ -26,16 +25,11 @@ class AttachImagesMixin(RawContentMixin):
Attach any embedded images from raw_content. Attach any embedded images from raw_content.
""" """
super().pre_send() super().pre_send()
if self._media_type != "text/markdown": for image in self.embedded_images:
return
regex = r"!\[([\w ]*)\]\((https?://[\w\d\-\./]+\.[\w]*((?<=jpg)|(?<=gif)|(?<=png)|(?<=jpeg)))\)"
matches = re.finditer(regex, self.raw_content, re.MULTILINE | re.IGNORECASE)
for match in matches:
groups = match.groups()
self._children.append( self._children.append(
ActivitypubImage( ActivitypubImage(
url=groups[1], url=image[0],
name=groups[0] or "", name=image[1],
inline=True, inline=True,
) )
) )

Wyświetl plik

@ -2,7 +2,7 @@ import datetime
import importlib import importlib
import re import re
import warnings import warnings
from typing import List, Set, Union, Dict from typing import List, Set, Union, Dict, Tuple
from commonmark import commonmark from commonmark import commonmark
@ -202,6 +202,24 @@ class RawContentMixin(BaseEntity):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._required += ["raw_content"] self._required += ["raw_content"]
@property
def embedded_images(self) -> List[Tuple[str, str]]:
"""
Returns a list of images from the raw_content.
Currently only markdown supported.
Returns a Tuple of (url, filename).
"""
images = []
if self._media_type != "text/markdown":
return images
regex = r"!\[([\w ]*)\]\((https?://[\w\d\-\./]+\.[\w]*((?<=jpg)|(?<=gif)|(?<=png)|(?<=jpeg)))\)"
matches = re.finditer(regex, self.raw_content, re.MULTILINE | re.IGNORECASE)
for match in matches:
groups = match.groups()
images.append((groups[1], groups[0] or ""))
return images
@property @property
def rendered_content(self) -> str: def rendered_content(self) -> str:
"""Returns the rendered version of raw_content, or just raw_content.""" """Returns the rendered version of raw_content, or just raw_content."""