Skip to content

Commit 12aa846

Browse files
committed
Hide links in text by default, add --show-links option
1 parent fa2ac9b commit 12aa846

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

reader/__main__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Usage:
44
------
55
6+
$ realpython [options] [<id>]
7+
68
List the latest tutorials:
79
810
$ realpython
@@ -18,6 +20,12 @@
1820
$ realpython 0
1921
2022
23+
Available options are:
24+
25+
-h, --help Show this help
26+
-l, --show-links Show links in text
27+
28+
2129
Contact:
2230
--------
2331
@@ -38,15 +46,22 @@
3846

3947
def main() -> None:
4048
"""Read the Real Python article feed"""
49+
args = [a for a in sys.argv[1:] if not a.startswith("-")]
50+
opts = [o for o in sys.argv[1:] if o.startswith("-")]
51+
4152
# Show help message
42-
if "-h" in sys.argv or "--help" in sys.argv:
53+
if "-h" in opts or "--help" in opts:
4354
viewer.show(__doc__)
4455
return
4556

57+
# Should links be shown in the text
58+
show_links = ("-l" in opts or "--show-links" in opts)
59+
4660
# An article ID is given, show article
47-
if len(sys.argv) > 1:
48-
article = feed.get_article(sys.argv[1])
49-
viewer.show(article)
61+
if args:
62+
for article_id in args:
63+
article = feed.get_article(article_id, show_links)
64+
viewer.show(article)
5065

5166
# No ID is given, show list of articles
5267
else:

reader/feed.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get_site() -> str:
2424
return f"{info.title} ({info.link})"
2525

2626

27-
def get_article(article_id: str) -> str:
27+
def get_article(article_id: str, links: bool = False) -> str:
2828
"""Get article from feed with the given ID"""
2929
articles = _feed().entries
3030
try:
@@ -34,11 +34,17 @@ def get_article(article_id: str) -> str:
3434
msg = f"Unknown article ID, use ID from 0 to {max_id}"
3535
raise SystemExit(f"Error: {msg}")
3636

37+
# Get article as HTML
3738
try:
3839
html = article.content[0].value
3940
except AttributeError:
4041
html = article.summary
41-
text = html2text.html2text(html)
42+
43+
# Convert HTML to plain text
44+
to_text = html2text.HTML2Text()
45+
to_text.ignore_links = not links
46+
text = to_text.handle(html)
47+
4248
return f"# {article.title}\n\n{text}"
4349

4450

0 commit comments

Comments
 (0)