If you know what literate programming is, then you may not be surprised to learn that PreTeXt provides excellent support primarily with two additional elements: <fragment> and <fragref>.
So the idea is that from one file you get a program for the compiler, and a beautiful, typeset explanation for a human reader in any format PreTeXt supports. An accessible introduction is Knuth's description of his WEB system for the Pascal programming language [1], or many more resources are at the literate programming site 1 . Full details on the PreTeXt implementation are at Section 5.39.
www.literateprogramming.com