Scala or XML for Documents? (March 24, 2006)
I have used DocBook in the past for documents where I would like to distribute both HTML and for-print versions. It works reasonably well, but there are three major shortcomings:
- There are no subroutines or macros. If you do something twenty times, you have to repeat yourself.
- It does not look as nice as a carefully written Latex file.
- It does not handle math formulas, where Latex remains king.
To address these, I have now tried using Scala case classes rather than XML, and a plain old Scala method to replace the sophisticated DocBoox style sheets. I believe this experiment is neutral or positive on all three points:
- Scala wins: Scala has subroutines.
- Scala wins: The HTML version looks about the same, and the PDF version looks vastly better.
- Tie: math formulas are difficult either way.
Here is the old version in DocBook:
manual.xml
: 795 lines, 33kb
Here is the new version, written in Scala:
Manual.scala
: 720 lines, 32kbDocuments.scala
: 41 lines, 1.5kbEmitHtml.scala
: 115 lines, 2kbEmitLatex.scala
: 138 lines, 3kb- Total: 1014 lines, 38kb
The two versions are about the same size as each other, so we can’t prefer one or the other over the length of code.
The quality of output is a matter of taste, but I would say that the HTML versions are about the same, and the PDF version is much better with Scala than DocBook.