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ó
 
(31 közbenső módosítás ugyanattól a felhasználótól nincs mutatva)
1. sor: 1. sor:
local p = {}
local p = {}


-- Segédfüggvény a képtípus meghatározására és tisztítására
-- =============================================================================
local function detektalKepTipus(nyers_bemenet)
-- SEGÉDFÜGGVÉNYEK
    -- Szétvágjuk a bemenetet a '|' karakter mentén (kép és felirat)
-- =============================================================================
    local kep_resz, felirat = nyers_bemenet:match("^%s*([^|]+)%s*|?%s*(.*)%s*$")
    if not kep_resz then kep_resz = nyers_bemenet end
    if felirat == "" then felirat = nil end


     -- Tisztítás (felesleges szóközök lekapása)
local function megtisztitString(s)
     local tiszta = kep_resz:match("^%s*(.-)%s*$")
     if not s then return "" end
    s = tostring(s)
     s = s:gsub("^%s*(.-)%s*$", "%1")
    s = s:gsub("[\n\r]", "")
    return s
end


     -- 1. Internetes link
local function detektalKepTipus(nyers_bemenet)
     if tiszta:find("^https?://") then
    if not nyers_bemenet or nyers_bemenet == "" then return nil end
         return { tipus = "URL", path = tiszta, felirat = felirat }
   
    local s = megtisztitString(nyers_bemenet)
      
     if s:lower():find("^commons:") then
        local tiszta_nev = s:sub(9)
        if not tiszta_nev:lower():find("^file:") and not tiszta_nev:lower():find("^fájl:") then
            tiszta_nev = "File:" .. tiszta_nev
        end
        local url_nev = tiszta_nev:gsub(" ", "_")
         return {
            tipus = "URL",
            path = "https://commons.wikimedia.org/wiki/Special:FilePath/" .. url_nev
        }
     end
     end
   
    if s:find("^https?://") or s:find("^encrypted%-tbn") or s:find("gstatic%.com") then
        return {
            tipus = "URL",
            path = s
        }
    end
   
    return {
        tipus = "WIKI",
        path = s
    }
end


    -- 2. Wikimedia Commons (tisztítjuk a commons:, c:, File:, Fájl: prefixeket is)
local function isKepFormatum(szoveg)
     if tiszta:find("^[Cc]ommons:") or tiszta:find("^c:") then
     if not szoveg or szoveg == "" then return false end
        local fajlnev = tiszta:gsub("^[Cc]ommons:", ""):gsub("^c:", "")
    local s = megtisztitString(szoveg):lower()
                            :gsub("^[Ff]ájl:", ""):gsub("^[Ff]ile:", "")
   
         return { tipus = "COMMONS", path = fajlnev, felirat = felirat }
    if s:find("^https?://") or s:find("^encrypted%-tbn") or s:find("gstatic%.com") or s:find("^commons:") then
         return true
     end
     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$") or s:find("%.jpg[^%%a]") or s:find("%.jpeg[^%%a]") then
        return true
    end
   
    return false
end


     -- 3. Helyi kép (szintén takarítjuk a prefixeket)
local function kinyerForrasNev(url)
     local helyi_fajl = tiszta:gsub("^[Ff]ájl:", ""):gsub("^[Ff]ile:", "")
    if not url or url == "" then return "Külső forrás" end
     return { tipus = "HELYI", path = helyi_fajl, felirat = felirat }
      
    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
end


