API Reference Pages#

krpc-docgen generates the API reference of a service, in the form of kRPC’s own API reference. It writes one page per template for a client language, as reStructuredText for Sphinx. It is part of the krpctools Python package:

pip install krpctools

Templates#

A template is the source of a page. Here is one for the LaunchControl service from the example:

.. default-domain:: {{ domain.sphinxname }}
.. highlight:: {{ domain.highlight }}
{{ domain.currentmodule('LaunchControl') }}
{% import domain.macros as macros with context %}

LaunchControl API
=================

{{ macros.service(services['LaunchControl']) }}

The first four lines set up the page for the client language. macros.service documents the service’s procedures and properties. A class, enumeration, structure or exception has a macro of its own, such as macros.class(services['SpaceCenter'].classes['Vessel']). The templates are Jinja, so a page can hold any other text as well. domain.codeext is the file extension of the language’s examples. It is empty for http, which has no examples, so {% if domain.codeext %} leaves an example out of those pages.

The ordering file lists each member of the service by its full name, in the order the pages document them:

LaunchControl.Throttle
LaunchControl.ActivateStage

A member missing from the ordering file stops krpc-docgen, which then lists every member missing. Start with an empty file to get the full list.

Generating the Pages#

krpc-docgen reads the service definitions, which krpc-servicedefs generates from the service’s assembly (see Generating Service Code for Static Clients). Then it writes a page for each --page template:

krpc-servicedefs --reference-dir=/path/to/ksp/KSP_Data/Managed \
    -o LaunchControl.json LaunchControl LaunchControl.dll
krpc-docgen python order.txt LaunchControl.json \
    --page launch-control.tmpl python/launch-control.rst

Run krpc-docgen once for each language: cnano, cpp, csharp, http, java, js, lua or python. http documents the path of each procedure on the HTTP server. Pass the definitions of every service the documentation links to, as well as the service’s own. --documented writes the full name of each member the pages document, to check that none is left out.

The text of the pages is the service’s documentation. krpc-servicedefs reads it from the XML documentation file beside the assembly. Build the assembly with <GenerateDocumentationFile>true</GenerateDocumentationFile> to write that file.

Sphinx Setup#

The Python, C++, C and JavaScript pages use the domains built into Sphinx. The C#, Java, Lua and HTTP pages need the sphinx-csharp, sphinx-java, sphinx-lua-ls and sphinxcontrib-httpdomain extensions. A service whose documentation has units or formulas also needs sphinx.ext.mathjax.