opensheaf-literate

opensheaf-literate, a literate program

This is the source of opensheaf-literate (the literate command), a small tool for literate programming in Markdown, written as a literate program and tangled by itself.

A literate program is a program written to be read: it is explained in documents, in the order in which it is best understood, and its code is given in pieces, chunks, in the course of the explanation. Two tools make it a program and a book. The tangler takes the chunks out of the documents and puts them together in the files the computer wants; the weaver turns the documents into a website on which every chunk is linked to the chunks it uses and to those that use it.

Here the documents are plain Markdown, readable as they stand on GitHub or in any editor: a chunk is an indented code block whose first line names it, and a line <<NAME>> in a chunk stands for the chunk NAME. Nothing else is needed, and nothing is hidden from Markdown. The syntax, and the idea that a few dozen lines of code are enough for it, come from Kragen Sitaker's handaxeweb; the notation of the woven pages comes from Norman Ramsey's noweb.

The tools were written for the programs of OpenSheaf, an open format for archives of scholarly documents: opensheaf-py, its reference tools in Python, and the OpenSheaf Explorer, in JavaScript. They were first a part of each, and became a package of their own when the two copies began to differ. Nothing in them is particular to OpenSheaf: what a project says about itself (its name, its repository, its logo) is in a small file of its own, literate.json.

The tangled files are kept in the repository of a project, next to the documents, so that the project builds and installs as if there were no documents at all; one edits the documents, never the files, and a check in the continuous integration (literate tangle --check) makes sure the two agree.

Reading the code

Code is set in grey boxes. Above each box stands the name of its chunk, with ≡ for the first fragment of a chunk and +≡ for the ones that continue it; a name in angle brackets inside the code, like ⟨the syntax as patterns⟩, is a reference to another chunk, and a link to it. Below each box: where the chunk is used, and where it continues. The index of chunks lists them all.

The chapters

  1. Tangling: from the documents to the program: the syntax, and the tangler.
  2. Weaving: from the documents to the website: the weaver, its template and its stylesheet.
  3. The configuration and the command line: literate.json, and literate tangle, weave and build.
  4. Bootstrapping: a tangler tangled by itself.
  5. The tests.