Closed simonh1000 closed 8 years ago
Hi Simon,
This is certainly possible, but I'd be hesitant to put it up on elm-packages as the quality isn't really up to scratch. I wouldn't want people relying on it in production. I made it as a learning exercise/challenge, without any experience writing parsers in the past. It works fine for small snippets on the html-to-elm website, but I imagine the performance would be terrible if you were looking to do animation or use it in production for large pages. If I were to write it again I would use either https://github.com/Dandandan/parser or https://github.com/Bogdanp/elm-combine rather than trying to roll my own parsing system from scratch without really knowing what I'm doing :)
The easiest way to parse html (if you only need it in a web context) is to just use the Browser's innerHtml
method, which will parse Html natively and return a DOM tree, which could then be traversed using native browser methods or even JQuery. This would require using a port or native though. It's most likely by far the most performant way to parse HTML with a browser.
There's also https://github.com/avh4/elm-compile-html which I believe using a Node module that does the html parsing.
The advantage of parsing in Elm is that it's just a simple pure function, and you can have good control of the format of the AST. Another advantage is that if one Elm one day has a compile target other than Javscript/Browsers, it will continue to work.
Could you describe what you'd like to use the parser for? Does it need to be able to handle badly formatted HTML (missing end tags, etc)?
Hi and thanks for your answer. In practise it was just for inserting 'safe' html, and I got a recommendation to use elm-markdown as a workaround. So I no longer need a parser as such, and perhaps that will generally be the case?
(BTW: your site inspired my attempt at parsing too!)
I would very much like to use this in a project I am working on