Ugrás a tartalomhoz

Modul:SimpleTaxobox

Innen: MKOE wiki
A lap korábbi változatát látod, amilyen Dr. Gyúró Zoltán (vitalap | szerkesztései) 2025. december 28., 11:11-kor történt szerkesztése után volt.

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

-- Modul:SimpleTaxobox (Javított verzió)
local p = {}

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

-- Segédfüggvény: Kinyeri a nemzetség nevét a fajnévből (pl. "Echinocactus grusonii" -> "Echinocactus")
local function extract_genus(full_species_name)
    -- Kinyeri az első szót (szóközig)
    local genus_name = full_species_name:match("([^%s]+)") 
    return genus_name
end

function p.buildTaxobox(frame)
    local args = frame:getParent().args
    
    -- A taxon neve, amit a cikkből kaptunk (ez lehet faj vagy nemzetség)
    local initialTaxon = args.taxon
    -- Javítás: biztosítsuk, hogy az image ne legyen nil, ha nincs megadva
    local taxonImage = args.image or ''
    
    local hierarchy = {}
    local taxon = initialTaxon -- Ez a változó lépked felfelé a hierarchiában
    local parentOverride = args.parent -- A manuálisan átadott szülő (pl. Szekció)

    if not initialTaxon or initialTaxon == '' then
        return '<div style="color:red;">Taxobox Hiba: Hiányzó <code>|taxon=</code> paraméter.</div>'
    end
    
    -- 1. Rekurzív adatgyűjtés a Modul:TaxonData-ból
    while taxon do
        local data = TaxonData.getData(taxon) 
        
        if data then
            -- 1a. Talált adatot (Rend, Család, Nemzetség, Szekció, stb.)
            table.insert(hierarchy, { rank = data.rank, name = taxon })
            taxon = data.parent -- Áttérés a szülőre
        
        else
            -- 1b. Nem talált adatot. Ez a pont csak a legelső elemre lehet a Faj!
            if taxon == initialTaxon then
				-- Megjegyzés: Ellenőrizzük, van-e szóköz a taxon nevében. 
                local is_species = initialTaxon:find("%s")
                
                if is_species then
                    -- Az eredeti logika fut: Ez egy faj (két szó).
                    local actual_parent
                    -- 1. Ellenőrizzük, van-e átadott szülő (parentOverride)
                    if parentOverride and parentOverride ~= '' then
                        actual_parent = parentOverride
                    else
                        -- 2. Különben kinyerjük a nemzetséget a fajnévből (automatikusan)
                        actual_parent = extract_genus(initialTaxon)
                    end
                    
                    -- Hozzáadjuk magát a fajt a hierarchiához
                    table.insert(hierarchy, { rank = "Faj", name = initialTaxon })
                    
                    -- Áttérünk a Nemzetségre VAGY a Szekcióra
                    taxon = actual_parent
                else
                    -- Ez egy Nemzetség VAGY magasabb taxon (egy szó), de nincs adata.
                    -- Ez hibás TaxonData bejegyzésre utal.
                    return '<div style="color:red;">Taxobox Hiba: A Modul:TaxonData nem tartalmazza a "' .. initialTaxon .. '" taxont, ami egy Nemzetség vagy magasabb szintű taxon. Kérem adja hozzá.</div>'
                end -- is_species vége
            else
                -- Nem a legelső elem, és nincs adat (a lánc megszakad)
                return '<div style="color:red;">Taxobox Hiba: Nem található adat a "' .. taxon .. '" taxonhoz a Modul:TaxonData-ban.</div>'
            end
        end
    end
    
    -- 2. Wikitext Taxobox táblázat felépítése (a régebbi kód, de javított változónevekkel)
    local output = '{| class="infobox biota" style="text-align: left; width: 22em;"\n'
	
	-- A caption kódja: "|+""
    -- output = output .. "|+'''" .. initialTaxon .. "'''\n"
    
    -- A caption helyett th-t használunk (a th kódja: "|-!"):
	output = output .. "|-\n"
	output = output .. '! colspan="2" style="text-align: center; font-size: 125%;" | ' .. initialTaxon .. "\n"
    if taxonImage and taxonImage ~= '' then -- nem egyenlő ~=
        output = output .. '|-\n'
        output = output .. '!colspan="2"|[[Fájl:' .. taxonImage .. '|340px]]\n'
    end
    output = output .. '|-\n'
    output = output .. '!colspan="2"|Rendszertani besorolás\n'
    
    -- A hierarchia fordított sorrendben van. Felülről lefelé haladunk.
    for i = #hierarchy, 1, -1 do
        local item = hierarchy[i]
        
        local name_display = item.name
        
        if item.rank == "Rend" then 
            name_display = "[[:Kategória:" .. name_display .."|".. name_display .. "]]"
        end
        
        if item.rank == "Család" then 
            name_display = "[[:Kategória:" .. name_display .."|".. name_display .. "]]"
        end
        
        if item.rank == "Alcsalád" then 
            name_display = "[[:Kategória:" .. name_display .."|".. name_display .. "]]"
        end
        
        if item.rank == "Nemzetségcsoport" then 
            item.rank = "Nemzetség-<br>csoport"
            name_display = "[[:Kategória:" .. name_display .."|".. name_display .. "]]"
        end
        
        if item.rank == "Alnemzetségcsoport" then 
            item.rank = "Alnemzetség-<br>csoport"
            name_display = "[[:Kategória:" .. name_display .."|".. name_display .. "]]"
        end

        if item.rank == "Nemzetség" then 
            name_display = "''[[:Kategória:" .. name_display .."|".. name_display .. "]]''"
        end

        -- Kiemeljük az aktuális cikk tárgyát (ami a hierarchia legalján van)
        if item.rank == "Faj" then 
            name_display = "''" .. name_display .. "''"
        end

        output = output .. '|-\n'
        output = output .. '| style="text-align: right; line-height: 95%;" | ' .. item.rank .. '\n'
        output = output .. '| style="line-height: 95%;" | ' .. name_display .. '\n'
		
    end

    -- 3. Google és Bing linkek (csak ha a taxon egy Faj)
    -- A hierarchy[1] az az elem, amit legelőször adtunk hozzá (az aktuális taxon)
    if hierarchy[1] and hierarchy[1].rank == "Faj" then 
        local searchTerm = initialTaxon -- Így nem csak egy növényre fog működni
		-- A szóközöket URL-kompatibilis + jellé alakítjuk
		-- local encodedTerm = searchTerm:gsub(" ", "+")
        local encodedTerm = mw.uri.encode(searchTerm) -- Biztonságos kódolás (szóköz -> %20)

        output = output .. '|-\n'
        -- output = output .. '!colspan="2" style="text-align:center;" |'
        output = output .. '!colspan="2" class="taxobox-icons" |'
        -- Itt használhatod a korábban megbeszélt ikonokat is, ha szeretnéd:
        -- output = output .. '[http://images.google.com/search?tbm=isch&q=' .. encodedTerm .. ' Google képek] '
        -- output = output .. '[https://www.bing.com/images/search?q=' .. encodedTerm .. ' Bing képek]\n'
		-- Az ikonok maradnak a span-ek között, de a stílust majd a CSS-ből kapják
		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 .. '|}\n'
    
    return output
end

return p