forked from esphome/esphome-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.py
51 lines (42 loc) · 1.81 KB
/
sitemap.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
import os
import xml.etree.ElementTree as ET
def setup(app):
"""Setup connects events to the sitemap builder"""
app.connect('html-page-context', add_html_link)
app.connect('build-finished', create_sitemap)
app.sitemap_links = []
return {"version": "1.0.0",
"parallel_read_safe": True,
"parallel_write_safe": True}
def add_html_link(app, pagename, templatename, context, doctree):
"""As each page is built, collect page names for the sitemap"""
app.sitemap_links.append(pagename + ".html")
def create_sitemap(app, exception):
"""Generates the sitemap.xml from the collected HTML page links"""
root = ET.Element("urlset")
root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
app.sitemap_links.sort()
for link in app.sitemap_links:
url = ET.SubElement(root, "url")
priority = 0.5
if link == 'index.html':
priority = 1.0
link = ''
elif link.endswith('index.html'):
priority += 0.25
link = link[:-len('index.html')]
if link.endswith('.html'):
link = link[:-len('.html')]
ET.SubElement(url, "loc").text = app.builder.config.html_baseurl + '/' + link
ET.SubElement(url, "priority").text = str(priority)
filename = os.path.join(app.outdir, "sitemap.xml")
ET.ElementTree(root).write(filename,
xml_declaration=True,
encoding='utf-8',
method="xml")
with open(os.path.join(app.builder.outdir, 'robots.txt'), 'wt') as f:
if os.getenv('PRODUCTION') != 'YES':
f.write('User-agent: *\nDisallow: /\n')
else:
f.write('User-agent: *\nDisallow: \n\n'
'Sitemap: https://esphome.io/sitemap.xml\n')