latex.js binary allows to translate LaTeX files in the console.
LaTeX.js produces almost the exact same output you would get with LaTeX—except where impossible: glue cannot be translated to HTML, and sometimes cannot even be interpreted in the context of HTML.
LaTeX.js only needs one pass over the document instead of several. References can be filled in by remembering and later modifying the relevant part of the DOM tree.
Of course, LaTeX.js is completely Open Source. You can find the code on GitHub.