Skip to content

Commit 42c723a

Browse files
committed
Update python-sitemap.py
1 parent 3f7ead5 commit 42c723a

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

python-sitemap.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
import os
32
import frontmatter
43
from pathlib import Path
54

@@ -8,43 +7,31 @@ def generate_sitemap():
87
urls = []
98

109
# Find all HTML and MD files in _posts/python
11-
for file_path in Path("_posts/python").rglob("*.html"):
10+
for file_path in Path("_posts/python").rglob("*.[hm][td]*"):
1211
try:
1312
post = frontmatter.load(file_path)
1413
if 'permalink' in post:
15-
urls.append(f"{base_url}/{post['permalink']}")
16-
else:
17-
# Generate URL from file path if no permalink
18-
relative_path = file_path.relative_to(Path("_posts/python"))
19-
url_path = relative_path.with_suffix('')
20-
urls.append(f"{base_url}/python/{url_path}")
14+
permalink = post['permalink']
15+
# Use permalink as-is if it has a domain, otherwise prepend base_url
16+
url = permalink if permalink.startswith(('http://', 'https://')) else f"{base_url}/{permalink}"
17+
urls.append(url)
2118
except:
2219
pass
2320

24-
for file_path in Path("_posts/python").rglob("*.md"):
25-
try:
26-
post = frontmatter.load(file_path)
27-
if 'permalink' in post:
28-
urls.append(f"{base_url}/{post['permalink']}")
29-
else:
30-
# Generate URL from file path if no permalink
31-
relative_path = file_path.relative_to(Path("_posts/python"))
32-
url_path = relative_path.with_suffix('')
33-
urls.append(f"{base_url}/python/{url_path}")
34-
except:
35-
pass
21+
# Remove duplicates and sort
22+
urls = sorted(set(urls))
3623

3724
# Generate sitemap
3825
xml = '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
39-
xml += ''.join(f' <url>\n <loc>{url}</loc>\n </url>\n' for url in sorted(set(urls)))
26+
xml += ''.join(f' <url>\n <loc>{url}</loc>\n </url>\n' for url in urls)
4027
xml += '</urlset>'
4128

42-
os.makedirs('python', exist_ok=True)
43-
with open('python/sitemap.xml', 'w') as f:
44-
f.write(xml)
29+
# Write to file
30+
Path('python').mkdir(exist_ok=True)
31+
Path('python/sitemap.xml').write_text(xml, encoding='utf-8')
4532

4633
print(f"Generated sitemap with {len(urls)} URLs")
4734

48-
if __name__ == '__main__':
35+
if __name__ == "__main__":
4936
generate_sitemap()
5037

0 commit comments

Comments
 (0)