Skip to content

Commit

Permalink
Removed she-bang line.
Browse files Browse the repository at this point in the history
  • Loading branch information
odkr committed Dec 21, 2018
1 parent beaf24d commit 0b80152
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ try `pandoc-zotxt <https://github.com/egh/zotxt>`_,
which works with Pandoc 1.12 or later (but also requires Python_ 2.7).

1. Download the `current release
<https://codeload.github.com/odkr/pandoc-zotxt/tar.gz/v0.2.2>`_.
<https://codeload.github.com/odkr/pandoc-zotxt/tar.gz/v0.2.3>`_.
2. Unpack it.
3. Copy the whole directory to the ``filters``
subdirectory of your Pandoc data directory.
Expand All @@ -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


Expand Down
65 changes: 43 additions & 22 deletions pandoc-zotxt.lua
Original file line number Diff line number Diff line change
@@ -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
--
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <https://github.com/egh/zotxt> 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
Expand All @@ -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
Expand All @@ -128,29 +143,34 @@ 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
end


do
local insert = insert
local ipairs = ipairs
local citekeys = {}
local seen = {}

Expand All @@ -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
Expand Down

0 comments on commit 0b80152

Please sign in to comment.