11
11
base_url = " http://api.crossref.org"
12
12
13
13
bibname = " __from_DOI.bib"
14
- bibpath = " __from_DOI.bib"
15
14
key_list = {};
16
15
doi_key_map = {};
17
16
doi_entry_map = {};
18
17
error_strs = {};
19
18
error_strs [" Resource not found." ] = 404
20
19
error_strs [" No acceptable resource available." ] = 406
21
- error_strs [" <html><body><h1>503 Service Unavailable</h1>\n " ..
22
- " No server is available to handle this request.\n " ..
23
- " </body></html>" ] = 503
20
+ error_strs [" <html><body><h1>503 Service Unavailable</h1>\n "
21
+ .. " No server is available to handle this request.\n "
22
+ .. " </body></html>" ] = 503
24
23
25
24
26
25
---- ----------------------------------------------------------------------------
@@ -30,14 +29,8 @@ error_strs["<html><body><h1>503 Service Unavailable</h1>\n"..
30
29
function Meta (m )
31
30
local bib_data = m .bibliography
32
31
local bibpaths = get_paths_from (bib_data )
33
- bibpath = get_filepath (bibname , bibpaths )
34
- if bibpath == nil then
35
- bibpath = " __from_DOI.bib"
36
- print (" [doi2cite WARNING]: "
37
- .. " Include '" .. bibpath .. " ' into bibliography list"
38
- .. " to be processed by citeproc."
39
- )
40
- end
32
+ bibpath = find_filepath (bibname , bibpaths )
33
+ bibpath = verify_path (bibpath )
41
34
local f = io.open (bibpath , " r" )
42
35
if f then
43
36
entries_str = f :read (' *all' )
@@ -50,13 +43,7 @@ function Meta(m)
50
43
end
51
44
f :close ()
52
45
else
53
- if io.open (bibpath , " w" ) == nil then
54
- error (" Unable to make bibtex file: " .. bibpath .. " .\n "
55
- .. " This error may come from the missing directory. \n "
56
- .. " doi2cite filter will not make directory by iteself. \n "
57
- .. " Make sure that the directory for bibtex file exists."
58
- )
59
- end
46
+ make_new_file (bibpath )
60
47
end
61
48
end
62
49
@@ -75,17 +62,16 @@ function Cite(c)
75
62
doi = nil
76
63
end
77
64
if doi then
78
- if doi_key_map [doi ] ~= nil then
79
- local entry_key = doi_key_map [doi ]
80
- citation .id = entry_key
65
+ if doi_key_map [doi ] then
66
+ citation .id = doi_key_map [doi ]
81
67
else
82
68
local entry_str = get_bibentry (doi )
83
- if entry_str == nil or error_strs [entry_str ] ~= nil then
69
+ if entry_str == nil or error_strs [entry_str ] then
84
70
print (" Failed to get ref from DOI: " .. doi )
85
71
else
86
72
entry_str = tex2raw (entry_str )
87
73
local entry_key = get_entrykey (entry_str )
88
- if key_list [entry_key ] ~= nil then
74
+ if key_list [entry_key ] then
89
75
entry_key = entry_key .. " _" .. doi
90
76
entry_str = replace_entrykey (entry_str , entry_key )
91
77
end
@@ -131,36 +117,39 @@ function get_paths_from(metadata)
131
117
filepaths [metadata [1 ].text ] = true
132
118
elseif type (metadata ) == " table" then
133
119
for _ , datum in pairs (metadata ) do
134
- if datum [1 ].text then
135
- filepaths [datum [1 ].text ] = true
120
+ if datum [1 ] then
121
+ if datum [1 ].text then
122
+ filepaths [datum [1 ].text ] = true
123
+ end
136
124
end
137
125
end
138
126
end
139
127
end
140
128
return filepaths
141
129
end
142
130
143
- -- Extract filename from a given a path
144
- function get_filename (path )
145
- local len = path :len ()
146
- local reversed = path :reverse ()
147
- if reversed :find (" /" ) then
148
- local pos = reversed :find (" /" )
149
- local fname_rev = reversed :sub (1 , pos - 1 )
150
- return fname_rev :reverse ()
151
- elseif reversed :find ([[ \]] ) then
152
- local pos = reversed :find ([[ \]] )
153
- local fname_rev = reversed :sub (1 , pos - 1 )
154
- return fname_rev :reverse ()
131
+ -- Extract filename and dirname from a given a path
132
+ function split_path (filepath )
133
+ local delim = nil
134
+ local len = filepath :len ()
135
+ local reversed = filepath :reverse ()
136
+ if filepath :find (" /" ) then
137
+ delim = " /"
138
+ elseif filepath :find ([[ \]] ) then
139
+ delim = [[ \]]
155
140
else
156
- return path
141
+ return { filename = filepath , dirname = nil }
157
142
end
143
+ local pos = reversed :find (delim )
144
+ local dirname = filepath :sub (1 , len - pos )
145
+ local filename = reversed :sub (1 , pos - 1 ):reverse ()
146
+ return {filename = filename , dirname = dirname }
158
147
end
159
148
160
149
-- Find bibname in a given filepath list and return the filepath if found
161
- function get_filepath (filename , filepaths )
150
+ function find_filepath (filename , filepaths )
162
151
for path , _ in pairs (filepaths ) do
163
- local filename = get_filename (path )
152
+ local filename = split_path (path )[ " filename " ]
164
153
if filename == bibname then
165
154
return path
166
155
end
@@ -220,6 +209,40 @@ function get_doi_key_map(bibtex_string)
220
209
return keys
221
210
end
222
211
212
+ -- function to make directories and files
213
+ function make_new_file (filepath )
214
+ if filepath then
215
+ print (filepath )
216
+ local filename = split_path (filepath )[" filename" ]
217
+ local dirname = split_path (filepath )[" dirname" ]
218
+ if filename then
219
+ os.execute (" mkdir " .. dirname )
220
+ end
221
+ f = io.open (filepath , " w" )
222
+ if f then
223
+ f :close ()
224
+ else
225
+ error (" Unable to make bibtex file: " .. bibpath .. " .\n "
226
+ .. " This error may come from the missing directory. \n "
227
+ )
228
+ end
229
+ end
230
+ end
231
+
232
+ -- Verify that the given filepath is correct.
233
+ -- Catch common Pandoc user mistakes about Windows-formatted filepath.
234
+ function verify_path (bibpath )
235
+ if bibpath == nil then
236
+ print (" [WARNING] doi2cite: "
237
+ .. " The given file path is incorrect or empty. "
238
+ .. " In Windows-formatted filepath, Pandoc recognizes "
239
+ .. " double backslash (" .. [[ \\]] .. " ) as the delimiters."
240
+ )
241
+ return " __from_DOI.bib"
242
+ else
243
+ return bibpath
244
+ end
245
+ end
223
246
224
247
---- ----------------------------------------------------------------------------
225
248
-- The main function --
0 commit comments