Skip to content

Commit

Permalink
docs: Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed May 8, 2022
1 parent bc70245 commit b77cf3f
Show file tree
Hide file tree
Showing 10 changed files with 463 additions and 200 deletions.
30 changes: 15 additions & 15 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ cd python
make setup
```

!!! note
If it fails for some reason,
you'll need to install
[PDM](https://github.com/pdm-project/pdm)
manually.

You can install it with:

```bash
python3 -m pip install --user pipx
pipx install pdm
```

Now you can try running `make setup` again,
or simply `pdm install`.
> NOTE
> If it fails for some reason,
> you'll need to install
> [PDM](https://github.com/pdm-project/pdm)
> manually.
>
> You can install it with:
>
> ```bash
> python3 -m pip install --user pipx
> pipx install pdm
> ```
>
> Now you can try running `make setup` again,
> or simply `pdm install`.
You now have the dependencies installed.
Expand Down
70 changes: 32 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
# mkdocstrings-python

[![ci](https://github.com/mkdocstrings/python/workflows/ci/badge.svg)](https://github.com/mkdocstrings/python/actions?query=workflow%3Aci)
[![documentation](https://img.shields.io/badge/docs-mkdocs%20material-blue.svg?style=flat)](https://mkdocstrings.github.io/python/)
[![pypi version](https://img.shields.io/pypi/v/python.svg)](https://pypi.org/project/python/)
[![gitpod](https://img.shields.io/badge/gitpod-workspace-blue.svg?style=flat)](https://gitpod.io/#https://github.com/mkdocstrings/python)
[![gitter](https://badges.gitter.im/join%20chat.svg)](https://gitter.im/mkdocstrings/python)

A Python handler for [mkdocstrings](https://github.com/mkdocstrings/mkdocstrings).

<!-- TODO: update the GIF with a more recent screen capture. Maybe use mp4 instead -->
![mkdocstrings_python_gif](https://user-images.githubusercontent.com/3999221/77157838-7184db80-6aa2-11ea-9f9a-fe77405202de.gif)

## Requirements

mkdocstrings-python requires Python 3.7 or above.

<details>
<summary>To install Python 3.7, I recommend using <a href="https://github.com/pyenv/pyenv"><code>pyenv</code></a>.</summary>

```bash
# install pyenv
git clone https://github.com/pyenv/pyenv ~/.pyenv

# setup pyenv (you should also put these three lines in .bashrc or similar)
export PATH="${HOME}/.pyenv/bin:${PATH}"
export PYENV_ROOT="${HOME}/.pyenv"
eval "$(pyenv init -)"

# install Python 3.7
pyenv install 3.7.12

# make it available globally
pyenv global system 3.7.12
```
</details>
<h1 align="center">mkdocstrings-python</h1>

<p align="center">A Python handler for <a href="https://github.com/mkdocstrings/mkdocstrings"><i>mkdocstrings</i></a>.</p>

<p align="center">
<a href="https://github.com/mkdocstrings/python/actions?query=workflow%3Aci">
<img alt="ci" src="https://github.com/mkdocstrings/python/workflows/ci/badge.svg" />
</a>
<a href="https://mkdocstrings.github.io/python/">
<img alt="documentation" src="https://img.shields.io/badge/docs-mkdocs%20material-blue.svg?style=flat" />
</a>
<a href="https://pypi.org/project/mkdocstrings-python/">
<img alt="pypi version" src="https://img.shields.io/pypi/v/mkdocstrings-python.svg" />
</a>
<a href="https://gitpod.io/#https://github.com/mkdocstrings/python">
<img alt="gitpod" src="https://img.shields.io/badge/gitpod-workspace-blue.svg?style=flat" />
</a>
<a href="https://gitter.im/mkdocstrings/python">
<img alt="gitter" src="https://badges.gitter.im/join%20chat.svg" />
</a>
</p>

---

<p align="center"><img src="logo.png"></p>

## Installation

You can install this handler as a *mkdocstrings* extra:

```toml
```toml title="pyproject.toml"
# PEP 621 dependencies declaration
# adapt to your dependencies manager
[project]
Expand All @@ -50,7 +39,7 @@ dependencies = [

You can also explicitely depend on the handler:

```toml
```toml title="pyproject.toml"
# PEP 621 dependencies declaration
# adapt to your dependencies manager
[project]
Expand All @@ -59,6 +48,11 @@ dependencies = [
]
```

## Preview

