mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-28 06:30:33 +03:00
Implement XML data support
Example: ``` {{ with resources.Get "https://example.com/rss.xml" | transform.Unmarshal }} {{ range .channel.item }} <strong>{{ .title | plainify | htmlUnescape }}</strong><br /> <p>{{ .description | plainify | htmlUnescape }}</p> {{ $link := .link | plainify | htmlUnescape }} <a href="{{ $link }}">{{ $link }}</a><br /> <hr> {{ end }} {{ end }} ``` Closes #4470
This commit is contained in:
parent
58adbeef88
commit
0eaaa8fee3
12 changed files with 167 additions and 12 deletions
|
@ -24,6 +24,7 @@ import (
|
|||
"github.com/gohugoio/hugo/common/herrors"
|
||||
"github.com/niklasfasching/go-org/org"
|
||||
|
||||
xml "github.com/clbanning/mxj/v2"
|
||||
toml "github.com/pelletier/go-toml/v2"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/afero"
|
||||
|
@ -135,6 +136,25 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v interface{}) error {
|
|||
err = d.unmarshalORG(data, v)
|
||||
case JSON:
|
||||
err = json.Unmarshal(data, v)
|
||||
case XML:
|
||||
var xmlRoot xml.Map
|
||||
xmlRoot, err = xml.NewMapXml(data)
|
||||
|
||||
var xmlValue map[string]interface{}
|
||||
if err == nil {
|
||||
xmlRootName, err := xmlRoot.Root()
|
||||
if err != nil {
|
||||
return toFileError(f, errors.Wrap(err, "failed to unmarshal XML"))
|
||||
}
|
||||
xmlValue = xmlRoot[xmlRootName].(map[string]interface{})
|
||||
}
|
||||
|
||||
switch v := v.(type) {
|
||||
case *map[string]interface{}:
|
||||
*v = xmlValue
|
||||
case *interface{}:
|
||||
*v = xmlValue
|
||||
}
|
||||
case TOML:
|
||||
err = toml.Unmarshal(data, v)
|
||||
case YAML:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue