Skip to content

Commit

Permalink
Multilingual sitemaps: fix #1610
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Apr 24, 2015
1 parent 4e983b8 commit dc4e256
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ New in master
Features
--------

* Multilingual sitemaps (Issue #1610)
* New translations (az, fil, tl, uk, zh_TW)
* Add reStructuredText transform support (Issue #1647)
* Produce Unicode output in ``nikola init`` (via Issue #1644)
Expand Down
27 changes: 25 additions & 2 deletions nikola/plugins/task/sitemap/__init__.py
Expand Up @@ -50,6 +50,7 @@
loc_format = """ <url>
<loc>{0}</loc>
<lastmod>{1}</lastmod>
{2}
</url>
"""

Expand All @@ -69,6 +70,13 @@
</sitemap>
"""

alternates_format = """<xhtml:link
rel="alternate"
hreflang="{0}"
href="{1}"
/>"""


sitemapindex_footer = "</sitemapindex>"


Expand Down Expand Up @@ -114,6 +122,7 @@ def gen_tasks(self):
"mapped_extensions": self.site.config.get('MAPPED_EXTENSIONS', ['.html', '.htm', '.xml', '.rss']),
"robots_exclusions": self.site.config["ROBOTS_EXCLUSIONS"],
"filters": self.site.config["FILTERS"],
"translations": self.site.config["TRANSLATIONS"],
}

output = kw['output_folder']
Expand All @@ -140,7 +149,14 @@ def scan_locs():
post = self.site.post_per_file.get(path + kw['index_file'])
if post and (post.is_draft or post.is_private or post.publish_later):
continue
urlset[loc] = loc_format.format(loc, lastmod)
alternates = []
if post:
for lang in kw['translations']:
alt_url = post.permalink(lang=lang, absolute=True)
if loc == alt_url:
continue
alternates.append(alternates_format.format(lang, alt_url))
urlset[loc] = loc_format.format(loc, lastmod, '\n'.join(alternates))
for fname in files:
if kw['strip_indexes'] and fname == kw['index_file']:
continue # We already mapped the folder
Expand Down Expand Up @@ -179,7 +195,14 @@ def scan_locs():
path = path.replace(os.sep, '/')
lastmod = self.get_lastmod(real_path)
loc = urljoin(base_url, base_path + path)
urlset[loc] = loc_format.format(loc, lastmod)
alternates = []
if post:
for lang in kw['translations']:
alt_url = post.permalink(lang=lang, absolute=True)
if loc == alt_url:
continue
alternates.append(alternates_format.format(lang, alt_url))
urlset[loc] = loc_format.format(loc, lastmod, '\n'.join(alternates))

def robot_fetch(path):
for rule in kw["robots_exclusions"]:
Expand Down

0 comments on commit dc4e256

Please sign in to comment.