From 769efffadfd26b7c1fea15ad51fc42426f18e067 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 6 May 2019 14:58:38 -0700 Subject: [PATCH] Generate sitemap in documentation --- presto-docs/pom.xml | 1 + presto-docs/src/main/sphinx/conf.py | 2 +- presto-docs/src/main/sphinx/ext/sitemap.py | 55 ++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 presto-docs/src/main/sphinx/ext/sitemap.py diff --git a/presto-docs/pom.xml b/presto-docs/pom.xml index 31959bb2dbc7a..4306c6e94af28 100644 --- a/presto-docs/pom.xml +++ b/presto-docs/pom.xml @@ -37,6 +37,7 @@ **/*.conf **/*.css_t + src/main/sphinx/ext/sitemap.py diff --git a/presto-docs/src/main/sphinx/conf.py b/presto-docs/src/main/sphinx/conf.py index 04638d55061b4..b02b9d2764d62 100644 --- a/presto-docs/src/main/sphinx/conf.py +++ b/presto-docs/src/main/sphinx/conf.py @@ -62,7 +62,7 @@ def get_version(): needs_sphinx = '1.1' -extensions = ['backquote', 'download', 'issue'] +extensions = ['backquote', 'download', 'issue', 'sitemap'] templates_path = ['_templates'] diff --git a/presto-docs/src/main/sphinx/ext/sitemap.py b/presto-docs/src/main/sphinx/ext/sitemap.py new file mode 100644 index 0000000000000..1202e9f5fc791 --- /dev/null +++ b/presto-docs/src/main/sphinx/ext/sitemap.py @@ -0,0 +1,55 @@ +# Forked from https://github.com/jdillard/sphinx-sitemap +# +# Copyright (c) 2013 Michael Dowling +# Copyright (c) 2017 Jared Dillard +# +# 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 xml.etree.ElementTree as ET +from xml.dom import minidom + + +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 = [] + app.locales = [] + + return { + 'parallel_read_safe': True, + 'parallel_write_safe': False, + } + + +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""" + if not app.sitemap_links: + raise (Exception('sitemap error: No pages generated for sitemap.xml')) + + root = ET.Element('urlset') + root.set('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9') + + for link in sorted(app.sitemap_links): + url = ET.SubElement(root, "url") + ET.SubElement(url, "loc").text = '/' + link + + xml = minidom.parseString(ET.tostring(root)).toprettyxml(' ') + + filename = app.outdir + "/sitemap.xml" + with open(filename, 'w') as f: + f.write(xml) + print('sitemap.xml was generated in %s' % filename)