Skip to content

Commit ac0e4dd

Browse files
authored
Add MLIR code block highlighting to python bindings docs (#245)
As mentioned in #239 (comment), currently many MLIR code blocks in the Python bindings’ Sphinx documentation are missing syntax highlighting. Sphinx uses the [Pygments](https://github.com/pygments/pygments) library for lexical analysis and syntax highlighting, but Pygments doesn’t include a built-in lexer for MLIR. In this PR, we leverage https://github.com/llvm/llvm-project/blob/main/mlir/utils/pygments/mlir_lexer.py (interestingly, the LLVM repository already contains a Pygments lexer for MLIR) and integrate it into the Sphinx setup. Before: <img width="1496" height="685" alt="image" src="https://github.com/user-attachments/assets/90ef57ca-9a59-4039-9e42-34a04eda494d" /> After: <img width="1482" height="658" alt="image" src="https://github.com/user-attachments/assets/a7b0c52e-3735-480f-9321-59268517bca4" /> You can preview this change at https://mlir-python-hl.surge.sh/. cc @jpienaar @makslevental
1 parent ded2375 commit ac0e4dd

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
run: |
6969
cd sphinx-mlir-python
7070
pip install -r requirements.txt
71-
make html
71+
SPHINX_LLVM_SRC_PATH=$(pwd)/../llvm_src make html
7272
cp -r _build/html ../website/static/python-bindings
7373
7474
- name: Build pattern search index
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.highlight .err,
2+
.highlight .err * {
3+
background: transparent !important;
4+
color: inherit !important;
5+
border: none !important;
6+
box-shadow: none !important;
7+
}

sphinx-mlir-python/conf.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ def prepare_docstring(doc):
5050
return rst
5151
_autoapi_parser._prepare_docstring = prepare_docstring
5252

53+
html_static_path = ['_static']
54+
html_css_files = [
55+
'ignore_highlight_err.css',
56+
]
57+
58+
if llvm_path := os.environ.get("SPHINX_LLVM_SRC_PATH"):
59+
import sphinx.highlighting as _hl
60+
import importlib
61+
62+
# load the lexer module
63+
lexer_path = llvm_path + "/mlir/utils/pygments/mlir_lexer.py"
64+
lexer_spec = importlib.util.spec_from_file_location("mlir_lexer", lexer_path)
65+
lexer_module = importlib.util.module_from_spec(lexer_spec)
66+
lexer_spec.loader.exec_module(lexer_module)
67+
68+
_hl.lexers["mlir"] = lexer_module.MlirLexer()
5369

5470
# generate an index page for the mlir namespace
5571
def ensure_mlir_index(_):

0 commit comments

Comments
 (0)