Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Oct 27, 2018
1 parent dbec514 commit 04dcef7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 58 deletions.
63 changes: 5 additions & 58 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@ package scholar

import (
"fmt"
"io/ioutil"
"sort"
"strings"
"time"

yaml "gopkg.in/yaml.v2"
)

type EntryType struct {
type entryType struct {
Type string
Description string `yaml:"desc"`
Required map[string]string `yaml:"req"`
Optional map[string]string `yaml:"opt"`
}

func (e *EntryType) Get() *Entry {
func (e *entryType) get() *Entry {
var c Entry
c.Type = e.Type
c.Required = make(map[string]string)
for k, _ := range e.Required {
for k := range e.Required {
c.Required[k] = ""
}

c.Optional = make(map[string]string)
for k, _ := range e.Optional {
for k := range e.Optional {
c.Optional[k] = ""
}

return &c
}

func (e *EntryType) Show(level int) {
func (e *entryType) Info(level int) {
fmt.Println(e.Type, ":", e.Description)

if level > 0 {
Expand Down Expand Up @@ -112,53 +109,3 @@ func (e *Entry) Bib() string {
bib = fmt.Sprintf("%s}", bib)
return bib
}

type Entries struct {
Map map[string]*EntryType
}

func (es *Entries) Parse(service, eType string) *Entry {
switch service {
case "crossref":
switch eType {
case "journal-article":
return es.Map["article"].Get()
case "proceedings-article":
return es.Map["inproceedings"].Get()
default:
return es.Map["article"].Get()
}
}

return &Entry{}
}

func (es *Entries) Load(file string) error {
d, err := ioutil.ReadFile(file)
if err != nil {
return err
}

err = yaml.Unmarshal(d, &es.Map)
if err != nil {
return err
}

for name, entry := range es.Map {
entry.Type = name
}

return nil
}

func (es *Entries) Show(level int) {
var eNames []string
for name := range es.Map {
eNames = append(eNames, name)
}
sort.Strings(eNames)
for _, name := range eNames {
es.Map[name].Show(level)
fmt.Println()
}
}
66 changes: 66 additions & 0 deletions scholar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package scholar

import (
"fmt"
"io/ioutil"
"sort"

yaml "gopkg.in/yaml.v2"
)

var entryTypes map[string]*entryType

// LoadTypes loads the configuration file for entry types and associated
// fields.
func LoadTypes(file string) error {
d, err := ioutil.ReadFile(file)
if err != nil {
return err
}

err = yaml.Unmarshal(d, &entryTypes)
if err != nil {
return err
}

for name, entry := range entryTypes {
entry.Type = name
}

return nil
}

// TypesInfo shows the information of each entry type. level indicates how much
// information to show (0 = only labels, 1 = labels and required fields, 2 =
// labels, required fields, and optional fields.)
func TypesInfo(level int) {
var eNames []string
for name := range entryTypes {
eNames = append(eNames, name)
}
sort.Strings(eNames)
for _, name := range eNames {
entryTypes[name].Info(level)
fmt.Println()
}
}

// NewEntry returns an empty copy of an entry according to the types of entries
// loaded.
func NewEntry(service, label string) *Entry {
switch service {
case "crossref":
switch label {
case "journal-article":
return entryTypes["article"].get()
case "proceedings-article":
return entryTypes["inproceedings"].get()
default:
return entryTypes["article"].get()
}
case "none":
return entryTypes[label].get()
}

return &Entry{}
}

0 comments on commit 04dcef7

Please sign in to comment.