| 1 |
Theming and other customizations |
| 2 |
================================ |
| 3 |
|
| 4 |
Configure sidebar expanding/collapsing |
| 5 |
-------------------------------------- |
| 6 |
|
| 7 |
By default, the sidebar only lists one level of objects (always expanded), |
| 8 |
to allow objects to expand/collapse and show first nested content, use the following option:: |
| 9 |
|
| 10 |
--sidebar-expand-depth=2 |
| 11 |
|
| 12 |
This value describe how many nested modules and classes should be expandable. |
| 13 |
|
| 14 |
.. note:: |
| 15 |
Careful, a value higher than ``1`` (which is the default) can make your HTML files |
| 16 |
significantly larger if you have many modules or classes. |
| 17 |
|
| 18 |
To disable completely the sidebar, use option ``--no-sidebar`` |
| 19 |
|
| 20 |
Theming |
| 21 |
------- |
| 22 |
|
| 23 |
Currently, there are 2 main themes packaged with pydoctor: ``classic`` and ``readthedocs``. |
| 24 |
|
| 25 |
Choose your theme with option:: |
| 26 |
|
| 27 |
--theme |
| 28 |
|
| 29 |
.. note:: |
| 30 |
Additionnaly, the ``base`` theme can be used as a base for customizations. |
| 31 |
|
| 32 |
Tweak HTML templates |
| 33 |
-------------------- |
| 34 |
|
| 35 |
They are 3 special files designed to be included in specific places of each pages. |
| 36 |
|
| 37 |
- ``header.html``: at the very beginning of the body |
| 38 |
- ``subheader.html``: after the main header, before the page title |
| 39 |
- ``extra.css``: extra CSS sheet for layout customization |
| 40 |
|
| 41 |
To include a file, write your custom HTML or CSS files to a directory |
| 42 |
and use the following option:: |
| 43 |
|
| 44 |
--template-dir=./pydoctor_templates |
| 45 |
|
| 46 |
If you want more customization, you can override the default templates in |
| 47 |
`pydoctor/themes/base <https://github.com/twisted/pydoctor/tree/master/pydoctor/themes/base>`_ |
| 48 |
with the same method. |
| 49 |
|
| 50 |
HTML templates have their own versioning system and warnings will be triggered when an outdated custom template is used. |
| 51 |
|
| 52 |
.. admonition:: Demo theme example |
| 53 |
|
| 54 |
There is a demo template inspired by Twisted web page for which the source code is `here <https://github.com/twisted/pydoctor/tree/master/docs/sample_template>`_. |
| 55 |
You can try the result by checking `this page <custom_template_demo/pydoctor.html>`_. |
| 56 |
|
| 57 |
.. note:: |
| 58 |
|
| 59 |
This example is using the ``base`` theme. |
| 60 |
|
| 61 |
.. _customize-privacy: |
| 62 |
|
| 63 |
Override objects privacy (show/hide) |
| 64 |
------------------------------------ |
| 65 |
|
| 66 |
Pydoctor supports 3 types of privacy. |
| 67 |
Below is the description of each type and the default association: |
| 68 |
|
| 69 |
- ``PRIVATE``: By default for objects whose name starts with an underscore and are not a dunder method. |
| 70 |
Rendered in HTML, but hidden via CSS by default. |
| 71 |
|
| 72 |
- ``PUBLIC``: By default everything else that is not private. |
| 73 |
Always rendered and visible in HTML. |
| 74 |
|
| 75 |
- ``HIDDEN``: Nothing is hidden by default. |
| 76 |
Not rendered at all and no links can be created to hidden objects. |
| 77 |
Not present in the search index nor the intersphinx inventory. |
| 78 |
Basically excluded from API documentation. If a module/package/class is hidden, then all it's members are hidden as well. |
| 79 |
|
| 80 |
When the default rules regarding privacy doesn't fit your use case, |
| 81 |
use the ``--privacy`` command line option. |
| 82 |
It can be used multiple times to define multiple privacy rules:: |
| 83 |
|
| 84 |
--privacy=<PRIVACY>:<PATTERN> |
| 85 |
|
| 86 |
where ``<PRIVACY>`` can be one of ``PUBLIC``, ``PRIVATE`` or ``HIDDEN`` (case insensitive), and ``<PATTERN>`` is fnmatch-like |
| 87 |
pattern matching objects fullName. |
| 88 |
|
| 89 |
Privacy tweak examples |
| 90 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 91 |
- ``--privacy="PUBLIC:**"`` |
| 92 |
Makes everything public. |
| 93 |
|
| 94 |
- ``--privacy="HIDDEN:twisted.test.*" --privacy="PUBLIC:twisted.test.proto_helpers"`` |
| 95 |
Makes everything under ``twisted.test`` hidden except ``twisted.test.proto_helpers``, which will be public. |
| 96 |
|
| 97 |
- ``--privacy="PRIVATE:**.__*__" --privacy="PUBLIC:**.__init__"`` |
| 98 |
Makes all dunder methods private except ``__init__``. |
| 99 |
|
| 100 |
.. important:: The order of arguments matters. Pattern added last have priority over a pattern added before, |
| 101 |
but an exact match wins over a fnmatch. |
| 102 |
|
| 103 |
.. note:: See :py:mod:`pydoctor.qnmatch` for more informations regarding the pattern syntax. |
| 104 |
|
| 105 |
.. note:: Quotation marks should be added around each rule to avoid shell expansions. |
| 106 |
Unless the arguments are passed directly to pydoctor, like in Sphinx's ``conf.py``, in this case you must not quote the privacy rules. |
| 107 |
|
| 108 |
Use a custom system class |
| 109 |
------------------------- |
| 110 |
|
| 111 |
You can subclass the :py:class:`pydoctor.model.System` |
| 112 |
and pass your custom class dotted name with the following argument:: |
| 113 |
|
| 114 |
--system-class=mylib._pydoctor.CustomSystem |
| 115 |
|
| 116 |
System class allows you to customize certain aspect of the system and configure the enabled extensions. |
| 117 |
If what you want to achieve has something to do with the state of some objects in the Documentable tree, |
| 118 |
it's very likely that you can do it without the need to override any system method, |
| 119 |
by using the extension mechanism described below. |
| 120 |
|
| 121 |
Brief on pydoctor extensions |
| 122 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 123 |
|
| 124 |
The AST builder can now be customized with extension modules. |
| 125 |
This is how we handle Zope Interfaces declarations and :py:mod:`twisted.python.deprecate` warnings. |
| 126 |
|
| 127 |
Each pydocotor extension is a Python module with at least a ``setup_pydoctor_extension()`` function. |
| 128 |
This function is called at initialization of the system with one argument, |
| 129 |
the :py:class:`pydoctor.extensions.ExtRegistrar` object representing the system. |
| 130 |
|
| 131 |
An extension can register multiple kind of components: |
| 132 |
- AST builder visitors |
| 133 |
- Mixin classes for :py:class:`pydoctor.model.Documentable` |
| 134 |
- Post processors |
| 135 |
|
| 136 |
Take a look at built-in extensions :py:mod:`pydoctor.extensions.zopeinterface` and :py:mod:`pydoctor.extensions.deprecate`. |
| 137 |
Navigate to the source code for a better overview. |
| 138 |
|
| 139 |
A concrete example |
| 140 |
^^^^^^^^^^^^^^^^^^ |
| 141 |
|
| 142 |
Let's say you want to write a extension for simple pydantic classes like this one: |
| 143 |
|
| 144 |
.. code:: python |
| 145 |
|
| 146 |
from typing import ClassVar |
| 147 |
from pydantic import BaseModel |
| 148 |
class Model(BaseModel): |
| 149 |
a: int |
| 150 |
b: int = Field(...) |
| 151 |
name:str = 'Jane Doe' |
| 152 |
kind:ClassVar = 'person' |
| 153 |
|
| 154 |
|
| 155 |
First, we need to create a new module that will hold our extension code: ``mylib._pydoctor``. |
| 156 |
This module will contain visitor code that visits ``ast.AnnAssign`` nodes after the main visitor. |
| 157 |
It will check if the current context object is a class derived from ``pydantic.BaseModel`` and |
| 158 |
transform each class variable into instance variables accordingly. |
| 159 |
|
| 160 |
.. code:: python |
| 161 |
|
| 162 |
# Module mylib._pydoctor |
| 163 |
|
| 164 |
import ast |
| 165 |
from pydoctor import astutils, extensions, model |
| 166 |
|
| 167 |
class PydanticModVisitor(extensions.ModuleVisitorExt): |
| 168 |
|
| 169 |
def depart_AnnAssign(self, node: ast.AnnAssign) -> None: |
| 170 |
""" |
| 171 |
Called after an annotated assignment definition is visited. |
| 172 |
""" |
| 173 |
ctx = self.visitor.builder.current |
| 174 |
if not isinstance(ctx, model.Class): |
| 175 |
# check if the current context object is a class |
| 176 |
return |
| 177 |
|
| 178 |
if not any(ctx.expandName(b) == 'pydantic.BaseModel' for b in ctx.bases): |
| 179 |
# check if the current context object if a class derived from ``pydantic.BaseModel`` |
| 180 |
return |
| 181 |
|
| 182 |
dottedname = astutils.node2dottedname(node.target) |
| 183 |
if not dottedname or len(dottedname)!=1: |
| 184 |
# check if the assignment is a simple name, otherwise ignore it |
| 185 |
return |
| 186 |
|
| 187 |
# Get the attribute from current context |
| 188 |
attr = ctx.contents[dottedname[0]] |
| 189 |
|
| 190 |
assert isinstance(attr, model.Attribute) |
| 191 |
|
| 192 |
# All class variables that are not annotated with ClassVar will be transformed to instance variables. |
| 193 |
if astutils.is_using_typing_classvar(attr.annotation, attr): |
| 194 |
return |
| 195 |
|
| 196 |
if attr.kind == model.DocumentableKind.CLASS_VARIABLE: |
| 197 |
attr.kind = model.DocumentableKind.INSTANCE_VARIABLE |
| 198 |
|
| 199 |
def setup_pydoctor_extension(r:extensions.ExtRegistrar) -> None: |
| 200 |
r.register_astbuilder_visitor(PydanticModVisitor) |
| 201 |
|
| 202 |
class PydanticSystem(model.System): |
| 203 |
# Declare that this system should load this additional extension |
| 204 |
custom_extensions = ['mylib._pydoctor'] |
| 205 |
|
| 206 |
Then, we would pass our custom class dotted name with the argument ``--system-class``:: |
| 207 |
|
| 208 |
--system-class=mylib._pydoctor.PydanticSystem |
| 209 |
|
| 210 |
Et voilĂ . |
| 211 |
|
| 212 |
If this extension mechanism doesn't support the tweak you want, you can consider overriding some |
| 213 |
:py:class:`pydoctor.model.System` methods. For instance, overriding :py:meth:`pydoctor.model.System.__init__` method could be useful, |
| 214 |
if some want to write a custom :py:class:`pydoctor.sphinx.SphinxInventory`. |
| 215 |
|
| 216 |
|
| 217 |
.. important:: |
| 218 |
If you feel like other users of the community might benefit from your extension as well, please |
| 219 |
don't hesitate to open a pull request adding your extension module to the package :py:mod:`pydoctor.extensions`. |
| 220 |
|
| 221 |
Use a custom writer class |
| 222 |
------------------------- |
| 223 |
|
| 224 |
You can subclass the :py:class:`pydoctor.templatewriter.TemplateWriter` (or the abstract super class :py:class:`pydoctor.templatewriter.IWriter`) |
| 225 |
and pass your custom class dotted name with the following argument:: |
| 226 |
|
| 227 |
--html-writer=mylib._pydoctor.CustomTemplateWriter |
| 228 |
|
| 229 |
The option is actually badly named because, theorically one could write a subclass |
| 230 |
of :py:class:`pydoctor.templatewriter.IWriter` (to be used alongside option ``--template-dir``) |
| 231 |
that would output Markdown, reStructuredText or JSON. |
| 232 |
|
| 233 |
.. warning:: Pydoctor does not have a stable API yet. Code customization is prone |
| 234 |
to break in future versions. |