Files
openmm/docs-source/api-python/process-docstring.py
Josh A. Mitchell ae2fe2fd7d Move to actively maintained dependencies for API docs (#3201)
* Proof of concept matching current behaviour with Breathe

* Reorganise C++ API docs to work without autosummary

* Revert to Sphinx-native search

* Move remaining pip deps to conda

* Remove unnecessary lunrsearch templates

* Remove lunrsearch from cmake

* Tidy up layout of API docs

* Get code blocks to work with Breathe

Breathe doesn't seem to support the Doxygen <preformatted> tag.
It does support the <code> tag, but better yet it supports using
<verbatim> tags to embed rst into docstrings. This commit changes
all <pre> tags to use RST verbatim, and updates the Python
documentation machinery to support it too.

* Clarified some language

* Have doxygen exclude undocumented classes and have sphinx fail on warnings for C++ API docs

* List custom forces and integrators last

* Add breathe to documentation CI

* Typo

* Fix link to custom forces in extras.rst

* Have Breathe process only public classes

* Strip OpenMM:: prefix from rst files to avoid colons in links

* Remove existing private classes from EXCLUDE_SYMBOLS

* Add comment to C++ cmake describing why we promote warnings to errors

* Revise documentation build instructions for new dependencies
2021-08-11 09:38:26 -07:00

78 lines
2.7 KiB
Python

import re
def count_leading_whitespace(s):
count = 0
for c in s:
if c.isspace():
count += 1
else:
break
return count
def process_docstring(app, what, name, obj, options, lines):
"""This hook edits the docstrings to replace "<tt><pre>" html tags,
Breathe-style verbatim embed blocks, and deprecated markers with
sphinx directives.
"""
def repl(m):
s = m.group(1)
if not s.startswith(linesep):
s = linesep + s
newline = '.. code-block:: c++' + linesep
return newline + ' ' + s.replace(linesep, linesep + ' ')
def repl2(m):
s = m.group(1)
if not s.startswith(linesep):
s = linesep + s
newline = '|LINEBREAK|.. admonition::|LINEBREAK| Deprecated' + linesep
return newline + ' ' + s.replace(linesep, linesep + ' ')
def repl3(m):
s = m.group(1)
return '*' + s + '*'
def repl4(m):
s = m.group(1)
if s.startswith("embed:rst"):
lines = s.split(linesep)[1:]
# Match behaviour of Breathe
if s.startswith("embed:rst:leading-asterisk"):
lines = [l.replace("*", " ", 1) for l in lines]
elif s.startswith("embed:rst:leading-slashes"):
lines = [l.replace("///", " ", 1) for l in lines]
# Strip leading whitespace to match first line
to_strip = count_leading_whitespace(lines[0])
lines = [l[to_strip:] for l in lines]
return linesep.join(lines)
else:
s = m.group(1)
if not s.startswith(linesep):
s = linesep + s
newline = '.. verbatim::' + linesep
return newline + ' ' + s.replace(linesep, linesep + ' ')
linesep = '|LINEBREAK|'
joined = linesep.join(lines)
joined = re.sub(r'<tt><pre>((|LINEBREAK|)?.*?)</pre></tt>', repl, joined)
joined = re.sub(r'<tt>(.*?)</tt>', repl, joined)
joined = re.sub(r'@deprecated(.*?\|LINEBREAK\|)', repl2, joined, flags=re.IGNORECASE)
joined = re.sub(r'<i>(.*?)</i>', repl3, joined)
joined = re.sub(r'<verbatim>(.*?)</verbatim>', repl4, joined)
lines[:] = [(l if not l.isspace() else '') for l in joined.split(linesep)]
def setup(app):
app.connect('autodoc-process-docstring', process_docstring)
def test():
lines = ['Hello World', '<tt><pre>', 'contents', '</pre></tt>', '', '<tt>contents2</tt>']
linesRef = ['Hello World', '.. code-block:: c++', '', ' contents', '', '', '.. code-block:: c++', '', ' contents2']
process_docstring(None, None, None, None, None, lines)
assert lines == linesRef
if __name__ == '__main__':
test()