Skip to content

Commit dc4e256

Browse files
committedApr 24, 2015
Multilingual sitemaps: fix #1610
1 parent 4e983b8 commit dc4e256

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed
 

‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ New in master
44
Features
55
--------
66

7+
* Multilingual sitemaps (Issue #1610)
78
* New translations (az, fil, tl, uk, zh_TW)
89
* Add reStructuredText transform support (Issue #1647)
910
* Produce Unicode output in ``nikola init`` (via Issue #1644)

‎nikola/plugins/task/sitemap/__init__.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
loc_format = """ <url>
5151
<loc>{0}</loc>
5252
<lastmod>{1}</lastmod>
53+
{2}
5354
</url>
5455
"""
5556

@@ -69,6 +70,13 @@
6970
</sitemap>
7071
"""
7172

73+
alternates_format = """<xhtml:link
74+
rel="alternate"
75+
hreflang="{0}"
76+
href="{1}"
77+
/>"""
78+
79+
7280
sitemapindex_footer = "</sitemapindex>"
7381

7482

@@ -114,6 +122,7 @@ def gen_tasks(self):
114122
"mapped_extensions": self.site.config.get('MAPPED_EXTENSIONS', ['.html', '.htm', '.xml', '.rss']),
115123
"robots_exclusions": self.site.config["ROBOTS_EXCLUSIONS"],
116124
"filters": self.site.config["FILTERS"],
125+
"translations": self.site.config["TRANSLATIONS"],
117126
}
118127

119128
output = kw['output_folder']
@@ -140,7 +149,14 @@ def scan_locs():
140149
post = self.site.post_per_file.get(path + kw['index_file'])
141150
if post and (post.is_draft or post.is_private or post.publish_later):
142151
continue
143-
urlset[loc] = loc_format.format(loc, lastmod)
152+
alternates = []
153+
if post:
154+
for lang in kw['translations']:
155+
alt_url = post.permalink(lang=lang, absolute=True)
156+
if loc == alt_url:
157+
continue
158+
alternates.append(alternates_format.format(lang, alt_url))
159+
urlset[loc] = loc_format.format(loc, lastmod, '\n'.join(alternates))
144160
for fname in files:
145161
if kw['strip_indexes'] and fname == kw['index_file']:
146162
continue # We already mapped the folder
@@ -179,7 +195,14 @@ def scan_locs():
179195
path = path.replace(os.sep, '/')
180196
lastmod = self.get_lastmod(real_path)
181197
loc = urljoin(base_url, base_path + path)
182-
urlset[loc] = loc_format.format(loc, lastmod)
198+
alternates = []
199+
if post:
200+
for lang in kw['translations']:
201+
alt_url = post.permalink(lang=lang, absolute=True)
202+
if loc == alt_url:
203+
continue
204+
alternates.append(alternates_format.format(lang, alt_url))
205+
urlset[loc] = loc_format.format(loc, lastmod, '\n'.join(alternates))
183206

184207
def robot_fetch(path):
185208
for rule in kw["robots_exclusions"]:

0 commit comments

Comments
 (0)
Please sign in to comment.