@@ -11,31 +11,46 @@ def github_path(toolname: str, source_docs: dict[str, str]):
11
11
return None
12
12
13
13
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
+
14
27
def on_env (env : Environment , config , files ):
15
28
"""Enable loopcontrols extension in Jinja2"""
16
29
env .add_extension ("jinja2.ext.loopcontrols" )
17
30
env .globals ["github_path" ] = github_path
18
31
return env
19
32
20
33
21
- def on_config ( config ):
34
+ def set_extra_source_docs ( source_dir : str , config : dict ):
22
35
"""
23
36
Read the list of tools from the source directory and
24
37
store it in MkDocs extra config. These are used to generate
25
38
the correct links to the documentation in GitHub.
26
39
"""
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 )
28
43
29
44
# Dict to store the documentation path for each tool in GitHub
30
45
# the key is the doc name and the value is the path in the GitHub repository
31
46
source_docs = {}
32
47
33
48
# Read the source files and extract the GitHub path from the Available at link at the bottom of the page
34
49
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>.*?)\)"
36
51
)
37
52
38
- for file in source_dir .glob ("*.md" ):
53
+ for file in source_dir .rglob ("*.md" ):
39
54
with file .open () as f :
40
55
for line in f :
41
56
match = pattern .search (line )
@@ -48,8 +63,19 @@ def on_config(config):
48
63
toolname = re .sub (
49
64
r"\s*source\s+code\s*$" , "" , text , flags = re .IGNORECASE
50
65
).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 )
52
68
53
69
# 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
+
55
81
return config
0 commit comments