Skip to content

Commit

Permalink
rss-bot: Add option to convert body to Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
i-ky committed Nov 9, 2023
1 parent a2ddac7 commit ebebc19
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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
18 changes: 17 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 functools import partial
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,8 @@ 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}"
convert = strip_tags if opts.strip else partial(markdownify, escape_underscores=False)
content = f"**[{entry.title}]({entry.link})**\n{convert(body)}\n{entry.link}"

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

0 comments on commit ebebc19

Please sign in to comment.