Content deleted Content added
link_author_citation using linkBotanicalAuthorites in Module:Taxon authority |
adapt getBotanicalAuthorityFromWikiData from Module:Taxon authority to link author name if it has d:Property:P428 (botanist author abbreviation) on Wikidata |
||
(5 intermediate revisions by the same user not shown) | |||
Line 34:
pos = advance_pos_if_starts_with(str, "%.? ?f?%.?", pos)
local last_char = mw.ustring.sub(str, pos - 1, pos - 1)
if last_char == " " or last_char == "-" or last_char == "'" then
pos = pos - 1
end
if pos and (not j or pos <= j) then
return pos - 1
end
end
Line 46 ⟶ 51:
function p.transform_author_abbrevs(citation, func)
local pos = 1
local output = {}
local remaining = citation
pos = advance_pos_if_starts_with(citation, "%s*[(),&]%s*", pos)▼
while #remaining > 0 do
local author_end = p.find_end_of_author_citation(citation, pos)▼
local orig_pos = pos
if orig_pos == pos and #output > 0 then
return nil
end
if not author_end then
return nil
end
▲ mw.ustring.sub(citation, pos, author_end),
pos = 1
▲ author = func(author) or author
end
if
return
end
end
-- Currently using IPNI convention of no spaces after initials.
local author_abbrevs = {
["L."] = "Carl Linnaeus",
["Schldl."] = "Diederich Franz Leonhard von Schlechtendal",
["Cham."] = "Adelbert von Chamisso",
["B.Boivin"] = "Joseph Robert Bernard Boivin",
["A.J.Eames"] = "Arthur Johnson Eames",
}
function p.standardize_abbrev(abbrev)
return (abbrev:gsub("%.%s+", "."))
end
-- Adapted from getBotanicalAuthorityFromWikiData in [[Module:Taxon authority]].
function p.get_botanist_author_abbreviation(name)
local wikidata_id = mw.wikibase.getEntityIdForTitle(name)
if not (wikidata_id and mw.wikibase.isValidEntityId(wikidata_id)) then
local title_obj = mw.title.new(name).redirectTarget
if title_obj and title_obj.text then
wikidata_id = mw.wikibase.getEntityIdForTitle(name)
end
end
if wikidata_id and mw.wikibase.isValidEntityId(wikidata_id) then -- valid Wikidata id
local item = mw.wikibase.getEntity(wikidata_id)
local statements = item:getBestStatements('P428')[1] -- botanist author abbreviation
if statements ~= nil then
return statements.mainsnak.datavalue.value
end
end
return nil
end
function p.link_author_citation(citation)
return p.transform_author_abbrevs(
citation,
function (author)
local full_name = author_abbrevs[p.standardize_abbrev(author)]
if full_name then
return "[[" .. full_name .. "|" .. author .. "]]"
elseif p.get_botanist_author_abbreviation(author) then
return "[[" .. author .. "]]"
end
end)
end
|