丑陋的 xml 输出

发布于 2024-11-09 08:46:32 字数 704 浏览 4 评论 0原文

我正在使用 HaXml 来转换 XML 文件,一切都运行良好。然而 HaXml 生成的输出看起来非常难看,主要是因为它几乎在每个右括号处插入了换行符。 下面是一些生成 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 进行渲染,但使用不同的 样式 没有太大变化。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日记撕了你也走了 2024-11-16 08:46:32

将您对 show 的调用替换为 Text.PrettyPrint.renderStyle 您可以获得一些不同的行为:

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

尝试不同的开箱即用样式:

默认样式

<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>

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

如果您不喜欢其中任何一个,您可以使用 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 函数中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文