Files
openmm/docs-source/api-c++/process-docstring.py
Peter Eastman 6e2f213fa7 Replace C++ code examples in Python API docs (#5043)
* Replace C++ code examples in Python API docs

* Use newer Python for building docs

* Remove sphinx version pin

* Add Python versions of code examples

* Minor edit

* Add Python version of one more code example
2025-08-13 15:02:25 -07:00

29 lines
919 B
Python

import os
import re
import sys
def replace_code(match):
lines = match.group(1).split('\n')
for i, line in enumerate(list(lines)):
if line.startswith('*'):
lines[i] = line[1:]
return f"\n* .. code-block:: c++\n* {'\n* '.join(lines)}"
def process_file(file):
"""Edit docstrings in an XML file to remove the Python versions of code examples,
and put the correct Sphinx directives around the C++ versions."""
with open(file) as input:
content = input.read()
processed = re.sub(r'<c\+\+>((.|\n)*?)</c\+\+>', replace_code, content)
processed = re.sub(r'<python>((.|\n)*?)</python>', '', processed)
print(file, processed != content)
if processed != content:
with open(file, 'w') as output:
output.write(processed)
dir = sys.argv[1]
for file in os.listdir(dir):
process_file(os.path.join(dir, file))