Ugrás a tartalomhoz

Modul:Taxobox

Innen: MKOE wiki

A modult a Modul:Taxobox/doc lapon tudod dokumentálni

-- Module:Taxobox
-- Egyszerű, hordozható taxobox renderelő modul

local p = {}
local mw = mw
local html = mw.html
local text = mw.text

-- Trim helper
local function trim(s)
    if not s then return nil end
    return (s:gsub('^%s+', ''):gsub('%s+$', ''))
end

-- Collect args: parent frame args (template) + direct invoke args
local function getArgs(frame)
    local args = {}
    -- parent can be nil in some calling contexts
    local parent = frame:getParent()
    if parent and parent.args then
        for k, v in pairs(parent.args) do
            if v ~= '' then args[k] = v end
        end
    end
    -- override with direct arguments if present
    if frame.args then
        for k, v in pairs(frame.args) do
            if v ~= '' then args[k] = v end
        end
    end
    return args
end

-- Create a table row node (returns an HTML node)
local function makeRowNode(label, value)
    if not value or value == '' then return nil end
    local tr = html.create('tr')
    tr:tag('th'):wikitext(label .. ':')
    tr:tag('td'):wikitext(value)
    return tr
end

-- Build the title string (scientific name + author + common)
local function makeTitle(args)
    local name = args.name or args[1] or args.scientific_name or args.species or args.genus or ''
    local author = args.author or ''
    local italic = args.italic or '1'
    local common = args.common or args.common_name or ''

    name = trim(name) or ''
    if name == '' then return '' end

    if italic == '1' or italic == 'true' then
        name = "''" .. name .. "''"
    end
    if author ~= '' then
        name = name .. ' ' .. author
    end
    if common ~= '' then
        name = name .. ' <small>(' .. common .. ')</small>'
    end
    return name
end

-- Render the whole table and return HTML string
local function renderBox(args)
    local tbl = html.create('table')
    tbl:addClass(args.class or 'taxobox')

    -- header / title
    if args.header ~= '0' and args.header ~= 'false' then
        local caption_tr = html.create('tr')
        caption_tr:tag('th'):attr('colspan', '2'):addClass('taxobox-title'):wikitext(makeTitle(args) or '')
        tbl:node(caption_tr)
    end

    -- rows order
    local rows_order = {
        {label='Kingdom', key='kingdom'},
        {label='Phylum', key='phylum'},
        {label='Class', key='class'},
        {label='Order', key='order'},
        {label='Family', key='family'},
        {label='Genus', key='genus'},
        {label='Species', key='species'},
        {label='Subspecies', key='subspecies'},
        {label='Variety', key='variety'},
        {label='Forma', key='forma'},
        {label='Cultivar', key='cultivar'}
    }

    for _, ro in ipairs(rows_order) do
        local val = args[ro.key] or args[string.lower(ro.key)] or ''
        if val and val ~= '' then
            local rownode = makeRowNode(ro.label, val)
            if rownode then tbl:node(rownode) end
        end
    end

    -- image (optional)
    if args.image and args.image ~= '' then
        local tr = html.create('tr')
        tr:tag('th') -- empty header cell
        tr:tag('td'):wikitext(string.format('[[File:%s|thumb|center|%s]]', args.image, args.imagedesc or ''))
        tbl:node(tr)
    end

    -- notes
    if args.notes and args.notes ~= '' then
        local notesnode = makeRowNode('Notes', args.notes)
        if notesnode then tbl:node(notesnode) end
    end

    return tostring(tbl)
end

-- Generic entrypoint: rank can be used if needed later
function p._main(frame, rank)
    local args = getArgs(frame)
    -- canonicalize param names
    if args.scientific_name and not args.name then args.name = args.scientific_name end
    if args.binomial and not args.species then args.species = args.binomial end

    if not args.italic then args.italic = '1' end

    return renderBox(args)
end

function p.species(frame)
    return p._main(frame, 'species')
end

function p.genus(frame)
    return p._main(frame, 'genus')
end

function p.subspecies(frame)
    return p._main(frame, 'subspecies')
end

function p.variety(frame)
    return p._main(frame, 'variety')
end

function p.forma(frame)
    return p._main(frame, 'forma')
end

function p.cultivar(frame)
    return p._main(frame, 'cultivar')
end

return p