Skip to content

Commit 3b642c0

Browse files
authored
Updated Library to Latest Version of SnakeMD (2.0.0b2) (#2)
* Updated SnakeMD version to beta * Replaced InlineText with Inline * Converted output_page to dump * Converted headers to headings * Converted URLs to links * Replaced deprecated verify url method * Working through documentation to spot bugs * Fixed bug where new verify code wasn't working * Added much needed docs * Updated a variety of dependencies * Upgraded test versions * Added new versions to the classifiers list
1 parent 1fa37d0 commit 3b642c0

File tree

4 files changed

+82
-37
lines changed

4 files changed

+82
-37
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
python-version: [3.9]
15+
python-version: ["3.9", "3.10", "3.11"]
1616

1717
steps:
1818
- uses: actions/checkout@v2

requirements.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
feedparser==6.0.6
2-
beautifulsoup4==4.9.3
3-
requests==2.25.1
4-
SnakeMD==0.7.0
5-
pytest==6.2.4
6-
pytest-cov==2.12.1
1+
feedparser~=6.0
2+
beautifulsoup4~=4.12
3+
requests~=2.28
4+
snakemd~=2.0.0b2
5+
pytest~=7.3
6+
pytest-cov~=4.0

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="yomu",
8-
version="0.1.0",
8+
version="0.2.0",
99
author="The Renegade Coder",
1010
author_email="[email protected]",
1111
description="Generates the README for the 'How to Python Code' repo",
@@ -16,7 +16,7 @@
1616
install_requires=[
1717
"feedparser>=6",
1818
"beautifulsoup4>=4",
19-
"SnakeMD>=0"
19+
"SnakeMD>=2"
2020
],
2121
entry_points={
2222
"console_scripts": [
@@ -25,6 +25,8 @@
2525
},
2626
classifiers=(
2727
"Programming Language :: Python :: 3.9",
28+
"Programming Language :: Python :: 3.10",
29+
"Programming Language :: Python :: 3.11",
2830
"Operating System :: OS Independent",
2931
"Development Status :: 5 - Production/Stable",
3032
"License :: OSI Approved :: MIT License"

yomu/readme.py

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from typing import Optional
44

55
import feedparser
6+
import requests
67
from bs4 import BeautifulSoup
7-
from snakemd import Document, InlineText, Table
8-
8+
from snakemd import Document, Inline, Table
99

1010
logger = logging.getLogger(__name__)
1111

@@ -22,7 +22,7 @@ def main() -> None:
2222
raise ValueError(f'Invalid log level: {loglevel}')
2323
logging.basicConfig(level=numeric_level)
2424
how_to = HowTo()
25-
how_to.page.output_page("")
25+
how_to.page.dump("README")
2626

2727

2828
def _get_log_level() -> str:
@@ -70,7 +70,7 @@ def get_series_posts() -> list:
7070
return feed
7171

7272

73-
def get_youtube_video(entry) -> InlineText:
73+
def get_youtube_video(entry) -> Inline:
7474
"""
7575
Generates an InlineText item corresponding to the YouTube
7676
video link if it exists. Otherwise, it returns an empty
@@ -81,41 +81,84 @@ def get_youtube_video(entry) -> InlineText:
8181
"""
8282
content = entry.content[0].value
8383
soup = BeautifulSoup(content, "html.parser")
84-
target = soup.find("h2", text="Video Summary")
84+
target = soup.find("h2", string="Video Summary")
8585
if target:
8686
url = target.find_next_sibling().find_all("a")[-1]["href"]
87-
return InlineText("Video", url=url)
88-
return InlineText("")
87+
return Inline("Video", link=url)
88+
return Inline("")
8989

9090

9191
def get_slug(title: str, sep: str) -> str:
92+
"""
93+
Converts a title to a slug.
94+
95+
:param title: title of item
96+
:param sep: the separator to use in place of whitespace
97+
:return: a slug from a title
98+
"""
9299
return title.split(":")[0][:-10].lower().replace(" ", sep)
93100

94101

95-
def get_challenge(title: str) -> InlineText:
102+
def verify_url(url: str) -> bool:
103+
"""
104+
Checks that a URL exists.
105+
106+
:param url: the URL to verify
107+
:return: True if the URL exists; False otherwise
108+
"""
109+
try:
110+
r = requests.get(url)
111+
if r.status_code == 404:
112+
logger.debug(f"URL does not exist: {url}")
113+
return False
114+
except:
115+
logger.warning(f"Issue loading URL: {url}")
116+
return False
117+
return True
118+
119+
120+
def get_challenge(title: str) -> Inline:
121+
"""
122+
Retrieves the link to the challenge code samples.
123+
124+
:param title: the title of the article
125+
:return: the link to the challenge folder, if it exists
126+
"""
96127
slug = get_slug(title, "-")
97128
base = "https://github.com/TheRenegadeCoder/how-to-python-code/tree/main/challenges/"
98-
challenge = InlineText("Challenge", url=f"{base}{slug}")
99-
if not challenge.verify_url():
100-
return InlineText("")
129+
challenge = Inline("Challenge", link=f"{base}{slug}")
130+
if not verify_url(challenge._link):
131+
return Inline("")
101132
return challenge
102133

103134

104-
def get_notebook(title: str) -> InlineText:
135+
def get_notebook(title: str) -> Inline:
136+
"""
137+
Retrieves the link to the Jupyter Notebook for the article.
138+
139+
:param title: the title of the article
140+
:return: the link to the notebook, if it exists
141+
"""
105142
slug = get_slug(title, "_")
106143
base = "https://github.com/TheRenegadeCoder/how-to-python-code/tree/main/notebooks/"
107-
notebook = InlineText("Notebook", f"{base}{slug}.ipynb")
108-
if not notebook.verify_url():
109-
return InlineText("")
144+
notebook = Inline("Notebook", link=f"{base}{slug}.ipynb")
145+
if not verify_url(notebook._link):
146+
return Inline("")
110147
return notebook
111148

112149

113-
def get_test(title: str) -> InlineText:
150+
def get_test(title: str) -> Inline:
151+
"""
152+
Retrieves the test file for the article.
153+
154+
:param title: the title of the article
155+
:return: the link to the test, if it exists
156+
"""
114157
slug = get_slug(title, "_")
115158
base = "https://github.com/TheRenegadeCoder/how-to-python-code/tree/main/testing/"
116-
test = InlineText("Test", f"{base}{slug}.py")
117-
if not test.verify_url():
118-
return InlineText("")
159+
test = Inline("Test", link=f"{base}{slug}.py")
160+
if not verify_url(test._link):
161+
return Inline("")
119162
return test
120163

121164

@@ -130,10 +173,10 @@ def _load_data(self):
130173
self.feed = get_series_posts()
131174

132175
def _build_readme(self):
133-
self.page = Document("README")
176+
self.page = Document()
134177

135178
# Introduction
136-
self.page.add_header("How to Python - Source Code")
179+
self.page.add_heading("How to Python - Source Code")
137180
self.page.add_paragraph(_get_intro_text()) \
138181
.insert_link("How to Python", "https://therenegadecoder.com/series/how-to-python/") \
139182
.insert_link(
@@ -153,25 +196,25 @@ def _build_readme(self):
153196
"Testing"
154197
]
155198
table = Table(
156-
[InlineText(header) for header in headers],
199+
[Inline(header) for header in headers],
157200
self.build_table()
158201
)
159-
self.page.add_element(table)
202+
self.page.add_block(table)
160203

161-
def build_table(self) -> list[list[InlineText]]:
204+
def build_table(self) -> list[list[Inline]]:
162205
index = 1
163206
body = []
164207
for entry in self.feed:
165208
if "Code Snippets" not in entry.title:
166-
article = InlineText("Article", url=entry.link)
209+
article = Inline("Article", link=entry.link)
167210
youtube = get_youtube_video(entry)
168211
challenge = get_challenge(entry.title)
169212
notebook = get_notebook(entry.title)
170213
test = get_test(entry.title)
171214
body.append([
172-
InlineText(str(index)),
173-
InlineText(entry.title),
174-
InlineText(entry.published),
215+
Inline(str(index)),
216+
Inline(entry.title),
217+
Inline(entry.published),
175218
article,
176219
youtube,
177220
challenge,

0 commit comments

Comments
 (0)