<!-- TODO: update the GIF with a more recent screen capture. Maybe use mp4 instead -->
![mkdocstrings_python_gif](https://user-images.githubusercontent.com/3999221/77157838-7184db80-6aa2-11ea-9f9a-fe77405202de.gif)

## Features

- **Data collection from source code**: collection of the object-tree and the docstrings is done thanks to
Expand Down
1 change: 0 additions & 1 deletion docs/css/mkdocstrings.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ a.autorefs-external::after {
top: 0.1em;
margin-left: 0.2em;
margin-right: 0.1em;
/* padding-top: 0.8em; */

height: 1em;
width: 1em;
Expand Down
152 changes: 152 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Customization

It is possible to customize the output of the generated documentation with CSS
and/or by overriding templates.

## CSS classes

The following CSS classes are used in the generated HTML:

- `doc`: on all the following elements
- `doc-children`: on `div`s containing the children of an object
- `doc-object`: on `div`s containing an object
- `doc-attribute`: on `div`s containing an attribute
- `doc-class`: on `div`s containing a class
- `doc-function`: on `div`s containing a function
- `doc-module`: on `div`s containing a module
- `doc-heading`: on objects headings
- `doc-object-name`: on `span`s wrapping objects names/paths in the heading
- `doc-KIND-name`: as above, specific to the kind of object (module, class, function, attribute)
- `doc-contents`: on `div`s wrapping the docstring then the children (if any)
- `first`: same, but only on the root object's contents `div`
- `doc-labels`: on `span`s wrapping the object's labels
- `doc-label`: on `small` elements containing a label
- `doc-label-LABEL`: same, where `LABEL` is replaced by the actual label

!!! example "Example with colorful labels"
=== "CSS"
```css
.doc-label { border-radius: 15px; padding: 0 5px; }
.doc-label-special { background-color: blue; color: white; }
.doc-label-private { background-color: red; color: white; }
.doc-label-property { background-color: green; color: white; }
.doc-label-read-only { background-color: yellow; color: black; }
```

=== "Result"
<style>
.lbl { border-radius: 15px; padding: 0 5px; }
</style>
<h3 style="margin: 0;"><span>
<small class="lbl" style="background-color: blue; color: white !important;">special</small>
<small class="lbl" style="background-color: red; color: white !important;">private</small>
<small class="lbl" style="background-color: green; color: white !important;">property</small>
<small class="lbl" style="background-color: yellow; color: black !important;">read-only</small>
</span></h3>


### Recommended style (Material)

Here are some CSS rules for the
[*Material for MkDocs*](https://squidfunk.github.io/mkdocs-material/) theme:

```css
/* Indentation. */
div.doc-contents:not(.first) {
padding-left: 25px;
border-left: .05rem solid var(--md-typeset-table-color);
}

/* Mark external links as such. */
a.autorefs-external::after {
/* https://primer.style/octicons/arrow-up-right-24 */
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="rgb(0, 0, 0)" d="M18.25 15.5a.75.75 0 00.75-.75v-9a.75.75 0 00-.75-.75h-9a.75.75 0 000 1.5h7.19L6.22 16.72a.75.75 0 101.06 1.06L17.5 7.56v7.19c0 .414.336.75.75.75z"></path></svg>');
content: ' ';

display: inline-block;
position: relative;
top: 0.1em;
margin-left: 0.2em;
margin-right: 0.1em;

height: 1em;
width: 1em;
border-radius: 100%;
background-color: var(--md-typeset-a-color);
}
a.autorefs-external:hover::after {
background-color: var(--md-accent-fg-color);
}

```

### Recommended style (ReadTheDocs)

Here are some CSS rules for the built-in *ReadTheDocs* theme:

```css
/* Indentation. */
div.doc-contents:not(.first) {
padding-left: 25px;
border-left: .05rem solid rgba(200, 200, 200, 0.2);
}
```

## Templates

Templates are organized into the following tree:

```
πŸ“ theme/
β”œβ”€β”€ πŸ“„ attribute.html
β”œβ”€β”€ πŸ“„ children.html
β”œβ”€β”€ πŸ“„ class.html
β”œβ”€β”€ πŸ“ docstring/
β”‚Β Β  β”œβ”€β”€ πŸ“„ admonition.html
β”‚Β Β  β”œβ”€β”€ πŸ“„ attributes.html
β”‚Β Β  β”œβ”€β”€ πŸ“„ examples.html
β”‚Β Β  β”œβ”€β”€ πŸ“„ other_parameters.html
β”‚Β Β  β”œβ”€β”€ πŸ“„ parameters.html
β”‚Β Β  β”œβ”€β”€ πŸ“„ raises.html
β”‚Β Β  β”œβ”€β”€ πŸ“„ receives.html
β”‚Β Β  β”œβ”€β”€ πŸ“„ returns.html
β”‚Β Β  β”œβ”€β”€ πŸ“„ warns.html
β”‚Β Β  └── πŸ“„ yields.html
β”œβ”€β”€ πŸ“„ docstring.html
β”œβ”€β”€ πŸ“„ expression.html
β”œβ”€β”€ πŸ“„ function.html
β”œβ”€β”€ πŸ“„ labels.html
β”œβ”€β”€ πŸ“„ module.html
└── πŸ“„ signature.html
```

See them [in the repository](https://github.com/mkdocstrings/python/tree/master/src/mkdocstrings_handlers/python/templates/).
See the general *mkdocstrings* documentation to learn how to override them: https://mkdocstrings.github.io/theming/#templates.

In preparation for Jinja2 blocks, which will improve customization,
each one of these templates extends in fact a base version in `theme/_base`. Example:

```html+jinja title="theme/docstring/admonition.html"
{% extends "_base/docstring/admonition.html" %}
```

```html+jinja title="theme/_base/docstring/admonition.html"
{{ log.debug() }}
<details class="{{ section.value.kind }}">
<summary>{{ section.title|convert_markdown(heading_level, html_id, strip_paragraph=True) }}</summary>
{{ section.value.contents|convert_markdown(heading_level, html_id) }}
</details>
```

It means you will be able to customize only *parts* of a template
without having to fully copy-paste it in your project:

```jinja title="templates/theme/docstring.html"
{% extends "_base/docstring.html" %}
{% block contents %}
{{ block.super }}
Additional contents
{% endblock contents %}
```

WARNING: **Block-level customization is not ready yet. We welcome [suggestions](https://github.com/mkdocstrings/python/issues/new).**
Binary file added docs/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b77cf3f

Please sign in to comment.