Skip to main content

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 markdown for a static site generator, then any linting, linking, footnoting, emoticons, etc. can be handled by the respective markdown ecosystem. e.g. remark plugins or similar.

If you need to generate MkDocs or Hugo documentation then you may want to use the pydoc-markdown package instead.

Docstrings

yapper supports a simplified version of numpy docstring syntax:

  • It recognises Parameter, Returns, Yields, and Raises headings;
  • Types will be inferred automatically from signature typehints. Explicitly documented types within docstrings are (intentionally) not supported.
  • Docstrings should otherwise use conventional markdown formatting, e.g. for tables or emphasis, and these will be passed-through into the generated markdown file.

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 update / build / publish

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yapper-0.2.0b9.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

yapper-0.2.0b9-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file yapper-0.2.0b9.tar.gz.

File metadata

  • Download URL: yapper-0.2.0b9.tar.gz
  • Upload date:
  • Size: 16.4 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

Hashes for yapper-0.2.0b9.tar.gz
Algorithm Hash digest
SHA256 ba1296b89c281e6d39d7e132499ff28957596fa60fa93a328af91fdba19b9b97
MD5 70eb319d5f28abef2dd196bea5faca3f
BLAKE2b-256 558b904dfdc4868f403d24e7b13e468ece364235eb3565f2185c8aaf5def4dbb

See more details on using hashes here.

File details

Details for the file yapper-0.2.0b9-py3-none-any.whl.

File metadata

  • Download URL: yapper-0.2.0b9-py3-none-any.whl
  • Upload date:
  • Size: 10.9 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

Hashes for yapper-0.2.0b9-py3-none-any.whl
Algorithm Hash digest
SHA256 6a2eda260863f9a965a3846ee37b86a4557b3fa8ace3b5127600ba1b21c58013
MD5 e146994be6db2c6ca53a4939cf67b573
BLAKE2b-256 669a4beb5863b0942405b432809e9704090d9b2d8fe9f0f8442242ca328c060d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page