Skip to content

Commit f2abb40

Browse files
committed
Fix Windows filepath problems
1 parent 5688bd5 commit f2abb40

File tree

3 files changed

+144
-121
lines changed

3 files changed

+144
-121
lines changed

doi2cite/README.md

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
# pandoc-doi2cite
2-
This pandoc lua filiter helps users to insert references in a document
3-
with using DOI(Digital Object Identifier) tags. With this filter, users
4-
do not need to make bibtex file by themselves. Instead, the filter
5-
automatically generate .bib file from the DOI tags, and convert the DOI
6-
tags into citation keys available by `--citeproc`.
7-
8-
<img src="https://user-images.githubusercontent.com/30950088/117561410-87ec5d00-b0d1-11eb-88be-931f3158ec44.png" width="960">
9-
10-
What the filter do are as follows:
11-
1. Search citations with DOI tags in the document
12-
2. Search corresponding bibtex data from `__from_DOI.bib` file
13-
3. If not found, get bibtex data of the DOI from
14-
http://api.crossref.org
15-
4. Add reference data to `__from_DOI.bib` file
16-
5. Check duplications of reference keys
17-
6. Replace DOI tags to the correspoinding citation keys
18-
19-
# Prerequisites
20-
- Pandoc version 2.0 or newer
21-
- This filter does not need any external dependencies
22-
- This filter must be executed before `pandoc-crossref` or
23-
`--citeproc`
24-
25-
# DOI tags
26-
Following DOI tags can be used:
27-
- @https://doi.org/
28-
- @doi.org/
29-
- @DOI:
30-
- @doi:
31-
32-
The first one (@https://doi.org/) may be the most useful because it is
33-
same as the accessible URL.
34-
35-
# YAML header
36-
The file **name** of the auto-generated bibliography file **MUST** be
37-
`__from_DOI.bib`, but the **place** of the file can be changed (e.g.
38-
`./refs/__from_DOI.bib`). You can designate the filepath in the
39-
document yaml header. The yaml key is `bibliography`, which is also
40-
used by Pandoc `--citeproc`.
41-
42-
43-
# Example
44-
45-
example1.md:
46-
47-
---
48-
bibliography:
49-
- "my_refs.bib"
50-
- "__from_DOI.bib"
51-
---
52-
53-
# Introduction
54-
The Laemmli system is one of the most widely used gel systems for the separation of proteins.[@LAEMMLI_1970]
55-
By the way, Einstein is genius.[@https://doi.org/10.1002/andp.19053220607; @doi.org/10.1002/andp.19053220806; @doi:10.1002/andp.19053221004]
56-
57-
Example command 1 (.md -\> .md)
58-
59-
``` {.sh}
60-
pandoc --lua-filter=doi2cite.lua --wrap=preserve -s example1.md -o expected1.md
61-
```
62-
63-
Example command 2 (.md -\> .pdf with
64-
[ACS](https://pubs.acs.org/journal/jacsat) style):
65-
66-
``` {.sh}
67-
pandoc --lua-filter=doi2cite.lua --filter=pandoc-crossref --citeproc --csl=sample1.csl -s example1.md -o expected1.pdf
68-
```
69-
70-
Example result
71-
72-
![expected1](https://user-images.githubusercontent.com/30950088/119964566-4d952200-bfe4-11eb-90d9-ed2366c639e8.png)
1+
# pandoc-doi2cite
2+
This pandoc lua filiter helps users to insert references in a document
3+
with using DOI(Digital Object Identifier) tags. With this filter, users
4+
do not need to make bibtex file by themselves. Instead, the filter
5+
automatically generate .bib file from the DOI tags, and convert the DOI
6+
tags into citation keys available by Pandoc-citeproc.
7+
8+
<img src="https://user-images.githubusercontent.com/30950088/117561410-87ec5d00-b0d1-11eb-88be-931f3158ec44.png" width="960">
9+
10+
What the filter do are as follows:
11+
1. Search citations with DOI tags in the document
12+
2. Search corresponding bibtex data from `__from_DOI.bib` file
13+
3. If not found, get bibtex data of the DOI from
14+
http://api.crossref.org
15+
4. Add reference data to `__from_DOI.bib` file
16+
5. Check duplications of reference keys
17+
6. Replace DOI tags to the correspoinding citation keys
18+
19+
# Prerequisites
20+
- Pandoc version 2.0 or newer
21+
- This filter does not need any external dependencies
22+
- This filter should be executed before `pandoc-crossref` or
23+
`--citeproc`
24+
25+
# DOI tags
26+
Following DOI tags can be used:
27+
- @https://doi.org/
28+
- @doi.org/
29+
- @DOI:
30+
- @doi:
31+
32+
The first one (@https://doi.org/) may be the most useful because it is
33+
same as the accessible URL.
34+
35+
# YAML header
36+
The file **name** of the auto-generated bibliography file **MUST** be
37+
`__from_DOI.bib`, but the **place** of the file can be changed (e.g.
38+
`'./refs/__from_DOI.bib'` or `'refs\\__from_DOI.bib'` for Windows). Yo
39+
u can designate the filepath in the document yaml header. The yaml key
40+
is `bibliography`, which is also used by Pandoc-citeproc.
41+
42+
43+
# Example
44+
45+
example1.md:
46+
47+
---
48+
bibliography:
49+
- 'my_refs.bib'
50+
- '__from_DOI.bib'
51+
---
52+
53+
# Introduction
54+
The Laemmli system is one of the most widely used gel systems for the separation of proteins.[@LAEMMLI_1970]
55+
By the way, Einstein is genius.[@https://doi.org/10.1002/andp.19053220607; @doi.org/10.1002/andp.19053220806; @doi:10.1002/andp.19053221004]
56+
57+
Example command 1 (.md -\> .md)
58+
59+
``` {.sh}
60+
pandoc --lua-filter=doi2cite.lua --wrap=preserve -s example1.md -o expected1.md
61+
```
62+
63+
Example command 2 (.md -\> .pdf with
64+
[ACS](https://pubs.acs.org/journal/jacsat) style):
65+
66+
``` {.sh}
67+
pandoc --lua-filter=doi2cite.lua --filter=pandoc-crossref --citeproc --csl=sample1.csl -s example1.md -o expected1.pdf
68+
```
69+
70+
Example result
71+
72+
![expected1](https://user-images.githubusercontent.com/30950088/119964566-4d952200-bfe4-11eb-90d9-ed2366c639e8.png)

doi2cite/doi2cite.lua

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
base_url = "http://api.crossref.org"
1212
1313
bibname = "__from_DOI.bib"
14-
bibpath = "__from_DOI.bib"
1514
key_list = {};
1615
doi_key_map = {};
1716
doi_entry_map = {};
1817
error_strs = {};
1918
error_strs["Resource not found."] = 404
2019
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
2423

2524

2625
--------------------------------------------------------------------------------
@@ -30,14 +29,8 @@ error_strs["<html><body><h1>503 Service Unavailable</h1>\n"..
3029
function Meta(m)
3130
local bib_data = m.bibliography
3231
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)
4134
local f = io.open(bibpath, "r")
4235
if f then
4336
entries_str = f:read('*all')
@@ -50,13 +43,7 @@ function Meta(m)
5043
end
5144
f:close()
5245
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)
6047
end
6148
end
6249

@@ -75,17 +62,16 @@ function Cite(c)
7562
doi = nil
7663
end
7764
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]
8167
else
8268
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
8470
print("Failed to get ref from DOI: " .. doi)
8571
else
8672
entry_str = tex2raw(entry_str)
8773
local entry_key = get_entrykey(entry_str)
88-
if key_list[entry_key] ~= nil then
74+
if key_list[entry_key] then
8975
entry_key = entry_key.."_"..doi
9076
entry_str = replace_entrykey(entry_str, entry_key)
9177
end
@@ -131,36 +117,39 @@ function get_paths_from(metadata)
131117
filepaths[metadata[1].text] = true
132118
elseif type(metadata) == "table" then
133119
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
136124
end
137125
end
138126
end
139127
end
140128
return filepaths
141129
end
142130

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 = [[\]]
155140
else
156-
return path
141+
return {filename = filepath, dirname = nil}
157142
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}
158147
end
159148

160149
-- 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)
162151
for path, _ in pairs(filepaths) do
163-
local filename = get_filename(path)
152+
local filename = split_path(path)["filename"]
164153
if filename == bibname then
165154
return path
166155
end
@@ -220,6 +209,40 @@ function get_doi_key_map(bibtex_string)
220209
return keys
221210
end
222211

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
223246

224247
--------------------------------------------------------------------------------
225248
-- The main function --

doi2cite/sample1.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
---
2-
bibliography:
3-
- "my_refs.bib"
4-
- "__from_DOI.bib"
5-
---
6-
7-
# Introduction
8-
The Laemmli system is one of the most widely used gel systems for the separation of proteins.[@LAEMMLI_1970]
1+
---
2+
bibliography:
3+
- 'my_refs.bib'
4+
- '__from_DOI.bib'
5+
---
6+
7+
# Introduction
8+
The Laemmli system is one of the most widely used gel systems for the separation of proteins.[@LAEMMLI_1970]
99
By the way, Einstein is genius.[@https://doi.org/10.1002/andp.19053220607; @doi.org/10.1002/andp.19053220806; @doi:10.1002/andp.19053221004]

0 commit comments

Comments
 (0)