Add sphinx_paramlinks extension and use :paramref:`` to reference params

This commit is contained in:
baldurk
2017-12-15 14:16:19 +00:00
parent 8d29758851
commit 2460bc998d
9 changed files with 533 additions and 18 deletions
+4 -2
View File
@@ -35,6 +35,8 @@ sys.path.insert(0, os.path.abspath(modulepath))
# path to module libraries for linux
sys.path.insert(0, os.path.abspath('../build/bin'))
sys.path.insert(0, os.path.abspath('sphinx_exts'))
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
@@ -43,7 +45,7 @@ sys.path.insert(0, os.path.abspath('../build/bin'))
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.autodoc']
extensions = ['sphinx.ext.autodoc', 'sphinx_paramlinks']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
@@ -100,7 +102,7 @@ language = None
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'python_api']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'python_api', 'sphinx_exts']
# The reST default role (used for this markup: `text`) to use for all
# documents.
@@ -0,0 +1,19 @@
This is the MIT license: http://www.opensource.org/licenses/mit-license.php
Copyright (C) by Michael Bayer.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
+126
View File
@@ -0,0 +1,126 @@
Metadata-Version: 1.1
Name: sphinx-paramlinks
Version: 0.3.4
Summary: Allows param links in Sphinx function/method descriptions to be linkable
Home-page: http://bitbucket.org/zzzeek/sphinx-paramlinks
Author: Mike Bayer
Author-email: mike@zzzcomputing.com
License: MIT
Description: ==================
Sphinx Paramlinks
==================
A `Sphinx <http://sphinx.pocoo.org/>`_ extension which allows ``:param:``
directives within Python documentation to be linkable.
This is an experimental, possibly-not-useful extension that's used by the
`SQLAlchemy <http://www.sqlalchemy.org>`_ project and related projects.
Configuration
=============
Just turn it on in ``conf.py``::
extensions = [
'sphinx_paramlinks',
# your other sphinx extensions
# ...
]
Stylesheet
==========
The paragraph link involves a short stylesheet, to allow the links to
be visible when hovered. This sheet is called
``sphinx_paramlinks.css`` and the plugin will copy it to the ``_static``
directory of the output automatically. The stylesheet is added to the
``css_files`` list present in the template namespace for Sphinx via the
``Sphinx.add_stylesheet()`` hook.
Features
========
* ``:param:`` directives within Sphinx function/method descriptions
will be given a paragraph link so that they can be linked
to externally.
* A new text role ``:paramref:`` is added, which works like ``:meth:``,
``:func:``, etc. Just append the parameter name as an additional token::
:paramref:`.EnvironmentContext.configure.transactional_ddl`
The directive makes use of the existing Python role to do the method/function
lookup, searching first the ``:meth:``, then the ``:class:``, and then the
``:func:`` role; then the parameter name is applied separately to produce the
final reference link. (new in 0.3.4, search for ``:meth:`` / ``:func:`` /
``:class:`` individually rather than using ``:obj:`` which catches lots of
things that don't have parameters)
* The paramlinks are also added to the master index as well as the list
of domain objects, which allows them to be searchable through the
searchindex.js system. (new in 0.3.0)
Compatibility
=============
Python Compatibility
--------------------
The extension was developed on Python 2.7, but is compatible with at least
Python 3.3 as well. It contains one ``u''`` literal - these are supported again
as of Python 3.3.
Sphinx Compatibility
--------------------
I've tried *very* hard to make as few assumptions as possible about Sphinx
and to use only very simple public APIs, so that architectural changes in future
Sphinx versions won't break this plugin. To come up with this plugin I
spent many hours with Sphinx source and tried many different approaches to
various elements of functionality; hopefully what's here is as simple and
stable as possible based on the current extension capabilities of Sphinx.
One element that involves using a bit of internals is the usage of the
``sphinx.domains.python.PyXRefRole`` class, which is currently the
Sphinx class that defines roles for things like ``:meth:``,
``:func:``, etc. The object is used as-is in order to define the
``:paramref:`` role; the product of this role is later transformed
using standard hooks.
Another assumption is that in order to locate the RST nodes Sphinx
creates for the ``:param:`` tags, we look at ``nodes.strong``,
assuming that this is the type of node currently used to render
``:param:`` within RST. If this changes, or needs to be expanded to
support other domains, this traversal can be opened up as needed.
This part was difficult as Sphinx really doesn't provide any hooks
into how the "Info Field List" aspect of domains is handled.
Overall, the approach here is to apply extra information to constructs
going into the Sphinx system, then do some transformations as the data
comes back out. This relies on as little of how Sphinx does its
thing as possible, rather than going with custom domains and heavy use
of injected APIs which may change in future releases.
Future Enhancements / Missing Features
======================================
The extension currently does only ``:param:`` elements within the
Python role, but could also be expanded to support other Python role
elements such as ``:returns:``, ``:raises:``, etc., and perhaps also
could be made to support similar features in other roles.
Another area that's not addressed at all is search. While the params here
are linked, these objects are currently not part of the search index Sphinx
creates in any usable way. I don't know what would be involved to make that
work.
Keywords: sphinx
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Documentation
@@ -0,0 +1,108 @@
==================
Sphinx Paramlinks
==================
A `Sphinx <http://sphinx.pocoo.org/>`_ extension which allows ``:param:``
directives within Python documentation to be linkable.
This is an experimental, possibly-not-useful extension that's used by the
`SQLAlchemy <http://www.sqlalchemy.org>`_ project and related projects.
Configuration
=============
Just turn it on in ``conf.py``::
extensions = [
'sphinx_paramlinks',
# your other sphinx extensions
# ...
]
Stylesheet
==========
The paragraph link involves a short stylesheet, to allow the links to
be visible when hovered. This sheet is called
``sphinx_paramlinks.css`` and the plugin will copy it to the ``_static``
directory of the output automatically. The stylesheet is added to the
``css_files`` list present in the template namespace for Sphinx via the
``Sphinx.add_stylesheet()`` hook.
Features
========
* ``:param:`` directives within Sphinx function/method descriptions
will be given a paragraph link so that they can be linked
to externally.
* A new text role ``:paramref:`` is added, which works like ``:meth:``,
``:func:``, etc. Just append the parameter name as an additional token::
:paramref:`.EnvironmentContext.configure.transactional_ddl`
The directive makes use of the existing Python role to do the method/function
lookup, searching first the ``:meth:``, then the ``:class:``, and then the
``:func:`` role; then the parameter name is applied separately to produce the
final reference link. (new in 0.3.4, search for ``:meth:`` / ``:func:`` /
``:class:`` individually rather than using ``:obj:`` which catches lots of
things that don't have parameters)
* The paramlinks are also added to the master index as well as the list
of domain objects, which allows them to be searchable through the
searchindex.js system. (new in 0.3.0)
Compatibility
=============
Python Compatibility
--------------------
The extension was developed on Python 2.7, but is compatible with at least
Python 3.3 as well. It contains one ``u''`` literal - these are supported again
as of Python 3.3.
Sphinx Compatibility
--------------------
I've tried *very* hard to make as few assumptions as possible about Sphinx
and to use only very simple public APIs, so that architectural changes in future
Sphinx versions won't break this plugin. To come up with this plugin I
spent many hours with Sphinx source and tried many different approaches to
various elements of functionality; hopefully what's here is as simple and
stable as possible based on the current extension capabilities of Sphinx.
One element that involves using a bit of internals is the usage of the
``sphinx.domains.python.PyXRefRole`` class, which is currently the
Sphinx class that defines roles for things like ``:meth:``,
``:func:``, etc. The object is used as-is in order to define the
``:paramref:`` role; the product of this role is later transformed
using standard hooks.
Another assumption is that in order to locate the RST nodes Sphinx
creates for the ``:param:`` tags, we look at ``nodes.strong``,
assuming that this is the type of node currently used to render
``:param:`` within RST. If this changes, or needs to be expanded to
support other domains, this traversal can be opened up as needed.
This part was difficult as Sphinx really doesn't provide any hooks
into how the "Info Field List" aspect of domains is handled.
Overall, the approach here is to apply extra information to constructs
going into the Sphinx system, then do some transformations as the data
comes back out. This relies on as little of how Sphinx does its
thing as possible, rather than going with custom domains and heavy use
of injected APIs which may change in future releases.
Future Enhancements / Missing Features
======================================
The extension currently does only ``:param:`` elements within the
Python role, but could also be expanded to support other Python role
elements such as ``:returns:``, ``:raises:``, etc., and perhaps also
could be made to support similar features in other roles.
Another area that's not addressed at all is search. While the params here
are linked, these objects are currently not part of the search index Sphinx
creates in any usable way. I don't know what would be involved to make that
work.
@@ -0,0 +1,3 @@
__version__ = '0.3.4'
from .sphinx_paramlinks import setup # noqa
@@ -0,0 +1,7 @@
a.paramlink {
visibility: hidden;
}
li:hover > a.paramlink {
visibility: visible;
}
@@ -0,0 +1,247 @@
#!coding: utf-8
import re
from docutils import nodes
from docutils.transforms import Transform
import os
from sphinx.util.osutil import copyfile
from sphinx.util.console import bold
from sphinx.domains.python import PyXRefRole
from sphinx.domains.python import PythonDomain
from distutils.version import LooseVersion
from sphinx import __version__
# the searchindex.js system relies upon the object types
# in the PythonDomain to create search entries
from sphinx.domains import ObjType
PythonDomain.object_types['parameter'] = ObjType('parameter', 'param')
def _is_html(app):
return app.builder.name in ('html', 'readthedocs')
def _tempdata(app):
if '_sphinx_paramlinks_index' in app.env.indexentries:
idx = app.env.indexentries['_sphinx_paramlinks_index']
else:
app.env.indexentries['_sphinx_paramlinks_index'] = idx = {}
return idx
def autodoc_process_docstring(app, what, name, obj, options, lines):
# locate :param: lines within docstrings. augment the parameter
# name with that of the parent object name plus a token we can
# spot later. Also put an index entry in a temporary collection.
idx = _tempdata(app)
docname = app.env.temp_data['docname']
if docname in idx:
doc_idx = idx[docname]
else:
idx[docname] = doc_idx = []
def _cvt_param(name, line):
if name.endswith(".__init__"):
# kill off __init__ if present, the links are always
# off the class
name = name[0:-9]
def cvt(m):
modifier, objname, paramname = m.group(1) or '', name, m.group(2)
refname = _refname_from_paramname(paramname, strip_markup=True)
item = ('single', '%s (%s parameter)' % (refname, objname),
'%s.params.%s' % (objname, refname), '')
if LooseVersion(__version__) >= LooseVersion('1.4.0'):
item += (None,)
doc_idx.append(item)
return ":param %s_sphinx_paramlinks_%s.%s:" % (
modifier, objname, paramname)
return re.sub(r'^:param ([^:]+? )?([^:]+?):', cvt, line)
if what in ('function', 'method', 'class'):
lines[:] = [_cvt_param(name, line) for line in lines]
def _refname_from_paramname(paramname, strip_markup=False):
literal_match = re.match(r'^``(.+?)``$', paramname)
if literal_match:
paramname = literal_match.group(1)
refname = paramname
eq_match = re.match(r'(.+?)=.+$', refname)
if eq_match:
refname = eq_match.group(1)
if strip_markup:
refname = re.sub(r'\\', '', refname)
return refname
class LinkParams(Transform):
# apply references targets and optional references
# to nodes that contain our target text.
default_priority = 210
def apply(self):
is_html = _is_html(self.document.settings.env.app)
# seach <strong> nodes, which will include the titles for
# those :param: directives, looking for our special token.
# then fix up the text within the node.
for ref in self.document.traverse(nodes.strong):
text = ref.astext()
if text.startswith("_sphinx_paramlinks_"):
components = re.match(r'_sphinx_paramlinks_(.+)\.(.+)$', text)
location, paramname = components.group(1, 2)
refname = _refname_from_paramname(paramname)
refid = "%s.params.%s" % (location, refname)
ref.parent.insert(
0,
nodes.target('', '', ids=[refid])
)
del ref[0]
ref.insert(0, nodes.Text(paramname, paramname))
if is_html:
# add the "p" thing only if we're the HTML builder.
# using a real ¶, surprising, right?
# http://docutils.sourceforge.net/FAQ.html#how-can-i-represent-esoteric-characters-e-g-character-entities-in-a-document
# "For example, say you want an em-dash (XML
# character entity &mdash;, Unicode character
# U+2014) in your document: use a real em-dash.
# Insert concrete characters (e.g. type a real em-
# dash) into your input file, using whatever
# encoding suits your application, and tell
# Docutils the input encoding. Docutils uses
# Unicode internally, so the em-dash character is
# a real em-dash internally." OK !
for pos, node in enumerate(ref.parent.children):
# try to figure out where the node with the
# paramname is. thought this was simple, but
# readthedocs proving..it's not.
# TODO: need to take into account a type name
# with the parens.
if isinstance(node, nodes.TextElement) and \
node.astext() == paramname:
break
else:
return
ref.parent.insert(
pos + 1,
nodes.reference(
'', '',
nodes.Text(u"", u""),
refid=refid,
# paramlink is our own CSS class, headerlink
# is theirs. Trying to get everything we can for
# existing symbols...
classes=['paramlink', 'headerlink']
)
)
def lookup_params(app, env, node, contnode):
# here, we catch the "pending xref" nodes that we created with
# the "paramref" role we added. The resolve_xref() routine
# knows nothing about this node type so it never finds anything;
# the Sphinx BuildEnvironment then gives us one more chance to do a lookup
# here.
if node['reftype'] != 'paramref':
return None
target = node['reftarget']
tokens = target.split(".")
resolve_target = ".".join(tokens[0:-1])
paramname = tokens[-1]
# emulate the approach within
# sphinx.environment.BuildEnvironment.resolve_references
try:
domain = env.domains[node['refdomain']] # hint: this will be 'py'
except KeyError:
return None
# BuildEnvironment doesn't pass us "fromdocname" here as the
# fallback, oh well
refdoc = node.get('refdoc', None)
# we call the same "resolve_xref" that BuildEnvironment just tried
# to call for us, but we load the call with information we know
# it can find, e.g. the "object" role (or we could use :meth:/:func:)
# along with the classname/methodname/funcname minus the parameter
# part.
for search in ["meth", "class", "func"]:
newnode = domain.resolve_xref(
env, refdoc, app.builder,
search, resolve_target, node, contnode)
if newnode is not None:
break
if newnode is not None:
# assuming we found it, tack the paramname back onto to the final
# URI.
if 'refuri' in newnode:
newnode['refuri'] += ".params." + paramname
elif 'refid' in newnode:
newnode['refid'] += ".params." + paramname
return newnode
def add_stylesheet(app):
app.add_stylesheet('sphinx_paramlinks.css')
def copy_stylesheet(app, exception):
app.info(
bold('The name of the builder is: %s' % app.builder.name), nonl=True)
if not _is_html(app) or exception:
return
app.info(bold('Copying sphinx_paramlinks stylesheet... '), nonl=True)
source = os.path.abspath(os.path.dirname(__file__))
# the '_static' directory name is hardcoded in
# sphinx.builders.html.StandaloneHTMLBuilder.copy_static_files.
# would be nice if Sphinx could improve the API here so that we just
# give it the path to a .css file and it does the right thing.
dest = os.path.join(app.builder.outdir, '_static', 'sphinx_paramlinks.css')
copyfile(os.path.join(source, "sphinx_paramlinks.css"), dest)
app.info('done')
def build_index(app, doctree):
entries = _tempdata(app)
for docname in entries:
doc_entries = entries[docname]
app.env.indexentries[docname].extend(doc_entries)
for entry in doc_entries:
sing, desc, ref, extra = entry[:4]
app.env.domains['py'].data['objects'][ref] = (docname, 'parameter')
app.env.indexentries.pop('_sphinx_paramlinks_index')
def setup(app):
app.add_transform(LinkParams)
# PyXRefRole is what the sphinx Python domain uses to set up
# role nodes like "meth", "func", etc. It produces a "pending xref"
# sphinx node along with contextual information.
app.add_role_to_domain("py", "paramref", PyXRefRole())
app.connect('autodoc-process-docstring', autodoc_process_docstring)
app.connect('builder-inited', add_stylesheet)
app.connect('build-finished', copy_stylesheet)
app.connect('missing-reference', lookup_params)
app.connect('doctree-read', build_index)
+1 -1
View File
@@ -138,7 +138,7 @@ See the documentation for :meth:`RegisterShortcut` for what these shortcuts are
:param str shortcut: The text string representing the shortcut, e.g. 'Ctrl+S'. To unregister all
shortcuts for a particular widget, you can pass an empty string here. In this case,
:param:`widget` must not be ``None``.
:paramref:`UnregisterShortcut.widget` must not be ``None``.
:param QWidget widget: A handle to the widget used as the context for the shortcut, or ``None``
if referring to a global shortcut.
)");
+18 -15
View File
@@ -1353,7 +1353,7 @@ struct ICaptureFile : public ICaptureAccess
DOCUMENT(R"(Initialises the capture handle from a file.
This method supports converting from non-native representations via structured data, by specifying
the input format in the :param:`filetype` parameter. The list of supported formats can be retrieved
the input format in the :paramref:`OpenFile.filetype` parameter. The list of supported formats can be retrieved
by calling :meth:`GetCaptureFileFormats`.
``rdc`` is guaranteed to always be a supported filetype, and will be assumed if the filetype is
@@ -1370,7 +1370,7 @@ empty or unrecognised.
This may be useful if you don't want to parse the whole file or already have the file in memory.
For the :param:`filetype` parameter, see :meth:`OpenFile`.
For the :paramref:`OpenBuffer.filetype` parameter, see :meth:`OpenFile`.
:param bytes buffer: The buffer containing the data to process.
:param str filetype: The format of the given file.
@@ -1452,7 +1452,7 @@ replay support.
This function may only be called if the handle is 'empty' - i.e. no file has been opened with
:meth:`OpenFile` or :meth:`OpenBuffer`.
.. note:: The only supported values for :param:`thumbType` are :data:`FileType.JPG`,
.. note:: The only supported values for :paramref:`SetMetadata.thumbType` are :data:`FileType.JPG`,
:data:`FileType.PNG`, :data:`FileType.TGA`, and :data:`FileType.BMP`.
:param str driverName: The name of the driver. Must be a recognised driver name (even if replay
@@ -1460,9 +1460,12 @@ This function may only be called if the handle is 'empty' - i.e. no file has bee
:param int machineIdent: The encoded machine identity value. Optional value and can be left to 0, as
the bits to set are internally defined, so only generally useful if copying a machine ident from
an existing capture.
:param FileType thumbType: The file type of the thumbnail. Ignored if :param:`thumbData` is empty.
:param int thumbWidth: The width of the thumbnail. Ignored if :param:`thumbData` is empty.
:param int thumbHeight: The height of the thumbnail. Ignored if :param:`thumbData` is empty.
:param FileType thumbType: The file type of the thumbnail. Ignored if
:paramref:`SetMetadata.thumbData` is empty.
:param int thumbWidth: The width of the thumbnail. Ignored if :paramref:`SetMetadata.thumbData` is
empty.
:param int thumbHeight: The height of the thumbnail. Ignored if :paramref:`SetMetadata.thumbData` is
empty.
:param bytes thumbData: The raw data of the thumbnail. If empty, no thumbnail is set.
)");
virtual void SetMetadata(const char *driverName, uint64_t machineIdent, FileType thumbType,
@@ -1508,14 +1511,14 @@ The data is copied internally so it can be destroyed after calling this function
DOCUMENT(R"(Retrieves the embedded thumbnail from the capture.
.. note:: The only supported values for :param:`type` are :data:`FileType.JPG`,
.. note:: The only supported values for :paramref:`GetThumbnail.type` are :data:`FileType.JPG`,
:data:`FileType.PNG`, :data:`FileType.TGA`, and :data:`FileType.BMP`.
:param FileType type: The image format to convert the thumbnail to.
:param int maxsize: The largest width or height allowed. If the thumbnail is larger, it's resized.
:return: The raw contents of the thumbnail, converted to the desired type at the desired max
resolution.
:rtype: Thumbnail.
:rtype: Thumbnail
)");
virtual Thumbnail GetThumbnail(FileType type, uint32_t maxsize) = 0;
@@ -1771,8 +1774,8 @@ DOCUMENT(R"(Begin injecting speculatively into all new processes started on the
supported by platform, configuration, and setup begin injecting speculatively into all new processes
started on the system.
This function can only be called if global hooking is supported (see :ref:`CanGlobalHook`) and if
global hooking is not active (see :ref:`IsGlobalHookActive`).
This function can only be called if global hooking is supported (see :func:`CanGlobalHook`) and if
global hooking is not active (see :func:`IsGlobalHookActive`).
This function must be called when the process is running with administrator/superuser permissions.
@@ -1781,23 +1784,23 @@ This function must be called when the process is running with administrator/supe
:param str logfile: Where to store any captures.
:param CaptureOptions opts: The capture options to use when injecting into the program.
:return: ``True`` if the hook is active, ``False`` if something went wrong. The hook must be closed
with :ref:`StopGlobalHook` before the application is closed.
with :func:`StopGlobalHook` before the application is closed.
:rtype: ``bool``
)");
extern "C" RENDERDOC_API bool RENDERDOC_CC RENDERDOC_StartGlobalHook(const char *pathmatch,
const char *logfile,
const CaptureOptions &opts);
DOCUMENT(R"(Stop the global hook that was activated by :ref:`StartGlobalHook`.
DOCUMENT(R"(Stop the global hook that was activated by :func:`StartGlobalHook`.
This function can only be called if global hooking is supported (see :ref:`CanGlobalHook`) and if
global hooking is active (see :ref:`IsGlobalHookActive`).
This function can only be called if global hooking is supported (see :func:`CanGlobalHook`) and if
global hooking is active (see :func:`IsGlobalHookActive`).
)");
extern "C" RENDERDOC_API void RENDERDOC_CC RENDERDOC_StopGlobalHook();
DOCUMENT(R"(Determines if the global hook is active or not.
This function can only be called if global hooking is supported (see :ref:`CanGlobalHook`).
This function can only be called if global hooking is supported (see :func:`CanGlobalHook`).
:return: ``True`` if the hook is active, or ``False`` if the hook is inactive.
:rtype: ``bool``