Skip to content

Commit 8b256ba

Browse files
authored
refactor(docs): parse docstrings as markdown (#491)
We use markdown in docstrings of our generated modules. This causes issues with sphinx autodoc parser. - Switch to autodoc2 and myst to properly parse markdown. - Patch invalid markdown before parsing. - Switch to "Read the Docs" theme and normalize TOC for better presentation. - Remove documentation on private module `couchdb_session_token_manager.py` Closes #78
1 parent b53e2af commit 8b256ba

File tree

6 files changed

+82
-19
lines changed

6 files changed

+82
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ python3/
5959
# resources
6060
resources/output.wav
6161

62+
apidocs/
6263
docs/_build/
6364
deploy.sh
6465
docs/apis

requirements-dev.txt

-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,3 @@ pytest-rerunfailures==11.1.2
99
# code coverage
1010
coverage==7.2.6
1111
pytest-cov==4.1.0
12-
13-
# documentation
14-
recommonmark==0.7.1
15-
Sphinx==5.3.0

requirements-docs.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# documentation
2+
recommonmark==0.7.1
3+
Sphinx==5.3.0
4+
sphinx-autodoc2==0.4.2
5+
myst-parser==1.0.0
6+
sphinx-rtd-theme==1.2.1

source/conf.py

+57-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
sys.path.insert(0, os.path.abspath('..'))
1919
print(os.path.abspath('..'))
2020

21-
2221
# -- Project information -----------------------------------------------------
2322

2423
project = 'Cloudant Python SDK'
25-
copyright = 'Copyright IBM Corp. 2021'
24+
copyright = 'Copyright IBM Corp. 2021, 2023.'
2625
author = '@IBM/cloudant-sdks'
2726

2827
# -- General configuration ---------------------------------------------------
@@ -31,7 +30,34 @@
3130
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3231
# ones.
3332
extensions = [
34-
'sphinx.ext.autodoc'
33+
'autodoc2',
34+
'myst_parser',
35+
'sphinx_rtd_theme',
36+
]
37+
38+
myst_enable_extensions = ['colon_fence', 'fieldlist']
39+
40+
autodoc2_packages = [
41+
{
42+
'path': '../ibmcloudant',
43+
'exclude_files': [
44+
'common.py',
45+
'version.py',
46+
'cloudant_base_service.py',
47+
'couchdb_session_get_authenticator_patch.py',
48+
'couchdb_session_token_manager.py'
49+
]
50+
}
51+
]
52+
53+
autodoc2_sort_names = True
54+
55+
autodoc2_index_template = None
56+
57+
autodoc2_hidden_objects = ['dunder', 'private', 'inherited']
58+
59+
autodoc2_docstring_parser_regexes = [
60+
(r'.*', 'myst'),
3561
]
3662

3763
# Add any paths that contain templates here, relative to this directory.
@@ -40,15 +66,41 @@
4066
# List of patterns, relative to source directory, that match files and
4167
# directories to ignore when looking for source files.
4268
# This pattern also affects html_static_path and html_extra_path.
43-
exclude_patterns = []
69+
exclude_patterns = ['apidocs/ibmcloudant/ibmcloudant.rst']
70+
71+
# -- Patch generated rst -----------------------------------------------------
4472

73+
file_path = '../ibmcloudant/cloudant_v1.py'
74+
print(f'[patching] file {file_path}')
75+
# read source file
76+
with open(file_path, 'r') as file:
77+
lines = file.readlines()
78+
# patch broken markup
79+
codeblock = False
80+
for i in range(len(lines)):
81+
# replace class vars to :param: for better rendering
82+
if ':attr ' in lines[i]:
83+
lines[i] = lines[i].replace(':attr', ':param')
84+
# replace note with tip admonition
85+
if '### Note' in lines[i]:
86+
lines[i] = ' :::{tip}\n'
87+
codeblock = True
88+
if codeblock and lines[i] == '\n':
89+
lines[i] = ' :::\n\n'
90+
codeblock = False
91+
# write back
92+
with open(file_path, 'w') as file:
93+
file.writelines(lines)
4594

4695
# -- Options for HTML output -------------------------------------------------
4796

4897
# The theme to use for HTML and HTML Help pages. See the documentation for
4998
# a list of builtin themes.
5099
#
51-
html_theme = 'alabaster'
100+
html_theme = 'sphinx_rtd_theme'
101+
html_theme_options = {
102+
'collapse_navigation': False
103+
}
52104

53105
# Add any paths that contain custom static files (such as style sheets) here,
54106
# relative to this directory. They are copied after the builtin static files,

source/index.rst

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
.. Cloudant Python SDK documentation master file, created by
22
sphinx-quickstart on Tue Jan 5 13:47:50 2021.
3-
Copyright IBM Corporation 2021.
3+
Copyright IBM Corporation 2021, 2023.
44
SPDX-License-Identifier: Apache-2.0
55
You can adapt this file completely to your liking, but it should at least
66
contain the root `toctree` directive.
77
8-
Welcome to Cloudant Python SDK's documentation!
9-
===============================================
8+
Cloudant Python SDK's Documentation
9+
===================================
10+
11+
CloudantV1 class
12+
----------------
13+
14+
:class:`CloudantV1 <ibmcloudant.cloudant_v1.CloudantV1>`
15+
16+
Modules
17+
-------
1018

1119
.. toctree::
1220
:maxdepth: 4
13-
:caption: Contents:
1421

15-
modules
22+
cloudant_v1 <apidocs/ibmcloudant/ibmcloudant.cloudant_v1>
23+
couchdb_session_authenticator <apidocs/ibmcloudant/ibmcloudant.couchdb_session_authenticator>
1624

1725
Indices and tables
18-
==================
26+
------------------
1927

2028
* :ref:`genindex`
2129
* :ref:`modindex`
22-
* :ref:`search`

tox.ini

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ exclude = .venv,.git,.tox,docs
1919

2020
[testenv:docs]
2121
description = invoke sphinx-build to build the HTML docs
22-
deps = {[testenv]deps}
22+
deps =
23+
-r{toxinidir}/requirements.txt
24+
-r{toxinidir}/requirements-docs.txt
2325
basepython = python3.11
24-
commands = sphinx-apidoc -o {toxinidir}/source ibmcloudant "**/common.py" "**/version.py" "**/cloudant_base_service.py" "**/couchdb_session_get_authenticator_patch.py"
25-
sphinx-build -d "{toxinidir}/docs_doctree" {toxinidir}/source "{toxinidir}/apidocs" --color -bhtml
26+
commands = sphinx-build {toxinidir}/source "{toxinidir}/apidocs" -bhtml

0 commit comments

Comments
 (0)