Ugrás a tartalomhoz

„Modul:Taxonbox” változatai közötti eltérés

Innen: MKOE wiki
Nincs szerkesztési összefoglaló
Nincs szerkesztési összefoglaló
6. sor: 6. sor:


-- Betölti a központi adatmodult, amiben a hierarchia szerepel.
-- Betölti a központi adatmodult, amiben a hierarchia szerepel.
local TaxonData = require('TaxonokData')
local TaxonData = require('Modul:TaxonokData')


-- Segédfüggvény: Kinyeri a nemzetség nevét a fajnévből
-- Segédfüggvény: Kinyeri a nemzetség nevét a fajnévből

A lap 2026. január 30., 23:39-kori változata

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

-- ==========================================================
-- TaxonokRawData.lua > TaxonokRawDataConvert.lua > Taxonbox.lua > Sablon:Taxonbox.wiki
-- ==========================================================

local p = {}

-- Betölti a központi adatmodult, amiben a hierarchia szerepel.
local TaxonData = require('Modul:TaxonokData')

-- Segédfüggvény: Kinyeri a nemzetség nevét a fajnévből
local function extract_genus(full_species_name)
    if not full_species_name then return "" end
    local genus_name = full_species_name:match("([^%s]+)") 
    return genus_name or ""
end

-- Segédfüggvény: Dőlt betűs taxonnév alapformázása (szükséges a clean funkcióhoz is)
local function format_name(name)
    if not name or name == '' then return "" end
    local formattedName = name

    -- Rangjelzők dőltté tétele (a rangjelző sp., var. stb. álló marad)
    if string.match(name, "subsp%.") or string.match(name, "var%.") or string.match(name, "f%.") then
        local minta = "([%a%-]+)%s+([%a%-]+)%s+([%a%.]+)%s+([%a%-]+)"
        local csere = "''%1 %2'' %3 ''%4''"
        formattedName = string.gsub(name, minta, csere)
    elseif string.match(name, "[%a%-]+%s+[%a%-]+") then
        local minta = "([%a%-]+)%s+([%a%-]+)"
        local csere = "''%1 %2''"
        formattedName = string.gsub(name, minta, csere)
    else
        formattedName = "''" .. name .. "''"
    end
    return formattedName
end

-- Segédfüggvény: Teljes formázás rangokkal és sec. megjegyzésekkel (A FEJLÉCHEZ)
local function format_taxon_full(text, rank)
    if not text or text == "" then return "" end

    local main_part = text
    local sec_part = ""

    local lower_text = text:lower()
    local start_index = lower_text:find("sec%.")

    if start_index then
        local syn_index = lower_text:find("syn%.%s+sec%.")
        if syn_index then start_index = syn_index end
        main_part = text:sub(1, start_index - 1):gsub("%s+$", "")
        sec_part = text:sub(start_index)
    end

    local sec_formatted = (sec_part ~= "") and (' <span class="sec">' .. sec_part .. '</span>') or ""

    if not rank or rank == "" then
        local first_word = main_part:match("^([%a%-]+)")
        if first_word then
            if     first_word:match("ales$")   then rank = "Rend"
            elseif first_word:match("aceae$")  then rank = "Család"
            elseif first_word:match("oideae$") then rank = "Alcsalád"
            elseif first_word:match("eae$")    then rank = "Nemzetségcsoport"
            elseif first_word:match("inae$")   then rank = "Alnemzetség-csoport"
            end
        end
    end

    local high_ranks = { ["Rend"]=true, ["Család"]=true, ["Alcsalád"]=true, ["Nemzetségcsoport"]=true, ["Alnemzetség-csoport"]=true }
    if rank and high_ranks[rank] then
        local name, rest = main_part:match("^([%a%-]+)%s*(.*)$")
        return "'''" .. (name or main_part) .. "''' " .. (rest or "") .. sec_formatted
    end

    local low_ranks = { "gen%.", "subgen%.", "sect%.", "subsect%.", "ser%.", "subser%.", "sp%.", "subsp%.", "var%.", "f%." }
    for _, r_pattern in ipairs(low_ranks) do
        local prefix, r_found, epithet, rest = main_part:match("^([%a%-%.%s]+)%s+("..r_pattern..")%s+([%a%-]+)%s*(.*)$")
        if prefix then
            local g, s = prefix:match("^([%a%-]+)%s*([%a%-]*)$")
            local full_prefix = g .. (s ~= "" and (" " .. s) or "")
            return "'''''" .. full_prefix .. "'' " .. r_found .. " ''" .. epithet .. "''''' " .. (rest or "") .. sec_formatted
        end
    end

    local genus, species, rest = main_part:match("^([%a%-]+)%s+([%a%-]+)%s*(.*)$")
    if genus and species and #species > 2 then
        return "'''''" .. genus .. " " .. species .. "''''' " .. (rest or "") .. sec_formatted
    else
        local single_name, rest2 = main_part:match("^([%a%-]+)%s*(.*)$")
        return "'''''" .. (single_name or "") .. "''''' " .. (rest2 or "") .. sec_formatted
    end
