Reputation: 2392
I'm using HaXml to transform a XML file and it's all quite working nicely. However the output HaXml generates looks really ugly, mainly because it inserts a linebreak at almost every closing bracket. Here's some code that generates a xml:
writeFile outPath (show . PP.content . head $ test (docContent (posInNewCxt "" Nothing) (xmlParse "" "")))
test =
mkElemAttr "test" [("a", literal "1"), ("b", literal "2")]
[
mkElem "nested" []
]
and here's the output it generates:
<test a="1" b="2"
><nested/></test>
Of course it is worse with more elements.
I know that HaXml uses Text.PrettyPrint.HughesPJ for rendering, but using different styles didn't change much.
So, is there a way to change the output?
Upvotes: 3
Views: 508
Reputation: 137947
Replacing you call to show
with Text.PrettyPrint.renderStyle
you can get a few different behaviors:
import Text.XML.HaXml
import Text.XML.HaXml.Util
import Text.XML.HaXml.Posn
import qualified Text.XML.HaXml.Pretty as PP
import Text.PrettyPrint
main = writeFile "/tmp/x.xml" (renderStyle s . PP.content
. head $
test (docContent (posInNewCxt "" Nothing) (xmlParse "" "")))
where
s = style { mode = LeftMode, lineLength = 2 }
test =
mkElemAttr "test" [("a", literal "1"), ("b", literal "2")]
[
mkElem "nested" []
]
Experimenting with different out-of-the-box styles:
Default style
<test a="1" b="2"
><nested/></test>
style { mode = OneLineMode }
<test a="1" b="2" ><nested/></test>
style { mode = LeftMode, lineLength = 2 }
<test a="1"
b="2"
><nested/></test>
So you can certainly do a few different things.
If you don't like any of these, you can write a custom processors, using fullRender
:
fullRender
:: Mode -- Rendering mode
-> Int -- Line length
-> Float -- Ribbons per line
-> (TextDetails -> a -> a) -- What to do with text
-> a -- What to do at the end
-> Doc -- The document
-> a -- Result
where your custom behavior can be programmed into the TextDetails
function.
Upvotes: 3