From 0b80152c855b1c87ef040d8e9a8b2f288f0287da Mon Sep 17 00:00:00 2001 From: Odin Kroeger Date: Fri, 21 Dec 2018 16:03:22 +0100 Subject: [PATCH] Removed she-bang line. --- README.rst | 6 ++--- pandoc-zotxt.lua | 65 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/README.rst b/README.rst index 789e6ed..4d8ae53 100644 --- a/README.rst +++ b/README.rst @@ -22,7 +22,7 @@ try `pandoc-zotxt `_, which works with Pandoc 1.12 or later (but also requires Python_ 2.7). 1. Download the `current release - `_. + `_. 2. Unpack it. 3. Copy the whole directory to the ``filters`` subdirectory of your Pandoc data directory. @@ -39,9 +39,9 @@ If you are using a Unix-ish operating system, you can do all of the above by:: sed -n 's/^Default user data directory: //p') mkdir -p "${PANDOC_DATA_DIR:?}/filters" cd "${PANDOC_DATA_DIR:?}/filters" - curl https://codeload.github.com/odkr/pandoc-zotxt.lua/tar.gz/v0.2.2 | + curl https://codeload.github.com/odkr/pandoc-zotxt.lua/tar.gz/v0.2.3 | tar -xz - sudo cp pandoc-zotxt.lua-0.2.2/man/pandoc-zotxt.lua.1 \ + sudo cp pandoc-zotxt.lua-0.2.3/man/pandoc-zotxt.lua.1 \ /usr/local/share/man/man1 diff --git a/pandoc-zotxt.lua b/pandoc-zotxt.lua index b80cb7a..60356ea 100644 --- a/pandoc-zotxt.lua +++ b/pandoc-zotxt.lua @@ -1,7 +1,6 @@ -#!/usr/local/bin/lua --- pandoc-zotxt.lua Looks up citations in Zotero and adds references. -- --- @release 0.2.2 +-- @release 0.2.3 -- @author Odin Kroeger -- @copyright 2018 Odin Kroeger -- @@ -38,11 +37,23 @@ local ZOTXT_KEYTYPES = {'easykey', 'betterbibtexkey', 'key'} -- Boilerplate -- =========== +local pairs = pairs +local ipairs = ipairs +local tostring = tostring +local type = type + local package = package +local string = string +local math = math +local floor = math.floor +local concat = table.concat +local insert = table.insert +local remove = table.remove + local text = require 'text' do - local s_dir = string.match(PANDOC_SCRIPT_FILE, '(.-)[\\/][^\\/]-$') or '.' + local s_dir = PANDOC_SCRIPT_FILE:match('(.-)[\\/][^\\/]-$') or '.' local path_sep = package.config:sub(1, 1) local lua_vers = {} for _, v in ipairs({_VERSION:sub(5, 7), '5.3'}) do lua_vers[v] = true end @@ -70,30 +81,34 @@ end do local keytypes = ZOTXT_KEYTYPES local fetch = pandoc.mediabag.fetch + local concat = concat + local insert = insert + local remove = remove --- Gets bibliographic data from Zotero. -- -- Tries to get bibliographic data by citation key, trying different - -- types of citation keys, starting with the last keytype using which + -- types of citation keys, starting with the last type for which -- a lookup was successful. -- -- The constant ``ZOTXT_QUERY_URL`` defines where to get data from. -- The constant ``ZOTXT_KEYTYPES`` defines what keytypes to try. -- See for details. -- + -- @tparam string key The lookup key. + -- -- @return If the cited source was found, bibliographic data for - -- that source as CSL JSON string. - -- @return Otherwise, nil and the error message of the lookup - -- attempt for the first keytype. - function get_source_json (citekey) + -- that source as CSL JSON string. Otherwise, nil and the error + -- message of the lookup attempt for the first keytype. + function get_source_json (key) local _, reply for i = 1, #keytypes do - local query_url = ZOTXT_QUERY_URL .. keytypes[i] .. '=' .. citekey + local query_url = concat({ZOTXT_QUERY_URL, keytypes[i], '=', key}) _, reply = fetch(query_url, '.') if reply:sub(1, 1) == '[' then if i > 1 then - local keytype = table.remove(keytypes, i) - table.insert(keytypes, 1, keytype) + local keytype = remove(keytypes, i) + insert(keytypes, 1, keytype) end return reply end @@ -112,14 +127,14 @@ end -- @parem data Data of any type. -- -- @return The given data, with all numbers converted into strings. -function stringify_values (data) +function stringify (data) local data_type = type(data) if data_type == 'table' then local s = {} - for k, v in pairs(data) do s[k] = stringify_values(v) end + for k, v in pairs(data) do s[k] = stringify(v) end return s elseif data_type == 'number' then - return tostring(math.floor(data)) + return tostring(floor(data)) else return data end @@ -128,22 +143,25 @@ end --- Retrieves bibliographic data for sources from Zotero. -- --- @param citekeys A list of citation keys. +-- @tparam {string,...} keys A list of keys. -- --- @return The cited sources, --- as a list of CSL compliant multi-dimensional tables. +-- @treturn {table,...} The cited sources, in CSL data format. -- -- Prints error messages to STDERR if a source cannot be found. function get_sources (citekeys) + local decode = json.decode + local insert = insert + local get_source_json = get_source_json + local stringify = stringify local sources = {} for _, citekey in ipairs(citekeys) do local data, err = get_source_json(citekey) if data == nil then io.stderr:write('pandoc-zotxt.lua: ' .. err .. '\n') else - local source = stringify_values(json.decode(data)[1]) + local source = stringify(decode(data)[1]) source.id = citekey - table.insert(sources, source) + insert(sources, source) end end return sources @@ -151,6 +169,8 @@ end do + local insert = insert + local ipairs = ipairs local citekeys = {} local seen = {} @@ -161,11 +181,12 @@ do -- -- @param citations A pandoc.Cite element. function collect_sources (citations) - for _, citation in ipairs(citations.citations) do - id = citation.id + local c = citations.citations + for i = 1, #c do + id = c[i].id if seen[id] == nil then seen[id] = true - table.insert(citekeys, id) + insert(citekeys, id) end end end