模块:LatinNameFormatter

维基百科,自由的百科全书
跳转至: 导航搜索
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]
  • []
  • Foobar
  • Foobar
  • Homo sapiens
  • Homo sapiens
  • E. coli
  • E. coli
  • Acanthocalycium klimpelianum var. macranthum
  • Acanthocalycium klimpelianum var. macranthum
  • Pinus nigra subsp. salzmannii var. corsicana
  • Pinus sylvestris L.
  • Pinus sylvestris L.
  • Pinus koraiensis Siebold & Zucc.
  • Sequoia sempervirens (D. Don) Endl., Syn. Conif. 198 (1847)
  • Branta albifrons Scopoli, 1769, Annus I Hist.-Nat. 69
  • Balaena mysticetus Linnaeus, 1758
local p = {}
local terms = {
	['subsp.'] = true,
	['ssp.'] = true,
	['var.'] = true,
	['subvar.'] = true,
	['f.'] = true,
	['subf.'] = true,
}

local function unformat( name )
	name = mw.ustring.gsub( name, "'", '' )
	name = mw.ustring.gsub( name, '<.->', '' )
	return name
end

local function split( name )
	local words = {}
	local extra = {}
	for piece in mw.ustring.gmatch( name, '%S+') do
		if #extra > 0 or (
			#words > 0 and
			not mw.ustring.match( mw.ustring.sub( piece, 1, 1 ), '[a-z]' )
		) then
			table.insert( extra, piece )
		else
			table.insert( words, piece )
		end
	end
	return words, extra
end

local function joinWords( words )
	modifiedWords = {}
	for i = 1, #words do
		local word = words[i]
		if not terms[word] then
			word = '<i>' .. word .. '</i>'
		end
		table.insert( modifiedWords, word )
	end
	return table.concat( modifiedWords, ' ' )
end

local function format( name )
	local words, extra = split( name )
	name = joinWords( words )
	if #extra > 0 then
		name = name .. ' ' .. table.concat( extra, ' ' )
	end
	return name
end

function p.format( frame )
	return format( unformat( frame.args.name ) )
end

return p