Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rss-bot: Add option to convert body to Markdown #808

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module = [
"gitlint.*",
"googleapiclient.*",
"irc.*",
"markdownify.*",
"mercurial.*",
"nio.*",
"oauth2client.*",
Expand Down
1 change: 1 addition & 0 deletions zulip/integrations/rss/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
feedparser>=6.0.10
markdownify>=0.11.6
21 changes: 20 additions & 1 deletion zulip/integrations/rss/rss-bot
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import re
import sys
import time
import urllib.parse
from collections.abc import Callable
from html.parser import HTMLParser
from typing import Any, Dict, List, Optional, Tuple

import feedparser
from markdownify import markdownify
from typing_extensions import override

import zulip
Expand Down Expand Up @@ -92,6 +94,19 @@ parser.add_argument(
help="Convert $ to $$ (for KaTeX processing)",
default=False,
)
body = parser.add_mutually_exclusive_group()
body.add_argument(
"--strip",
dest="strip",
action="store_true",
help="Strip HTML tags from body",
)
body.add_argument(
"--markdownify",
dest="strip",
action="store_false",
help="Convert body from HTML to Markdown",
)

opts = parser.parse_args()

Expand Down Expand Up @@ -177,7 +192,11 @@ def send_zulip(entry: Any, feed_name: str) -> Dict[str, Any]:
if opts.unwrap:
body = unwrap_text(body)

content = f"**[{entry.title}]({entry.link})**\n{strip_tags(body)}\n{entry.link}"
def md(html: str) -> str:
return markdownify(html, escape_underscores=False)

convert: Callable[[str], str] = strip_tags if opts.strip else md
content = f"**[{entry.title}]({entry.link})**\n{convert(body)}\n{entry.link}"

if opts.math:
content = content.replace("$", "$$")
Expand Down
Loading