Skip to main content

Generate RST Swift documentation for Sphinx.

Project description

swift-rst-docs

Python library and cli to generate reStructuredText documentation for Swift projects from SourceKitten output files to use with Sphinx.

This tool assumes the Sphinx output type is HTML because it embeds raw HTML and uses links ending with .html.

See the documentation for API usage and an example of a generated Swift documentation.

Installation

$ pip install swift-rst-docs
$ brew install sourcekitten

Usage

You need to generate a JSON file with SourceKitten first. To reference symbols in your documentation, place their full name between 4 backticks. For example: ``MyStructure.hello(world:)`` or ``MySwiftLibrary.MyStructure.hello(world:)`` when referencing from another module or the documentation's overview text. This will create a link to the symbol's page.

$ sourcekitten doc [--spm] [-- <swift / xcodebuild arguments>] > documentation.json

Command Line

Run swift-rst-docs to generate RST files. You can provide multiple JSON paths to generate documentation for multiple modules.

$ swift-rst-docs \
--title MySwiftPackage \
--output-path swift-docs \
--documentation-file-path tests/MySwiftPackage/MySwiftLibrary.json \
--documentation-file-path tests/MySwiftPackage/MyOtherSwiftLibrary.json \
[...]

This will generate the pages inside the swift-docs directory. All the files are placed in the same directory and are named after the unique identifiers ('usr' documentation entry) of the symbols they document.

More options:

usage: swift-rst-docs [-h] --documentation-file-path DOCUMENTATION_FILE_PATH --output-path OUTPUT_PATH
                      --title TITLE [--overview OVERVIEW] [--files FILES] [--symbols SYMBOLS]
                      [--min-accessibility {public,internal,fileprivate,private,open}]

options:
  -h, --help            show this help message and exit
  --documentation-file-path, -d DOCUMENTATION_FILE_PATH
                        SourceKitten JSON file path.
  --output-path, -o OUTPUT_PATH
                        Output directory path.
  --title, -t TITLE     Index title.
  --overview, -v OVERVIEW
                        Overview file path or contents.
  --files, -f FILES     File names to document. Can be base names or paths relative to any directory in the source
                        code. Defaults to all.
  --symbols, -s SYMBOLS
                        Symbols to document. Defaults to all.
  --min-accessibility, -a {public,internal,fileprivate,private,open}
                        Minimum accessibility of symbols to document. Defaults to public.

API

You can also use the Python API to programatically generate documentation. See the documentation for more info.

Example:

from swift_rst_docs import (
    GenerationContext, fetch_documents, generate_documentation
)
from swift_rst_docs.types import Accessibility

context = GenerationContext(
    index_title="MySwiftPackage",
    overview="Example Sphinx documentation for a Swift Package with two libraries.",
    documented_objects=[
        # Skip MyOtherSwiftLibrary/MyHiddenSymbols.swift
        #
        # The paths are relative to anything: 
        # you can just write the base names but if you do so there is no way disambiguate between modules.
        "MySwiftLibrary/MySwiftLibrary.swift",
        "MyOtherSwiftLibrary/MyOtherSwiftLibrary.swift"
        ],
        min_accessibility=Accessibility.PUBLIC
)

# Fetch symbols and writes them to `context`
for json_path in [
    "MySwiftPackage/MySwiftLibrary.json",
    "MySwiftPackage/MyOtherSwiftLibrary.json",
]:
    path = os.path.join(OUTPUT_PATH, json_path)
    fetch_documents(path, context)

# Write pages
for page in generate_documentation(context):
    with open(os.path.join(OUTPUT_PATH, page.file_name), "w+") as f:
        f.write(page.contents)

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

swift_rst_docs-1.1.tar.gz (32.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swift_rst_docs-1.1-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file swift_rst_docs-1.1.tar.gz.

File metadata

  • Download URL: swift_rst_docs-1.1.tar.gz
  • Upload date:
  • Size: 32.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for swift_rst_docs-1.1.tar.gz
Algorithm Hash digest
SHA256 4b63e7b37cb5d7894f1c1f813c585401c6cf2a6f4ce6155ff2ffe477620fc46e
MD5 18c9b5e9dd8abb1231d0886ef0261e9d
BLAKE2b-256 d41fa0108c3816abf22cf274d4fef317ad2102f8d12793aae8fd9a50a6143dbb

See more details on using hashes here.

File details

Details for the file swift_rst_docs-1.1-py3-none-any.whl.

File metadata

  • Download URL: swift_rst_docs-1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for swift_rst_docs-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b799b9e84222cbb3bf29dcaaf8736cd538c26ec776a52f6f77570ca1db6f41e4
MD5 958e785dc7a397a1e70a9eeca5c15c14
BLAKE2b-256 fedb0dc3b4fb49d2ef4466c7840b3805ca061779be6bb267e817fb7efefabd28

See more details on using hashes here.

Supported by

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