-- =============================================================================
-- FŐ FUNKCIÓ: GALÉRIA GENERÁLÁSA
-- =============================================================================
function p.generalGaleria(frame)
function p.generalGaleria(frame)
     local args = {}
     local args = {}
    -- Megvizsgáljuk, hogy valódi MediaWiki környezetben vagyunk-e
     if type(frame) == "table" and type(frame.getParent) == "function" then
     if type(frame) == "table" and type(frame.getParent) == "function" then
        -- Ha sablonból hívták (pl. {{Galéria...}}), akkor a szülő (Parent) paraméterei kellenek
         args = frame:getParent().args
         args = frame:getParent().args
        -- Biztonsági mentés: ha közvetlenül #invoke-ból hívták paraméterekkel
         if not args or (not args[1] and not args['k1']) then
         if not args or not args[1] then
             args = frame.args
             args = frame.args
         end
         end
     else
     else
        -- Ha offline futtatod a gépeden (parancssorból)
         args = frame
         args = frame
     end
     end


     -- Ha teljesen üres a bemenet, adjunk vissza egy látható hibaüzenetet, ne a semmit
     -- 1. Kigyűjtjük a törött Google linkeket
     if not args or not args[1] then
    local elrontott_google_linkek = {}
         return '<span style="color:red; font-weight:bold;">[Hiba: A galéria nem kapott képeket!]</span>'
     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)
                table.insert(elrott_google_linkek, teljes_url)
            end
        end
    end
 
    -- 2. Lineáris lista másolása
    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
 
    local ideiglenes_lista = {}
    for i = 1, max_index do
        if args[i] then
            table.insert(ideiglenes_lista, megtisztitString(args[i]))
        end
     end
     end


    -- 3. Rekonstrukció eltolással
    local rendezett_args = {}
    local google_idx = 1
    local idx = 1
    while ideiglenes_lista[idx] do
        local aktualis = ideiglenes_lista[idx]
        if elrontott_google_linkek[google_idx] and not isKepFormatum(aktualis) then
            local utolso_rendezett = rendezett_args[#rendezett_args]
            if not utolso_rendezett or not isKepFormatum(utolso_rendezett) then
                table.insert(rendezett_args, elrontott_google_linkek[google_idx])
                google_idx = google_idx + 1
            end
        end
        table.insert(rendezett_args, aktualis)
        idx = idx + 1
    end
    while elrontott_google_linkek[google_idx] do
        table.insert(rendezett_args, elrontott_google_linkek[google_idx])
        google_idx = google_idx + 1
    end
    -- 4. FŐ RENDERELŐ CIKLUS
     local html = {}
     local html = {}
     table.insert(html, '<div class="egyedi-galeria-container">')
     table.insert(html, '<div class="egyedi-galeria-container">')


     for k, v in ipairs(args) do
     local i = 1
        local kepAdat = detektalKepTipus(v)
    while rendezett_args[i] do
        local imgTag = ""
         local v = rendezett_args[i]
         local feliratHTML = ""


         -- Felirat előkészítése (ha nincs megadva, üresen hagyjuk)
         if v ~= "" and isKepFormatum(v) then
        local tiszta_felirat = kepAdat.felirat or ""
            local kepAdat = detektalKepTipus(v)


        -- A Luában a külső kép ága így módosul:
            if kepAdat then
        if kepAdat.tipus == "URL" then
                local felirat_resz = ""
            -- A képet beágyazzuk egy LightGallery-kompatibilis linkbe
                if rendezett_args[i+1] then
            imgTag = string.format('<a href="%s" class="lg-vegyes-item" data-src="%s" data-sub-html="<h4>%s</h4>"><img src="%s" alt="%s" class="galeria-kep url-kep" /></a>', kepAdat.path, kepAdat.path, tiszta_felirat, kepAdat.path, tiszta_felirat)
                    local kovetkezo = rendezett_args[i+1]
            if kepAdat.felirat then
                    if kovetkezo ~= "" and not isKepFormatum(kovetkezo) then
                feliratHTML = string.format('<div class="galeria-felirat">%s</div>', tiszta_felirat)
                        felirat_resz = kovetkezo
            end
                        i = i + 1
                    end
                end
 
                local tartalom = ""
 
                if kepAdat.tipus == "URL" then
                    local forras_nev = ""
                    if kepAdat.path:find("commons%.wikimedia%.org") then
                        forras_nev = "Wikimedia Commons"
                    else
                        forras_nev = kinyerForrasNev(kepAdat.path)
                    end


        elseif kepAdat.tipus == "COMMONS" then
                    -- BIZTONSÁGOS GENERÁLÁS: Csak tiszta adatok attribútumokban, nincs <img> tag, amit a böngésző letilthatna!
            -- Commons kép (a MediaWiki parsernek a sima File: is jó, ha be van kötve,
                    -- A CSS fog neki adni egy elegáns és egyedi placeholder kinézetet a MediaWiki:Common.css-ből.
            -- de adhatunk neki egyedi osztályt is, ha tiszta HTML-t akarunk)
                    tartalom = string.format('<div class="kulso-url-doboz" data-src="%s" data-forras-nev="%s"><div class="kulso-link-ikon">🌐</div><span class="galeria-felirat">%s</span><br><small class="forras-felirat">%s</small></div>',
            if kepAdat.felirat then
                                                kepAdat.path,
                imgTag = string.format('[[File:%s|thumb|180px|%s]]', kepAdat.path, tiszta_felirat)
                                                forras_nev,
            else
                                                felirat_resz,
                imgTag = string.format('[[File:%s|thumb|180px]]', kepAdat.path)
                                                forras_nev)
            end
                else
                    -- HELYI WIKI KÉP
                    if felirat_resz ~= "" then
                        tartalom = string.format('[[Fájl:%s|thumb|200x200px|%s]]', kepAdat.path, felirat_resz)
                    else
                        tartalom = string.format('[[Fájl:%s|thumb|200x200px]]', kepAdat.path)
                    end
                end


        else -- HELYI
                table.insert(html, '  <div class="galeria-elem">')
            if kepAdat.felirat then
                 table.insert(html, '   ' .. tartalom)
                 imgTag = string.format('[[File:%s|thumb|180px|%s]]', kepAdat.path, tiszta_felirat)
                 table.insert(html, ' </div>')
            else
                 imgTag = string.format('[[File:%s|thumb|180px]]', kepAdat.path)
             end
             end
         end
         end


         -- Közös tárolóba rakás
         i = i + 1
        table.insert(html, '  <div class="galeria-elem">')
        table.insert(html, '    <div class="galeria-kep-doboz">' .. imgTag .. '</div>')
        if kepAdat.tipus == "URL" and feliratHTML ~= "" then
            table.insert(html, '    ' .. feliratHTML)
        end
        table.insert(html, '  </div>')
     end
     end


     table.insert(html, '\n</div>')
     table.insert(html, '</div>')
     return table.concat(html, "\n")
 
     local vegleges_html = table.concat(html, "\n")
    return frame:preprocess(vegleges_html)
end
end
-- =============================================================================
-- OFFLINE TESZTELÉS
-- =============================================================================
-- local teszt_adatok = {
--    "kaktusz_helyi_01.jpg | Mammillaria sp. helyi leírás",
--    "https://example.com/kepek/sivatag.jpg | Élőhelyi fotó Mexikóból",
--    "commons:File:Mammillaria_dioica_flower.jpg | Commons virág fotó",
--    "Fájl:ritka_pozsgas.png" -- Felirat nélkül
-- }
-- print("--- FRISSÍTETT OFFLINE KIMENET ---")
-- print(p.generalGaleria(teszt_adatok))


return p
return p

A lap jelenlegi, 2026. június 19., 07:58-kori változata

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

local p = {}

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

local function megtisztitString(s)
    if not s then return "" end
    s = tostring(s)
    s = s:gsub("^%s*(.-)%s*$", "%1")
    s = s:gsub("[\n\r]", "")
    return s
end

local function detektalKepTipus(nyers_bemenet)
    if not nyers_bemenet or nyers_bemenet == "" then return nil end
    
    local s = megtisztitString(nyers_bemenet)
    
    if s:lower():find("^commons:") then
        local tiszta_nev = s:sub(9)
        if not tiszta_nev:lower():find("^file:") and not tiszta_nev:lower():find("^fájl:") then
            tiszta_nev = "File:" .. tiszta_nev
        end
        local url_nev = tiszta_nev:gsub(" ", "_")
        return {
            tipus = "URL",
            path = "https://commons.wikimedia.org/wiki/Special:FilePath/" .. url_nev
        }
    end
    
    if s:find("^https?://") or s:find("^encrypted%-tbn") or s:find("gstatic%.com") then
        return {
            tipus = "URL",
            path = s
        }
    end
    
    return {
        tipus = "WIKI",
        path = s
    }
end

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$") or s:find("%.jpg[^%%a]") or s:find("%.jpeg[^%%a]") then
        return true
    end
    
    return false
end

local function kinyerForrasNev(url)
    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

    -- 1. Kigyűjtjük a törött Google linkeket
    local elrontott_google_linkek = {}
    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)
                table.insert(elrott_google_linkek, teljes_url)
            end
        end
    end

    -- 2. Lineáris lista másolása
    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

    local ideiglenes_lista = {}
    for i = 1, max_index do
        if args[i] then
            table.insert(ideiglenes_lista, megtisztitString(args[i]))
        end
    end

    -- 3. Rekonstrukció eltolással
    local rendezett_args = {}
    local google_idx = 1

    local idx = 1
    while ideiglenes_lista[idx] do
        local aktualis = ideiglenes_lista[idx]

        if elrontott_google_linkek[google_idx] and not isKepFormatum(aktualis) then
            local utolso_rendezett = rendezett_args[#rendezett_args]
            if not utolso_rendezett or not isKepFormatum(utolso_rendezett) then
                table.insert(rendezett_args, elrontott_google_linkek[google_idx])
                google_idx = google_idx + 1
            end
        end

        table.insert(rendezett_args, aktualis)
        idx = idx + 1
    end

    while elrontott_google_linkek[google_idx] do
        table.insert(rendezett_args, elrontott_google_linkek[google_idx])
        google_idx = google_idx + 1
    end

    -- 4. 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
                local felirat_resz = ""
                if rendezett_args[i+1] then
                    local kovetkezo = rendezett_args[i+1]
                    if kovetkezo ~= "" and not isKepFormatum(kovetkezo) then
                        felirat_resz = kovetkezo
                        i = i + 1
                    end
                end

                local tartalom = ""

                if kepAdat.tipus == "URL" then
                    local forras_nev = ""
                    if kepAdat.path:find("commons%.wikimedia%.org") then
                        forras_nev = "Wikimedia Commons"
                    else
                        forras_nev = kinyerForrasNev(kepAdat.path)
                    end

                    -- BIZTONSÁGOS GENERÁLÁS: Csak tiszta adatok attribútumokban, nincs <img> tag, amit a böngésző letilthatna!
                    -- A CSS fog neki adni egy elegáns és egyedi placeholder kinézetet a MediaWiki:Common.css-ből.
                    tartalom = string.format('<div class="kulso-url-doboz" data-src="%s" data-forras-nev="%s"><div class="kulso-link-ikon">🌐</div><span class="galeria-felirat">%s</span><br><small class="forras-felirat">%s</small></div>',
                                                kepAdat.path,
                                                forras_nev,
                                                felirat_resz,
                                                forras_nev)
                else
                    -- HELYI WIKI KÉP
                    if felirat_resz ~= "" then
                        tartalom = string.format('[[Fájl:%s|thumb|200x200px|%s]]', kepAdat.path, felirat_resz)
                    else
                        tartalom = string.format('[[Fájl:%s|thumb|200x200px]]', 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