end

-- Segédfüggvény: Csak a tiszta, dőlt nevet adja vissza sallangok nélkül (A TÁBLÁZATHOZ)
local function format_taxon_clean(text)
    if not text or text == "" then return "", "" end

    -- 1. Sec eltávolítása és tisztítás
    local raw_clean = text:lower():find("sec%.") and text:sub(1, text:lower():find("sec%.") - 1) or text
    raw_clean = raw_clean:gsub("%s+$", "") 

    local formatted = ""
    local plain = ""

    -- 2. Szerzők/Évszámok levágása
    local ranks = {"subsp%.", "var%.", "f%."}
    local found = false
    
    for _, r in ipairs(ranks) do
        local genus, species, found_rank, epithet = raw_clean:match("^([%a%-]+)%s+([%a%-]+)%s+("..r..")%s+([%a%-]+)")
        if genus then
            formatted = "''" .. genus .. " " .. species .. "'' " .. found_rank:gsub("%%", "") .. " ''" .. epithet .. "''"
            plain = genus .. " " .. species .. " " .. found_rank:gsub("%%", "") .. " " .. epithet
            found = true
            break
        end
    end

    if not found then
        -- Ha sima binomiális név (Genus species)
        local genus, species = raw_clean:match("^([%a%-]+)%s+([%a%-]+)")
        if genus and species then
            formatted = "''" .. genus .. " " .. species .. "''"
            plain = genus .. " " .. species
        else
            -- Ha csak egy szó (magasabb szint)
            local single = raw_clean:match("^([%a%-]+)") or raw_clean
            formatted = "''" .. single .. "''"
            plain = single
        end
    end

    -- Két értékkel térünk vissza: 1. Formázott, 2. Nyers
    return formatted, plain
end

