Hello everyone!
I just released the first version of https://html2elm.obtuse.io! (source: https://github.com/obtuseio/html2elm)
I know https://github.com/mbylstra/html-to-elm exists but I took a different approach than that – I parse the HTML as well as HTML elements’ style attribute using the browser’s native parser.
This has one major benefit over mbylstra’s project: the converted HTML will behave exactly the same as the original one renders in a spec compliant browser.
It’s also way faster – pasting 1,500 lines of HTML freezes Chrome for 15 seconds on mbylstra’s version while in my version it is converted in milliseconds.
Other than that,
-
HTML comments are converted to Elm comments.
-
Int and Bool attributes are converted to their equivalents in elm-lang/html.
-
The
style
attribute is parsed to name/value pairs and generate a call toHtml.Attributes.style
. -
The order of the attributes is preserved in the generated Elm code.
-
And there are options to clean up text nodes, by trimming them, completely removing whitespace only nodes, and the best one: collapsing consecutive whitespace into a single space.
Two features I’ll be working on soon are:
-
Adding support for SVG. They work right now but all of them are converted to calls to the “node” function.
-
Generating import/exposing statements so you don’t have to type them by hand or use
exposing (..)
.
Please let me know if you have any feedback, bug report, feature request, or suggestion!