Skip to content

Commit

Permalink
Fixed error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
odkr committed May 3, 2019
1 parent e3a89b7 commit 01d3d52
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ following commands into a bourne shell:
```sh
(
set -Cefu
NAME=pandoc-zotxt.lua VERS=0.3.11
NAME=pandoc-zotxt.lua VERS=0.3.12
URL="https://github.com/odkr/${NAME:?}/archive/v${VERS:?}.tar.gz"
FILTERS="${HOME:?}/.pandoc/filters"
mkdir -p "${FILTERS:?}"
Expand Down
66 changes: 33 additions & 33 deletions pandoc-zotxt.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- pandoc-zotxt.lua Looks up citations in Zotero and adds references.
--
-- @script pandoc-zotxt.lua
-- @release 0.3.11
-- @release 0.3.12
-- @author Odin Kroeger
-- @copyright 2018, 2019 Odin Kroeger
-- @license MIT
Expand Down Expand Up @@ -36,7 +36,7 @@ local ZOTXT_QUERY_URL = 'http://localhost:23119/zotxt/items?'
local ZOTXT_KEYTYPES = {'easykey', 'betterbibtexkey', 'key'}

-- The version of this script.
local VERSION = '0.3.11'
local VERSION = '0.3.12'


-- Shorthands
Expand Down Expand Up @@ -87,6 +87,16 @@ local encode = json.encode
-- Functions
-- =========

--- Prints warnings to STDERR.
--
-- @tparam string ... Strings to be written to STDERR.
--
-- Prefixes messages with 'pandoc-zotxt.lua: ' and appends a linefeed.
function warn (...)
io.stderr:write('pandoc-zotxt.lua: ', concat({...}), '\n')
end


--- Moves an element to the beginning of a list.
--
-- @tparam list The list.
Expand All @@ -99,13 +109,25 @@ function move_to_front (list, i)
end


--- Prints warnings to STDERR.
--- Converts all numbers in a multi-dimensional table to strings.
--
-- @tparam string ... Strings to be written to STDERR.
-- Also converts floating point numbers to integers.
-- This is needed because in JavaScript, all numbers are
-- floating point numbers. But Pandoc expects integers.
--
-- Prefixes messages with 'pandoc-zotxt.lua: ' and appends a linefeed.
function warn (...)
io.stderr:write('pandoc-zotxt.lua: ', concat({...}), '\n')
-- @param data Data of any type.
-- @return The given data, with all numbers converted into strings.
function numtostr (data)
local data_type = type(data)
if data_type == 'table' then
local s = {}
for k, v in pairs(data) do s[k] = numtostr(v) end
return s
elseif data_type == 'number' then
return tostring(floor(data))
else
return data
end
end


Expand Down Expand Up @@ -141,7 +163,7 @@ end
function read_json_file (fname)
local f, err, errno = open(fname, 'r')
if not f then return nil, err, errno end
local data, err, errno = f:read("a")
local data, err, errno = f:read('a')
if not data then return nil, err, errno end
local ok, err, errno = f:close()
if not ok then return nil, err, errno end
Expand Down Expand Up @@ -172,7 +194,7 @@ function write_json_file (data, fname)
end


--- Retrieves bibliographic data from Zotero.
--- Retrieves bibliographic data from Zotero in JSON.
--
-- Retrieves bibliographic data by citation key, trying different
-- types of citation keys, starting with the last type for which
Expand Down Expand Up @@ -204,29 +226,7 @@ do
end


--- Converts all numbers in a multi-dimensional table to strings.
--
-- Also converts floating point numbers to integers.
-- This is needed because in JavaScript, all numbers are
-- floating point numbers. But Pandoc expects integers.
--
-- @param data Data of any type.
-- @return The given data, with all numbers converted into strings.
function numtostr (data)
local data_type = type(data)
if data_type == 'table' then
local s = {}
for k, v in pairs(data) do s[k] = numtostr(v) end
return s
elseif data_type == 'number' then
return tostring(floor(data))
else
return data
end
end


--- Retrieves bibliographic data for sources from Zotero.
--- Retrieves bibliographic data for a single source from Zotero.
--
-- @tparam string citekey A citation key.
-- @treturn table Bibliographic data for that source in CSL format,
Expand Down Expand Up @@ -362,7 +362,7 @@ function add_sources (meta)
if not is_path_absolute(biblio) then
biblio = get_input_directory() .. PATH_SEP .. biblio
end
local ok, err = pcall(update_bibliography, biblio)
local ok, err = update_bibliography(biblio)
if ok then
if not meta.bibliography then
meta.bibliography = biblio
Expand Down

0 comments on commit 01d3d52

Please sign in to comment.