Ugrás a tartalomhoz

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

Innen: MKOE wiki
Nincs szerkesztési összefoglaló
Nincs szerkesztési összefoglaló
1. sor: 1. sor:
local p = {}
local p = {}


-- Letisztítja az összes láthatatlan karaktert és újsort a szöveg elejéről/végéről
-- =============================================================================
-- SEGÉDFÜGGVÉNYEK
-- =============================================================================
 
-- Eltávolítja a felesleges szóközöket és a láthatatlan sortörés karaktereket
local function megtisztitString(s)
local function megtisztitString(s)
     if not s then return "" end
     if not s then return "" end
     return s:match("^%s*(.-)%s*$")
     s = s:gsub("^%s*(.-)%s*$", "%1")
    s = s:gsub("[\n\r]", "")
    return s
end
end


-- Képtípus meghatározása és tisztítása
-- Felismeri a kép típusát és visszaadja a letisztított elérési utat
local function detektalKepTipus(nyers_bemenet)
local function detektalKepTipus(nyers_bemenet)
     local tiszta_bemenet = megtisztitString(nyers_bemenet)
     if not nyers_bemenet or nyers_bemenet == "" then return nil end
    if tiszta_bemenet == "" then return nil end


     local kep_resz = tiszta_bemenet
     local s = megtisztitString(nyers_bemenet)
    local felirat = nil


     -- Megkeressük a '|' karaktert, ha esetleg mégis átjönne a stringben
     -- 1. Eset: Wikimedia Commons hivatkozás
     local pipe_pos = tiszta_bemenet:find("|")
     if s:lower():find("^commons:") then
    if pipe_pos then
         local tiszta_nev = s:sub(9) -- levágjuk a "commons:" részt
         kep_resz = megtisztitString(tiszta_bemenet:sub(1, pipe_pos - 1))
        -- Ha a "File:" vagy "Fájl:" sincs ott, kiegészítjük
         felirat = megtisztitString(tiszta_bemenet:sub(pipe_pos + 1))
        if not tiszta_nev:lower():find("^file:") and not tiszta_nev:lower():find("^fájl:") then
            tiszta_nev = "File:" .. tiszta_nev
        end
         -- Átalakítjuk közvetlen URL-lé, hogy a LightGallery külső képként kezelhesse
        local url_nev = tiszta_nev:gsub(" ", "_")
        return {
            tipus = "URL",
            path = "https://commons.wikimedia.org/wiki/Special:FilePath/" .. url_nev,
            felirat = nil
        }
     end
     end


    if felirat == "" then felirat = nil end
     -- 2. Eset: Külső HTTP/HTTPS vagy Google Cache URL
 
     if s:find("^https?://") or s:find("^encrypted%-tbn") or s:find("gstatic%.com") then
     -- Ha gstatic vagy google link, de lemaradt a protokoll, pótoljuk
         return {
     if (kep_resz:find("^encrypted%-tbn") or kep_resz:find("^lh%d+%.google")) and not kep_resz:find("^https?://") then
            tipus = "URL",
         kep_resz = "https://" .. kep_resz
            path = s,
            felirat = nil
        }
     end
     end


     -- 1. Internetes link vizsgálata
     -- 3. Eset: Helyi wiki kép
     if kep_resz:find("^https?://") then
     return {
         return { tipus = "URL", path = kep_resz, felirat = felirat }
         tipus = "WIKI",
    end
        path = s,
        felirat = nil
    }
end


    -- 2. Wikimedia Commons átalakítása közvetlen külső URL-
-- Eldönti egy stringről, hogy kép-e vagy pedig felirat
     if kep_resz:find("^[Cc]ommons:") or kep_resz:find("^[Cc]:") then
local function isKepFormatum(szoveg)
        local fajlnev = kep_resz:gsub("^[Cc]ommons:", ""):gsub("^[Cc]:", "")
     if not szoveg or szoveg == "" then return false end
                              :gsub("^[Ff]ájl:", ""):gsub("^[Ff]ile:", "")
    local s = megtisztitString(szoveg):lower()
                              :match("^%s*(.-)%s*$")


        local tiszta_fajlnev = fajlnev:gsub(" ", "_")
    if s:find("^https?://") or s:find("^encrypted%-tbn") or s:find("gstatic%.com") or s:find("^commons:") then
        local commons_url = "https://commons.wikimedia.org/wiki/Special:FilePath/" .. tiszta_fajlnev
        return true
    end


         return { tipus = "URL", path = commons_url, felirat = felirat }
    if s:find("%.jpg$") or s:find("%.jpeg$") or s:find("%.png$") or s:find("%.gif$") or s:find("%.svg$") or s:find("%.webp$") then
         return true
     end
     end


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


