-
Notifications
You must be signed in to change notification settings - Fork 2
/
converter.go
143 lines (124 loc) · 3.21 KB
/
converter.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"log"
"os"
"strings"
)
type UnusedBibKey struct {
key string
val string
}
type BibEntry struct {
bibtype string
id string
authors []string
title string
journal string
year string
volume string
issue string
startpg string
endpg string
month string
url string
doi string
unused []UnusedBibKey
}
func (b *BibEntry) BibMap(key, val string) {
FieldMap := map[string]*string {
"TI" : &b.title,
"T1" : &b.title,
"JO" : &b.journal,
"UR" : &b.url,
"DO" : &b.doi,
"VL" : &b.volume,
"ID" : &b.id,
"TY" : &b.bibtype,
"PY" : &b.year,
"SP" : &b.startpg,
"EP" : &b.endpg,
"IS" : &b.issue,
}
if key == "AU" {
b.authors = append(b.authors, val)
} else if field, found := FieldMap[key]; found {
*field = val
} else {
b.unused = append(b.unused, UnusedBibKey{key, val})
}
}
func (b *BibEntry) CheckBibEntry() error {
if len(b.title) < 4 {
return fmt.Errorf("Error: title is incorrect! Parsed title : " + b.title)
}
return nil
}
func (b *BibEntry) OutputDebug() {
fmt.Println("unused information for debugging: ")
for _, entry := range b.unused {
fmt.Printf("%s: %s\n", entry.key, entry.val)
}
}
func CreateBibEntry(content []string) *BibEntry {
b := &BibEntry{}
for _, line := range content {
parts := strings.SplitN(line, " - ", 2)
if len(parts) == 2 {
key, val := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
b.BibMap(key, val)
}
}
return b
}
func Convert(filename string, filedata string, id string) error {
contents := strings.Split(filedata, "\n")
bib := CreateBibEntry(contents)
if err := bib.CheckBibEntry(); err != nil {
bib.OutputDebug()
return err
}
return WriteToFile(bib, filename, id)
}
func ConvertWithoutId(filename string, filedata string) error {
contents := strings.Split(filedata, "\n")
bib := CreateBibEntry(contents)
if err := bib.CheckBibEntry(); err != nil {
bib.OutputDebug()
return err
}
TitleWords := strings.Split(bib.title, " ")
idx := 0
for len(TitleWords[idx]) < 3 {
idx++
}
name := strings.Replace(strings.Split(bib.authors[0], ",")[0], " ", "", -1)
id := name + bib.year + TitleWords[idx]
return WriteToFile(bib, filename, id)
}
func WriteToFile(bib *BibEntry, filename string, id string) error {
var BibFile string
if strings.Contains(filename, ".ris") {
BibFile = id + ".bib"
} else {
BibFile = filename
}
log.Println("Creating file: ", BibFile)
out, err := os.Create(BibFile)
if err != nil {
return err
}
defer out.Close()
out.WriteString("@article{" + id + ",\n")
out.WriteString("author = " + "{" + strings.Join(bib.authors, " and ") + "}" + ",\n")
out.WriteString("title = " + "{" + bib.title + "}" + ",\n")
out.WriteString("journal = " + "{" + bib.journal + "}" + ",\n")
out.WriteString("year = " + "{" + bib.year + "}" + ",\n")
out.WriteString("volume = " + "{" + bib.volume + "}" + ",\n")
out.WriteString("issue = " + "{" + bib.issue + "}" + ",\n")
out.WriteString("pages = " + "{" + bib.startpg + "-" + bib.endpg + "}" + ",\n")
out.WriteString("doi = " + "{" + bib.doi + "}" + ",\n")
out.WriteString("url = " + "{" + bib.url + "}\n")
out.WriteString("}")
return err
}