From 9fee37b37c1460596b4ce7d2126e9f81b974e83a Mon Sep 17 00:00:00 2001 From: Marcell Mars Date: Sun, 3 Apr 2022 01:27:13 +0200 Subject: [PATCH] maybe etree output is already good enough for simplest struct to unmarshal to --- cmd/build.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cmd/build.go b/cmd/build.go index 51cb114..d6ed048 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -1,7 +1,9 @@ package cmd import ( + "encoding/xml" "fmt" + "log" "os" "accorder/pkg/calibre" @@ -11,6 +13,18 @@ import ( "github.com/spf13/viper" ) +type FileAttachment struct { + XMLName xml.Name `xml:"resource"` + Path string `xml:"resource,attr"` +} + +type ZoteroAttachment struct { + XMLName xml.Name `xml:"Attachment"` + ReferenceID string `xml:"about,attr"` + FileAttachment FileAttachment + FileType string `xml:"type"` +} + var buildCmd = &cobra.Command{ Use: "build", Short: "Build standalone, portable webapp from Calibre library.", @@ -60,6 +74,7 @@ should be enough for the next successful build).`, if err := doc.ReadFromFile(bibtexPath); err != nil { panic(err) } + var zoteroAttachment ZoteroAttachment root := doc.SelectElement("rdf:RDF") for _, z := range root.FindElements("[name()='link:type']") { doc := etree.NewDocument() @@ -67,6 +82,16 @@ should be enough for the next successful build).`, if z.Text() != "application/pdf" { continue } + doc.WriteTo(os.Stdout) + b, err := doc.WriteToBytes() + if err != nil { + log.Fatal(err) + } + if err := xml.Unmarshal(b, &zoteroAttachment); err != nil { + log.Fatalln(err) + } + fmt.Printf("\nZOTEROATTACHMENT: %#v\n", zoteroAttachment) + fmt.Println(z.Text()) fmt.Println("~ ~ ~ ~") fmt.Println(doc.WriteTo(os.Stdout))