-- Segédfüggvény annak eldöntésére, hogy egy paraméter kép-e
-- Kinyeri a domaint a külső URL-ekből a forrás megjelöléséhez
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)
local function kinyerForrasNev(url, eredeti_bemenet)
     if not url then return "Külső forrás" end
     if not url or 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("google%.com") or url:find("gstatic%.com") or url:find("^encrypted%-tbn") then
     if url:find("gstatic%.com") or url:find("googleusercontent") then
         return "Google Cache"
         return "Google Cache"
    end
    if url:find("giromagi%.com") then
        return "Giromagi Cactus"
     end
     end


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


97. sor: 85. sor:
end
end


-- =============================================================================
-- FŐ FUNKCIÓ: GALÉRIA GENERÁLÁSA
-- =============================================================================
function p.generalGaleria(frame)
function p.generalGaleria(frame)
     local args = {}
     local args = {}
109. sor: 100. sor:


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


     -- Először kigyűjtjük a normális számozott paramétereket, hogy megőrizzük a wikitext sorrendet
     -- 1. Lépés: Gyűjtsük ki a tiszta numerikus paramétereket
    local max_index = 0
     for k, v in pairs(args) do
     for k, v in pairs(args) do
         if type(k) == "number" and k > max_index then max_index = k end
         if type(k) == "number" and k > max_index then max_index = k end
     end
     end
     for i = 1, max_index do
     for i = 1, max_index do
         if args[i] then
         if args[i] then
             table.insert(rendezett_args, megtisztitString(args[i]))
             nyers_elemek[i] = megtisztitString(args[i])
         end
         end
     end
     end


     -- Most megkeressük azokat a linkeket, amiket a MediaWiki szöveges kulccsá alakított az "=" jel miatt
     -- 2. Lépés: Vadásszuk le a sérült Google/külső URL kulcsokat
     for k, v in pairs(args) do
     for k, v in pairs(args) do
         if type(k) == "string" then
         if type(k) == "string" then
             local tiszta_kulcs = megtisztitString(k)
             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
             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)
                 local teljes_url = tiszta_kulcs .. "=" .. megtisztitString(v)


                -- OKOSSÁG: Megkeressük, hova tartozik a leírása. A MediaWiki a leírást
                 local beszurt_hely = nil
                -- közvetlenül a lyukas pozícióra teszi. Beillesztjük az URL-t pont a leírása ELÉ.
                 for i = 1, max_index + 5 do
                 local beszurtuk = false
                     if nyers_elemek[i] and not isKepFormatum(nyers_elemek[i]) and nyers_elemek[i] ~= "" then
                 for r_idx, r_val in ipairs(rendezett_args) do
                         table.insert(nyers_elemek, i, teljes_url)
                     if r_val == "Élőhelyi fotó Mexikóból" or (not isKepFormatum(r_val) and r_idx == 2) then
                         beszurt_hely = i
                         table.insert(rendezett_args, r_idx, teljes_url)
                        break
                         beszurtuk = true
                    elseif not nyers_elemek[i] or nyers_elemek[i] == "" then
                        nyers_elemek[i] = teljes_url
                        beszurt_hely = i
                         break
                         break
                     end
                     end
                 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
                 if beszurt_hely and beszurt_hely > max_index then
                     table.insert(rendezett_args, 2, teljes_url) -- Alapértelmezetten a 2. helyre szúrjuk (a példád alapján)
                     max_index = beszurt_hely
                 end
                 end
             end
             end
        end
    end
    -- 3. Lépés: Tömörítés és a rendezett_args véglegesítése
    local rendezett_args = {}
    for i = 1, max_index + 5 do
        if nyers_elemek[i] and nyers_elemek[i] ~= "" then
            table.insert(rendezett_args, nyers_elemek[i])
         end
         end
     end
     end


     -- -------------------------------------------------------------------------
     -- -------------------------------------------------------------------------
     -- FŐ RENDERELŐ CIKLUS (Most már a tiszta, helyreállított listából dolgozunk)
     -- FŐ RENDERELŐ CIKLUS
     -- -------------------------------------------------------------------------
     -- -------------------------------------------------------------------------
     local html = {}
     local html = {}
