1
1
#!/usr/bin/env python3
2
- import os
3
2
import frontmatter
4
3
from pathlib import Path
5
4
@@ -8,43 +7,31 @@ def generate_sitemap():
8
7
urls = []
9
8
10
9
# 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]* " ):
12
11
try :
13
12
post = frontmatter .load (file_path )
14
13
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 )
21
18
except :
22
19
pass
23
20
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 ))
36
23
37
24
# Generate sitemap
38
25
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 )
40
27
xml += '</urlset>'
41
28
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' )
45
32
46
33
print (f"Generated sitemap with { len (urls )} URLs" )
47
34
48
- if __name__ == ' __main__' :
35
+ if __name__ == " __main__" :
49
36
generate_sitemap ()
50
37
0 commit comments