mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 12:33:40 +00:00
push_notification: Fix bad rendering of math formulas.
Earlier, for the push notifications having latex math like "$$1 \oplus 0 = 1$$, the notification had the math included multiple times. This commit fixes the incorrect behavior by replacing the KaTeX with the raw LaTeX source. Fixes part of #25289.
This commit is contained in:
committed by
Tim Abbott
parent
9fe1e38f98
commit
6f3b25d749
@@ -27,6 +27,7 @@ from zerver.lib.notification_data import get_mentioned_user_group_name
|
||||
from zerver.lib.queue import queue_json_publish
|
||||
from zerver.lib.send_email import FromAddress, send_future_email
|
||||
from zerver.lib.soft_deactivation import soft_reactivate_if_personal_notification
|
||||
from zerver.lib.tex import change_katex_to_raw_latex
|
||||
from zerver.lib.topic import get_topic_resolution_and_bare_name
|
||||
from zerver.lib.url_encoding import (
|
||||
huddle_narrow_url,
|
||||
@@ -248,19 +249,7 @@ def build_message_list(
|
||||
relative_to_full_url(fragment, user.realm.uri)
|
||||
fix_emojis(fragment, user.emojiset)
|
||||
fix_spoilers_in_html(fragment, user.default_language)
|
||||
|
||||
# Selecting the <span> elements with class 'katex'
|
||||
katex_spans = fragment.xpath("//span[@class='katex']")
|
||||
|
||||
# Iterate through 'katex_spans' and replace with a new <span> having LaTeX text.
|
||||
for katex_span in katex_spans:
|
||||
latex_text = katex_span.xpath(".//annotation[@encoding='application/x-tex']")[0].text
|
||||
# We store 'tail' to insert them back as the replace operation removes it.
|
||||
tail = katex_span.tail
|
||||
latex_span = lxml.html.Element("span")
|
||||
latex_span.text = f"$${latex_text}$$"
|
||||
katex_span.getparent().replace(katex_span, latex_span)
|
||||
latex_span.tail = tail
|
||||
change_katex_to_raw_latex(fragment)
|
||||
|
||||
html = lxml.html.tostring(fragment, encoding="unicode")
|
||||
if sender:
|
||||
|
||||
@@ -40,6 +40,7 @@ from zerver.lib.message import access_message, huddle_users
|
||||
from zerver.lib.outgoing_http import OutgoingSession
|
||||
from zerver.lib.remote_server import send_json_to_push_bouncer, send_to_push_bouncer
|
||||
from zerver.lib.soft_deactivation import soft_reactivate_if_personal_notification
|
||||
from zerver.lib.tex import change_katex_to_raw_latex
|
||||
from zerver.lib.timestamp import datetime_to_timestamp
|
||||
from zerver.models import (
|
||||
AbstractPushDeviceToken,
|
||||
@@ -812,6 +813,7 @@ def get_mobile_push_content(rendered_content: str) -> str:
|
||||
)
|
||||
|
||||
elem = lxml.html.fragment_fromstring(rendered_content, create_parent=True)
|
||||
change_katex_to_raw_latex(elem)
|
||||
plain_text = process(elem)
|
||||
return plain_text
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import os
|
||||
import subprocess
|
||||
from typing import Optional
|
||||
|
||||
import lxml.html
|
||||
from django.conf import settings
|
||||
|
||||
from zerver.lib.storage import static_path
|
||||
@@ -39,3 +40,18 @@ def render_tex(tex: str, is_inline: bool = True) -> Optional[str]:
|
||||
return stdout.strip()
|
||||
except subprocess.CalledProcessError:
|
||||
return None
|
||||
|
||||
|
||||
def change_katex_to_raw_latex(fragment: lxml.html.HtmlElement) -> None:
|
||||
# Selecting the <span> elements with class 'katex'
|
||||
katex_spans = fragment.xpath("//span[@class='katex']")
|
||||
|
||||
# Iterate through 'katex_spans' and replace with a new <span> having LaTeX text.
|
||||
for katex_span in katex_spans:
|
||||
latex_text = katex_span.xpath(".//annotation[@encoding='application/x-tex']")[0].text
|
||||
# We store 'tail' to insert them back as the replace operation removes it.
|
||||
tail = katex_span.tail
|
||||
latex_span = lxml.html.Element("span")
|
||||
latex_span.text = f"$${latex_text}$$"
|
||||
katex_span.getparent().replace(katex_span, latex_span)
|
||||
latex_span.tail = tail
|
||||
|
||||
@@ -847,7 +847,8 @@
|
||||
{
|
||||
"name": "tex_inline",
|
||||
"input": "$$1 \\oplus 0 = 1$$",
|
||||
"expected_output": "<p><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mn>1</mn><mo>⊕</mo><mn>0</mn><mo>=</mo><mn>1</mn></mrow><annotation encoding=\"application/x-tex\">1 \\oplus 0 = 1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">1</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">⊕</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">0</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">1</span></span></span></span></p>"
|
||||
"expected_output": "<p><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mn>1</mn><mo>⊕</mo><mn>0</mn><mo>=</mo><mn>1</mn></mrow><annotation encoding=\"application/x-tex\">1 \\oplus 0 = 1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">1</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">⊕</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">0</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">1</span></span></span></span></p>",
|
||||
"text_content": "$$1 \\oplus 0 = 1$$"
|
||||
},
|
||||
{
|
||||
"name": "tex_complex",
|
||||
@@ -862,7 +863,8 @@
|
||||
{
|
||||
"name": "tex_fenced_math",
|
||||
"input": "```math\na^2 = b^2 + c^2\n```",
|
||||
"expected_output": "<p><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><msup><mi>a</mi><mn>2</mn></msup><mo>=</mo><msup><mi>b</mi><mn>2</mn></msup><mo>+</mo><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding=\"application/x-tex\">a^2 = b^2 + c^2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8641em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">a</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8641em;\"><span style=\"top:-3.113em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.9474em;vertical-align:-0.0833em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">b</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8641em;\"><span style=\"top:-3.113em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.8641em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">c</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8641em;\"><span style=\"top:-3.113em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span></span></span></span></span></p>"
|
||||
"expected_output": "<p><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><msup><mi>a</mi><mn>2</mn></msup><mo>=</mo><msup><mi>b</mi><mn>2</mn></msup><mo>+</mo><msup><mi>c</mi><mn>2</mn></msup></mrow><annotation encoding=\"application/x-tex\">a^2 = b^2 + c^2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8641em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">a</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8641em;\"><span style=\"top:-3.113em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.9474em;vertical-align:-0.0833em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">b</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8641em;\"><span style=\"top:-3.113em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.8641em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">c</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8641em;\"><span style=\"top:-3.113em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span></span></span></span></span></p>",
|
||||
"text_content": "$$a^2 = b^2 + c^2$$"
|
||||
},
|
||||
{
|
||||
"name": "tex_fenced_tex",
|
||||
|
||||
Reference in New Issue
Block a user