165. sor: 164. sor:


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


                -- Előre legyártjuk a forrás linket a LightGallery számára, ha külső képről van szó
                local forras_link_html = ""
                 if kepAdat.tipus == "URL" then
                 if kepAdat.tipus == "URL" then
                     local biztonsagos_path = kepAdat.path:gsub(":", ":")
                     local biztonsagos_path = kepAdat.path:gsub(":", ":")
                    local forras_nev = kinyerForrasNev(kepAdat.path, rendezett_args[i])
                    forras_link_html = string.format('<br/><span class="galeria-forras-jelzo" style="font-size:0.85em; color:#bbb; display:block; margin-top:4px;">Forrás: <a href="%s" target="_blank" rel="noopener noreferrer" style="color:#66b2ff; text-decoration:underline;">%s</a> ↗</span>', biztonsagos_path, forras_nev)
                end


                if kepAdat.tipus == "URL" then
                    local forras_nev = ""
                     -- Elrejtjük a kettőspontot a preprocess elől
                     if kepAdat.path:find("commons%.wikimedia%.org") then
                    local biztonsagos_path = kepAdat.path:gsub(":", "&#58;")
                        forras_nev = "Wikimedia Commons"
 
                     else
                    -- Kinyerjük a forrás tiszta nevét (pl. "Giromagi Cactus")
                        forras_nev = kinyerForrasNev(kepAdat.path, rendezett_args[i])
                     local forras_nev = kinyerForrasNev(kepAdat.path, rendezett_args[i])
                    end


                    -- Nem generálunk HTML-t, csak tiszta adat-attribútumként átadjuk a JS-nek!
                     tartalom = string.format('<div class="kulso-url-doboz" data-src="%s" data-forras-nev="%s"><span class="url-link-szoveg">Külső kép:<br/>[Link]</span><div class="galeria-felirat">%s</div></div>',
                     tartalom = string.format('<div class="kulso-url-doboz" data-src="%s" data-forras-nev="%s"><span class="url-link-szoveg">Külső kép:<br/>[Link]</span><div class="galeria-felirat">%s</div></div>',
                                            biztonsagos_path,
                                                biztonsagos_path,
                                            forras_nev,
                                                forras_nev,
                                            tiszta_felirat)
                                                tiszta_felirat)


                 else -- HELYI WIKI KÉP
                 else
                    -- A helyi képeknél a MediaWiki gyári [[Fájl:...]] szintaxisa nem enged trükközést a data-attribútumokkal,
                    -- de szerencsére a helyi képekhez nem is kell külső forráslink!
                     if kepAdat.felirat and kepAdat.felirat ~= "" then
                     if kepAdat.felirat and kepAdat.felirat ~= "" then
                         tartalom = string.format('[[Fájl:%s|thumb|180px|%s]]', kepAdat.path, tiszta_felirat)
                         tartalom = string.format('[[Fájl:%s|thumb|180px|%s]]', kepAdat.path, tiszta_felirat)

A lap 2026. június 17., 07:29-kori változata

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

local p = {}

-- =============================================================================
-- SEGÉDFÜGGVÉNYEK
-- =============================================================================

-- Eltávolítja a felesleges szóközöket és a láthatatlan sortörés karaktereket
local function megtisztitString(s)
    if not s then return "" end
    s = s:gsub("^%s*(.-)%s*$", "%1")
    s = s:gsub("[\n\r]", "")
    return s
end

