From 8bbc22f9e8b5b46903a0eb3f577bdb7742ee2660 Mon Sep 17 00:00:00 2001 From: Eiji Onchi Date: Wed, 19 Dec 2018 21:48:44 +0900 Subject: [PATCH] Added date formatter, pages, and fixed metadata search for new entry without a file --- cmd/add.go | 18 +++++++++++------- cmd/general.go | 22 +++++++++++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 3d8bcbd..b175400 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -66,13 +66,17 @@ TODO: Add a flag for manual/auto input of metadata if askYesNo("Would you like to search the web for metadata?") { doi = query(requestSearch()) } - fmt.Println() - fmt.Println("Adding the entry manually...") - fmt.Println("What kind of entry is it?") - t := selectType() - fmt.Println() - fmt.Println("Please, add the required fields:") - entry = add(t) + if doi != "" { + entry = addDOI(doi) + } else { + fmt.Println() + fmt.Println("Adding the entry manually...") + fmt.Println("What kind of entry is it?") + t := selectType() + fmt.Println() + fmt.Println("Please, add the required fields:") + entry = add(t) + } } commit(entry) edit(entry) diff --git a/cmd/general.go b/cmd/general.go index 3bb1bbf..735dbaf 100644 --- a/cmd/general.go +++ b/cmd/general.go @@ -28,6 +28,7 @@ import ( "path/filepath" "regexp" "runtime" + "strconv" "strings" "sync" @@ -263,7 +264,7 @@ func parseCrossref(work *crossref.Work) *scholar.Entry { } e.Required["author"] = strings.TrimSuffix(e.Required["author"], " and ") - e.Required["date"] = work.Date + e.Required["date"] = formatDate(work.Date) e.Required["title"] = work.Title for _, a := range work.Editors { @@ -272,8 +273,27 @@ func parseCrossref(work *crossref.Work) *scholar.Entry { e.Optional["editor"] = strings.TrimSuffix(e.Optional["editor"], " and ") e.Optional["volume"] = work.Volume + e.Optional["pages"] = work.Pages e.Optional["number"] = work.Issue e.Optional["doi"] = work.DOI return e } + +func formatDate(date string) string { + parts := strings.Split(date, "-") + for i := range parts { + fixed := "" + p, err := strconv.Atoi(parts[i]) + if err != nil { + panic(err) + } + if p < 10 { + fixed += "0" + } + fixed += strconv.Itoa(p) + parts[i] = fixed + } + + return strings.Join(parts, "-") +}