Skip to content

Commit 63b929d

Browse files
authored
Add custom edit URLs to generated website pages. (#15605)
The MLIR dialect reference pages at https://iree.dev/reference/mlir-dialects/ are generated from a mix of .td (tablegen) files. This fixes the "edit this page" links for those pages to point at valid source locations ![image](https://github.com/openxla/iree/assets/4010439/a1129072-5371-4555-87ac-6c80ec6fe07d) For example, https://iree.dev/reference/mlir-dialects/HAL/ now points to https://github.com/openxla/iree/tree/main/compiler/src/iree/compiler/Dialect/HAL/IR instead of https://github.com/openxla/iree/blob/main/docs/website/docs/reference/mlir-dialects/HAL.md (which does not exist)
1 parent ac95484 commit 63b929d

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

docs/website/custom_edit_url.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2023 The IREE Authors
3+
#
4+
# Licensed under the Apache License v2.0 with LLVM Exceptions.
5+
# See https://llvm.org/LICENSE.txt for license information.
6+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
8+
"""Custom edit URL plugin for mkdocs.
9+
10+
MkDocs can create links to view/edit the source of a website page in the
11+
associated source location (e.g. GitHub repository).
12+
13+
* https://www.mkdocs.org/user-guide/configuration/#edit_uri
14+
* https://squidfunk.github.io/mkdocs-material/setup/adding-a-git-repository/#code-actions
15+
16+
For markdown files in the documentation folder, this is a straightforward
17+
process, taking the relative path and appending it to the source location:
18+
/repo_root/docs/section/page.md -->
19+
https://github.com/[org]/[repo]/blob/main/docs/section/page.md
20+
21+
For generated files, the inferred URL does not match the actual source:
22+
/repo_root/src/file.cc -->
23+
/repo_root/docs/gen/file.md -->
24+
[broken link] https://github.com/[org]/[repo]/blob/main/docs/gen/file.md
25+
26+
This plugin allows for pages to explicitly specify their source URL via
27+
markdown frontmatter.
28+
29+
References:
30+
* https://github.com/renovatebot/renovatebot.github.io/pull/187
31+
* https://github.com/mkdocs/mkdocs/discussions/2757
32+
33+
Usage:
34+
35+
1. Add a hook to mkdocs.yml by following
36+
https://www.mkdocs.org/user-guide/configuration/#hooks
37+
38+
2. Add frontmatter to any pages you want to customize:
39+
40+
---
41+
custom_edit_url: [full hyperlink to the source location, e.g. https://github.com/...]
42+
---
43+
"""
44+
45+
46+
def on_page_context(context, page, config, **kwargs):
47+
if "custom_edit_url" in page.meta:
48+
page.edit_url = page.meta["custom_edit_url"]
49+
return context

docs/website/docs/reference/mlir-dialects/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ Dialect | Description
4141
------------------------------------| -----------
4242
[IREEInput](./IREEInput.md) | Structural ops legal as input to IREE's compiler
4343
[IREELinalgExt](./IREELinalgExt.md) | Extensions to the Linalg dialect for specific operations
44+
[IREEVectorExt](./IREEVectorExt.md) | Extensions to the Vector dialect for specific operations
4445

4546
[^1]: Hardware Abstraction Layer

docs/website/generate_extra_files.sh

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ done
5757
# Rename iree-dialect files.
5858
mv "${BUILD_DOCS_PROCESSED_DIR}/InputOps.md" "${BUILD_DOCS_PROCESSED_DIR}/IREEInput.md"
5959
mv "${BUILD_DOCS_PROCESSED_DIR}/LinalgExtOps.md" "${BUILD_DOCS_PROCESSED_DIR}/IREELinalgExt.md"
60+
mv "${BUILD_DOCS_PROCESSED_DIR}/VectorExtOps.md" "${BUILD_DOCS_PROCESSED_DIR}/IREEVectorExt.md"
6061
# mv "${BUILD_DOCS_PROCESSED_DIR}/StructuredTransformOpsExt.md" "${BUILD_DOCS_PROCESSED_DIR}/IREEStructuredTransformExt.md"
6162

6263
# Postprocess the dialect docs (e.g. making tweaks to the markdown source).

docs/website/mkdocs.yml

+6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ nav:
163163
- "Public dialects":
164164
- IREEInput: "reference/mlir-dialects/IREEInput.md"
165165
- IREELinalgExt: "reference/mlir-dialects/IREELinalgExt.md"
166+
- IREEVectorExt: "reference/mlir-dialects/IREEVectorExt.md"
166167
- "Other topics":
167168
- Glossary: "reference/glossary.md"
168169
- Optimization options: "reference/optimization-options.md"
@@ -206,6 +207,11 @@ nav:
206207
- "Blog":
207208
- "community/blog/index.md"
208209
- "community/tags.md"
210+
211+
# https://www.mkdocs.org/user-guide/configuration/#hooks
212+
hooks:
213+
- custom_edit_url.py
214+
209215
plugins:
210216
# https://squidfunk.github.io/mkdocs-material/setup/setting-up-a-blog/
211217
- blog:

docs/website/postprocess_dialect_docs.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import argparse
2020
import fileinput
21+
import os
2122
import pathlib
2223
import re
2324

@@ -64,7 +65,40 @@ def main(args):
6465

6566
print(line, end="")
6667

67-
# TODO(scotttodd): replace [TOC] with \n[TOC] as needed (transform ops)
68+
# Add explicit custom_edit_url links as these markdown files were generated
69+
# from other source files.
70+
dialect_sources_map = {
71+
"Check.md": "compiler/src/iree/compiler/Modules/Check/IR",
72+
"Flow.md": "compiler/src/iree/compiler/Dialect/Flow/IR",
73+
"HAL.md": "compiler/src/iree/compiler/Dialect/HAL/IR",
74+
"HALInline.md": "compiler/src/iree/compiler/Modules/HAL/Inline/IR",
75+
"HALLoader.md": "compiler/src/iree/compiler/Modules/HAL/Loader/IR",
76+
"IOParameters.md": "compiler/src/iree/compiler/Modules/IO/Parameters/IR",
77+
"IREEInput.md": "llvm-external-projects/iree-dialects/include/iree-dialects/Dialect/Input",
78+
"IREELinalgExt.md": "llvm-external-projects/iree-dialects/include/iree-dialects/Dialect/LinalgExt/IR",
79+
"IREEVectorExt.md": "llvm-external-projects/iree-dialects/include/iree-dialects/Dialect/VectorExt/IR",
80+
"Stream.md": "compiler/src/iree/compiler/Dialect/Stream/IR",
81+
"Util.md": "compiler/src/iree/compiler/Dialect/Util/IR",
82+
"VM.md": "compiler/src/iree/compiler/Dialect/VM/IR",
83+
"VMVX.md": "compiler/src/iree/compiler/Dialect/VMVX/IR",
84+
}
85+
base_url = "https://github.com/openxla/iree/tree/main/"
86+
for file in files:
87+
filename = pathlib.Path(file).name
88+
relative_path = dialect_sources_map.get(filename, None)
89+
if not relative_path:
90+
print("Warning: missing dialect source path for '%s'" % filename)
91+
continue
92+
93+
full_url = base_url + relative_path
94+
with open(file, "r+") as f:
95+
content = f.read()
96+
f.seek(0, 0)
97+
frontmatter = f"""---
98+
custom_edit_url: {full_url}
99+
---
100+
"""
101+
f.write(frontmatter + os.linesep + content)
68102

69103

70104
def parse_arguments():

0 commit comments

Comments
 (0)