/
ris.go
111 lines (101 loc) · 2.41 KB
/
ris.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package scholar
import (
"fmt"
"strings"
)
type exRIS struct {
dict map[string]string
}
func (ex *exRIS) parse(v string) string {
if s, ok := ex.dict[v]; ok {
return s
}
return ""
}
func (ex *exRIS) export(e *Entry) string {
ris := new(strings.Builder)
// TY - type
fmt.Fprintf(ris, "TY - %s", ex.parse(e.Type))
// field - value
for field := range e.Required {
if value := e.Required[field]; value != "" {
switch field {
case "author":
authors := strings.Split(value, " and ")
for _, author := range authors {
fmt.Fprintf(ris, "\n%s - %s", "AU", author)
}
case "date":
date := strings.Split(value, "-")
fmt.Fprintf(ris, "\n%s - %s", "Y1", date[0])
if len(date) > 1 {
fmt.Fprintf(ris, "/%s", date[1])
}
fmt.Fprint(ris, "//")
case "urldate":
date := strings.Split(value, "-")
fmt.Fprintf(ris, "\n%s - %s", "Y2", date[0])
if len(date) > 1 {
fmt.Fprintf(ris, "/%s", date[1])
}
fmt.Fprint(ris, "//")
default:
if f := ex.parse(field); f != "" {
fmt.Fprintf(ris, "\n%s - %s", f, value)
}
}
}
}
// field - value
for field := range e.Optional {
if value := e.Optional[field]; value != "" {
switch field {
case "editor":
authors := strings.Split(value, " and ")
for _, author := range authors {
fmt.Fprintf(ris, "\n%s - %s", "ED", author)
}
case "urldate":
date := strings.Split(value, "-")
fmt.Fprintf(ris, "\n%s - %s", "Y2", date[0])
if len(date) > 1 {
fmt.Fprintf(ris, "/%s", date[1])
}
fmt.Fprint(ris, "//")
case "pages":
pages := strings.Split(value, "-")
fmt.Fprintf(ris, "\n%s - %s", "SP", pages[0])
fmt.Fprintf(ris, "\n%s - %s", "EP", pages[len(pages)-1])
default:
if f := ex.parse(field); f != "" {
fmt.Fprintf(ris, "\n%s - %s", f, value)
}
}
}
}
ris.WriteString("\nER - ")
return ris.String()
}
var ris = &exRIS{
dict: map[string]string{
"online": "ELEC",
"article": "JOUR",
"thesis": "THES",
"inproceedings": "CPAPER",
"book": "BOOK",
"inbook": "CHAP",
"patent": "PAT",
"report": "RPRT",
"title": "TI",
"journaltitle": "JO",
"booktitle": "T2",
"doi": "DO",
"number": "M1",
"abstract": "N2",
"publisher": "PB",
"ISBN": "SN",
"ISSN": "SN",
"url": "UR",
"volume": "VL",
},
}