|
| 1 | +-------------------------------------------------------------------------------- |
| 2 | +-- Copyright © 2021 Takuro Hosomi |
| 3 | +-- This library is free software; you can redistribute it and/or modify it |
| 4 | +-- under the terms of the MIT license. See LICENSE for details. |
| 5 | +-------------------------------------------------------------------------------- |
| 6 | + |
| 7 | + |
| 8 | +-------------------------------------------------------------------------------- |
| 9 | +-- Global variables -- |
| 10 | +-------------------------------------------------------------------------------- |
| 11 | +base_url = "http://api.crossref.org" |
| 12 | + |
| 13 | +bibname = "__from_DOI.bib" |
| 14 | +key_list = {}; |
| 15 | +doi_key_map = {}; |
| 16 | +doi_entry_map = {}; |
| 17 | +error_strs = {}; |
| 18 | +error_strs["Resource not found."] = 404 |
| 19 | +error_strs["No acceptable resource available."] = 406 |
| 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 |
| 23 | + |
| 24 | + |
| 25 | +-------------------------------------------------------------------------------- |
| 26 | +-- Pandoc Functions -- |
| 27 | +-------------------------------------------------------------------------------- |
| 28 | +-- Get bibliography filepath from yaml metadata |
| 29 | +function Meta(m) |
| 30 | + local bib_data = m.bibliography |
| 31 | + local bibpaths = get_paths_from(bib_data) |
| 32 | + bibpath = find_filepath(bibname, bibpaths) |
| 33 | + bibpath = verify_path(bibpath) |
| 34 | + local f = io.open(bibpath, "r") |
| 35 | + if f then |
| 36 | + entries_str = f:read('*all') |
| 37 | + if entries_str then |
| 38 | + doi_entry_map = get_doi_entry_map(entries_str) |
| 39 | + doi_key_map = get_doi_key_map(entries_str) |
| 40 | + for doi,key in pairs(doi_key_map) do |
| 41 | + key_list[key] = true |
| 42 | + end |
| 43 | + end |
| 44 | + f:close() |
| 45 | + else |
| 46 | + make_new_file(bibpath) |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +-- Get bibtex data of doi-based citation.id and make bibliography. |
| 51 | +-- Then, replace "citation.id" |
| 52 | +function Cite(c) |
| 53 | + for _, citation in pairs(c.citations) do |
| 54 | + local id = citation.id:gsub('%s+', ''):gsub('%%2F', '/') |
| 55 | + if id:sub(1,16) == "https://doi.org/" then |
| 56 | + doi = id:sub(17):lower() |
| 57 | + elseif id:sub(1,8) == "doi.org/" then |
| 58 | + doi = id:sub(9):lower() |
| 59 | + elseif id:sub(1,4) == "DOI:" or id:sub(1,4) == "doi:" then |
| 60 | + doi = id:sub(5):lower() |
| 61 | + else |
| 62 | + doi = nil |
| 63 | + end |
| 64 | + if doi then |
| 65 | + if doi_key_map[doi] then |
| 66 | + citation.id = doi_key_map[doi] |
| 67 | + else |
| 68 | + local entry_str = get_bibentry(doi) |
| 69 | + if entry_str == nil or error_strs[entry_str] then |
| 70 | + print("Failed to get ref from DOI: " .. doi) |
| 71 | + else |
| 72 | + entry_str = tex2raw(entry_str) |
| 73 | + local entry_key = get_entrykey(entry_str) |
| 74 | + if key_list[entry_key] then |
| 75 | + entry_key = entry_key.."_"..doi |
| 76 | + entry_str = replace_entrykey(entry_str, entry_key) |
| 77 | + end |
| 78 | + key_list[entry_key] = true |
| 79 | + doi_key_map[doi] = entry_key |
| 80 | + citation.id = entry_key |
| 81 | + local f = io.open(bibpath, "a+") |
| 82 | + if f then |
| 83 | + f:write(entry_str .. "\n") |
| 84 | + f:close() |
| 85 | + else |
| 86 | + error("Unable to open file: "..bibpath) |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + return c |
| 93 | +end |
| 94 | + |
| 95 | + |
| 96 | +-------------------------------------------------------------------------------- |
| 97 | +-- Common Functions -- |
| 98 | +-------------------------------------------------------------------------------- |
| 99 | +-- Get bib of DOI from http://api.crossref.org |
| 100 | +function get_bibentry(doi) |
| 101 | + local entry_str = doi_entry_map[doi] |
| 102 | + if entry_str == nil then |
| 103 | + print("Request DOI: " .. doi) |
| 104 | + local url = base_url.."/works/" |
| 105 | + ..doi.."/transform/application/x-bibtex" |
| 106 | + .."?mailto="..mailto |
| 107 | + mt, entry_str = pandoc.mediabag.fetch(url) |
| 108 | + end |
| 109 | + return entry_str |
| 110 | +end |
| 111 | + |
| 112 | +-- Extract designated filepaths from 1 or 2 dimensional metadata |
| 113 | +function get_paths_from(metadata) |
| 114 | + local filepaths = {}; |
| 115 | + if metadata then |
| 116 | + if metadata[1].text then |
| 117 | + filepaths[metadata[1].text] = true |
| 118 | + elseif type(metadata) == "table" then |
| 119 | + for _, datum in pairs(metadata) do |
| 120 | + if datum[1] then |
| 121 | + if datum[1].text then |
| 122 | + filepaths[datum[1].text] = true |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + return filepaths |
| 129 | +end |
| 130 | + |
| 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 = [[\]] |
| 140 | + else |
| 141 | + return {filename = filepath, dirname = nil} |
| 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} |
| 147 | +end |
| 148 | + |
| 149 | +-- Find bibname in a given filepath list and return the filepath if found |
| 150 | +function find_filepath(filename, filepaths) |
| 151 | + for path, _ in pairs(filepaths) do |
| 152 | + local filename = split_path(path)["filename"] |
| 153 | + if filename == bibname then |
| 154 | + return path |
| 155 | + end |
| 156 | + end |
| 157 | + return nil |
| 158 | +end |
| 159 | + |
| 160 | +-- Make some TeX descriptions processable by citeproc |
| 161 | +function tex2raw(string) |
| 162 | + local symbols = {}; |
| 163 | + symbols["{\textendash}"] = "–" |
| 164 | + symbols["{\textemdash}"] = "—" |
| 165 | + symbols["{\textquoteright}"] = "’" |
| 166 | + symbols["{\textquoteleft}"] = "‘" |
| 167 | + for tex, raw in pairs(symbols) do |
| 168 | + local string = string:gsub(tex, raw) |
| 169 | + end |
| 170 | + return string |
| 171 | +end |
| 172 | + |
| 173 | +-- get bibtex entry key from bibtex entry string |
| 174 | +function get_entrykey(entry_string) |
| 175 | + local key = entry_string:match('@%w+{(.-),') or '' |
| 176 | + return key |
| 177 | +end |
| 178 | + |
| 179 | +-- get bibtex entry doi from bibtex entry string |
| 180 | +function get_entrydoi(entry_string) |
| 181 | + local doi = entry_string:match('doi%s*=%s*["{]*(.-)["}],?') or '' |
| 182 | + return doi |
| 183 | +end |
| 184 | + |
| 185 | +-- Replace entry key of "entry_string" to newkey |
| 186 | +function replace_entrykey(entry_string, newkey) |
| 187 | + entry_string = entry_string:gsub('(@%w+{).-(,)', '%1'..newkey..'%2') |
| 188 | + return entry_string |
| 189 | +end |
| 190 | + |
| 191 | +-- Make hashmap which key = DOI, value = bibtex entry string |
| 192 | +function get_doi_entry_map(bibtex_string) |
| 193 | + local entries = {}; |
| 194 | + for entry_str in bibtex_string:gmatch('@.-\n}\n') do |
| 195 | + local doi = get_entrydoi(entry_str) |
| 196 | + entries[doi] = entry_str |
| 197 | + end |
| 198 | + return entries |
| 199 | +end |
| 200 | + |
| 201 | +-- Make hashmap which key = DOI, value = bibtex key string |
| 202 | +function get_doi_key_map(bibtex_string) |
| 203 | + local keys = {}; |
| 204 | + for entry_str in bibtex_string:gmatch('@.-\n}\n') do |
| 205 | + local doi = get_entrydoi(entry_str) |
| 206 | + local key = get_entrykey(entry_str) |
| 207 | + keys[doi] = key |
| 208 | + end |
| 209 | + return keys |
| 210 | +end |
| 211 | + |
| 212 | +-- function to make directories and files |
| 213 | +function make_new_file(filepath) |
| 214 | + if filepath then |
| 215 | + print("doi2cite: creating "..filepath) |
| 216 | + local dirname = split_path(filepath)["dirname"] |
| 217 | + if dirname then |
| 218 | + os.execute("mkdir "..dirname) |
| 219 | + end |
| 220 | + f = io.open(filepath, "w") |
| 221 | + if f then |
| 222 | + f:close() |
| 223 | + else |
| 224 | + error("Unable to make bibtex file: "..bibpath..".\n" |
| 225 | + .."This error may come from the missing directory. \n" |
| 226 | + ) |
| 227 | + end |
| 228 | + end |
| 229 | +end |
| 230 | + |
| 231 | +-- Verify that the given filepath is correct. |
| 232 | +-- Catch common Pandoc user mistakes about Windows-formatted filepath. |
| 233 | +function verify_path(bibpath) |
| 234 | + if bibpath == nil then |
| 235 | + print("[WARNING] doi2cite: " |
| 236 | + .."The given file path is incorrect or empty. " |
| 237 | + .."In Windows-formatted filepath, Pandoc recognizes " |
| 238 | + .."double backslash ("..[[\\]]..") as the delimiters." |
| 239 | + ) |
| 240 | + return "__from_DOI.bib" |
| 241 | + else |
| 242 | + return bibpath |
| 243 | + end |
| 244 | +end |
| 245 | + |
| 246 | +-------------------------------------------------------------------------------- |
| 247 | +-- The main function -- |
| 248 | +-------------------------------------------------------------------------------- |
| 249 | +return { |
| 250 | + { Meta = Meta }, |
| 251 | + { Cite = Cite } |
| 252 | +} |
0 commit comments