HXT: Amazing behavior when reading and writing HTML in String in pure code

I want to read HTML from String, process it, and return the modified document as a string using HXT. Since this operation does not require I / O, I would rather runLA arrow with runLA than with runX .

The code looks like this (excluding processing for simplicity):

 runLA (hread >>> writeDocumentToString [withOutputHTML, withIndent yes]) html 

However, the html tag is missing the result:

 ["\n <head>\n <title>Bogus</title>\n </head>\n <body>\n Some trivial bogus text.\n </body>\n",""] 

When I use runX instead:

 runX (readString [] html >>> writeDocumentToString [withOutputHTML, withIndent yes]) 

I get the expected result:

 ["<html>\n <head>\n <title>Bogus</title>\n </head>\n <body>\n Some trivial bogus text.\n </body>\n</html>\n"] 

Why is this and how can I fix it?

+4
source share
1 answer

If you look at XmlTree for both, you will see that readString adds a top-level "/" element. For non- IO runLA :

 > putStr . formatTree show . head $ runLA xread html ---XTag "html" [] | +---XText "\n " | +---XTag "head" [] ... 

And with runX :

 > putStr . formatTree show . head =<< runX (readString [] html) ---XTag "/" [NTree (XAttr "transfer-Status") [NTree (XText "200")... | +---XTag "html" [] | +---XText "\n " | +---XTag "head" [] ... 

writeDocumentToString uses getChildren to remove this root element.

One easy way is to use something like selem to wrap the xread output in a similar root element so that it looks like the type of writeDocumentToString input:

 > runLA (selem "/" [xread] >>> writeDocumentToString [withOutputHTML, withIndent yes]) html ["<html>\n <head>\n <title>Bogus</title>\n </head>\n <body>\n Some trivial bogus text.\n </body>\n</html>\n"] 

This gives the desired result.

+5
source

Source: https://habr.com/ru/post/1369211/


All Articles