forked from conan-io/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
49 lines (35 loc) · 1.44 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
"""Sphinx ReadTheDocs theme.
From https://github.com/ryan-roemer/sphinx-bootstrap-theme.
"""
import os
import xml.etree.ElementTree as ET
__version__ = '0.2.0'
__version_full__ = __version__
def setup(app):
"""Setup conntects events to the sitemap builder"""
app.connect('html-page-context', add_html_link)
app.connect('build-finished', create_sitemap)
app.sitemap_links = []
def get_html_theme_path():
"""Return list of HTML theme paths."""
cur_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
return [cur_dir]
def add_html_link(app, pagename, templatename, context, doctree):
"""As each page is built, collect page names for the sitemap"""
base_url = app.config['html_theme_options'].get('base_url', '')
if base_url:
app.sitemap_links.append(base_url + pagename + ".html")
def create_sitemap(app, exception):
"""Generates the sitemap.xml from the collected HTML page links"""
if (not app.config['html_theme_options'].get('base_url', '') or
exception is not None or
not app.sitemap_links):
return
filename = app.outdir + "/sitemap.xml"
print("Generating sitemap.xml in %s" % filename)
root = ET.Element("urlset")
root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
for link in app.sitemap_links:
url = ET.SubElement(root, "url")
ET.SubElement(url, "loc").text = link
ET.ElementTree(root).write(filename)