-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Integration modules UUID redirection
- Loading branch information
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import re | ||
import string | ||
from pathlib import Path | ||
|
||
import mkdocs | ||
from mkdocs.config import Config | ||
from mkdocs.structure.files import File, Files | ||
from mkdocs.utils.meta import get_data | ||
|
||
|
||
class ModulesByUUIDPlugin(mkdocs.plugins.BasePlugin): | ||
"""Reading Markdown files that contains an `uuid` metadata to provide | ||
a redirection. | ||
When such a file is identified, a new | ||
`integration/action_library/uuid//uuid/$uuid.md` file is faked | ||
which will redirect to it.""" | ||
|
||
template = """<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Redirecting...</title> | ||
<link rel="canonical" href="../../../../{destination}"> | ||
<meta name="robots" content="noindex"> | ||
<script>var anchor=window.location.hash.substr(1);location.href="../../../../{destination}"+(anchor?"#"+anchor:"")</script> | ||
<meta http-equiv="refresh" content="0; url=../../../../{destination}"> | ||
</head> | ||
<body> | ||
Redirecting... | ||
</body> | ||
</html>""" | ||
|
||
_redirection_table: dict[str, str] = {} | ||
|
||
def on_files(self, files: Files, config: Config): | ||
for source_file in files: | ||
if not source_file.src_path.endswith(".md"): | ||
continue | ||
|
||
filename = Path(config["docs_dir"]) / Path(source_file.src_path) | ||
try: | ||
with filename.open() as f: | ||
_, metadata = get_data(f.read()) | ||
except: | ||
# File may have been generated by an other plugin | ||
continue | ||
|
||
if ( | ||
"uuid" not in metadata | ||
or metadata.get("type").lower() != "playbook" | ||
or not source_file.url.startswith("integration/action_library/") | ||
): | ||
continue | ||
|
||
self._redirection_table[metadata["uuid"]] = source_file.url | ||
|
||
files._files.append( | ||
File( | ||
path=f"integration/action_library/uuid/{metadata['uuid']}.md", | ||
src_dir="integration/action_library/uuid", | ||
dest_dir=config["site_dir"], | ||
use_directory_urls=True, | ||
) | ||
) | ||
|
||
def on_page_read_source(self, page, config): | ||
if page.file.src_path.startswith("integration/action_library/uuid/"): | ||
if page.file.name in self._redirection_table: | ||
return self.template.format( | ||
destination=self._redirection_table[page.file.name] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters