-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic generation for updates (#2761)
* Add dynamic generation for updates * Update links
- Loading branch information
1 parent
7a7d30c
commit 1535e4b
Showing
17 changed files
with
238 additions
and
3,349 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -16,7 +16,7 @@ bin/ | |
obj/ | ||
out/ | ||
!out/dist/ | ||
!docs/CHANGELOG-v3.md | ||
!docs/changelog.md | ||
|
||
.eslintignore | ||
.gitignore | ||
|
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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,108 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
# NOTES: | ||
# This file implements replacement for shortcodes in markdown using MkDocs native hooks. | ||
|
||
import logging | ||
import os | ||
import re | ||
|
||
from mkdocs.config.defaults import MkDocsConfig | ||
from mkdocs.structure.files import File, Files | ||
from mkdocs.structure.pages import Page | ||
|
||
log = logging.getLogger(f"mkdocs") | ||
|
||
# | ||
# Hooks | ||
# | ||
|
||
|
||
def on_page_markdown(markdown: str, *, page: Page, config: MkDocsConfig, files: Files) -> str: | ||
'''Hook on_page_markdown event.''' | ||
|
||
return module(markdown, page, config, files) | ||
|
||
# | ||
# Supporting functions | ||
# | ||
|
||
|
||
def module(markdown: str, page: Page, config: MkDocsConfig, files: Files) -> str: | ||
'''Replace module shortcodes in markdown.''' | ||
|
||
# Callback for regular expression replacement. | ||
def replace(match: re.Match) -> str: | ||
type, args = match.groups() | ||
args = args.strip() | ||
if type == "version": | ||
return _badge_for_version(args, page, files) | ||
elif type == "issues": | ||
return _link_to_patch_issues(args, page, files) | ||
|
||
raise RuntimeError(f"Unknown shortcode module:{type}") | ||
|
||
# Replace module shortcodes. | ||
return re.sub( | ||
r"<!-- module:(\w+)(.*?) -->", | ||
replace, markdown, flags=re.I | re.M | ||
) | ||
|
||
|
||
def _relative_uri(path: str, page: Page, files: Files) -> str: | ||
'''Get relative URI for a file including anchor.''' | ||
|
||
path, anchor, *_ = f"{path}#".split("#") | ||
path = _relative_path(files.get_file_from_path(path), page) | ||
return "#".join([path, anchor]) if anchor else path | ||
|
||
|
||
def _relative_path(file: File, page: Page) -> str: | ||
'''Get relative source path for a file to a page.''' | ||
|
||
path = os.path.relpath(file.src_uri, page.file.src_uri) | ||
return os.path.sep.join(path.split(os.path.sep)[1:]) | ||
|
||
|
||
def _badge(icon: str, text: str = "") -> str: | ||
'''Create span block for a badge.''' | ||
|
||
classes = "badge" | ||
return "".join([ | ||
f"<span class=\"{classes}\">", | ||
*([f"<span class=\"badge__icon\">{icon}</span>"] if icon else []), | ||
*([f"<span class=\"badge__text\">{text}</span>"] if text else []), | ||
f"</span>", | ||
]) | ||
|
||
|
||
def _badge_for_version(text: str, page: Page, files: Files) -> str: | ||
'''Create badge for minimum version.''' | ||
|
||
# Get place in changelog. | ||
version = text | ||
major = version.split('.')[0] | ||
anchor = version.replace('.', '') | ||
path = f"changelog.md#v{anchor}" | ||
|
||
icon = "octicons-milestone-24" | ||
href = _relative_uri(path, page, files) | ||
return _badge( | ||
icon=f"[:{icon}:]({href} 'Minimum version')", | ||
text=f"[{text}]({href})" | ||
) | ||
|
||
|
||
def _link_to_patch_issues(text: str, page: Page, files: Files) -> str: | ||
'''Link to GitHub milestone issues.''' | ||
|
||
# Get place in changelog. | ||
version = text | ||
major = version.split('.')[0] | ||
anchor = version.replace('.', '') | ||
path = f"changelog.md#v{anchor}" | ||
|
||
icon = "octicons-milestone-24" | ||
href = f"<strong>Update {version}:</strong> The update addresses these [issues](https://github.com/microsoft/PSRule/issues?q=is%3Aissue%20state%3Aclosed%20milestone%3Av{version}).\n\n" | ||
return href |
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,93 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
# NOTES: | ||
# This file implements dynamic navigation for updates using MkDocs native hooks. | ||
|
||
import logging | ||
import re | ||
import semver | ||
|
||
from mkdocs.config import Config | ||
from mkdocs.config.defaults import MkDocsConfig | ||
from mkdocs.structure.files import File, Files | ||
from mkdocs.structure.pages import Page | ||
from mkdocs.structure.nav import Section, Navigation, _add_parent_links | ||
|
||
log = logging.getLogger(f"mkdocs") | ||
|
||
# | ||
# Hooks | ||
# | ||
|
||
|
||
def on_nav(nav: Navigation, config: Config, files: Files) -> Navigation: | ||
'''Hook on_nav event.''' | ||
add_updates_to_nav(nav, config, files) | ||
|
||
return nav | ||
|
||
|
||
def on_page_markdown(markdown: str, page: Page, config: Config, files: Files) -> str: | ||
'''Hook on_page_markdown event.''' | ||
markdown = add_update_version_to_title(markdown, page) | ||
|
||
return markdown | ||
|
||
# | ||
# Supporting functions | ||
# | ||
|
||
|
||
def add_updates_to_nav(nav: Navigation, config: Config, files: Files): | ||
'''Add updates to the nav.''' | ||
|
||
section: Section = next( | ||
x for x in nav if x.title == "Updates") | ||
|
||
# Get the list of files that are update pages. | ||
children = [] | ||
for f in files: | ||
if not f.is_documentation_page(): | ||
continue | ||
|
||
# Check if the page already exists in section children that are Page. | ||
if any(isinstance(child, Page) and child.file.src_path == f.src_path for child in section.children): | ||
continue | ||
|
||
destPath = f._get_dest_path(False) | ||
if not is_update_page(destPath): | ||
continue | ||
children.append(f) | ||
|
||
# Sort by semver version string. | ||
children.sort(key=lambda x: semver.VersionInfo.parse( | ||
x.src_path.split('/')[-1].replace(".md", ".0").replace("v", "")), reverse=False) | ||
|
||
# Add the more recent 10 updates to the nav. | ||
for child in children[:10]: | ||
log.info(f"Added {child.src_path} to list of updates.") | ||
section.children.insert(0, Page(None, child, config)) | ||
|
||
_add_parent_links(nav) | ||
|
||
|
||
def add_update_version_to_title(markdown: str, page: Page) -> str: | ||
'''Add version to title of update pages.''' | ||
|
||
if not is_update_page(page.canonical_url): | ||
return markdown | ||
|
||
version = page.meta.get('version', None) | ||
if not version: | ||
return markdown | ||
|
||
title = re.search(r"^# (.+)$", markdown, flags=re.M).group(1) | ||
page.title = title | ||
|
||
# Append the version number to the first H1 title of the page | ||
return markdown.replace(f"# {title}", f"# {title} (version {version})") | ||
|
||
|
||
def is_update_page(path: str) -> bool: | ||
return path.__contains__("updates/v") |
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
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,3 @@ | ||
|
||
*[CI]: continuous integration | ||
*[IaC]: Infrastructure as Code |
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
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