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.0.tar.gz (31.9 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.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: swift_rst_docs-1.0.tar.gz
  • Upload date:
  • Size: 31.9 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.0.tar.gz
Algorithm Hash digest
SHA256 8dab6158aa9b9d285748a6c4f5d822bbb025d8b3a1e365ca201afb18d7eabc63
MD5 d7250f9166ff94ab1887b96fc8c41265
BLAKE2b-256 f30ce1c12218395b4d962f254ec985170f18aab43e32a051a8082fcb5b74d4d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swift_rst_docs-1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9541fb2c1aa46864a9b272dd4a9c003c25c16a2f96a0df2c44a2eff119c9b0ad
MD5 f7aa3e192ae82154de62c16a0dbd97f8
BLAKE2b-256 623b207f9ad3379126c370226cd8b7655f34c1b25bec1fce902a29ce83dea5c5

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