[DOCS] Extend sphinx_sitemap to add custom metadata (#19579)
This commit is contained in:
parent
90ef7096b9
commit
9297bc5128
15
docs/conf.py
15
docs/conf.py
@ -44,7 +44,7 @@ extensions = [
|
||||
'cpplexer',
|
||||
'sphinx.ext.autodoc',
|
||||
'sphinx.ext.autosummary',
|
||||
'sphinx_sitemap'
|
||||
'openvino_custom_sphinx_sitemap'
|
||||
]
|
||||
|
||||
html_baseurl = 'https://docs.openvino.ai/canonical/'
|
||||
@ -54,6 +54,19 @@ html_baseurl = 'https://docs.openvino.ai/canonical/'
|
||||
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,
|
||||
})
|
||||
]
|
||||
|
||||
# ----------------------------------------------------
|
||||
|
||||
|
||||
|
@ -0,0 +1,5 @@
|
||||
Metadata-Version: 2.1
|
||||
Name: openvino-custom-sphinx-sitemap
|
||||
Version: 0.0.1
|
||||
Summary: Extends sphinx-sitemap plugin with additional sitemap metadata
|
||||
Requires-Python: >=3.9
|
@ -0,0 +1,7 @@
|
||||
pyproject.toml
|
||||
openvino_custom_sphinx_sitemap/__init__.py
|
||||
openvino_custom_sphinx_sitemap.egg-info/PKG-INFO
|
||||
openvino_custom_sphinx_sitemap.egg-info/SOURCES.txt
|
||||
openvino_custom_sphinx_sitemap.egg-info/dependency_links.txt
|
||||
openvino_custom_sphinx_sitemap.egg-info/requires.txt
|
||||
openvino_custom_sphinx_sitemap.egg-info/top_level.txt
|
@ -0,0 +1 @@
|
||||
|
@ -0,0 +1,2 @@
|
||||
sphinx>=4.5.0
|
||||
sphinx-sitemap>=2.2.0
|
@ -0,0 +1 @@
|
||||
openvino_custom_sphinx_sitemap
|
@ -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
docs/openvino_custom_sphinx_sitemap/pyproject.toml
Normal file
13
docs/openvino_custom_sphinx_sitemap/pyproject.toml
Normal file
@ -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.9"
|
@ -45,3 +45,4 @@ sphinxcontrib-serializinghtml==1.1.5
|
||||
toml==0.10.2
|
||||
urllib3==1.26.5
|
||||
zipp==3.4.1
|
||||
docs/openvino_custom_sphinx_sitemap
|
||||
|
Loading…
Reference in New Issue
Block a user