This script produces a pdf-like paged document from basic HTML. For the prototype I decided to use the last draft of my ill-fated Masters thesis on the history of self-esteem. Some readers may find this content as or more interesting than the code itself. Special thanks to Timothy Verhoeven and Michael Hau for the feedback that helped shape this draft. It is available for viewing here, along with the PDF.
The code mainly focuses on dividing the content across pages intelligently. I have taken great pains to prevent single lines, including headings, appearing in isolation at the tops and bottoms of pages. To my judgment, I have succeeded in making my history writing look almost as nice as the concise and declarative compositions and recursion-based machinery behind it.