Skip to content

Commit 6d4338a

Browse files
oraNodwebknjaz
andauthored
Extension for Ansible redirects (#2418)
* add reredirects dependency * update conf file to generate ansible redirects * add extra tags to makefile * define redirect rules in key value pairs * update build workflows to generate redirects * add redirect template for SEO purposes --------- Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]>
1 parent 8f8cc5d commit 6d4338a

File tree

8 files changed

+499
-3
lines changed

8 files changed

+499
-3
lines changed

.github/workflows/build-package-docs.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ name: Build and deploy docs
3232
- '11'
3333
- '10'
3434
- '9'
35+
generate-redirects:
36+
description: Generate page redirects
37+
type: boolean
38+
default: true
3539
deploy:
3640
description: Deploy the build
3741
type: boolean
3842
required: true
43+
default: false
3944
deployment-environment:
4045
description: Deployment environment
4146
type: choice
@@ -54,6 +59,9 @@ jobs:
5459
repository-name: ${{ github.event.inputs.repository-name || 'ansible-documentation' }}
5560
repository-branch: ${{ github.event.inputs.repository-branch || 'devel' }}
5661
ansible-package-version: ${{ github.event.inputs.ansible-package-version || 'devel' }}
62+
generate-redirects: >-
63+
${{ github.event_name == 'workflow_dispatch'
64+
&& github.event.inputs.generate-redirects == 'true' }}
5765
secrets:
5866
DOCS_BOT_TOKEN: ${{ secrets.DOCS_BOT_TOKEN }}
5967

.github/workflows/reusable-build-docs.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ name: Build docs
2424
description: Ansible community package version
2525
required: false
2626
default: devel
27+
generate-redirects:
28+
description: Generate page redirects
29+
type: boolean
30+
default: true
2731
secrets:
2832
DOCS_BOT_TOKEN:
2933
required: true
@@ -69,7 +73,9 @@ jobs:
6973

7074
- name: Build the Ansible community package docs
7175
run: >-
72-
make webdocs ANSIBLE_VERSION="${{
76+
make webdocs ${{
77+
inputs.generate-redirects && 'EXTRA_TAGS="-t redirects"' || ''
78+
}} ANSIBLE_VERSION="${{
7379
env.PACKAGE_VERSION != 'devel' && env.PACKAGE_VERSION || ''
7480
}}"
7581
working-directory: build-directory/docs/docsite
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html><head><meta name="robots" content="noindex"><noscript><meta http-equiv="refresh" content="0; url=${to_uri}" /></noscript><script>var target = "${to_uri}";if (window.location.hash) {window.location.replace(target + window.location.hash);} else {window.location.replace(target);}</script></head></html>

docs/docsite/Makefile.sphinx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#
33

44
# You can set these variables from the command line.
5+
EXTRA_TAGS ?=
56
DOCS_VARIANTS ?=
67
SPHINXCONFDIR = rst
78
LANGOPTS ?=
8-
SPHINXOPTS ?= -j $(CPUS) -n -w rst_warnings -c "$(SPHINXCONFDIR)" $(DOCS_VARIANTS) $(LANGOPTS)
9+
SPHINXOPTS ?= -j $(CPUS) -n -w rst_warnings -c "$(SPHINXCONFDIR)" $(DOCS_VARIANTS) $(EXTRA_TAGS) $(LANGOPTS)
910
SPHINXBUILD = sphinx-build
1011
SPHINXPROJ = sdfsdf
1112
SOURCEDIR = rst

docs/docsite/declarative-configs/ansible_redirects.toml

Lines changed: 451 additions & 0 deletions
Large diffs are not rendered by default.

docs/docsite/rst/conf.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
import sys
2020
import os
21+
import tomllib
22+
from pathlib import Path
23+
24+
from sphinx.application import Sphinx
25+
26+
DOCS_ROOT_DIR = Path(__file__).parent.resolve()
2127

2228
# If your extensions are in another directory, add it here. If the directory
2329
# is relative to the documentation root, use os.path.abspath to make it
@@ -376,7 +382,7 @@
376382
autoclass_content = 'both'
377383

378384
# Note: Our strategy for intersphinx mappings is to have the upstream build location as the
379-
# canonical source.
385+
# canonical source.
380386
intersphinx_mapping = {
381387
'python': ('https://docs.python.org/2/', None),
382388
'python3': ('https://docs.python.org/3/', None),
@@ -390,3 +396,22 @@
390396
]
391397
linkcheck_workers = 25
392398
# linkcheck_anchors = False
399+
400+
# Generate redirects for pages when building on Read The Docs
401+
def setup(app: Sphinx) -> dict[str, bool | str]:
402+
403+
if 'redirects' in app.tags:
404+
405+
redirects_config_path = DOCS_ROOT_DIR.parent / "declarative-configs" / "ansible_redirects.toml"
406+
redirects = tomllib.loads(redirects_config_path.read_text())
407+
redirect_template = DOCS_ROOT_DIR.parent / ".templates" / "redirect_template.html"
408+
409+
app.config.redirects = redirects
410+
app.config.redirect_html_template_file = redirect_template
411+
app.setup_extension('sphinx_reredirects') # redirect pages that have been restructured or removed
412+
413+
return {
414+
"parallel_read_safe": True,
415+
"parallel_write_safe": True,
416+
"version": app.config.release,
417+
}

tests/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ sphinx
99
sphinx-intl # translation utility used by docs/docsite/Makefile
1010
sphinx-notfound-page # extension used for the custom 404 page (cowsay)
1111
sphinx-ansible-theme # extension used for the custom docs theme
12+
sphinx-reredirects # extension to create redirects for moved pages
1213
sphinx-rtd-theme
1314
rstcheck
1415
yamllint

tests/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ sphinx==7.2.5
164164
# sphinx-copybutton
165165
# sphinx-intl
166166
# sphinx-notfound-page
167+
# sphinx-reredirects
167168
# sphinx-rtd-theme
168169
# sphinxcontrib-jquery
169170
sphinx-ansible-theme==0.10.3
@@ -174,6 +175,8 @@ sphinx-intl==2.3.1
174175
# via -r tests/requirements.in
175176
sphinx-notfound-page==1.1.0
176177
# via -r tests/requirements.in
178+
sphinx-reredirects==0.1.6
179+
# via -r tests/requirements.in
177180
sphinx-rtd-theme==3.0.2
178181
# via
179182
# -c tests/constraints.in

0 commit comments

Comments
 (0)