Skip to main content

MkDocs plugin to generate a manpage from the documentation site.

Project description

MkDocs Manpage

ci documentation pypi version gitter

MkDocs plugin to generate a manpage from the documentation site.

Requirements

Pandoc must be installed and available as pandoc.

Installation

pip install mkdocs-manpage[preprocess]

Usage

# mkdocs.yml
plugins:
- manpage:
    pages:
    - title: My Project  # defaults to site name
      output: share/man/man1/my-project.1
      inputs:
      - index.md
      - usage.md
    - title: my-project API
      header: Python Library APIs  # defaults to common header for section 3 (see `man man`)
      output: share/man/man3/my_project.3
      inputs:
      - reference/my_project/*.md

To enable/disable the plugin with an environment variable:

# mkdocs.yml
plugins:
- manpage:
    enabled: !ENV [MANPAGE, false]

Then set the environment variable and run MkDocs:

MANPAGE=true mkdocs build

Pre-processing HTML

This plugin works by concatenating the HTML from all selected pages into a single file that is then converted to a manual page using Pandoc.

With a complete conversion, the final manual page will not look so good. For example images and SVG will be rendered as long strings of data and/or URIs. So this plugin allows users to pre-process the HTML, to remove unwanted HTML elements before converting the whole thing to a manpage.

First, you must make sure to install the preprocess extra:

pip install mkdocs-manpage[preprocess]

To pre-process the HTML, we use BeautifulSoup. Users have to write their own preprocess function in order to modify the soup returned by BeautifulSoup:

from bs4 import BeautifulSoup, Tag


def to_remove(tag: Tag) -> bool:
    # remove images and SVGs
    if tag.name in {"img", "svg"}:
        return True
    # remove links containing images or SVGs
    if tag.name == "a" and tag.img and to_remove(tag.img):
        return True
    # remove permalinks
    if tag.name == "a" and "headerlink" in tag.get("class", ()):
        return True
    return False


def preprocess(soup: BeautifulSoup, output: str) -> None:
    for element in soup.find_all(to_remove):
        element.decompose()

Then, instruct the plugin to use this module and its preprocess function:

plugins:
- manpage:
    preprocess: scripts/preprocess.py

See the documentation of both [BeautifulSoup][bs4.BeautifulSoup] and [Tag][bs4.Tag] to know what methods are available to correctly select the elements to remove.

The alternative to HTML processing for improving the final manpage is disabling some options from other plugins/extensions:

  • no source code through mkdocstrings:

    - mkdocstrings:
        handlers:
          python:
            options:
              show_source: !ENV [SHOW_SOURCE, true]
    
  • no permalink through toc:

    markdown_extensions:
    - toc:
        permalink: !ENV [PERMALINK, true]
    

Then set these environment variables before building the documentation and generating the manpage:

export MANPAGE=true
export PERMALINK=false
export SHOW_SOURCE=false
mkdocs build

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

mkdocs_manpage-2.0.1.tar.gz (47.6 kB view details)

Uploaded Source

Built Distribution

mkdocs_manpage-2.0.1-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file mkdocs_manpage-2.0.1.tar.gz.

File metadata

  • Download URL: mkdocs_manpage-2.0.1.tar.gz
  • Upload date:
  • Size: 47.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for mkdocs_manpage-2.0.1.tar.gz
Algorithm Hash digest
SHA256 1b588df0e6f36e5f2bcdb87ae0a21d3359b5a3bf759e514898f96e06ea8062f6
MD5 638e5791ff625d1eba105fb5f79ef97f
BLAKE2b-256 3a82597a7cb522da5c7bad566780b8c887110566a57c659db675c82b089d404d

See more details on using hashes here.

File details

Details for the file mkdocs_manpage-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mkdocs_manpage-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0c4299b86452bd6d68fd2b6deceaa93e9aa636299f191a5cc89eab084b46250b
MD5 b60f58f919c6dc5cb4bbbf1054fe6a7f
BLAKE2b-256 9d6b396970e0b8827fa6ac7e0c7aa6e4c172caab82b1b02f2c196ad1132127d2

See more details on using hashes here.

Supported by

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