Skip to content

Commit c1da5fd

Browse files
authored
docs: mkdocs edit and view on github (#5114)
1 parent cdf9fe7 commit c1da5fd

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

man/Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ DSTFILES := \
1616
$(MDDIR)/mkdocs.yml \
1717
$(MDDIR)/source/grass_logo.png \
1818
$(MDDIR)/source/grassdocs.css \
19-
$(MDDIR)/overrides/partials/footer.html
19+
$(MDDIR)/scripts/hook_list_scripts.py \
20+
$(MDDIR)/overrides/partials/footer.html \
21+
$(MDDIR)/overrides/partials/actions.html
2022

2123
categories = \
2224
d:display \
@@ -248,9 +250,18 @@ $(MDDIR)/mkdocs.yml: mkdocs/mkdocs.yml
248250
$(MDDIR)/source/grassdocs.css: mkdocs/grassdocs.css
249251
$(INSTALL_DATA) $< $@
250252

253+
$(MDDIR)/scripts/hook_list_scripts.py: mkdocs/scripts/hook_list_scripts.py | $(MDDIR)/scripts
254+
$(INSTALL_DATA) $< $@
255+
256+
$(MDDIR)/scripts:
257+
$(MKDIR) $@
258+
251259
$(MDDIR)/overrides/partials/footer.html: mkdocs/overrides/partials/footer.html | $(MDDIR)/overrides/partials
252260
$(INSTALL_DATA) $< $@
253261

262+
$(MDDIR)/overrides/partials/actions.html: mkdocs/overrides/partials/actions.html | $(MDDIR)/overrides/partials
263+
$(INSTALL_DATA) $< $@
264+
254265
$(MDDIR)/overrides/partials:
255266
$(MKDIR) $@
256267

man/mkdocs/mkdocs.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ site_url: https://grass.osgeo.org/grass-stable/manuals/
66
# Repository information
77
repo_name: GitHub
88
repo_url: https://github.com/OSGeo/grass
9-
# edit_uri_template: edit/main/{path}
9+
edit_uri_template: edit/main/{path!q}
1010

1111
# Project Configuration
1212
docs_dir: source
@@ -33,20 +33,25 @@ theme:
3333
# - navigation.expand
3434
- navigation.tabs
3535
- navigation.tabs.sticky
36-
37-
# - content.action.edit # Edit on GitHub
36+
- content.action.view
37+
- content.action.edit # Edit on GitHub
3838
palette:
3939
primary: custom
4040
# accent: custom
4141
icon:
4242
repo: fontawesome/brands/github
43-
# edit: material/pencil
43+
edit: material/pencil
4444
view: material/eye
4545

4646
# Customization
4747
extra:
4848
homepage: ./index.html
4949

50+
# Hooks
51+
hooks:
52+
- scripts/hook_list_scripts.py
53+
54+
# Custom CSS
5055
extra_css:
5156
- grassdocs.css
5257

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% if page.edit_url %}
2+
3+
{% set toolname = page.file.src_path.split("/")[-1] | replace(".md", "") %}
4+
{# Default to the normal edit URL #}
5+
{% set ghpath = github_path(toolname, config.extra.source_docs) %}
6+
{% if ghpath %}
7+
{% set custom_edit_url = page.edit_url | replace(toolname, ghpath ~ "/" ~ toolname) %}
8+
{% if "content.action.edit" in features %}
9+
<a href="{{ custom_edit_url }}" title="{{ lang.t('action.edit') }}" class="md-content__button md-icon">
10+
{% set icon = config.theme.icon.edit or "material/file-edit-outline" %}
11+
{% include ".icons/" ~ icon ~ ".svg" %}
12+
</a>
13+
{% endif %}
14+
15+
{% if "content.action.view" in features %}
16+
{% if "/blob/" in custom_edit_url %}
17+
{% set part = "blob" %}
18+
{% else %}
19+
{% set part = "edit" %}
20+
{% endif %}
21+
<a href="{{ custom_edit_url | replace(part, 'tree') }}" title="{{ lang.t('action.view') }}" class="md-content__button md-icon">
22+
{% set icon = config.theme.icon.view or "material/file-eye-outline" %}
23+
{% include ".icons/" ~ icon ~ ".svg" %}
24+
</a>
25+
{% endif %}
26+
{% endif %}
27+
{% endif %}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from pathlib import Path
2+
import re
3+
from jinja2 import Environment
4+
5+
6+
def github_path(toolname: str, source_docs: dict[str, str]):
7+
"""Return the GitHub path for the given toolname"""
8+
try:
9+
return source_docs[toolname]
10+
except KeyError:
11+
return None
12+
13+
14+
def on_env(env: Environment, config, files):
15+
"""Enable loopcontrols extension in Jinja2"""
16+
env.add_extension("jinja2.ext.loopcontrols")
17+
env.globals["github_path"] = github_path
18+
return env
19+
20+
21+
def on_config(config):
22+
"""
23+
Read the list of tools from the source directory and
24+
store it in MkDocs extra config. These are used to generate
25+
the correct links to the documentation in GitHub.
26+
"""
27+
source_dir = Path("source")
28+
29+
# Dict to store the documentation path for each tool in GitHub
30+
# the key is the doc name and the value is the path in the GitHub repository
31+
source_docs = {}
32+
33+
# Read the source files and extract the GitHub path from the Available at link at the bottom of the page
34+
pattern = re.compile(
35+
r"Available at:\s*\[(?P<text>.*?)\]\(https://github\.com/OSGeo/grass/tree/main/(?P<gh_path>.*?)\)"
36+
)
37+
38+
for file in source_dir.glob("*.md"):
39+
with file.open() as f:
40+
for line in f:
41+
match = pattern.search(line)
42+
if match:
43+
# Extract the entire link text and the GitHub path.
44+
text = match.group("text")
45+
gh_path = match.group("gh_path").strip()
46+
47+
# Remove the trailing "source code" from the text to get the tool name.
48+
toolname = re.sub(
49+
r"\s*source\s+code\s*$", "", text, flags=re.IGNORECASE
50+
).strip()
51+
source_docs[toolname] = gh_path
52+
53+
# Store in MkDocs extra config
54+
config["extra"]["source_docs"] = source_docs
55+
return config

0 commit comments

Comments
 (0)