Skip to content

Commit 99ed5dd

Browse files
authored
docs: Update mkdocs edit on GitHub to work with addons (#5164)
Switches the URL between core and addon tools based on the 'Available at' which is already present in the generated Markdown files.
1 parent 144279e commit 99ed5dd

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

man/mkdocs/mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ site_url: https://grass.osgeo.org/grass-stable/manuals/
55

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

1111
# Project Configuration

man/mkdocs/overrides/partials/actions.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{# Default to the normal edit URL #}
55
{% set ghpath = github_path(toolname, config.extra.source_docs) %}
66
{% if ghpath %}
7-
{% set custom_edit_url = page.edit_url | replace(toolname, ghpath ~ "/" ~ toolname) %}
7+
{% set custom_edit_url = page.edit_url | replace(page.edit_url, ghpath ~ "/" ~ toolname ~ ".md") %}
88
{% if "content.action.edit" in features %}
99
<a href="{{ custom_edit_url }}" title="{{ lang.t('action.edit') }}" class="md-content__button md-icon">
1010
{% set icon = config.theme.icon.edit or "material/file-edit-outline" %}

man/mkdocs/scripts/hook_list_scripts.py

+32-6
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,46 @@ def github_path(toolname: str, source_docs: dict[str, str]):
1111
return None
1212

1313

14+
def github_edit_path(ghpath: str, base_repo_url: str):
15+
"""Return the GitHub edit path for the given path"""
16+
path_parts = ghpath.split("/")
17+
18+
# Gracefully handle invalid paths
19+
if len(path_parts) < 3:
20+
return None
21+
22+
repo = path_parts[0].strip()
23+
doc_path = "/".join(path_parts[2:]).strip() # Drop repo_name/tree
24+
return f"{base_repo_url}/{repo}/edit/{doc_path}"
25+
26+
1427
def on_env(env: Environment, config, files):
1528
"""Enable loopcontrols extension in Jinja2"""
1629
env.add_extension("jinja2.ext.loopcontrols")
1730
env.globals["github_path"] = github_path
1831
return env
1932

2033

21-
def on_config(config):
34+
def set_extra_source_docs(source_dir: str, config: dict):
2235
"""
2336
Read the list of tools from the source directory and
2437
store it in MkDocs extra config. These are used to generate
2538
the correct links to the documentation in GitHub.
2639
"""
27-
source_dir = Path("source")
40+
41+
base_repo_url = config["repo_url"] # Set in mkdocs.yml as repo_url
42+
source_dir = Path(source_dir)
2843

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

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

38-
for file in source_dir.glob("*.md"):
53+
for file in source_dir.rglob("*.md"):
3954
with file.open() as f:
4055
for line in f:
4156
match = pattern.search(line)
@@ -48,8 +63,19 @@ def on_config(config):
4863
toolname = re.sub(
4964
r"\s*source\s+code\s*$", "", text, flags=re.IGNORECASE
5065
).strip()
51-
source_docs[toolname] = gh_path
66+
print(f"Found {toolname} at {gh_path}")
67+
source_docs[toolname] = github_edit_path(gh_path, base_repo_url)
5268

5369
# Store in MkDocs extra config
54-
config["extra"]["source_docs"] = source_docs
70+
return source_docs
71+
72+
73+
def on_config(config):
74+
"""
75+
Runs on MkDocs config to set the extra config with the source_docs
76+
"""
77+
78+
# Store in MkDocs extra config
79+
config["extra"]["source_docs"] = set_extra_source_docs("source", config)
80+
5581
return config

0 commit comments

Comments
 (0)