Add special CSS classes to mention links

This commit is contained in:
miruka 2020-03-23 15:02:31 -04:00
parent 710668da5d
commit a20cfcffe5

View File

@ -138,6 +138,16 @@ class HTMLProcessor:
user_id_regex, room_id_regex, room_alias_regex, user_id_regex, room_id_regex, room_alias_regex,
]] ]]
link_is_user_id_regex = re.compile(
r"https?://matrix.to/#/@.+", re.IGNORECASE,
)
link_is_room_id_regex = re.compile(
r"https?://matrix.to/#/!.+", re.IGNORECASE,
)
link_is_room_alias_regex = re.compile(
r"https?://matrix.to/#/#.+", re.IGNORECASE,
)
inline_quote_regex = re.compile(r"(^|⏎)(\s*>[^⏎\n]*)", re.MULTILINE) inline_quote_regex = re.compile(r"(^|⏎)(\s*>[^⏎\n]*)", re.MULTILINE)
quote_regex = re.compile( quote_regex = re.compile(
@ -224,7 +234,10 @@ class HTMLProcessor:
) )
for a_tag in tree.iterdescendants("a"): for a_tag in tree.iterdescendants("a"):
self._matrix_toify(a_tag, room_id) self._mentions_to_matrix_to_links(a_tag, room_id)
if not outgoing:
self._matrix_to_links_add_classes(a_tag)
html = etree.tostring(tree, encoding="utf-8", method="html").decode() html = etree.tostring(tree, encoding="utf-8", method="html").decode()
html = sanit.sanitize(html).rstrip("\n") html = sanit.sanitize(html).rstrip("\n")
@ -258,7 +271,7 @@ class HTMLProcessor:
inlines_attributes = { inlines_attributes = {
"font": {"color"}, "font": {"color"},
"a": {"href"}, "a": {"href", "class"},
"code": {"class"}, "code": {"class"},
} }
attributes = {**inlines_attributes, **{ attributes = {**inlines_attributes, **{
@ -398,8 +411,10 @@ class HTMLProcessor:
return el return el
def _matrix_toify(self, el: HtmlElement, room_id: str = "") -> HtmlElement: def _mentions_to_matrix_to_links(
"""Turn userID, usernames, roomID, room aliases into matrix.to URL.""" self, el: HtmlElement, room_id: str = "",
) -> HtmlElement:
"""Turn user ID/names and room ID/aliases into matrix.to URL."""
if el.tag != "a" or not el.attrib.get("href"): if el.tag != "a" or not el.attrib.get("href"):
return el return el
@ -419,7 +434,27 @@ class HTMLProcessor:
if unquote(el.attrib["href"]) == username: if unquote(el.attrib["href"]) == username:
el.attrib["href"] = f"https://matrix.to/#/{user_id}" el.attrib["href"] = f"https://matrix.to/#/{user_id}"
# print("ret 3", el.tag, el.attrib, el.text, el.tail, sep="||") return el
def _matrix_to_links_add_classes(self, el: HtmlElement) -> HtmlElement:
href = el.attrib.get("href")
if not href:
return el
if self.link_is_user_id_regex.match(href):
if el.text.lstrip().startswith("@"):
el.attrib["class"] = "mention user-id-mention"
else:
el.attrib["class"] = "mention username-mention"
elif self.link_is_room_id_regex.match(href):
el.attrib["class"] = "mention room-id-mention"
elif self.link_is_room_alias_regex.match(href):
el.attrib["class"] = "mention room-alias-mention"
return el return el