A lot of the code is copy/pasted from the document::run implementation, our command::runner and HTMLOutput (custom URL protocol).