Modul:Switch by pattern: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
preuzeto s hr.wikipedije
 
mNema sažetka uređivanja
 
Nisu prikazane 2 međuinačice
Redak 1: Redak 1:
require('strict')
local p = {}
local p = {}


--will find either "human" or "homo" as part of the following _input, and return "orange"
--args = {_input="[[Homo sampiens|humans]]", ["pink"]="virus", ["green"]="plant", ["light gray"]="fung", ["orange"]="homo;human", _returnall="", _respectcase="", _sep=";"}
--aliases to search keys are separated by _sep, so _sep cannot be part of any key
--returns the first key found unless _returnall is nonempty, in which case it returns "all#keys#found" (for further processing?)
function p._found_in(args)
function p._found_in(args)
     local skip_args = "#_input#_respectcase#_returncaptures#_returnall#_sep#_outputsep#_default#" --each key must be surrounded by #'s'
     local skip_args = "#_input#_respectcase#_returncaptures#_returnall#_sep#_outputsep#_default#"
     local res = {}
     local res = {}


     local all = args["_returnall"] and args["_returnall"]~=""
     local all = args["_returnall"] and args["_returnall"] ~= ""
     local returnkeys = not(args["_returncaptures"] and args["_returncaptures"]~="")
     local returnkeys = not (args["_returncaptures"] and args["_returncaptures"] ~= "")
     local sep = args["_sep"]~="" and args["_sep"] or "#" --should not be blank
     local sep = (args["_sep"] and args["_sep"] ~= "") and args["_sep"] or "#"
     local outputsep = args["_outputsep"] or "#" --can be blank
     local outputsep = args["_outputsep"] or "#"
     local lowercase = not (args["_respectcase"] and args["_respectcase"]~="") --lowercase input (but not search keys, they can be regex patterns)
     local lowercase = not (args["_respectcase"] and args["_respectcase"] ~= "")
     local input = args["_input"] or ""
     local input = args["_input"] or ""
     if input == "" then return "" end
   
     if input == "" then return args["_default"] or "" end
      
      
     if lowercase then input = mw.ustring.lower(input) end
     if lowercase then input = mw.ustring.lower(input) end
     for k,v in pairs(args) do
   
    if not mw.ustring.match(skip_args, "#"..k.."#" ) then
     for k, v in pairs(args) do
    local aliases = mw.text.split(v, sep, true)
        if not mw.ustring.match(skip_args, "#" .. tostring(k) .. "#") then
    for _,a in ipairs(aliases) do
            local aliases = mw.text.split(v, sep, true)
    if a~="" then
            for _, a in ipairs(aliases) do
    local match = mw.ustring.match(input, a)
                if a ~= "" then
    if match then
                    local match = mw.ustring.match(input, a)
    local key_or_capture = returnkeys and k or match
                    if match then
    if not all then return key_or_capture end
                        local key_or_capture = returnkeys and k or match
    table.insert(res, key_or_capture)
                        if not all then return key_or_capture end
    break --first found alias
                        table.insert(res, key_or_capture)
    end
                        break
    end
                    end
    end
                end
    end
            end
        end
     end
     end
    
    
  if #res>0 then return table.concat(res, outputsep) end --returnall was not blank
    if #res > 0 then return table.concat(res, outputsep) end
  return args["_default"] or ""
    return args["_default"] or ""
end
end


function p.found_in(frame)
function p.found_in(frame)
local args = frame:getParent() and frame:getParent().args
    local args = {}
if not (args["_input"] and args["_input"]~="") then args = frame.args end
   
if args["_input"] and args["_input"]~="" then return p._found_in(args) end
    -- Sigurno dohvaćanje roditeljskih argumenata
return args["_default"] or ""
    local parent = frame:getParent()
    local p_args = parent and parent.args or {}
    local f_args = frame.args or {}
 
    -- Logika odabira izvora:  
    -- Ako roditelj ima _input, koristi roditelja. Inače koristi frame.
    local source = (p_args["_input"] and p_args["_input"] ~= "") and p_args or f_args
   
    -- KLJUČNI POPRAVAK: Provjera je li source tablica prije 'pairs'
    if type(source) == "table" then
        for k, v in pairs(source) do
            args[k] = v
        end
    end
 
    -- Provjera imamo li uopće ulazne podatke
    if args["_input"] and args["_input"] ~= "" then  
        return p._found_in(args)  
    end
   
    return args["_default"] or ""
end
end


return p
return p

Posljednja izmjena od 16. travanj 2026. u 11:47

Dokumentacija modula
Kao funkcija parsera switch, ali pronalazi dijelove ili Luaine uzorke, a ne samo pune vrijednosti.

Upotreba

{{#invoke:Switch by pattern|found_in|_input= |patterns1|patterns2|patterns3|keyA=patternsA |keyB=patternsB |_respectcase= |_returnall= |_sep= |_outputsep= |_#default= }}

Primjeri

  • {{#invoke:Switch by pattern|found_in|_input=[[Animalia|Životinje]]|dark gray=vir|pink=život#animal|lightblue=fung}}pink


  • {{#invoke:Switch by pattern|found_in|_input=[[Riboviria]]|dark gray=vir|pink=život#animal|lightblue=fung}}dark gray


  • {{#invoke:Switch by pattern|found_in|ima jednu znamenku=%D%d$|_input=vrijednost12|ima dvije znamenke=%D%d%d$}}ima dvije znamenke


local p = {}

function p._found_in(args)
    local skip_args = "#_input#_respectcase#_returncaptures#_returnall#_sep#_outputsep#_default#"
    local res = {}

    local all = args["_returnall"] and args["_returnall"] ~= ""
    local returnkeys = not (args["_returncaptures"] and args["_returncaptures"] ~= "")
    local sep = (args["_sep"] and args["_sep"] ~= "") and args["_sep"] or "#"
    local outputsep = args["_outputsep"] or "#"
    local lowercase = not (args["_respectcase"] and args["_respectcase"] ~= "")
    local input = args["_input"] or ""
    
    if input == "" then return args["_default"] or "" end
    
    if lowercase then input = mw.ustring.lower(input) end
    
    for k, v in pairs(args) do
        if not mw.ustring.match(skip_args, "#" .. tostring(k) .. "#") then
            local aliases = mw.text.split(v, sep, true)
            for _, a in ipairs(aliases) do
                if a ~= "" then
                    local match = mw.ustring.match(input, a)
                    if match then
                        local key_or_capture = returnkeys and k or match
                        if not all then return key_or_capture end
                        table.insert(res, key_or_capture)
                        break
                    end
                end
            end
        end
    end
   
    if #res > 0 then return table.concat(res, outputsep) end
    return args["_default"] or ""
end

function p.found_in(frame)
    local args = {}
    
    -- Sigurno dohvaćanje roditeljskih argumenata
    local parent = frame:getParent()
    local p_args = parent and parent.args or {}
    local f_args = frame.args or {}

    -- Logika odabira izvora: 
    -- Ako roditelj ima _input, koristi roditelja. Inače koristi frame.
    local source = (p_args["_input"] and p_args["_input"] ~= "") and p_args or f_args
    
    -- KLJUČNI POPRAVAK: Provjera je li source tablica prije 'pairs'
    if type(source) == "table" then
        for k, v in pairs(source) do
            args[k] = v
        end
    end

    -- Provjera imamo li uopće ulazne podatke
    if args["_input"] and args["_input"] ~= "" then 
        return p._found_in(args) 
    end
    
    return args["_default"] or ""
end

return p