function p.buildTaxonbox(frame)
    local args = frame:getParent().args

    local acceptedName   = (args.accepted ~= '' and args.accepted) or nil
    local synonymName    = (args.synonym ~= '' and args.synonym) or nil
    local unresolvedName = (args.unresolved ~= '' and args.unresolved) or nil
    local hybridName     = (args.hybrid ~= '' and args.hybrid) or nil
    local taxonImage     = args.image or ''
    local imageTitle     = args.imageTitle or ''

    local initialTaxonName = synonymName or unresolvedName or hybridName or acceptedName

    if not initialTaxonName then
        return '<div style="color:red;">Taxobox Hiba: Hiányzó taxon név.</div>'
    end

    local synBoo   = (synonymName ~= nil)
    local unresBoo = (unresolvedName ~= nil)
    local hybBoo   = (hybridName ~= nil)

    local hierarchy = {}
    local taxonName = initialTaxonName
    local parentOverride = args.parent

    while taxonName and taxonName ~= "" and taxonName ~= "nil" do
        local data = TaxonData[taxonName]
        if data then
            table.insert(hierarchy, { rank = data.rank, name = taxonName })
            taxonName = data.parent
        else
            if taxonName == initialTaxonName and taxonName:find("%s") then
                local actual_parent = (parentOverride ~= '' and parentOverride) or extract_genus(taxonName)
                table.insert(hierarchy, { rank = "Faj", name = taxonName })
                taxonName = actual_parent
            else
                break
            end
        end
    end

    if #hierarchy == 0 then
        return '<div style="color:red;">Taxobox Hiba: Nem található adat a "' .. tostring(initialTaxonName) .. '" taxonhoz.</div>'
    end

    local tipus, szoveg, megjelenitendoNev
    -- Itt kapja meg a két értéket
    local clean_formatted_acceptedName, clean_plain_acceptedName = format_taxon_clean(acceptedName)
    if synBoo then
        tipus = "syn"
        megjelenitendoNev = synonymName
        szoveg = "Ez egy taxonómiai szinonima név.<br>Az érvényes leírást lásd itt:<br>[[" .. clean_plain_acceptedName .. "|" .. clean_formatted_acceptedName .. "]]"
    elseif unresBoo then
        tipus = "unres"
        megjelenitendoNev = unresolvedName
        szoveg = "A név státusza bizonytalan (unresolved)."
    elseif hybBoo then
        tipus = "hyb"
        megjelenitendoNev = hybridName
        szoveg = "Ez egy hibrid taxon."
    else
        tipus = "acc"
        megjelenitendoNev = acceptedName
        szoveg = "Érvényes, elfogadott név."
    end

    -- Itt kapja meg a két értéket
    local clean_formatted_megjelenitendoNev, clean_plain_megjelenitendoNev = format_taxon_clean(megjelenitendoNev)

    local output = '{| class="infobox biota ' .. tipus .. '" style="text-align: left; width: 22em;"\n'
    output = output .. "|-\n"
    output = output .. '! class="taxonnev" colspan="2" | ' .. clean_formatted_megjelenitendoNev .. "\n"
    
    output = output .. "|-\n"
    local statusImg = (synBoo and "syn.svg") or (unresBoo and "unres.svg") or (hybBoo and "hyb.svg") or "acc.svg"
    output = output .. '! class="suha-svg" colspan="2"|[[Fájl:' .. statusImg .. '|x160px|link=]]\n'

    output = output .. "|-\n"
    output = output .. '! colspan="2" | ' .. szoveg .. "\n"

    if taxonImage ~= '' then
        output = output .. '|-\n'
        output = output .. '! class="suha-svg" colspan="2"|[[Fájl:' .. taxonImage .. '|340px|' .. imageTitle .. ']]\n'
    end

    output = output .. '|-\n'
    output = output .. '!colspan="2"|Rendszertani besorolás\n'

    for i = #hierarchy, 1, -1 do
        local item = hierarchy[i]
        local rank_display = item.rank

        -- Itt kapja meg a két értéket
        local clean_formatted, clean_plain = format_taxon_clean(item.name)

        local name_display = ""

        -- Kategória hivatkozás tisztított névvel
        if item.rank == "Faj" then 
            name_display = clean_formatted
        else
            -- Megkeressük az alapnevet a kategória linkhez (szerzők nélkül)
            local category_target = item.name:match("^([%a%-%.%s]+)") or item.name
            -- A kategória linkhez a plain (formázás mentes) változót használjuk
            name_display = "[[:Kategória:" .. clean_plain .. "|" .. clean_formatted .. "]]"
        end

        if rank_display == "Nemzetségcsoport" or rank_display == "Alnemzetségcsoport" then
            rank_display = rank_display:gsub("csoport", "-<br>csoport")
        end

        output = output .. '|-\n'
        output = output .. '| class = "left" | ' .. rank_display .. '\n'
        output = output .. '| class = "right" | ' .. name_display .. '\n'
    end

    if hierarchy[1] and hierarchy[1].rank == "Faj" then 
        local encodedTerm = mw.uri.encode(initialTaxonName)
        output = output .. '|-\n'
        output = output .. '!colspan="2" class="taxobox-icons" |'
        output = output .. '[[Fájl:Google logo gyz.svg|x30px|link=http://images.google.com/search?tbm=isch&q=' .. encodedTerm .. '|Google képek]] '
        output = output .. '[[Fájl:Bing fluent logo gyz.svg|x30px|link=https://www.bing.com/images/search?q=' .. encodedTerm .. '|Bing képek]]\n'
    end
    
    output = output .. '|}'
    return output
end

function p.format_tudomanyos_nev(frame)
    local args = frame:getParent().args
    local names = {
        { val = args.accepted,   class = "acc" },
        { val = args.synonym,    class = "syn" },
        { val = args.unresolved, class = "unres" },
        { val = args.hybrid,     class = "hyb" }
    }

    local output = '== Tudományos név ==\n'
    for _, n in ipairs(names) do
        if n.val and n.val ~= '' then
            output = output .. '* <span class="' .. n.class .. '">' .. format_taxon_full(n.val) .. '</span>\n'
        end
    end
    return output
end

-- ==========================================================
-- OFFLINE TESZTELŐ SZAKASZ
-- ==========================================================
if arg then
    mw = { uri = { encode = function(s) return s:gsub(" ", "%%20") end } }
    local mock_frame = {
        getParent = function()
            return {
                args = {
                    accepted = arg[1] or "Echinopsis leucantha",
                    synonym = arg[2] or "",
                    image = "test.jpg",
                    imageTitle = "Teszt kép"
                }
            }
        end
    }

    print("\n--- TAXONBOX GENERÁLT WIKITEXT ---")
    local success, result = pcall(p.buildTaxonbox, mock_frame)
    print(success and result or "HIBA: " .. result)

    print("\n--- TUDOMÁNYOS NÉV SZAKASZ ---")
    local success2, result2 = pcall(p.format_tudomanyos_nev, mock_frame)
    print(success2 and result2 or "HIBA: " .. result2)
end

return p