Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Move convert_html_to_markdown to a new library file.
Dhruv Shetty9e5d927b2e12
diff --git a/zerver/lib/email_mirror.py b/zerver/lib/email_mirror.pyindex a3b9e3c767f..afbb545e8a9 100644--- a/zerver/lib/email_mirror.py+++ b/zerver/lib/email_mirror.py@@ -24,9 +24,9 @@ from zerver.lib.email_mirror_helpers import (decode_email_address,get_email_gateway_message_string_from_address,)-from zerver.lib.email_notifications import convert_html_to_markdownfrom zerver.lib.exceptions import JsonableError, RateLimitedErrorfrom zerver.lib.markdown import get_markdown_link_for_url+from zerver.lib.markdown.from_html import convert_html_to_markdownfrom zerver.lib.message import normalize_body, truncate_content, truncate_topicfrom zerver.lib.rate_limiter import RateLimitedObjectfrom zerver.lib.send_email import FromAddressdiff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.pyindex 9f774e4b2e1..6dd55526a33 100644--- a/zerver/lib/email_notifications.py+++ b/zerver/lib/email_notifications.py@@ -2,10 +2,7 @@import base64import logging-import osimport re-import subprocess-import sysimport zoneinfofrom collections import defaultdictfrom dataclasses import dataclass@@ -1005,17 +1002,3 @@ def enqueue_welcome_emails(context=onboarding_team_to_zulip_context,delay=onboarding_email_schedule["onboarding_team_to_zulip"],)---def convert_html_to_markdown(html: str) -> str:- # html2text is GPL licensed, so run it as a subprocess.- markdown = subprocess.check_output(- [os.path.join(sys.prefix, "bin", "html2text"), "--unicode-snob"], input=html, text=True- ).strip()-- # We want images to get linked and inline previewed, but html2text will turn- # them into links of the form ``, which is- # ugly. Run a regex over the resulting description, turning links of the- # form `` into- # `[image.png](http://foo.com/image.png)`.- return re.sub(r"!\[\]\((\S*)/(\S*)\?(\S*)\)", "[\\2](\\1/\\2)", markdown)diff --git a/zerver/lib/markdown/from_html.py b/zerver/lib/markdown/from_html.pynew file mode 100644index 00000000000..6a505c7650d--- /dev/null+++ b/zerver/lib/markdown/from_html.py@@ -0,0 +1,18 @@+import os+import re+import subprocess+import sys+++def convert_html_to_markdown(html: str) -> str:+ # html2text is GPL licensed, so run it as a subprocess.+ markdown = subprocess.check_output(+ [os.path.join(sys.prefix, "bin", "html2text"), "--unicode-snob"], input=html, text=True+ ).strip()++ # We want images to get linked and inline previewed, but html2text will turn+ # them into links of the form ``, which is+ # ugly. Run a regex over the resulting description, turning links of the+ # form `` into+ # `[image.png](http://foo.com/image.png)`.+ return re.sub(r"!\[\]\((\S*)/(\S*)\?(\S*)\)", "[\\2](\\1/\\2)", markdown)diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex 63b81deaa1d..015e029c735 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -46,7 +46,7 @@ from zerver.lib.email_mirror_helpers import (get_email_gateway_message_string_from_address,)from zerver.lib.email_mirror_server import ZulipMessageHandler, send_to_postmaster-from zerver.lib.email_notifications import convert_html_to_markdown+from zerver.lib.markdown.from_html import convert_html_to_markdownfrom zerver.lib.send_email import FromAddressfrom zerver.lib.streams import ensure_streamfrom zerver.lib.test_classes import ZulipTestCasediff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.pyindex e346e01eb57..ad1d6e74aa5 100644--- a/zerver/tests/test_email_notifications.py+++ b/zerver/tests/test_email_notifications.py@@ -10,7 +10,6 @@ from django.test import override_settingsfrom django_auth_ldap.config import LDAPSearchfrom zerver.lib.email_notifications import (- convert_html_to_markdown,enqueue_welcome_emails,get_onboarding_email_schedule,send_account_registered_email,@@ -645,13 +644,6 @@ class TestCustomWelcomeEmailSender(ZulipTestCase):self.assertEqual(email_data["from_address"], email)-class TestHtmlToMarkdown(ZulipTestCase):- def test_html_to_markdown_unicode(self) -> None:- self.assertEqual(- convert_html_to_markdown("a rose is not a rosé"), "a rose is not a rosé"- )--class TestUtmParamsInEmailLinks(ZulipTestCase):def test_add_utm_paras_to_links(self) -> None:from zerver.lib.send_email import add_utm_params_to_linksdiff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 7cd5efda679..835097b7ab4 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -50,6 +50,7 @@ from zerver.lib.markdown import (url_to_a,)from zerver.lib.markdown.fenced_code import FencedBlockPreprocessor+from zerver.lib.markdown.from_html import convert_html_to_markdownfrom zerver.lib.mdiff import diff_stringsfrom zerver.lib.mention import (FullNameInfo,@@ -3860,3 +3861,10 @@ class MarkdownErrorTests(ZulipTestCase):self.assertIn("print('pygments fallback test')", rendered_html)mocked_hilite.assert_called()self.assertIn("Failed to highlight fenced code block", log.output[0])+++class TestHtmlToMarkdown(ZulipTestCase):+ def test_unicode(self) -> None:+ self.assertEqual(+ convert_html_to_markdown("a rose is not a rosé"), "a rose is not a rosé"+ )diff --git a/zerver/webhooks/freshdesk/view.py b/zerver/webhooks/freshdesk/view.pyindex 51ee043426c..8e2e9312c5f 100644--- a/zerver/webhooks/freshdesk/view.py+++ b/zerver/webhooks/freshdesk/view.py@@ -3,7 +3,7 @@from django.http import HttpRequest, HttpResponsefrom zerver.decorator import authenticated_rest_api_view-from zerver.lib.email_notifications import convert_html_to_markdown+from zerver.lib.markdown.from_html import convert_html_to_markdownfrom zerver.lib.response import json_successfrom zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpointfrom zerver.lib.validator import WildValue, check_string