Module:BiologyList

提供: VANGUARD FLIGHT wiki
2014年3月30日 (日) 23:14時点におけるTaku.oshino (トーク | 投稿記録)による版 (1版)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索
Module:BiologyList (talk<dot-separator>edit<dot-separator>hist<dot-separator>links<dot-separator>doc<dot-separator>subpages<dot-separator>tests / results<dot-separator>sandbox<dot-separator>all modules)



Usage
This module is used by {{Subspecies}}, {{Species}}, {{Species2}}, {{Genera}}, {{Genera2}}, {{Minerals}}, {{Taxa}}, {{SimpleTaxa}}
How to improve and test this module
  1. developp your modification in Module:BiologyList/sandbox, the sandbox of this module
    which is used in the sandbox templates {{Subspecies/sandbox}}, {{Species/sandbox}}, {{Species2/sandbox}}, {{Genera/sandbox}}, {{Genera2/sandbox}}, {{Taxa/sandbox}}, {{SimpleTaxa/sandbox}}
  2. verify your changes in {{Subspecies/testcases}}, {{Species/testcases}}, {{Species2/testcases}}, {{Genera/testcases}}, {{Genera2/testcases}}, {{Taxa/testcases}}, {{SimpleTaxa/testcases}}
  3. verify your changes in Module_talk:BiologyList/sandbox/testcases
  4. if needed improve the testcases Module:BiologyList/sandbox/testcases
  5. report your modifications in Module:BiologyList
  6. verify again your changes in {{Subspecies/testcases}}, {{Species/testcases}}, {{Species2/testcases}}, {{Genera/testcases}}, {{Genera2/testcases}}, {{Taxa/testcases}}, {{SimpleTaxa/testcases}}
  7. verify again your changes in Module_talk:BiologyList/testcases
  8. if needed improve the testcases Module:BiologyList/testcases
Todo
Suppress unused {{Species/}}, {{Genera/}}, {{Genera/category}}, {{Taxa/}}, {{Taxa/category}}, {{Genus species concategory}}

-- global variable which receives debug info if not nil
_debug=Nil
function addDebug(functionName,text)
	if not _debug then
		return
	end
	_debug = _debug .. functionName .. ': ' .. mw.text.nowiki(text) .. '<BR/>'
end

-- stringIsNilOrEmpty() returns
-- * true for Nil or ''
-- * false otherwise
function stringIsNilOrEmpty(stringValue)
	if not stringValue then
		return true
	end
	if string.len(stringValue) == 0 then
		return true
	end
	return false
end

-- isTrue() transforms a string into a bool
-- Nil, '', '0', 'false', 'no' return false
-- other values return true
function isTrue(stringValue)
	if stringIsNilOrEmpty(stringValue) then
		return false
	end
	stringValue = string.lower(stringValue)
	if stringValue == '0' or stringValue == 'false' or stringValue == 'no' then
		return false
	end
	return true
end

-- formatList() is called by Template:Genera
function formatList(options)
	if not options then
		return "options is nul"
	end
	local italic = isTrue(options['italic'])

	local extinctCross = '†'
	if italic then
		extinctCross = "</i>†<i>"
	end
	
	local link = options['link']
	if not link then
		link = 'true'
	end
	link = isTrue(link)
	
	local nameprefix = options['nameprefix']
	if link then
		local linkPrefix = '[[:Category:'
		if options['linkto'] and options['linkto'] == 'gallery' then
			linkPrefix = '[['
		end
		if stringIsNilOrEmpty(nameprefix) then
			nameprefix = linkPrefix
		else
			nameprefix = linkPrefix .. nameprefix .. ' '
		end
	else
		nameprefix = Nil
	end
	
	local displayprefix = options['displayprefix']
	if stringIsNilOrEmpty(displayprefix) then
		displayprefix = ''
	else
		displayprefix = displayprefix .. ' '
	end
	if link then
		displayprefix = '|' .. displayprefix
	else
		--displayprefix = displayprefix
	end

	local formatedList = Nil
	for index = 1, 1602 do
		local indexStr = tostring(index)
		local name = options[indexStr]
		if name and string.len(name) > 0 then
			-- Manage extinct sign
			local namee = options['†' .. indexStr]
			local extinct = false
			if not stringIsNilOrEmpty(namee) then
				extinct = true
			elseif string.find(name,'†',1,true) then
				extinct = true
				name = string.gsub(name, '†', '', 5)
			end

			-- Manage disamb
			local displayName = name
			local parenthesisStart = string.find(displayName,' (',1,true)
   	 		if parenthesisStart then
				local parenthesisEnd = string.find(displayName,')',parenthesisStart,true)
				if parenthesisEnd then
					displayName = string.sub(displayName,1,parenthesisStart-1) .. '<small>' .. string.sub(displayName,parenthesisStart,parenthesisEnd) .. '</small>' .. string.sub(displayName,parenthesisEnd+1)
				end
			end
			local named = options['d' .. indexStr]
			if not stringIsNilOrEmpty(named) then
				name = name .. ' ' .. named
				displayName = displayName .. ' <small>' .. named .. '</small>'
			end

			if formatedList then
				formatedList = formatedList .. ', '
			else
				formatedList = ''
			end
			if extinct then
				formatedList = formatedList .. extinctCross
			end
			if link then
				formatedList = formatedList .. nameprefix .. name .. displayprefix .. displayName .. ']]'
			else
				formatedList = formatedList .. displayprefix .. displayName
			end
		else
			return formatedList
		end
	end
	return formatedList
end

-- suppressNothospeciesX() is used Template:Kew_list to transform "Cattleya × ballantiniana" in "Cattleya ballantiniana"
function suppressNothospeciesX(text)
	if not text then
		return ''
	end
	text = string.gsub(text, '×', ' ', 5)
	text = string.gsub(text, '  ', ' ', 5)
	text = string.gsub(text, '  ', ' ', 5)
	text = string.gsub(text, '  ', ' ', 5)
	return mw.text.trim(text)
end

local p = {}

-- public version of  formatList
function p.formatList(frame)
	local formatedList = formatList(frame.args)
	if formatedList then
		local italic = isTrue(frame.args['italic'])
		if italic then
			formatedList = "<i>" .. formatedList .. "</i>"
		end
	else
		formatedList = ''
	end
	-- Now add debug traces if activated
	if _debug and string.len(_debug) > 0 then
		formatedList = formatedList .. '\n<BR/>Debug:<BR/>' .. _debug
	end
	return formatedList
end

-- public version of suppressNothospeciesX()
function p.suppressNothospeciesX(frame)
	return suppressNothospeciesX(frame.args['1'])
end

function p.stringIsNilOrEmpty(frame)
	-- testcase (return string when normal function returns boolean)
	return tostring(not not stringIsNilOrEmpty(frame.args['1']))
end

function p.isTrue(frame)
	-- testcase (return string when normal function returns boolean)
	return tostring(not not isTrue(frame.args['1']))
end

function p.formatListForTestcase(frame)
	-- testcase (return nowiki string when normal function returns wiki syntax)
	return mw.text.nowiki(p.formatList(frame))
end

return p