-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
__init__.py
184 lines (149 loc) · 5.83 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Copyright (c) 2013 Michael Dowling <mtdowling@gmail.com>
# Copyright (c) 2017 Jared Dillard <jared.dillard@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
import os
import queue
import xml.etree.ElementTree as ET
from multiprocessing import Manager
from sphinx.util.logging import getLogger
__version__ = "2.4.0"
logger = getLogger(__name__)
def setup(app):
"""Setup connects events to the sitemap builder"""
app.add_config_value("site_url", default=None, rebuild="")
app.add_config_value(
"sitemap_url_scheme", default="{lang}{version}{link}", rebuild=""
)
app.add_config_value("sitemap_locales", default=None, rebuild="")
app.add_config_value("sitemap_filename", default="sitemap.xml", rebuild="")
try:
app.add_config_value("html_baseurl", default=None, rebuild="")
except BaseException:
pass
app.connect("builder-inited", record_builder_type)
app.connect("html-page-context", add_html_link)
app.connect("build-finished", create_sitemap)
return {
"parallel_read_safe": True,
"parallel_write_safe": True,
"version": __version__,
}
def get_locales(app, exception):
# Manually configured list of locales
sitemap_locales = app.builder.config.sitemap_locales
if sitemap_locales:
# special value to add nothing -> use primary language only
if sitemap_locales == [None]:
return []
# otherwise, add each locale
return [locale for locale in sitemap_locales]
# Or autodetect locales
locales = []
for locale_dir in app.builder.config.locale_dirs:
locale_dir = os.path.join(app.confdir, locale_dir)
if os.path.isdir(locale_dir):
for locale in os.listdir(locale_dir):
if os.path.isdir(os.path.join(locale_dir, locale)):
locales.append(locale)
return locales
def record_builder_type(app):
# builder isn't initialized in the setup so we do it here
builder = getattr(app, "builder", None)
if builder is None:
return
builder.env.is_directory_builder = type(builder).__name__ == "DirectoryHTMLBuilder"
builder.env.sitemap_links = Manager().Queue()
def hreflang_formatter(lang):
"""
sitemap hreflang should follow correct format.
Use hyphen instead of underscore in language and country value.
ref: https://en.wikipedia.org/wiki/Hreflang#Common_Mistakes
source: https://github.com/readthedocs/readthedocs.org/pull/5638
"""
if "_" in lang:
return lang.replace("_", "-")
return lang
def add_html_link(app, pagename, templatename, context, doctree):
"""As each page is built, collect page names for the sitemap"""
env = app.builder.env
# Support DirectoryHTMLBuilder path structure
# where generated links between pages omit the index.html
if env.is_directory_builder:
if pagename == "index":
sitemap_link = ""
elif pagename.endswith("/index"):
sitemap_link = pagename[:-6] + "/"
else:
sitemap_link = pagename + "/"
else:
sitemap_link = pagename + ".html"
env.sitemap_links.put(sitemap_link)
def create_sitemap(app, exception):
"""Generates the sitemap.xml from the collected HTML page links"""
site_url = app.builder.config.site_url or app.builder.config.html_baseurl
if site_url:
site_url.rstrip("/") + "/"
else:
logger.warning(
"sphinx-sitemap: html_baseurl is required in conf.py." "Sitemap not built.",
type="sitemap",
subtype="configuration",
)
return
env = app.builder.env
if env.sitemap_links.empty():
logger.info(
"sphinx-sitemap: No pages generated for %s" % app.config.sitemap_filename,
type="sitemap",
subtype="information",
)
return
ET.register_namespace("xhtml", "http://www.w3.org/1999/xhtml")
root = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
locales = get_locales(app, exception)
if app.builder.config.version:
version = app.builder.config.version + "/"
else:
version = ""
while True:
try:
link = env.sitemap_links.get_nowait()
except queue.Empty:
break
url = ET.SubElement(root, "url")
scheme = app.config.sitemap_url_scheme
if app.builder.config.language:
lang = app.builder.config.language + "/"
else:
lang = ""
ET.SubElement(url, "loc").text = site_url + scheme.format(
lang=lang, version=version, link=link
)
for lang in locales:
lang = lang + "/"
ET.SubElement(
url,
"{http://www.w3.org/1999/xhtml}link",
rel="alternate",
hreflang=hreflang_formatter(lang.rstrip("/")),
href=site_url + scheme.format(lang=lang, version=version, link=link),
)
filename = app.outdir + "/" + app.config.sitemap_filename
ET.ElementTree(root).write(
filename, xml_declaration=True, encoding="utf-8", method="xml"
)
logger.info(
"sphinx-sitemap: %s was generated for URL %s in %s"
% (app.config.sitemap_filename, site_url, filename),
type="sitemap",
subtype="information",
)