-
Notifications
You must be signed in to change notification settings - Fork 1
/
bibads.go
187 lines (173 loc) · 4.46 KB
/
bibads.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"errors"
"flag"
"io/ioutil"
"net/http"
"os"
"strings"
)
func getCacheFromBibFile(fileName string) (cache map[string]string, err error) {
fileBytes, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
fileStr := string(fileBytes)
sep := strings.Split(fileStr, "@")
cache = make(map[string]string)
for _, entry := range sep[1:] {
entry = strings.TrimSpace(entry)
code := strings.Split(strings.Split(entry, "{")[1], ",")[0]
cache[code] = "@" + entry
}
return cache, nil
}
func getBibCodeAliasesFromSource(fileName string) (bibCodeAliases map[string]string, err error) {
fileBytes, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
fileStr := string(fileBytes)
sep := strings.Split(fileStr, "\n")
bibCodeAliases = make(map[string]string)
for _, line := range sep {
lineFields := strings.Fields(line)
if len(lineFields) < 3 {
continue
}
if strings.Index(lineFields[0], "%") != 0 {
continue
}
if lineFields[0] == "%" && len(lineFields) < 4 {
continue
}
if lineFields[0] == "%" && lineFields[1] != "bibalias" {
continue
} else if lineFields[1] != "bibalias" && lineFields[0] != "%bibalias" {
continue
}
keyIndex := 2
if lineFields[0] == "%bibalias" {
keyIndex--
}
key := lineFields[keyIndex]
val := lineFields[keyIndex+1]
bibCodeAliases[key] = val
}
return bibCodeAliases, nil
}
func getBibFileNameAndBibCodesFromSource(fileName string) (bibFileName string, bibCodes map[string]bool, err error) {
fileBytes, err := ioutil.ReadFile(fileName)
if err != nil {
return "", nil, err
}
fileStr := string(fileBytes)
sep := strings.Split(fileStr, "\\bibliography{")
if len(sep) < 2 {
return "", nil, errors.New("No bib file name found in " + fileName)
}
bibFileName = strings.Split(sep[1], "}")[0]
bibCodes = make(map[string]bool)
sep = strings.Split(fileStr, "\\cite")
for _, cl := range sep[1:] {
cl = strings.Split(cl, "{")[1]
cl = strings.Split(cl, "}")[0]
for _, c := range strings.Split(cl, ",") {
bibCodes[strings.TrimSpace(c)] = true
}
}
return bibFileName, bibCodes, nil
}
func padRight(str, pad string, lenght int) string {
for {
str += pad
if len(str) > lenght {
return str[0:lenght]
}
}
}
func getBibRef(bibCode string) (bibRef string, err error) {
const querystr = "http://adsabs.harvard.edu/cgi-bin/nph-bib_query?data_type=BIBTEX&bibcode="
response, err := http.Get(querystr + bibCode)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return "", errors.New(response.Status)
}
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
bodyStr := string(bodyBytes)
bibRef = "@" + strings.Split(bodyStr, "@")[1]
return bibRef, nil
}
func getAliasedCachedBibText(code string, bibCodeAliases map[string]string, cache map[string]string, out chan string) {
realCode, isAlias := bibCodeAliases[code]
var alias string
if isAlias {
alias = code
code = realCode
}
msg := padRight(code, " ", 19) + " " + padRight(alias, " ", 15) + " ... "
isInCache := false
bibRefText := ""
var err error
if cache != nil {
if isAlias {
bibRefText, isInCache = cache[alias]
} else {
bibRefText, isInCache = cache[code]
}
}
if !isInCache {
bibRefText, err = getBibRef(code)
if err != nil {
println(msg, err.Error())
out <- ""
return
}
println(msg, "OK")
} else {
println(msg, "OK (cached)")
}
if isAlias {
bibRefText = strings.Replace(bibRefText, "{"+realCode, "{"+alias, 1)
}
out <- bibRefText
}
func main() {
noCachePtr := flag.Bool("nocache", false, "force fetch data from ads even if present in current bib file")
flag.Parse()
if flag.NArg() == 0 {
println("please supply name of file to operate on. Add a -nocache flag to not use existing entries")
os.Exit(1)
}
texFileName := flag.Args()[0]
bibCodeAliases, err := getBibCodeAliasesFromSource(texFileName)
if err != nil {
panic(err)
}
bibFileName, codes, err := getBibFileNameAndBibCodesFromSource(texFileName)
if err != nil {
panic(err)
}
println("bib file name: ", bibFileName)
var cache map[string]string
println("nocache=", *noCachePtr)
if !(*noCachePtr) {
cache, _ = getCacheFromBibFile(bibFileName)
}
bibFileText := ""
c := make(chan string)
for code := range codes {
go getAliasedCachedBibText(code, bibCodeAliases, cache, c)
}
for _ = range codes {
bibText := <-c
bibFileText += "\n\n" + bibText
}
ioutil.WriteFile(bibFileName, []byte(bibFileText), 0644)
}