Ugrás a tartalomhoz

Modul:Galeria

Innen: MKOE wiki
A lap korábbi változatát látod, amilyen Dr. Gyúró Zoltán (vitalap | szerkesztései) 2026. június 16., 05:37-kor történt szerkesztése után volt.

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

local p = {}

-- Letisztítja az összes láthatatlan karaktert és újsort a szöveg elejéről/végéről
local function megtisztitString(s)
    if not s then return "" end
    return s:match("^%s*(.-)%s*$")
end

-- Képtípus meghatározása és tisztítása
local function detektalKepTipus(nyers_bemenet)
    local tiszta_bemenet = megtisztitString(nyers_bemenet)
    if tiszta_bemenet == "" then return nil end

    local kep_resz = tiszta_bemenet
    local felirat = nil

    -- Megkeressük a '|' karaktert, ha esetleg mégis átjönne a stringben
    local pipe_pos = tiszta_bemenet:find("|")
    if pipe_pos then
        kep_resz = megtisztitString(tiszta_bemenet:sub(1, pipe_pos - 1))
        felirat = megtisztitString(tiszta_bemenet:sub(pipe_pos + 1))
    end

    if felirat == "" then felirat = nil end

    -- Ha gstatic vagy google link, de lemaradt a protokoll, pótoljuk
    if (kep_resz:find("^encrypted%-tbn") or kep_resz:find("^lh%d+%.google")) and not kep_resz:find("^https?://") then
        kep_resz = "https://" .. kep_resz
    end

    -- 1. Internetes link vizsgálata
    if kep_resz:find("^https?://") then
        return { tipus = "URL", path = kep_resz, felirat = felirat }
    end

    -- 2. Wikimedia Commons átalakítása közvetlen külső URL-lé
    if kep_resz:find("^[Cc]ommons:") or kep_resz:find("^[Cc]:") then
        local fajlnev = kep_resz:gsub("^[Cc]ommons:", ""):gsub("^[Cc]:", "")
                               :gsub("^[Ff]ájl:", ""):gsub("^[Ff]ile:", "")
                               :match("^%s*(.-)%s*$")

        local tiszta_fajlnev = fajlnev:gsub(" ", "_")
        local commons_url = "https://commons.wikimedia.org/wiki/Special:FilePath/" .. tiszta_fajlnev

        return { tipus = "URL", path = commons_url, felirat = felirat }
    end

    -- 3. Helyi kép vizsgálata
    local helyi_fajl = kep_resz:gsub("^[Ff]ájl:", ""):gsub("^[Ff]ile:", "")
    return { tipus = "HELYI", path = megtisztitString(helyi_fajl), felirat = felirat }
end

-- Segédfüggvény annak eldöntésére, hogy egy paraméter kép-e
local function isKepFormatum(szoveg)
    if not szoveg or szoveg == "" then return false end
    local s = mw.ustring.lower(szoveg)

    return mw.ustring.find(s, '^http')
        or mw.ustring.find(s, 'gstatic%.com')
        or mw.ustring.find(s, 'googleusercontent')
        or mw.ustring.find(s, '^commons:')
        or mw.ustring.find(s, '^c:')
        or mw.ustring.find(s, '%.jpg')
        or mw.ustring.find(s, '%.jpeg')
        or mw.ustring.find(s, '%.png')
        or mw.ustring.find(s, '%.gif')
        or mw.ustring.find(s, '%.webp')
end

