Go:你会如何"漂亮打印"/"Prettify"HTML?

Xpl*_*ane 7 html xml pretty-print go xml-parsing

在Python,PHP和许多其他语言中,可以转换html文档并"美化"它.在Go中,使用MarshIndent函数可以非常轻松地完成JSON和XML(来自结构/接口).

Go中的XML示例:

http://play.golang.org/p/aBNfNxTEG1

package main

import (
    "encoding/xml"
    "fmt"
    "os"
)

func main() {
    type Address struct {
        City, State string
    }
    type Person struct {
        XMLName   xml.Name `xml:"person"`
        Id        int      `xml:"id,attr"`
        FirstName string   `xml:"name>first"`
        LastName  string   `xml:"name>last"`
        Age       int      `xml:"age"`
        Height    float32  `xml:"height,omitempty"`
        Married   bool
        Address
        Comment string `xml:",comment"`
    }

    v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
    v.Comment = " Need more details. "
    v.Address = Address{"Hanga Roa", "Easter Island"}

    output, err := xml.MarshalIndent(v, "  ", "    ")
    if err != nil {
        fmt.Printf("error: %v\n", err)
    }

    os.Stdout.Write(output)
}
Run Code Online (Sandbox Code Playgroud)

但是,这仅适用于将struct/interface转换为[]字节.我想要的是自动转换一串html代码并缩进.例:

原始HTML

<!doctype html><html><head>
<title>Website Title</title>
</head><body>
<div class="random-class">
<h1>I like pie</h1><p>It's true!</p></div>
</body></html>
Run Code Online (Sandbox Code Playgroud)

Prettified HTML

<!doctype html>
<html>
    <head>
        <title>Website Title</title>
    </head>
    <body>
        <div class="random-class">
            <h1>I like pie</h1>
            <p>It's true!</p>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何使用字符串完成此操作?

小智 10

我遇到了同样的问题,我只是通过在Go中自己创建一个HTML格式化包来解决它.

这里是:

GoHTML - Go的HTML格式化程序

请检查此包裹.

谢谢,

敬事


小智 7

我试图找出如何在Go中打印xml时发现了这个问题.由于我没有在任何地方找到答案,这是我的解决方案:

import (
    "bytes"
    "encoding/xml"
    "io"
)

func formatXML(data []byte) ([]byte, error) {
    b := &bytes.Buffer{}
    decoder := xml.NewDecoder(bytes.NewReader(data))
    encoder := xml.NewEncoder(b)
    encoder.Indent("", "  ")
    for {
        token, err := decoder.Token()
        if err == io.EOF {
            encoder.Flush()
            return b.Bytes(), nil
        }
        if err != nil {
            return nil, err
        }
        err = encoder.EncodeToken(token)
        if err != nil {
            return nil, err
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个解决方案,但仍然在寻找不重写文档的Golang XML格式化程序/ prettyprinter(格式化空格除外).编组或使用编码器将更改名称空间声明.例如,像"<ns1:Element />"这样的元素将被翻译为类似'<Element xmlns ="ns1"/>'的内容,除非意图不改变xml而不是格式化,否则它似乎无害. (3认同)

Not*_*fer 5

编辑:找到了使用 XML 解析器的好方法:

package main

import (
    "encoding/xml"
    "fmt"
)

func main() {
    html := "<html><head><title>Website Title</title></head><body><div class=\"random-class\"><h1>I like pie</h1><p>It's true!</p></div></body></html>"
    type node struct {
        Attr     []xml.Attr
        XMLName  xml.Name
        Children []node `xml:",any"`
        Text     string `xml:",chardata"`
    }
    x := node{}
    _ = xml.Unmarshal([]byte(html), &x)
    buf, _ := xml.MarshalIndent(x, "", "\t")
    fmt.Println(string(buf))
}
Run Code Online (Sandbox Code Playgroud)

将输出以下内容:

<html>
    <head>
        <title>Website Title</title>
    </head>
    <body>
        <div>
            <h1>I like pie</h1>
            <p>It&#39;s true!</p>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)