Parser for converting python docstrings to .astro files for the Astro static site generator.
Project description
Yapper
Yapper converts Python docstrings to markdown for use by static site generators. It uses the docspec package to read python files.
- It is based on a simple-as-possible configuration file. These configuration parameters can be set to control selected styling and component templates for subsequent interpretation by downstream static site-generators.
- Linting and any other markdown processing is left to downstream workflows; for example, most IDEs have built-in linting and, if using the
markdownfor a static site generator, then any linting, linking, footnoting, emoticons, etc. can be handled by the respectivemarkdownecosystem. e.g.remarkplugins or similar.
If you need to generate
MkDocsorHugodocumentation then you may want to use thepydoc-markdownpackage instead.
Docstrings
yapper supports a simplified version of numpy docstring syntax:
- It recognises
Parameter,Returns,Yields, andRaisesheadings; - Types will be inferred automatically from signature typehints. Explicitly documented types within docstrings are (intentionally) not supported.
- Docstrings should otherwise use conventional
markdownformatting, e.g. for tables or emphasis, and these will be passed-through into the generatedmarkdownfile.
For example:
def mock_function(param_a: str, param_b: int = 1) -> int:
"""
A mock function for testing purposes
Parameters
----------
param_a
A *test* _param_.
param_b
Another *test* _param_.
| col A |: col B |
|=======|========|
| boo | baa |
"""
pass
Will be interpreted as:
## mock\_function
```py
mock_function(param_a,
param_b=1)
-> int
```
A mock function for testing purposes
#### Parameters
**param_a** _str_: A *test* _param_.
**param_b** _int_: Another *test* _param_.
| col A |: col B |
|=======|========|
| boo | baa |
Configuration
Configuration is provided in the form of a .yap_config.yaml file placed in the current directory, else a --config parameter can be provided with a relative or absolute filepath to the config file.
yapper --config ./my_config.yaml
Any parameter keys specified in the configuration file must match one of those available in the default configuration, which is as follows:
yap_template_config = {
'package_root_relative_path': '.',
'frontmatter_template': None,
'module_name_template': '# {module_name}\n\n',
'toc_template': None,
'function_name_template': '\n\n## {function_name}\n\n',
'class_name_template': '\n\n## **class** {class_name}\n\n',
'class_property_template': '\n\n#### {prop_name}\n\n',
'signature_template': '\n\n```py\n{signature}\n```\n\n',
'heading_template': '\n\n#### {heading}\n\n',
'param_template': '\n\n**{name}** _{type}_: {description}\n\n',
'return_template': '\n\n**{name}**: {description}\n\n',
'module_map': None
}
When overriding a default config parameter, use \n for newlines and retain the same argument names for string interoplation (i.e curly brackets). For example, changing the heading template from a fourth-level to third-level markdown heading would look like this:
heading_template: "\n\n### {heading}\n\n"
The configuration can be used to generate custom formatted markdown files that work for static site generators, and this is particularly useful when using custom javascript components from a static site generator such as vuepress / vitepress / gridsome, etc.
The module_map key is mandatory and specifies the python modules which should be processed by yapper mapped to the relative output filepath to which the generated markdown file will saved.
The following config:
signature_template: "\n\n<FuncSignature>\n<pre>\n{signature}\n</pre>\n</FuncSignature>\n\n"
heading_template: "\n\n<FuncHeading>{heading}</FuncHeading>\n\n"
param_template: "\n\n<FuncElement name='{name}' type='{type}'>\n\n{description}\n\n</FuncElement>\n\n"
return_template: "\n\n<FuncElement name='{name}'>\n\n{description}\n\n</FuncElement>\n\n"
module_map:
tests.mock_file: mock_custom_file.md
Would generate a ./mock_custom_file.md file with the following markdown (for the same mock_function example shown above):
## mock\_function
<FuncSignature>
<pre>
mock_function(param_a,
param_b=1)
-> int
</pre>
</FuncSignature>
A mock function for testing purposes
<FuncHeading>Parameters</FuncHeading>
<FuncElement name='param_a' type='str'>
A *test* _param_.
</FuncElement>
<FuncElement name='param_b' type='int'>
Another *test* _param_.
| col A |: col B |
|=======|========|
| boo | baa |
</FuncElement>
Use from javascript
Many or most static site generators make use of javascript and the related ecosystem available from npm / yarn package managers.
As such, you may want to invoke yapper from a package.json file when building your documentation for development. This can be done with the help of python-shell. For example:
const path = require('path')
const { PythonShell } = require('python-shell')
const options = {
mode: 'text',
pythonOptions: ['-u'],
pythonPath: path.resolve(__dirname, '../venv/bin/python'),
scriptPath: path.resolve(__dirname, '../venv/lib/python3.9/site-packages/yapper'),
args: ['--config', '../.yap_config.yaml'],
}
PythonShell.run('__init__.py', options, function (err) {
if (err) throw err
})
yapper can then be invoked from node or the package.json file's scripts parameter, and can be coupled to other steps such as linting or validation, e.g.
{
"scripts": {
"generate": "node generateDocs.js",
"lint": "markdownlint 'content/**/*.md' --fix",
"validateLinks": "remark -u validate-links ."
}
}
Dev Setup
brew install pdm
pdm --pep582 >> ~/.zshrc
brew install zsh-completions
mkdir ~/.zfunc
pdm completion zsh > ~/.zfunc/_pdm
pdm plugin add pdm-publish
pdm init
pdm run tests
pdm run publish
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yapper-0.2.0b13.tar.gz.
File metadata
- Download URL: yapper-0.2.0b13.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6619dea83f52b79635ea1d4c8c304b35f6cb06e70a7f3834f9f4b4c67c81033
|
|
| MD5 |
334292b5c80c6ecb87279e30f144c089
|
|
| BLAKE2b-256 |
83c23bb9d5240ce8b559078de1c5847c460e474c2de609797d72bd50ccb587b6
|
File details
Details for the file yapper-0.2.0b13-py3-none-any.whl.
File metadata
- Download URL: yapper-0.2.0b13-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b7bac6594130a6e4ec1f12f9a42aca7e37eb0a8a0932401d1c0f5b5afaa0419
|
|
| MD5 |
711d23197ff6b4b395d807c3c29fc824
|
|
| BLAKE2b-256 |
57fec3142826719a7a0e0c6678ed4ac6a18e987767acfd82bb2d6ed22824bff6
|