2011-05-22 88 views
3

我正在使用HaXml来转换XML文件,它都很好地工作。然而,输出HaXml产生的外观真的很难看,主要是因为它几乎在每个右括号处插入一个换行符。 下面是一些代码,生成一个xml:丑陋的XML输出

writeFile outPath (show . PP.content . head $ test (docContent (posInNewCxt "" Nothing) (xmlParse "" ""))) 

test = 
    mkElemAttr "test" [("a", literal "1"), ("b", literal "2")] 
     [ 
      mkElem "nested" [] 
     ] 

,这里是它的输出:

<test a="1" b="2" 
    ><nested/></test> 

当然是有更多的元素变得更糟。

我知道的HaXml使用Text.PrettyPrint.HughesPJ进行渲染,但使用不同的styles 并没有太大变化。

那么,有没有办法改变输出?

回答

3

更换你打电话showText.PrettyPrint.renderStyle你可以得到一些不同的行为:

默认样式

<test a="1" b="2" 
    ><nested/></test> 

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" [] 
     ] 

不同出的现成的款式做实验

style {mode = OneLineMode}

<test a="1" b="2" ><nested/></test> 

风格{模式= LeftMode,线路长度= 2}

<test a="1" 
b="2" 
><nested/></test> 

所以你当然可以做一些不同的事情。

如果你不喜欢任何这些,你可以写一个自定义的处理器,使用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 

在您自定义的行为可以被编程到TextDetails功能。

+0

恩,我想我必须写自己的fullRender,或者干脆结果。也许我会尝试复制现有的默认样式并删除换行符。 – bzn 2011-05-22 19:21:08