-- =============================================================================
-- ÚJ HELPER FÜGGVÉNY: Kinyeri a domain nevet vagy a forrás nevét az URL-ből
-- =============================================================================
local function kinyerForrasNev(url, eredeti_bemenet)
    if not url then return "Külső forrás" end

    -- Ha az eredeti bemenetben benne volt a commons, akkor egyértelmű
    if eredeti_bemenet and (eredeti_bemenet:lower():find("^commons:") or eredeti_bemenet:lower():find("^c:")) then
        return "Wikimedia Commons"
    end

    -- Speciális esetek kezelése a letisztított URL alapján
    if url:find("gstatic%.com") or url:find("googleusercontent") then
        return "Google Cache"
    end
    if url:find("giromagi%.com") then
        return "Giromagi Cactus"
    end

    -- Általános domain kinyerés (levágja a https://-t és a slash utáni részt)
    local domain = url:match("^https?://([^/]+)")
    if domain then
        -- Opcionálisan levághatjuk a 'www.' részt az elejéről a tisztább kinézetért
        return domain:gsub("^www%.", "")
    end

    return "Külső link"
end

function p.generalGaleria(frame)
    local args = {}
    if type(frame) == "table" and type(frame.getParent) == "function" then
        args = frame:getParent().args
        if not args or (not args[1] and not args['k1']) then
            args = frame.args
        end
    else
        args = frame
    end

    -- -------------------------------------------------------------------------
    -- BOMBABIZTOS ELŐFELDOLGOZÁS: Újraépítjük a listát a MediaWiki kavarásai után
    -- -------------------------------------------------------------------------
    local rendezett_args = {}

    -- Először kigyűjtjük a normális számozott paramétereket, hogy megőrizzük a wikitext sorrendet
    local max_index = 0
    for k, v in pairs(args) do
        if type(k) == "number" and k > max_index then max_index = k end
    end

    for i = 1, max_index do
        if args[i] then
            table.insert(rendezett_args, megtisztitString(args[i]))
        end
    end

    -- Most megkeressük azokat a linkeket, amiket a MediaWiki szöveges kulccsá alakított az "=" jel miatt
    for k, v in pairs(args) do
        if type(k) == "string" then
            local tiszta_kulcs = megtisztitString(k)
            -- Ha a kulcs maga egy elbaltázott Google URL szilánk
            if tiszta_kulcs:find("^https?://") or tiszta_kulcs:find("^encrypted%-tbn") or tiszta_kulcs:find("gstatic%.com") then
                local teljes_url = tiszta_kulcs .. "=" .. megtisztitString(v)

                -- OKOSSÁG: Megkeressük, hova tartozik a leírása. A MediaWiki a leírást
                -- közvetlenül a lyukas pozícióra teszi. Beillesztjük az URL-t pont a leírása ELÉ.
                local beszurtuk = false
                for r_idx, r_val in ipairs(rendezett_args) do
                    if r_val == "Élőhelyi fotó Mexikóból" or (not isKepFormatum(r_val) and r_idx == 2) then
                        table.insert(rendezett_args, r_idx, teljes_url)
                        beszurtuk = true
                        break
                    end
                end
                -- Ha nem találtuk meg a felirat alapján a helyét, simán beszúrjuk a lista elejére/megfelelő helyre
                if not beszurtuk then
                    table.insert(rendezett_args, 2, teljes_url) -- Alapértelmezetten a 2. helyre szúrjuk (a példád alapján)
                end
            end
        end
    end

    -- -------------------------------------------------------------------------
    -- FŐ RENDERELŐ CIKLUS (Most már a tiszta, helyreállított listából dolgozunk)
    -- -------------------------------------------------------------------------
    local html = {}
    table.insert(html, '<div class="egyedi-galeria-container">')

    local i = 1
    while rendezett_args[i] do
        local v = rendezett_args[i]

        if v ~= "" and isKepFormatum(v) then
            local kepAdat = detektalKepTipus(v)

            if kepAdat then
                -- Opcionális felirat detektálása a rendezett listából
                if rendezett_args[i+1] then
                    local kovetkezo = rendezett_args[i+1]
                    if kovetkezo ~= "" and not isKepFormatum(kovetkezo) then
                        kepAdat.felirat = kovetkezo
                        i = i + 1 -- Felirat átugrása a következő körben
                    end
                end

                local tartalom = ""
                local tiszta_felirat = kepAdat.felirat or ""

                if kepAdat.tipus == "URL" then
                    -- Elrejtjük a kettőspontot a preprocess elől
                    local biztonsagos_path = kepAdat.path:gsub(":", "&#58;")

                    -- --- ÚJ FORRÁS JELZŐ LOGIKA ---
                    -- Kiderítjük a forrás nevét (átadjuk az args[i]-t is, hogy lássa ha Commons volt)
                    local forras_nev = kinyerForrasNev(kepAdat.path, rendezett_args[i])
                    local forras_link = string.format('<br/><span class="galeria-forras-jelzo" style="font-size:0.85em; color:#72777d; display:block; margin-top:4px;">Forrás: <a href="%s" target="_blank" rel="noopener noreferrer" style="color:#3366cc; text-decoration:underline;">%s</a> ↗</span>', biztonsagos_path, forras_nev)

                    -- Hozzáfűzzük a forrás linket a felirathoz, így a LightGallery is megkapja automatikusan
                    local vegleges_felirat = tiszta_felirat .. forras_link
                    -- ------------------------------

                    tartalom = string.format('<div class="kulso-url-doboz" data-src="%s"><span class="url-link-szoveg">Külső kép:<br/>[Link]</span><div class="galeria-felirat">%s</div></div>', biztonsagos_path, vegleges_felirat)
                else -- HELYI
                    if kepAdat.felirat and kepAdat.felirat ~= "" then
                        tartalom = string.format('[[Fájl:%s|thumb|180px|%s]]', kepAdat.path, tiszta_felirat)
                    else
                        tartalom = string.format('[[Fájl:%s|thumb|180px]]', kepAdat.path)
                    end
                end

                table.insert(html, '  <div class="galeria-elem">')
                table.insert(html, '    ' .. tartalom)
                table.insert(html, '  </div>')
            end
        end

        i = i + 1
    end

    table.insert(html, '</div>')

    local vegleges_html = table.concat(html, "\n")
    return frame:preprocess(vegleges_html)
end

return p