Skip to content

Commit 4b378bc

Browse files
authored
Update generate_sitemap.py
1 parent ae9e7d2 commit 4b378bc

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

generate_sitemap.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ def add_static_urls_without_translations(root, urls):
5353
loc.text = encode_url(url)
5454
root.append(url_element)
5555

56+
def add_translated_urls(url_element, original_url):
57+
"""Add translated URLs with language codes appended to the path."""
58+
for hreflang, lang_code in languages.items():
59+
# Add the language code to the path
60+
path_parts = original_url.split('/', 3)
61+
if len(path_parts) > 3: # Ensure there's a path to modify
62+
translated_url = f"{path_parts[0]}//{path_parts[2]}/{lang_code}/{path_parts[3]}"
63+
else: # For root-level paths
64+
translated_url = f"{original_url}/{lang_code}"
65+
66+
# Create <xhtml:link>
67+
alt_link = ET.SubElement(url_element, '{http://www.w3.org/1999/xhtml}link')
68+
alt_link.set('rel', 'alternate')
69+
alt_link.set('hreflang', hreflang)
70+
alt_link.set('href', encode_url(translated_url))
71+
5672
def main():
5773
# URLs of the sitemaps
5874
book_sitemap_url = "https://book.hacktricks.xyz/sitemap.xml"
@@ -74,13 +90,9 @@ def main():
7490
ET.register_namespace('xhtml', "http://www.w3.org/1999/xhtml")
7591
new_root = ET.Element('{http://www.sitemaps.org/schemas/sitemap/0.9}urlset')
7692

77-
# Add static entry for https://www.hacktricks.xyz/
78-
add_static_urls_without_translations(new_root, [
79-
"https://www.hacktricks.xyz/"
80-
])
81-
8293
# Add static URLs for training.hacktricks.xyz without translations
8394
static_training_urls = [
95+
"https://www.hacktricks.xyz/",
8496
"https://training.hacktricks.xyz/",
8597
"https://training.hacktricks.xyz/courses/arte",
8698
"https://training.hacktricks.xyz/courses/arta",
@@ -123,14 +135,8 @@ def main():
123135
lastmod_el = ET.SubElement(url_entry, '{http://www.sitemaps.org/schemas/sitemap/0.9}lastmod')
124136
lastmod_el.text = lastmod.text
125137

126-
# Add alternate links for translations
127-
base_domain = loc_text.split('/')[0:3]
128-
base_domain = '/'.join(base_domain)
129-
for hreflang, lang_path in languages.items():
130-
alt_link = ET.SubElement(url_entry, '{http://www.w3.org/1999/xhtml}link')
131-
alt_link.set('rel', 'alternate')
132-
alt_link.set('hreflang', hreflang)
133-
alt_link.set('href', encode_url(f"{base_domain}/{lang_path}"))
138+
# Add translations
139+
add_translated_urls(url_entry, loc_text)
134140

135141
new_root.append(url_entry)
136142

0 commit comments

Comments
 (0)