Skip to content

armgit / zulip

Public Imported from GitHub
0

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.py
index 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_markdown
from zerver.lib.exceptions import JsonableError, RateLimitedError
from zerver.lib.markdown import get_markdown_link_for_url
+from zerver.lib.markdown.from_html import convert_html_to_markdown
from zerver.lib.message import normalize_body, truncate_content, truncate_topic
from zerver.lib.rate_limiter import RateLimitedObject
from zerver.lib.send_email import FromAddress
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.py
index 9f774e4b2e1..6dd55526a33 100644
--- a/zerver/lib/email_notifications.py
+++ b/zerver/lib/email_notifications.py
@@ -2,10 +2,7 @@
import base64
import logging
-import os
import re
-import subprocess
-import sys
import zoneinfo
from collections import defaultdict
from 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 `![](http://foo.com/image.png)`, which is
- # ugly. Run a regex over the resulting description, turning links of the
- # form `![](http://foo.com/image.png?12345)` 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.py
new file mode 100644
index 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 `![](http://foo.com/image.png)`, which is
+ # ugly. Run a regex over the resulting description, turning links of the
+ # form `![](http://foo.com/image.png?12345)` 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.py
index 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_markdown
from zerver.lib.send_email import FromAddress
from zerver.lib.streams import ensure_stream
from zerver.lib.test_classes import ZulipTestCase
diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.py
index 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_settings
from django_auth_ldap.config import LDAPSearch
from 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_links
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index 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_markdown
from zerver.lib.mdiff import diff_strings
from 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.py
index 51ee043426c..8e2e9312c5f 100644
--- a/zerver/webhooks/freshdesk/view.py
+++ b/zerver/webhooks/freshdesk/view.py
@@ -3,7 +3,7 @@
from django.http import HttpRequest, HttpResponse
from 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_markdown
from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
from zerver.lib.validator import WildValue, check_string