Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Update mkdocs edit on github to work with addons #5164

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion man/mkdocs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ site_url: https://grass.osgeo.org/grass-stable/manuals/

# Repository information
repo_name: GitHub
repo_url: https://github.com/OSGeo/grass
repo_url: https://github.com/OSGeo/ # Set to OSGeo so we can added grass and grass-addons
edit_uri_template: edit/main/{path!q}

# Project Configuration
Expand Down
2 changes: 1 addition & 1 deletion man/mkdocs/overrides/partials/actions.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{# Default to the normal edit URL #}
{% set ghpath = github_path(toolname, config.extra.source_docs) %}
{% if ghpath %}
{% set custom_edit_url = page.edit_url | replace(toolname, ghpath ~ "/" ~ toolname) %}
{% set custom_edit_url = page.edit_url | replace(page.edit_url, ghpath ~ "/" ~ toolname ~ ".md") %}
{% if "content.action.edit" in features %}
<a href="{{ custom_edit_url }}" title="{{ lang.t('action.edit') }}" class="md-content__button md-icon">
{% set icon = config.theme.icon.edit or "material/file-edit-outline" %}
Expand Down
38 changes: 32 additions & 6 deletions man/mkdocs/scripts/hook_list_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,46 @@ def github_path(toolname: str, source_docs: dict[str, str]):
return None


def github_edit_path(ghpath: str):
"""Return the GitHub edit path for the given path"""
path_parts = ghpath.split("/")

# Gracefully handle invalid paths
if len(path_parts) < 3:
return None

repo = path_parts[0].strip()
doc_path = "/".join(path_parts[2:]).strip() # Drop repo_name/tree
osgeo_org = "https://github.com/OSGeo"
return f"{osgeo_org}/{repo}/edit/{doc_path}"


def on_env(env: Environment, config, files):
"""Enable loopcontrols extension in Jinja2"""
env.add_extension("jinja2.ext.loopcontrols")
env.globals["github_path"] = github_path
return env


def on_config(config):
def set_extra_source_docs(source_dir: str):
"""
Read the list of tools from the source directory and
store it in MkDocs extra config. These are used to generate
the correct links to the documentation in GitHub.
"""
source_dir = Path("source")
source_dir = Path(source_dir)

# Dict to store the documentation path for each tool in GitHub
# the key is the doc name and the value is the path in the GitHub repository
source_docs = {}

# Read the source files and extract the GitHub path from the Available at link at the bottom of the page
pattern = re.compile(
r"Available at:\s*\[(?P<text>.*?)\]\(https://github\.com/OSGeo/grass/tree/main/(?P<gh_path>.*?)\)"
# r"Available at:\s*\[(?P<text>.*?)\]\(https://github\.com/OSGeo/(grass|grass-addons)/tree/(main|grass8)/(?P<gh_path>.*?)\)"
r"Available at:\s*\[(?P<text>.*?)\]\(https://github\.com/OSGeo/(?P<gh_path>.*?)\)"
)

for file in source_dir.glob("*.md"):
for file in source_dir.rglob("*.md"):
with file.open() as f:
for line in f:
match = pattern.search(line)
Expand All @@ -48,8 +63,19 @@ def on_config(config):
toolname = re.sub(
r"\s*source\s+code\s*$", "", text, flags=re.IGNORECASE
).strip()
source_docs[toolname] = gh_path
print(f"Found {toolname} at {gh_path}")
source_docs[toolname] = github_edit_path(gh_path)

# Store in MkDocs extra config
return source_docs


def on_config(config):
"""
Runs on MkDocs config to set the extra config with the source_docs
"""

# Store in MkDocs extra config
config["extra"]["source_docs"] = source_docs
config["extra"]["source_docs"] = set_extra_source_docs("source")

return config
Loading