Модуль:Biology

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация
--[[Функции, связанные с таксономией и всем таким прочим, что растёт и ползает]]

local mRedirect = require('Module:Redirect')

local p = {}

local function getTT(title)
	local page = mw.title.new(title,'Template')
	if page and page.exists and page.isRedirect then
		return mw.title.new(mRedirect.getTarget(page))
	else
		return page
	end
end

local function ParseTT(tt)-- Обрабатывает шаблон таксона без вызова парсера, tt — объект mw.title
	local TaxInfo={'parent','latin','name','rang','monoaut','ext'}
    local t,r=tt:getContent(),{}
    for _,v in ipairs(TaxInfo) do 
    	r[v] = t:match('|%s*'..v..'%s*=%s*([^|]-)%s*[|}]') 
    end
    r['ref'] = t:match('|%s*ref%s*=%s*([^}]-)%s*}') 
    return r
end

local function Rang(r)-- ≈ Шаблон:Rang
    return ("|Надцарство|Домен|Царство|Тип|Отдел|Класс|Отряд|Порядок|Семейство|Род|Вид|"):find('|'..r..'|',1,1) and 1 or nil
end

function p.taxtable(f)--человеческая версия Шаблон:TaxRecursion и его окружения в Шаблон:Таксо-карточка
    local res, a = {}, f:getParent().args;-- с ключом u рассматривает параметры вызвавшего шаблона
    local hir,nolast,topt=nil,1,getTT(a['latin']);
    -- Защиты от зацикливания нет, будет выдавать красную ошибку "время кончилось", но это редко
    if not ( topt and topt.exists ) then
        topt = getTT(a['parent'])
        if topt and topt.exists then
        	nolast=nil
        else
            return '<div style="background-color:Gainsboro;font-size:85%;color:Dimgray">СООБЩЕНИЕ ОБ ОШИБКЕ<br />'..
            ( (a['parent'] or a['latin']) and string.format(
[=[Нет [[Шаблон:Таксон/doc#Таксономические шаблоны|таксономического шаблона]] '''{{[[T:%s|%s]]}}''', который должен описывать положение [[таксон]]а ''[[%s]]'' в [[Биологическая классификация#Наименование и описание таксонов|биологической классификации]].]=],
              a['parent'] or a['latin'], a['parent'] or a['latin'], a['parent'] or a['latin'])
              or "<br />Параметры '''<big>latin</big>''' и '''<big>parent</big>''' пусты."
            )..( (mw.title.getCurrentTitle().namespace==0) and '</div>[[Category:Википедия:Биологические статьи без таксошаблона]]' or '</div>' )
        end
    end-- выбор верхнего таксошаблона topt
    --TaxRecursion + FirstRang
    local parent, q = nolast and a.latin or a.parent
    while parent ~= nil do
    	topt = getTT(parent)
        if topt and topt.exists then 
        	q = ParseTT(topt)
        else
        	break
        end
        if q['ext'] and q['ext'] ~= '' then
        	q['Вымер'] = q['ext']
        end
        table.insert(res,1,f:expandTemplate{title='TaxString',args=q})
        mw.log('Loaded Template:' .. topt.text)
        parent = q.parent
        if Rang(q.rang) == nil then 
        	hir = 1
        end
    end
    return ( hir and
        '<div class="NavHead nomobile" style="background-color: transparent; border:0"><small>промежуточные ранги</small></div>\n'
        or '' ) .. table.concat(res,"\n") .. f:expandTemplate{title=nolast and 'TaxString' or 'LastRang', args=a}
end

return p