forked from TensorNetwork/tensornetwork.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bibtex.jl
167 lines (153 loc) · 3.87 KB
/
bibtex.jl
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
#module BibTex
#
# Example of a BibTex entry:
#
# @article{name,
# author = {Lastname1, Firstname1 and Lastname2, Firstname2},
# title = {Title of the article},
# journal = {Fancy journal},
# year = {1982},
# volume = {36},
# pages = {1000-1010},
# url = {http://arxiv.org/abs/1801.00315},
# doi = {10.1103/PhysRevB.90.155136}
# }
#
# note that multi-byte characters (umlauts, accented letters, long
# dashes) are not supported and will lead to StringIndexError in getEntry().
mutable struct BTEntry
ref_num::Int
btype::String
name::String
authors ::Array{String,1}
journal ::String
title ::String
doi::String
url::String
eprint::String
year::String
pages::String
volume::String
school::String
function BTEntry(btype_::String,name_::String)
return new(0,
btype_,
name_,
[], #authors
"", #journal
"", #title
"", #doi
"", #url
"", #eprint
"", #year
"", #pages
"", #volume
"") #school
end
end
function convertToMD(bt::BTEntry)::String
md = ""
if bt.btype == "phdthesis"
for a in bt.authors
md *= "$a: "
end
if bt.url != ""
md *= "[_$(bt.title)_]($(bt.url))"
else
md *= "_$(bt.title)_"
end
md *= " (Doctoral Thesis)"
if bt.school != ""
md *= ", $(bt.school)"
end
if bt.year != ""
md *= ", $(bt.year)"
end
else
if (bt.url != "" || bt.doi != "")
if bt.url != ""
md *= "[_$(bt.title)_]($(bt.url))"
else
md *= "[_$(bt.title)_](https://dx.doi.org/$(bt.doi))"
end
else
md *= "_$(bt.title)_"
end
for a in bt.authors
md *= ", $a"
end
if bt.journal != ""
md *= ", <i>"*bt.journal*"</i>"
if bt.volume != ""
md *= " <b>$(bt.volume)</b>"
end
if bt.pages != ""
md *= ", $(bt.pages)"
end
end
if bt.year != ""
md *= " ($(bt.year))"
end
if bt.eprint != ""
md *= ", "*bt.eprint
end
end
return md
end
function getEntry(contents::String,start::Int)::String
lev = 1
n = start
while (lev > 0 && n < length(contents))
if (contents[n] == '{') lev += 1 end
if (contents[n] == '}') lev -= 1 end
#@show n,contents[n],lev
n += 1
end
return contents[start:n-1]
end
function parseBibTex(fname::String)
contents = open(fname) do file read(file,String) end
entries = Dict{String,BTEntry}()
btre = r"@(.+?){(.+?),"s
function getField(re,source::String)::String
if occursin(re,source)
return strip(match(re,source).captures[1])
end
return ""
end
for m in eachmatch(btre,contents)
btype = convert(String,m.captures[1])
name = convert(String,m.captures[2])
bt = BTEntry(btype,name)
start = m.offset+length(m.match)
entry = getEntry(contents,start)
bt.btype = btype
bt.title = getField(r"title\W*=\W*{(.+?)}"is,entry)
bt.journal = getField(r"journal\W*=\W*{(.+?)}"is,entry)
bt.volume = getField(r"volume\W*=\W*{(.+?)}"is,entry)
bt.pages = getField(r"pages\W*=\W*{(.+?)}"is,entry)
bt.year = getField(r"year\W*=\W*{(.+?)}"is,entry)
bt.doi = getField(r"doi\W*=\W*{(.+?)}"is,entry)
bt.eprint = getField(r"eprint\W*=\W*{(.+?)}"is,entry)
bt.url = getField(r"url\W*=\W*{(.+?)}"is,entry)
bt.school = getField(r"school\W*=\W*{(.+?)}"is,entry)
all_authors = getField(r"author[s]*\W*=\W*{(.+?)}"is,entry)
if all_authors != ""
authors = split(all_authors," and")
for a in authors
a = strip(a)
rev = r"(\w+?), (.+)"
if occursin(rev,a)
m = match(rev,a)
push!(bt.authors,"$(m.captures[2]) $(m.captures[1])")
else
push!(bt.authors,a)
end
end
end
entries[name] = bt
end
return entries
end
#export parseBibTex
#end