more parsing of zotero's rdf..
This commit is contained in:
parent
9fee37b37c
commit
5dfd1950d1
96
cmd/build.go
96
cmd/build.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
|
|
||||||
"accorder/pkg/calibre"
|
"accorder/pkg/calibre"
|
||||||
|
|
||||||
|
@ -13,16 +12,23 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileAttachment struct {
|
// best zotero rdf documentation:
|
||||||
XMLName xml.Name `xml:"resource"`
|
// https://github.com/zotero/translators/blob/master/Zotero%20RDF.js
|
||||||
Path string `xml:"resource,attr"`
|
//
|
||||||
|
|
||||||
|
type ZoteroItem struct {
|
||||||
|
XMLName xml.Name `xml:"zoteroItem"`
|
||||||
|
FilePath string `xml:"filePath"`
|
||||||
|
MimeType string `xml:"mimeType"`
|
||||||
|
Publisher string `xml:"publisher,omitempty"`
|
||||||
|
Authors []ZoteroAuthor `xml:"authors"`
|
||||||
|
Title string `xml:"title"`
|
||||||
|
Description string `xml:"description,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ZoteroAttachment struct {
|
type ZoteroAuthor struct {
|
||||||
XMLName xml.Name `xml:"Attachment"`
|
XMLName xml.Name `xml:"authors"`
|
||||||
ReferenceID string `xml:"about,attr"`
|
Author string `xml:"author"`
|
||||||
FileAttachment FileAttachment
|
|
||||||
FileType string `xml:"type"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var buildCmd = &cobra.Command{
|
var buildCmd = &cobra.Command{
|
||||||
|
@ -74,32 +80,70 @@ should be enough for the next successful build).`,
|
||||||
if err := doc.ReadFromFile(bibtexPath); err != nil {
|
if err := doc.ReadFromFile(bibtexPath); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
var zoteroAttachment ZoteroAttachment
|
|
||||||
root := doc.SelectElement("rdf:RDF")
|
root := doc.SelectElement("rdf:RDF")
|
||||||
for _, z := range root.FindElements("[name()='link:type']") {
|
for _, attachmentNode := range root.FindElements("[name()='link:type']") {
|
||||||
doc := etree.NewDocument()
|
var zoteroItem ZoteroItem
|
||||||
doc.SetRoot(z.Parent().Copy())
|
if attachmentNode.Text() != "application/pdf" {
|
||||||
if z.Text() != "application/pdf" {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
doc.WriteTo(os.Stdout)
|
newDoc := etree.NewDocument()
|
||||||
b, err := doc.WriteToBytes()
|
zoteroUnion := newDoc.CreateElement("zoteroItem")
|
||||||
|
filePathElement := zoteroUnion.CreateElement("filePath")
|
||||||
|
filePathQuery := attachmentNode.Parent().FindElement("[name()='rdf:resource']").SelectAttr("rdf:resource").Value
|
||||||
|
filePathElement.CreateText(filePathQuery)
|
||||||
|
mimeTypeElement := zoteroUnion.CreateElement("mimeType")
|
||||||
|
mimeType := attachmentNode.Text()
|
||||||
|
mimeTypeElement.CreateText(mimeType)
|
||||||
|
|
||||||
|
bibliographyNode := root.FindElement(fmt.Sprintf("[@rdf:resource='%s']", attachmentNode.Parent().SelectAttr("rdf:about").Value)).Parent().Copy()
|
||||||
|
|
||||||
|
publisherQuery := bibliographyNode.FindElement("[name()='foaf:name']")
|
||||||
|
if publisherQuery != nil {
|
||||||
|
publisherElement := zoteroUnion.CreateElement("publisher")
|
||||||
|
publisher := publisherQuery.Text()
|
||||||
|
publisherElement.CreateText(publisher)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorsQuery := bibliographyNode.FindElements("[name()='foaf:Person']")
|
||||||
|
authors := zoteroUnion.CreateElement("authors")
|
||||||
|
for _, authorNode := range authorsQuery {
|
||||||
|
var firstName, surName string
|
||||||
|
author := authors.CreateElement("author")
|
||||||
|
|
||||||
|
firstNameNode := authorNode.FindElement("[name()='foaf:givenName']")
|
||||||
|
if firstNameNode != nil {
|
||||||
|
firstName = firstNameNode.Text()
|
||||||
|
}
|
||||||
|
surNameNode := authorNode.FindElement("[name()='foaf:surname']")
|
||||||
|
if surNameNode != nil {
|
||||||
|
surName = surNameNode.Text()
|
||||||
|
}
|
||||||
|
author.CreateText(fmt.Sprintf("%s %s", firstName, surName))
|
||||||
|
}
|
||||||
|
|
||||||
|
titleQuery := bibliographyNode.FindElement("[name()='dc:title']")
|
||||||
|
if titleQuery != nil {
|
||||||
|
titleNode := zoteroUnion.CreateElement("title")
|
||||||
|
title := titleQuery.Text()
|
||||||
|
titleNode.CreateText(title)
|
||||||
|
}
|
||||||
|
|
||||||
|
descriptionQuery := bibliographyNode.FindElement("[name()='dcterms:abstract']")
|
||||||
|
if descriptionQuery != nil {
|
||||||
|
descriptionNode := zoteroUnion.CreateElement("description")
|
||||||
|
description := descriptionQuery.Text()
|
||||||
|
descriptionNode.CreateText(description)
|
||||||
|
}
|
||||||
|
// newDoc.WriteTo(os.Stdout)
|
||||||
|
b, err := newDoc.WriteToBytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := xml.Unmarshal(b, &zoteroAttachment); err != nil {
|
if err := xml.Unmarshal(b, &zoteroItem); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("\nZOTEROATTACHMENT: %#v\n", zoteroAttachment)
|
fmt.Printf("\nZoteroItem: %#v\n", zoteroItem)
|
||||||
|
|
||||||
fmt.Println(z.Text())
|
|
||||||
fmt.Println("~ ~ ~ ~")
|
|
||||||
fmt.Println(doc.WriteTo(os.Stdout))
|
|
||||||
elm := root.FindElement(fmt.Sprintf("[@rdf:resource='%s']", z.Parent().SelectAttr("rdf:about").Value))
|
|
||||||
doc.SetRoot(elm.Parent().Copy())
|
|
||||||
fmt.Println("+++++")
|
|
||||||
fmt.Println(doc.WriteTo(os.Stdout))
|
|
||||||
fmt.Println("~ ~ ~ ~")
|
|
||||||
}
|
}
|
||||||
calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret, jsonPath)
|
calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret, jsonPath)
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue