Literate Programming Examples from my Website


I have become rather interested in literate programming, which I find a very interesting and elegant approach. Literate programming was first proposed by Donald Knuth and it is supported very well by Emacs: consult the Org Mode manual for more information.

This page lists the pages of this website which use literate programming.

The first version has a list compiled by hand… I then realized I could be more efficient and precise by using code to extract the information and this page has become itself an example of literate programming.

First of all, we find all occurrences of #+BEGIN_SRC in the source files (with the exception of this file) which are required to produce results. We use sed to process the output, using a TAB to separate the filename from the content and by extracting the programming language, which is the first word after #+BEGIN_SRC.

grep BEGIN_SRC $(find ../.. -name "*.org" -a \! -name ) | grep results | grep -v "_site" | sed 's/:/\t/' | sed -E 's/#\+BEGIN_SRC +([^ ]+) .*/\1/' 

The output, which is not exported, looks like:

| ../../                                    | ruby    |
| ../../notes/covid/                         | R       |
| ../../notes/covid/                         | ruby    |

We now use datamash to make the data into a Pivot table, with the crosstab command. Sed is used to generate links to the files, by prepending file: to the filenames and to remove the “N/A” entries.

echo "$table" | sort | datamash crosstab 1,2 | sed 's-N/A--'g | sed -E 's/\.\./file:../'