<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FErutuon%2Fauthor_citation</id>
	<title>Module:Sandbox/Erutuon/author citation - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FErutuon%2Fauthor_citation"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Erutuon/author_citation&amp;action=history"/>
	<updated>2026-05-27T10:13:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Sandbox/Erutuon/author_citation&amp;diff=145392&amp;oldid=prev</id>
		<title>imported&gt;Erutuon: adapt getBotanicalAuthorityFromWikiData from Module:Taxon authority to link author name if it has d:Property:P428 (botanist author abbreviation) on Wikidata</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Erutuon/author_citation&amp;diff=145392&amp;oldid=prev"/>
		<updated>2019-07-05T19:15:03Z</updated>

		<summary type="html">&lt;p&gt;adapt getBotanicalAuthorityFromWikiData from &lt;a href=&quot;/research/Module:Taxon_authority&quot; title=&quot;Module:Taxon authority&quot;&gt;Module:Taxon authority&lt;/a&gt; to link author name if it has &lt;a href=&quot;/index.php?title=D:Property:P428&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;D:Property:P428 (page does not exist)&quot;&gt;d:Property:P428&lt;/a&gt; (botanist author abbreviation) on Wikidata&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
local function advance_pos_if_starts_with(str, pattern, pos)&lt;br /&gt;
	local i, j = mw.ustring.find(str, pattern, pos)&lt;br /&gt;
	if i == pos and j &amp;gt;= i then&lt;br /&gt;
		return j + 1&lt;br /&gt;
	else&lt;br /&gt;
		return pos&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function advance_by_prefixes(str, prefixes, pos)&lt;br /&gt;
	for _, prefix in ipairs(prefixes) do&lt;br /&gt;
		pos = advance_pos_if_starts_with(str, prefix, pos)&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	return pos&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- [[d:Property:P428#P1793]]&lt;br /&gt;
-- (&amp;#039;t )?(d&amp;#039;)?(de )?(la )?(van (der )?)?(Ma?c)?(De)?(Di)?\p{Lu}?C?[&amp;#039;\p{Ll}]*([-&amp;#039;. ]*(van )?(y )?(d[ae][nr]?[- ])?(Ma?c)?[\p{Lu}bht]?C?[&amp;#039;\p{Ll}]*)*\.? ?f?\.?&lt;br /&gt;
function p.find_end_of_author_citation(str, i, j)&lt;br /&gt;
	local pos = i or 1&lt;br /&gt;
	pos = advance_by_prefixes(str, {&lt;br /&gt;
		&amp;quot;&amp;#039;t &amp;quot;, &amp;quot;d&amp;#039;&amp;quot;, &amp;quot;de &amp;quot;, &amp;quot;la &amp;quot;, &amp;quot;van der &amp;quot;, &amp;quot;van &amp;quot;, &amp;quot;Ma?c&amp;quot;, &amp;quot;De&amp;quot;, &amp;quot;Di&amp;quot;, &amp;quot;%u?C?[&amp;#039;%l]*&amp;quot;,&lt;br /&gt;
	}, pos)&lt;br /&gt;
	&lt;br /&gt;
	repeat&lt;br /&gt;
		local orig_pos = pos&lt;br /&gt;
		pos = advance_by_prefixes(str, {&lt;br /&gt;
			&amp;quot;Ma?c&amp;quot;, &amp;quot;[%ubht]?C?&amp;quot;, &amp;quot;[&amp;#039;%l]*&amp;quot;, &amp;quot;[-&amp;#039;. ]*&amp;quot;, &amp;quot;d[ae][nr]?[- ]&amp;quot;, &amp;quot;van &amp;quot;, &amp;quot;y &amp;quot;&lt;br /&gt;
		}, pos)&lt;br /&gt;
	until orig_pos == pos&lt;br /&gt;
	&lt;br /&gt;
	pos = advance_pos_if_starts_with(str, &amp;quot;%.? ?f?%.?&amp;quot;, pos)&lt;br /&gt;
	&lt;br /&gt;
	local last_char = mw.ustring.sub(str, pos - 1, pos - 1)&lt;br /&gt;
	if last_char == &amp;quot; &amp;quot; or last_char == &amp;quot;-&amp;quot; or last_char == &amp;quot;&amp;#039;&amp;quot; then&lt;br /&gt;
		pos = pos - 1&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if pos and (not j or pos &amp;lt;= j) then&lt;br /&gt;
		return pos - 1&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Does not attempt to validate form of author citation. Returns successfully if&lt;br /&gt;
-- the citation is composed of valid author citations separated by&lt;br /&gt;
-- single commas, parentheses, or ampersands with optional whitespace around&lt;br /&gt;
-- them.&lt;br /&gt;
function p.transform_author_abbrevs(citation, func)&lt;br /&gt;
	local pos = 1&lt;br /&gt;
	local output = {}&lt;br /&gt;
	local remaining = citation&lt;br /&gt;
	while #remaining &amp;gt; 0 do&lt;br /&gt;
		local orig_pos = pos&lt;br /&gt;
		pos = advance_pos_if_starts_with(remaining, &amp;quot;%s*[(),&amp;amp;]%s*&amp;quot;, pos)&lt;br /&gt;
		if orig_pos == pos and #output &amp;gt; 0 then&lt;br /&gt;
			return nil&lt;br /&gt;
		end&lt;br /&gt;
		local author_end = p.find_end_of_author_citation(remaining, pos)&lt;br /&gt;
		if not author_end then&lt;br /&gt;
			return nil&lt;br /&gt;
		end&lt;br /&gt;
		local author = mw.ustring.sub(remaining, pos, author_end)&lt;br /&gt;
		table.insert(output, mw.ustring.sub(remaining, 1, pos - 1))&lt;br /&gt;
		table.insert(output, func(author) or author)&lt;br /&gt;
		remaining = mw.ustring.sub(remaining, author_end + 1)&lt;br /&gt;
		pos = 1&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if remaining == &amp;quot;&amp;quot; then&lt;br /&gt;
		return table.concat(output)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Currently using IPNI convention of no spaces after initials.&lt;br /&gt;
local author_abbrevs = {&lt;br /&gt;
	[&amp;quot;L.&amp;quot;] = &amp;quot;Carl Linnaeus&amp;quot;,&lt;br /&gt;
	[&amp;quot;Schldl.&amp;quot;] = &amp;quot;Diederich Franz Leonhard von Schlechtendal&amp;quot;,&lt;br /&gt;
    [&amp;quot;Cham.&amp;quot;] = &amp;quot;Adelbert von Chamisso&amp;quot;,&lt;br /&gt;
    [&amp;quot;B.Boivin&amp;quot;] = &amp;quot;Joseph Robert Bernard Boivin&amp;quot;,&lt;br /&gt;
    [&amp;quot;A.J.Eames&amp;quot;] = &amp;quot;Arthur Johnson Eames&amp;quot;,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function p.standardize_abbrev(abbrev)&lt;br /&gt;
	return (abbrev:gsub(&amp;quot;%.%s+&amp;quot;, &amp;quot;.&amp;quot;))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Adapted from getBotanicalAuthorityFromWikiData in [[Module:Taxon authority]].&lt;br /&gt;
function p.get_botanist_author_abbreviation(name)&lt;br /&gt;
	local wikidata_id = mw.wikibase.getEntityIdForTitle(name)&lt;br /&gt;
&lt;br /&gt;
    if not (wikidata_id and mw.wikibase.isValidEntityId(wikidata_id)) then&lt;br /&gt;
    	local title_obj = mw.title.new(name).redirectTarget&lt;br /&gt;
		if title_obj and title_obj.text then&lt;br /&gt;
			wikidata_id = mw.wikibase.getEntityIdForTitle(name)&lt;br /&gt;
		end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
	if wikidata_id and mw.wikibase.isValidEntityId(wikidata_id) then -- valid Wikidata id&lt;br /&gt;
    	local item = mw.wikibase.getEntity(wikidata_id)&lt;br /&gt;
    	local statements = item:getBestStatements(&amp;#039;P428&amp;#039;)[1] -- botanist author abbreviation&lt;br /&gt;
        if statements ~= nil then&lt;br /&gt;
	    	return statements.mainsnak.datavalue.value&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return nil&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.link_author_citation(citation)&lt;br /&gt;
	return p.transform_author_abbrevs(&lt;br /&gt;
		citation,&lt;br /&gt;
		function (author)&lt;br /&gt;
			local full_name = author_abbrevs[p.standardize_abbrev(author)]&lt;br /&gt;
			if full_name then&lt;br /&gt;
				return &amp;quot;[[&amp;quot; .. full_name .. &amp;quot;|&amp;quot; .. author .. &amp;quot;]]&amp;quot;&lt;br /&gt;
			elseif p.get_botanist_author_abbreviation(author) then&lt;br /&gt;
				return &amp;quot;[[&amp;quot; .. author .. &amp;quot;]]&amp;quot;&lt;br /&gt;
			end&lt;br /&gt;
		end)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Erutuon</name></author>
	</entry>
</feed>