0

How can I parse an html string for example <p>Test</p> and have this be converted to value that would be similar to p_ "Test"?

So the function type would be String -> Html ().

I've found the following project https://github.com/dbaynard/lucid-from-html but this generates actual Haskell source code as a String.

I'm aware that there are HTML parsing libraries, but I'm just wondering if there already exists a library that has implemented this?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286

1 Answers1

2

http://hackage.haskell.org/package/lucid-2.9.11/docs/Lucid-Base.html#v:toHtmlRaw

toHtmlRaw "<p>Test</p>"

The above seems to work well enough, though possibly it does not check for valid syntax.

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • 1
    +1. Note that the value of `p_ "Test"` is basically a complicated monadic representation of the byte string `

    Test

    `, so its actually already rendered HTML rather than any sort of DOM. As such, you don't really gain anything by parsing the HTML and then re-rendering it to `Html ()` -- it'll be a sort of expensive identity function. If you need to validate or normalize some HTML before rendering, you'd *still* be better off using a completely separate library (maybe HXT?) to parse and re-render it, and then use Lucid's `toHtmlRaw` on your re-rendered HTML.
    – K. A. Buhr Apr 07 '19 at 19:16