Skip to content

Commit

Permalink
[DOCS] Port sitemap extension from nightly (#19835)
Browse files Browse the repository at this point in the history
* Port sitemap extension from nightly

* Bugfix requirements

* Bugfix requirements 2
  • Loading branch information
bstankix authored Sep 18, 2023
1 parent 30ae454 commit a771bf3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/build_doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ jobs:
python3 -m pip install -r docs/requirements.txt --user
cd docs/openvino_sphinx_theme
python3 setup.py install --user
cd ../openvino_custom_sphinx_sitemap
python3 setup.py install --user
cd ../..
# install doxyrest
wget https://github.com/vovkos/doxyrest/releases/download/doxyrest-2.1.3/doxyrest-2.1.3-linux-amd64.tar.xz
Expand Down
15 changes: 14 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
'cpplexer',
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx_sitemap'
'openvino_custom_sphinx_sitemap'
]

html_baseurl = 'https://docs.openvino.ai/canonical/'
Expand All @@ -54,6 +54,19 @@
sitemap_url_scheme = "{link}"
site_url = f'https://docs.openvino.ai/{version_name}/'

ov_sitemap_urlset = [
("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"),
("xmlns:coveo", "https://www.coveo.com/en/company/about-us"),
("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd")
]

ov_sitemap_meta = [
('coveo:metadata', {
'ovversion': version_name,
})
]

# ----------------------------------------------------


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import xml.etree.ElementTree as ET
from sphinx_sitemap import setup as base_setup, get_locales, hreflang_formatter


def setup(app):
app.add_config_value(
'ov_sitemap_urlset',
default=None,
rebuild=''
)

app.add_config_value(
'ov_sitemap_meta',
default=None,
rebuild=''
)

setup = base_setup(app)
for listener in app.events.listeners['build-finished']:
if listener.handler.__name__ == 'create_sitemap':
app.disconnect(listener.id)

app.connect('build-finished', create_sitemap)
return setup


def create_sitemap(app, exception):
"""Generates the sitemap.xml from the collected HTML page links"""

urlset = app.builder.config.ov_sitemap_urlset
meta = app.builder.config.ov_sitemap_meta

site_url = app.builder.config.site_url or app.builder.config.html_baseurl
site_url = site_url.rstrip('/') + '/'
if not site_url:
print("sphinx-sitemap error: neither html_baseurl nor site_url "
"are set in conf.py. Sitemap not built.")
return
if (not app.sitemap_links):
print("sphinx-sitemap warning: No pages generated for %s" %
app.config.sitemap_filename)
return

ET.register_namespace('xhtml', "http://www.w3.org/1999/xhtml")

root = ET.Element("urlset")

if not urlset:
root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
else:
for item in urlset:
root.set(*item)

get_locales(app, exception)

if app.builder.config.version:
version = app.builder.config.version + '/'
else:
version = ""

for link in app.sitemap_links:
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
)

if meta:
for entry in meta:
namespace, values = entry
namespace_element = ET.SubElement(url, namespace)
for tag_name, tag_value in values.items():
ET.SubElement(namespace_element, tag_name).text = tag_value

if len(app.locales) > 0:
for lang in app.locales:
lang = lang + '/'
linktag = ET.SubElement(
url,
"{http://www.w3.org/1999/xhtml}link"
)
linktag.set("rel", "alternate")
linktag.set("hreflang", hreflang_formatter(lang.rstrip('/')))
linktag.set("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")
print("%s was generated for URL %s in %s" % (app.config.sitemap_filename,
site_url, filename))
13 changes: 13 additions & 0 deletions docs/openvino_custom_sphinx_sitemap/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "openvino_custom_sphinx_sitemap"
version = "0.0.1"
description = "Extends sphinx-sitemap plugin with additional sitemap metadata"
dependencies = [
"sphinx >= 4.5.0",
"sphinx-sitemap >= 2.2.0"
]
requires-python = ">=3.7"
9 changes: 9 additions & 0 deletions docs/openvino_custom_sphinx_sitemap/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from setuptools import setup


setup(
name="openvino_custom_sphinx_sitemap",
version="0.0.1",
install_requires=['sphinx>=4.5.0', 'sphinx-sitemap>=2.2.0'],
packages=['openvino_custom_sphinx_sitemap'],
)

0 comments on commit a771bf3

Please sign in to comment.