Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
push_notification: Fix bad rendering of math formulas.
Prakhar Pratyush6f3b25d74939
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.pyindex c74bdab6e6d..5d591b01438 100644--- a/zerver/lib/email_notifications.py+++ b/zerver/lib/email_notifications.py@@ -27,6 +27,7 @@ from zerver.lib.notification_data import get_mentioned_user_group_namefrom zerver.lib.queue import queue_json_publishfrom zerver.lib.send_email import FromAddress, send_future_emailfrom zerver.lib.soft_deactivation import soft_reactivate_if_personal_notification+from zerver.lib.tex import change_katex_to_raw_latexfrom zerver.lib.topic import get_topic_resolution_and_bare_namefrom 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:diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.pyindex fb474912099..4d0043a95ab 100644--- a/zerver/lib/push_notifications.py+++ b/zerver/lib/push_notifications.py@@ -40,6 +40,7 @@ from zerver.lib.message import access_message, huddle_usersfrom zerver.lib.outgoing_http import OutgoingSessionfrom zerver.lib.remote_server import send_json_to_push_bouncer, send_to_push_bouncerfrom zerver.lib.soft_deactivation import soft_reactivate_if_personal_notification+from zerver.lib.tex import change_katex_to_raw_latexfrom zerver.lib.timestamp import datetime_to_timestampfrom 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_textdiff --git a/zerver/lib/tex.py b/zerver/lib/tex.pyindex d3bfd37c0b1..23de3a725f4 100644--- a/zerver/lib/tex.py+++ b/zerver/lib/tex.py@@ -3,6 +3,7 @@ import osimport subprocessfrom typing import Optional+import lxml.htmlfrom django.conf import settingsfrom 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 = taildiff --git a/zerver/tests/fixtures/markdown_test_cases.json b/zerver/tests/fixtures/markdown_test_cases.jsonindex 7080b0e04db..eeb95214dcf 100644--- a/zerver/tests/fixtures/markdown_test_cases.json+++ b/zerver/tests/fixtures/markdown_test_cases.json@@ -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",