2023-07-30 15:39:17 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-04-02 23:27:13 +00:00
|
|
|
"encoding/xml"
|
2023-07-30 15:39:17 +00:00
|
|
|
"fmt"
|
2022-04-02 23:27:13 +00:00
|
|
|
"log"
|
2022-03-23 22:48:42 +00:00
|
|
|
"os"
|
2023-07-30 15:39:17 +00:00
|
|
|
|
|
|
|
"accorder/pkg/calibre"
|
|
|
|
|
|
|
|
"github.com/beevik/etree"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2022-04-02 23:27:13 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-07-30 15:39:17 +00:00
|
|
|
var buildCmd = &cobra.Command{
|
|
|
|
Use: "build",
|
|
|
|
Short: "Build standalone, portable webapp from Calibre library.",
|
|
|
|
Long: `Build searchable, standalone, portable webapp against the local Calibre
|
|
|
|
library including all the metadata needed. It creates BROWSE_LIBRARY.html in
|
|
|
|
root directory of the local Calibre library. For search (authors, titles,
|
|
|
|
tags...) it uses rendered metadata from static/data{1-8}.js files.
|
|
|
|
|
|
|
|
Every time the directory path and/or librarian is provided it is saved in
|
|
|
|
the configuration file for the future use (therefore: 'accorder build SESSION'
|
|
|
|
should be enough for the next successful build).`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
session := args[0]
|
|
|
|
|
|
|
|
for vipFlag, cliFlag := range map[string]string{
|
|
|
|
"librarian_name": "librarian",
|
|
|
|
"local_upload": "directory",
|
|
|
|
"library_uuid": "library-uuid",
|
|
|
|
"library_secret": "library-secret",
|
|
|
|
"server_upload": "server",
|
|
|
|
"bucket_upload": "bucket",
|
|
|
|
} {
|
|
|
|
viper.BindPFlag(fmt.Sprintf("%s.%s", session, vipFlag), cmd.Flags().Lookup(cliFlag))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
session := args[0]
|
|
|
|
|
|
|
|
MissingRequiredFlags(
|
|
|
|
map[string]string{
|
|
|
|
"librarian_name": "librarian",
|
|
|
|
"local_upload": "directory",
|
|
|
|
},
|
|
|
|
session,
|
|
|
|
cmd,
|
|
|
|
)
|
|
|
|
|
|
|
|
libraryUUID := ViperValue(session, "library_uuid")
|
|
|
|
librarySecret := ViperValue(session, "library_secret")
|
|
|
|
calibrePath := ViperValue(session, "local_upload")
|
|
|
|
librarianName := ViperValue(session, "librarian_name")
|
|
|
|
jsonPath := CliFlagValue(cmd, "jsonpath")
|
|
|
|
bibtexPath := CliFlagValue(cmd, "import-bibtex")
|
|
|
|
|
|
|
|
doc := etree.NewDocument()
|
|
|
|
if err := doc.ReadFromFile(bibtexPath); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-04-02 23:27:13 +00:00
|
|
|
var zoteroAttachment ZoteroAttachment
|
2023-07-30 15:39:17 +00:00
|
|
|
root := doc.SelectElement("rdf:RDF")
|
2022-03-24 10:25:03 +00:00
|
|
|
for _, z := range root.FindElements("[name()='link:type']") {
|
2022-03-23 22:48:42 +00:00
|
|
|
doc := etree.NewDocument()
|
2022-03-24 10:25:03 +00:00
|
|
|
doc.SetRoot(z.Parent().Copy())
|
|
|
|
if z.Text() != "application/pdf" {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-02 23:27:13 +00:00
|
|
|
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)
|
|
|
|
|
2022-03-24 10:25:03 +00:00
|
|
|
fmt.Println(z.Text())
|
2022-03-23 22:48:42 +00:00
|
|
|
fmt.Println("~ ~ ~ ~")
|
|
|
|
fmt.Println(doc.WriteTo(os.Stdout))
|
2022-03-24 10:25:03 +00:00
|
|
|
elm := root.FindElement(fmt.Sprintf("[@rdf:resource='%s']", z.Parent().SelectAttr("rdf:about").Value))
|
2022-03-23 22:48:42 +00:00
|
|
|
doc.SetRoot(elm.Parent().Copy())
|
|
|
|
fmt.Println("+++++")
|
|
|
|
fmt.Println(doc.WriteTo(os.Stdout))
|
|
|
|
fmt.Println("~ ~ ~ ~")
|
2023-07-30 15:39:17 +00:00
|
|
|
}
|
|
|
|
calibre.RenderStandaloneApp(calibrePath, librarianName, libraryUUID, librarySecret, jsonPath)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(buildCmd)
|
|
|
|
|
|
|
|
buildCmd.Flags().StringP("directory", "d", "", "A local Calibre directory.")
|
|
|
|
buildCmd.Flags().StringP("librarian", "l", "", "Librarian's name.")
|
|
|
|
buildCmd.Flags().StringP("export-json", "e", "", "Path where to render all metadata into JSON.")
|
|
|
|
buildCmd.Flags().StringP("import-bibtex", "i", "", "Import books from BibTex file into Calibre.")
|
|
|
|
|
|
|
|
buildCmd.Flags().StringP("library-uuid", "u", "", "A library's UUID used if part of MotW.")
|
|
|
|
buildCmd.Flags().StringP("library-secret", "p", "", "A password used if part of MotW.")
|
|
|
|
buildCmd.Flags().StringP("server", "s", "minio.memoryoftheworld.org", "Server.")
|
|
|
|
buildCmd.Flags().StringP("bucket", "b", "", "Server.")
|
|
|
|
CustomHelpOutput(buildCmd)
|
|
|
|
|
|
|
|
buildCmd.Flags().MarkHidden("library-uuid")
|
|
|
|
buildCmd.Flags().MarkHidden("library-secret")
|
|
|
|
buildCmd.Flags().MarkHidden("server")
|
|
|
|
buildCmd.Flags().MarkHidden("bucket")
|
|
|
|
}
|