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.2.tar.gz (75.7 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.2-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: swift_rst_docs-1.2.tar.gz
  • Upload date:
  • Size: 75.7 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.2.tar.gz
Algorithm Hash digest
SHA256 3fc83bd1af1bb10f872bba8fbc448f1a699fd9792c8b4e34d79db4ca4ac02ac6
MD5 c7b57242b0faedb6ba9c8431789e95b3
BLAKE2b-256 a08d531e9bd9e82a360029982590ebaf653e59b40f02d42e2a06bf7ab613aa83

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swift_rst_docs-1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9a7cb7bda86e7db6f675aff20ac22cbbe3dae570410abe7abdebbacfc6e42cdb
MD5 7c2343d4238871475579237777bdb6db
BLAKE2b-256 16cb2e6461166c3a49e6ec075500073c832ea7c363ba2a429749a41b6ee4f838

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