Switching to reStructuredText and ReadTheDocs for GitHub Project Documentation

Documentation is never something I’ve really cared much about writing. It’s tedious and often boring to craft, requires constant updates as things change, and, well … it’s just not fun. As a user, however, I love documentation!

I recently set about solving this conflict between creating documentation and reading documentation by browsing around open source projects that I respect to see what they are doing. This search bore fruit: by embedding most of the documentation directly into the code, and by using a fancy parser that was above and beyond what Markdown could do, I saw the potential for making better documentation that wasn’t quite so labor intensive.

This discovery lead to exploring the world of reStructuredText (RST) and ReadTheDocs. RST is a rather powerful markup language that can be parsed by ReadTheDocs to create some really fancy stuff, including automatic generation of a PDF, Epub, and HTML pages. By including RST files in my GitHub projects and creating an integration with ReadTheDocs, I’m able to make documentation a part of the project files. This is in contrast to using a Wiki or some other data collection system, which is a bit more constraining on options and portability.

Making this all work really boils down to three steps: learning how to write RST files, writing a bit of code to generate RST files for the project, and then making sure the RST files live in the correct location so that ReadTheDocs can use them for a build. Before you go on, here’s the results of my labor.

Writing reStructuredText (RST) Files

The first step in using RST is learning the syntax. Here are the three pages I tend to float around for reference:

I’ll admit that the format took me a little while to absorb. The syntax for links and other reference pointers were especially goofy at first blush. However, I now really enjoy how clean it all looks in both the raw edit mode and when it’s published. Not having hyperlinks directly in the text is actually quite crisp, and commonly used links can be re-used throughout the documentation.

To illustrate this, take a look at part of the index.rst file I’m using for the Rubrik PowerShell Module.

Notice the use of two periods to escape the text and send a command, such as the .. image:: or .. _available on GitHub: portions. The fact that I can reference a link by simply adding accent marks and an underscore anywhere in the text is snazzy. It certainly takes a little bit of getting used to when compared to Markdown, which uses the (link) style format, but is really clean looking and easy to spot when reading the raw file.

Also, GitHub can render the RST files natively, which is nice.

Programmatically Creating Documentation

As I eluded to earlier, the ability to dynamically create documentation files is really what I was after for most of the work. To do this, I decided to plant all of the detailed help information into the PowerShell functions that are part of the Module. This is a good idea in any scenario – the Get-Help cmdlet is extremely helpful (pun intended) and should be loaded with information. Therefore, why not just output the detailed help directly into RST files and use a bit of additional markup to craft fancy documentation?

The code to do this is small and simple. A PowerShell script named BuildDocs.ps1 is given the task of making the RST files. Let’s start by examining the code.

  • L1-5: Orders the documentation by the PowerShell verb, such as Get, Set, and New. It starts by grabbing all of the functions in the module and seeing how many unique verbs are being used.
  • L7: Walk through each verb type.
  • L9-14: A $data variable will hold all of the page content. The top of the RST page header is created to fancy things up a bit.
  • L15-23: Using the Get-Help cmdlet with the -Detailed parameter, the code creates a second level header with the function’s name and then outputs the help data.
  • L26-27: Results are sent from $data to an RST file using the verb’s name. The UTF8 encoding parameter is required for proper parsing by ReadTheDocs.

Parsing RST Files with ReadTheDocs

The last step is to save all of the RST files into a folder named docs within the project. Note that the case is sensitive – using Docs won’t work. Place an index.rst file in the root of the directory, which also describes the table of contents (TOC) for Sphinx, which is used by ReadTheDocs. Here’s another sample from the Rubrik PowerShell Module:

All of the document names below the .. toctree:: section correspond to RST files. Therefore, by including the word requirements I must also have a file named requirements.rst in the docs folder. The TOC name for the requirements page is then derived from the title within requirements.rst. It makes the update process quite painless – a simple update in one place trickles down elsewhere with ease. Every time a commit is made to the “master” branch, the docs rebuild themselves.

In order to figure most of this out, I ended up just seeing how ReadTheDocs created their own documentation and reverse engineering the efforts. I still have further tuning I want to do, and I need to get more documentation online for the Vester project – you can see a preview here. But, I’m quite happy with how simple the workflow has become and how much less work is required to maintain help for the PowerShell functions.

5 Responses

  1. Mathieu Buisson
    Mathieu Buisson at |

    Really cool stuff.
    I use a custom module to generate a markdown file from comment-based help :
    https://github.com/MathieuBuisson/Powershell-Utility/tree/master/ReadmeFromHelp

    But it’s less sophisticated and the result certainly doesn’t look as nice.

    By the way, maybe you already knew that, but PowerShell cmdlets have a “Verb” property of the type “string”.
    So, you could simplify line 3 of your BuildDocs.ps1 script, like so :
    $_.Verb

    Reply
  2. HPE StoreOnce PowerShell Module Version 2 - my cloud-(r)evolution

    […] wie in dem Artikel von Chris […]

  3. An Open Request for Feedback - vSphere Configuration Management with Vester - Wahl Network

    […] documentation. I’ve begun the process of migrating those over to Read the Docs because I’m rather taken by RST files these days. In a nutshell you just need to download the files from the GitHub project and drop the […]

  4. My Custom Plaster Template - my cloud-(r)evolution

    […] Read the Docs bin ich ursprünglich durch den Artikel Switching to reStructuredText and ReadTheDocs for GitHub Project Documentation von Chris Wahl gekommen. Ich finde das eine tolle Möglichkeit, schnell und unkompliziert eine […]

Share your point of view!