Skip to content

Commit 7e24298

Browse files
authored
Fix render issue of markdown for python bindings sphinx docs (#239)
Sphinx renders docstrings as reStructuredText, but in our MLIR Python bindings, many op documentations are generated from TableGen and written in Markdown. As a result, elements like code blocks are broken in the rendered output, making the docs hard to read. In this PR, we hook into Sphinx AutoAPI’s docstring processing function (since Sphinx AutoAPI doesn’t provide an official way to override it) and use a CommonMark parser to parse the docstrings and re-emit them as reStructuredText. This allows Sphinx to render the documentation correctly. ### Before: https://mlir.llvm.org/python-bindings/autoapi/mlir/dialects/_affine_ops_gen/index.html#mlir.dialects._affine_ops_gen.AffineApplyOp <img width="1576" height="1094" alt="image" src="https://github.com/user-attachments/assets/bbfc299b-9efb-46b6-863e-ac21903e0ed6" /> ### After: https://mlir-python.surge.sh/autoapi/mlir/dialects/_affine_ops_gen/index.html#mlir.dialects._affine_ops_gen.AffineApplyOp <img width="1545" height="1199" alt="image" src="https://github.com/user-attachments/assets/224f5d9b-0677-4169-a32a-eea2edf2fb23" /> ### Demo You can preview the website after this change at: https://mlir-python.surge.sh/ cc @jpienaar @makslevental
1 parent c59604f commit 7e24298

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

sphinx-mlir-python/conf.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,22 @@
3636
autoapi_dirs = list(mlir.__path__)
3737
autoapi_python_use_implicit_namespaces = True
3838

39+
import autoapi._parser as _autoapi_parser
40+
import commonmark
3941

42+
43+
# hook the _prepare_docstring function in sphinx-autoapi,
44+
# so that we can convert markdown to rst.
45+
_prepare_docstring = _autoapi_parser._prepare_docstring
46+
def prepare_docstring(doc):
47+
md = _prepare_docstring(doc)
48+
ast = commonmark.Parser().parse(md)
49+
rst = commonmark.ReStructuredTextRenderer().render(ast)
50+
return rst
51+
_autoapi_parser._prepare_docstring = prepare_docstring
52+
53+
54+
# generate an index page for the mlir namespace
4055
def ensure_mlir_index(_):
4156
mlir_dir = os.path.join(os.path.dirname(__file__), "autoapi/mlir")
4257
os.makedirs(mlir_dir, exist_ok=True)
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
sphinx
2-
sphinx-autoapi
3-
furo
1+
Sphinx==8.1.3
2+
sphinx-autoapi==3.6.1
3+
furo==2025.9.25
4+
commonmark==0.9.1

0 commit comments

Comments
 (0)