-- Felismeri a kép típusát és visszaadja a letisztított elérési utat
local function detektalKepTipus(nyers_bemenet)
    if not nyers_bemenet or nyers_bemenet == "" then return nil end

    local s = megtisztitString(nyers_bemenet)

    -- 1. Eset: Wikimedia Commons hivatkozás
    if s:lower():find("^commons:") then
        local tiszta_nev = s:sub(9) -- levágjuk a "commons:" részt
        -- Ha a "File:" vagy "Fájl:" sincs ott, kiegészítjük
        if not tiszta_nev:lower():find("^file:") and not tiszta_nev:lower():find("^fájl:") then
            tiszta_nev = "File:" .. tiszta_nev
        end
        -- Átalakítjuk közvetlen URL-lé, hogy a LightGallery külső képként kezelhesse
        local url_nev = tiszta_nev:gsub(" ", "_")
        return {
            tipus = "URL",
            path = "https://commons.wikimedia.org/wiki/Special:FilePath/" .. url_nev,
            felirat = nil
        }
    end

    -- 2. Eset: Külső HTTP/HTTPS vagy Google Cache URL
    if s:find("^https?://") or s:find("^encrypted%-tbn") or s:find("gstatic%.com") then
        return {
            tipus = "URL",
            path = s,
            felirat = nil
        }
    end

    -- 3. Eset: Helyi wiki kép
    return {
        tipus = "WIKI",
        path = s,
        felirat = nil
    }
end

-- Eldönti egy stringről, hogy kép-e vagy pedig felirat
local function isKepFormatum(szoveg)
    if not szoveg or szoveg == "" then return false end
    local s = megtisztitString(szoveg):lower()

    if s:find("^https?://") or s:find("^encrypted%-tbn") or s:find("gstatic%.com") or s:find("^commons:") then
        return true
    end

    if s:find("%.jpg$") or s:find("%.jpeg$") or s:find("%.png$") or s:find("%.gif$") or s:find("%.svg$") or s:find("%.webp$") then
        return true
    end

    return false
end

-- Kinyeri a domaint a külső URL-ekből a forrás megjelöléséhez
local function kinyerForrasNev(url, eredeti_bemenet)
    if not url or url == "" then return "Külső forrás" end

    if url:find("google%.com") or url:find("gstatic%.com") or url:find("^encrypted%-tbn") then
        return "Google Cache"
    end

    local domain = url:match("^https?://([^/]+)")
    if domain then
        domain = domain:gsub("^www%.", "")
        return domain
    end

    return "Külső link"
end

-- =============================================================================
-- FŐ FUNKCIÓ: GALÉRIA GENERÁLÁSA
-- =============================================================================
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 sorrendhelyesen
    -- -------------------------------------------------------------------------
    local nyers_elemek = {}
    local max_index = 0

    -- 1. Lépés: Gyűjtsük ki a tiszta numerikus paramétereket
    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
            nyers_elemek[i] = megtisztitString(args[i])
        end
    end

    -- 2. Lépés: Vadásszuk le a sérült Google/külső URL kulcsokat
    for k, v in pairs(args) do
        if type(k) == "string" then
            local tiszta_kulcs = megtisztitString(k)
            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)

                local beszurt_hely = nil
                for i = 1, max_index + 5 do
                    if nyers_elemek[i] and not isKepFormatum(nyers_elemek[i]) and nyers_elemek[i] ~= "" then
                        table.insert(nyers_elemek, i, teljes_url)
                        beszurt_hely = i
                        break
                    elseif not nyers_elemek[i] or nyers_elemek[i] == "" then
                        nyers_elemek[i] = teljes_url
                        beszurt_hely = i
                        break
                    end
                end

                if beszurt_hely and beszurt_hely > max_index then
                    max_index = beszurt_hely
                end
            end
        end
    end

    -- 3. Lépés: Tömörítés és a rendezett_args véglegesítése
    local rendezett_args = {}
    for i = 1, max_index + 5 do
        if nyers_elemek[i] and nyers_elemek[i] ~= "" then
            table.insert(rendezett_args, nyers_elemek[i])
        end
    end

    -- -------------------------------------------------------------------------
    -- FŐ RENDERELŐ CIKLUS
    -- -------------------------------------------------------------------------
    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
                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
                    end
                end

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

                if kepAdat.tipus == "URL" then
                    local biztonsagos_path = kepAdat.path:gsub(":", "&#58;")

                    local forras_nev = ""
                    if kepAdat.path:find("commons%.wikimedia%.org") then
                        forras_nev = "Wikimedia Commons"
                    else
                        forras_nev = kinyerForrasNev(kepAdat.path, rendezett_args[i])
                    end

                    tartalom = string.format('<div class="kulso-url-doboz" data-src="%s" data-forras-nev="%s"><span class="url-link-szoveg">Külső kép:<br/>[Link]</span><div class="galeria-felirat">%s</div></div>',
                                                biztonsagos_path,
                                                forras_nev,
                                                tiszta_felirat)

                else
                    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