mirror of
https://github.com/go-shiori/shiori.git
synced 2025-01-16 21:09:44 +08:00
Create command for exporting bookmarks
This commit is contained in:
parent
34f4bc957f
commit
ecbaa9c4f4
2 changed files with 89 additions and 1 deletions
88
cmd/export.go
Normal file
88
cmd/export.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/RadhiFadlillah/shiori/model"
|
||||
"github.com/spf13/cobra"
|
||||
"html/template"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
exportCmd = &cobra.Command{
|
||||
Use: "export target-file",
|
||||
Short: "Export bookmarks into HTML file in Firefox format.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := exportBookmarks(args[0])
|
||||
if err != nil {
|
||||
cError.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Export finished")
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(exportCmd)
|
||||
}
|
||||
|
||||
func exportBookmarks(dstPath string) error {
|
||||
// Read bookmarks from database
|
||||
bookmarks, err := DB.GetBookmarks(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(bookmarks) == 0 {
|
||||
return fmt.Errorf("No saved bookmarks yet")
|
||||
}
|
||||
|
||||
// Open destination file
|
||||
dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
// Create template
|
||||
funcMap := template.FuncMap{
|
||||
"unix": func(str string) int64 {
|
||||
t, err := time.Parse("2006-01-02 15:04:05", str)
|
||||
if err != nil {
|
||||
return time.Now().Unix()
|
||||
}
|
||||
|
||||
return t.Unix()
|
||||
},
|
||||
"combine": func(tags []model.Tag) string {
|
||||
strTags := make([]string, len(tags))
|
||||
for i, tag := range tags {
|
||||
strTags[i] = tag.Name
|
||||
}
|
||||
|
||||
return strings.Join(strTags, ",")
|
||||
},
|
||||
}
|
||||
|
||||
tplFile := `<!DOCTYPE NETSCAPE-Bookmark-file-1>` +
|
||||
`<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">` +
|
||||
`<TITLE>Bookmarks</TITLE>` +
|
||||
`<H1>Bookmarks</H1>` +
|
||||
`<DL><p>` +
|
||||
`{{range $book := .}}` +
|
||||
`<DT><A HREF="{{$book.URL}}" ADD_DATE="{{unix $book.Modified}}" TAGS="{{combine $book.Tags}}">{{$book.Title}}</A>` +
|
||||
`{{if gt (len $book.Excerpt) 0}}<DD>{{$book.Excerpt}}{{end}}{{end}}` +
|
||||
`</DL><p>`
|
||||
|
||||
tpl, err := template.New("export").Funcs(funcMap).Parse(tplFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tpl.Execute(dstFile, &bookmarks)
|
||||
}
|
|
@ -13,7 +13,7 @@ import (
|
|||
|
||||
var (
|
||||
importCmd = &cobra.Command{
|
||||
Use: "import file",
|
||||
Use: "import source-file",
|
||||
Short: "Import bookmarks from HTML file in Firefox format.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
|
Loading…
Reference in a new issue