diff --git a/module_citeq.lua b/module_citeq.lua index 1b8d366..985a482 100644 --- a/module_citeq.lua +++ b/module_citeq.lua @@ -80,8 +80,31 @@ local function accept_value( param_val, wd_val ) return val end +-- Split string of comma-separated first names into table of initials +local function getInitials(str) + local initials = {} + for _, word in pairs(mw.text.split(str, ' ', true)) do + local initial = mw.text.trim(word) + if #initial > 0 then + table.insert(initials, initial:sub(1, 1):upper()) + end + end + return initials +end + +-- Return initials given a style (e.g. Vancouver or APA) +local function getStyledNames(names, style) + if style == 'Vancouver' then + return table.concat(getInitials(names)) + elseif style == 'APA' then + return table.concat(getInitials(names), '. ') .. '.' + else + return mw.text.trim(names:gsub(', ', ' ')) + end +end + -- function to fetch a value to display -local function makelink(v, out, link, maxpos, wdl) +local function makelink(v, out, link, maxpos, wdl, namestyle) local label if v.mainsnak.snaktype == "value" then if v.mainsnak.datatype == "wikibase-item" then @@ -89,11 +112,20 @@ local function makelink(v, out, link, maxpos, wdl) local sitelink = mw.wikibase.getSitelink(qnumber) if qnumber == "Q2818964" then sitelink = nil end -- suppress link to "Various authors" - -- Last name, first name(s) - local last_name = getValue({"P734", qid=qnumber, maxvals=1, ps=2}) - local first_names = getValue({"P735", qid=qnumber, maxvals=0, ps=2}) + -- Last name, first name(s) from "author given names" and "author last names" qualifiers + local last_name + local first_names + if v.qualifiers and v.qualifiers.P9688 and v.qualifiers.P9688[1] then + last_name = v.qualifiers.P9688[1].datavalue.value + end + if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then + first_names = v.qualifiers.P9687[1].datavalue.value + end + -- Otherwise, get last name, first name(s) from author item properties + last_name = last_name or getValue({"P734", qid=qnumber, maxvals=1, ps=2}) + first_names = first_names or getValue({"P735", qid=qnumber, maxvals=0, ps=2}) if first_names then - first_names = mw.text.trim(first_names:gsub(', ', ' ')) + first_names = getStyledNames(first_names, namestyle) end if v.qualifiers and v.qualifiers.P1932 then @@ -162,7 +194,7 @@ local function makelink(v, out, link, maxpos, wdl) last_name = v.qualifiers.P9688[1].datavalue.value end if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then - first_names = mw.text.trim(v.qualifiers.P9687[1].datavalue.value:gsub(', ', ' ')) + first_names = getStyledNames(v.qualifiers.P9687[1].datavalue.value, namestyle) end if last_name and first_names then @@ -189,7 +221,7 @@ local function makelink(v, out, link, maxpos, wdl) last_name = v.qualifiers.P9688[1].datavalue.value end if v.qualifiers and v.qualifiers.P9687 and v.qualifiers.P9687[1] then - first_names = mw.text.trim(v.qualifiers.P9687[1].datavalue.value:gsub(', ', ' ')) + first_names = getStyledNames(v.qualifiers.P9687[1].datavalue.value, namestyle) end if last_name and first_names then label = { @@ -273,7 +305,7 @@ local function get_name_list (nl_type, args, qid, wdl) if props and props[1] then for k, v in pairs(props) do if 'deprecated' ~= v.rank then -- ignore deprecated names - maxpos = makelink(v, out, link, maxpos, wdl) + maxpos = makelink(v, out, link, maxpos, wdl, args.namestyle) end end end @@ -281,7 +313,7 @@ local function get_name_list (nl_type, args, qid, wdl) -- second properties for k, v in pairs(fallback) do if 'deprecated' ~= v.rank then -- ignore deprecated names - maxpos = makelink(v, out, link, maxpos, wdl) + maxpos = makelink(v, out, link, maxpos, wdl, args.namestyle) end end end @@ -506,6 +538,7 @@ local function _cite_q (citeq_args) citeq_args.qid = nil citeq_args.wdl = nil citeq_args.template = nil + citeq_args.namestyle = citeq_args.namestyle or citeq_args['name-style'] or nil -- if title supplied, flag to not read html title local titleforced = (citeq_args.title ~= nil) @@ -960,6 +993,8 @@ local function _cite_q (citeq_args) -- remove any remaining non-CS1 fields citeq_args['instance-of'] = nil + citeq_args.namestyle = nil + citeq_args['name-style'] = nil -- render the template local lc_template = template and template:lower() or "citation"