Ir para o conteúdo

Módulo:Demo: mudanças entre as edições

De Wiki TokuDrive
Importando predefinição/módulo da Wikipédia em português para manter layout
 
Importando predefinição/módulo da Wikipédia em português para manter layout
 
Linha 1: Linha 1:
local p = {}
local p = {}


--creates a frame object that cannot access any of the parent's args
-- Creates a frame object that cannot access any of the parent's args
--unless a table containing a list keys of not to inherit is provided
-- unless a table containing a list keys of not to inherit is provided
function disinherit(frame, onlyTheseKeys)
function disinherit(frame, onlyTheseKeys)
local parent = frame:getParent() or frame
    local parent = frame:getParent() or frame
local orphan = parent:newChild{}
    local orphan = parent:newChild{}
orphan.getParent = parent.getParent --returns nil
    orphan.getParent = parent.getParent
orphan.args = {}
    orphan.args = {}
if onlyTheseKeys then
    if onlyTheseKeys then
local family = {parent, frame}
        local family = {parent, frame}
for f = 1, 2 do
        for f = 1, 2 do
for k, v in pairs(family[f] and family[f].args or {}) do
            for k, v in pairs(family[f] and family[f].args or {}) do
orphan.args[k] = orphan.args[k] or v
                orphan.args[k] = orphan.args[k] or v
end
            end
end
        end
parent.args = mw.clone(orphan.args)
        parent.args = mw.clone(orphan.args)
setmetatable(orphan.args, nil)
        setmetatable(orphan.args, nil)
for _, k in ipairs(onlyTheseKeys) do
        for _, k in ipairs(onlyTheseKeys) do
rawset(orphan.args, k, nil)
            rawset(orphan.args, k, nil)
end
        end
end
    end
return orphan, parent
    return orphan, parent
end
end


function getSeparator(args, default)
function getSeparator(args, default)
local br = tonumber(args.br) and ('<br>'):rep(args.br) or args.br
    local br = tonumber(args.br) and ('<br>'):rep(args.br) or args.br
local sep = args.sep or br or default
    local sep = args.sep or br or default
return #sep > 0 and ' ' .. sep .. ' ' or sep
    return #sep > 0 and ' ' .. sep .. ' ' or sep
end
end


function p.get(frame, arg, passArgs)
function p.get(frame, arg, passArgs)
local orphan, frame = disinherit(frame, passArgs and {arg or 1})
    local orphan, frame = disinherit(frame, passArgs and {arg or 1})
local code = frame.args[arg or 1] or ''
    local code = frame.args[arg or 1] or ''
if code:match'UNIQ%-%-nowiki' then
    if code:match'UNIQ%-%-nowiki' then
code = mw.text.unstripNoWiki(code)
        code = mw.text.unstripNoWiki(code)
:gsub('&lt;', '<')
            :gsub('&lt;', '<')
:gsub('&gt;', '>')
            :gsub('&gt;', '>')
:gsub('&quot;', '"')
            :gsub('&quot;', '"')
-- Replace `&#125;%-` with `}-` because of some server quirk leading to
            -- Replace `&#125;%-` with `}-` because of some server quirk
-- =mw.text.unstripNoWiki(mw.getCurrentFrame():preprocess('<nowiki>}-</nowiki>'))
            :gsub('&#125;%-', '}-')
-- outputting `&#125;-` instead of `}-`, while it's ok with `<nowiki>} -</nowiki>`
            -- The same with `-&#123;`
:gsub('&#125;%-', '}-')
            :gsub('%-&#123;', '-{')
-- The same with `-&#123;`
    end
:gsub('%-&#123;', '-{')
    local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
end
    return {
local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
        source = code,
return {
        output = orphan:preprocess(code):gsub(kill_categories and '%[%[Categor[ia].-%]%]' or '', ''),
source = code,
        frame = frame
output = orphan:preprocess(code):gsub(kill_categories and '%[%[Category.-%]%]' or kill_categories and '%[%[Categoria.-%]%]' or '', ''),
    }
frame = frame
}
end
end


function p.main(frame, demoTable)
function p.main(frame, demoTable)
local show = demoTable or p.get(frame)
    local show = demoTable or p.get(frame)
local args = show.frame.args
    local args = show.frame.args
if show[args.result_arg] then
    if show[args.result_arg] then
return show[args.result_arg]
        return show[args.result_arg]
end
    end
local yesno = require('Module:Yesno')
    local yesno = require('Module:Yesno')
args.reverse = yesno(args.reverse or args.invertido, false)
    args.reverse = yesno(args.reverse or args.invertido, false)
args.sep = getSeparator(args, '')
    args.sep = getSeparator(args, '')
local source = frame:extensionTag{
    local source = frame:extensionTag{
name = 'syntaxhighlight',
        name = 'syntaxhighlight',
args = {
        args = {
lang = 'wikitext',
            lang = 'wikitext',
style = args.style or args.estilo
            style = args.style or args.estilo
},
        },
content = show.source
        content = show.source
}
    }
return args.reverse and
    return args.reverse and
show.output .. args.sep .. source or
        show.output .. args.sep .. source or
source .. args.sep .. show.output
        source .. args.sep .. show.output
end
end


-- Alternate function to return an inline result
-- Alternate function to return an inline result
function p.inline(frame, demoTable)
function p.inline(frame, demoTable)
local show = demoTable or p.get(frame)
    local show = demoTable or p.get(frame)
local args = show.frame.args
    local args = show.frame.args
if show[args.result_arg] then
    if show[args.result_arg] then
return show[args.result_arg]
        return show[args.result_arg]
end
    end
local yesno = require('Module:Yesno')
    local yesno = require('Module:Yesno')
args.reverse = yesno(args.reverse or args.invertido, false)
    args.reverse = yesno(args.reverse or args.invertido, false)
args.sep = getSeparator(args, args.reverse and '←' or '→')
    args.sep = getSeparator(args, args.reverse and '←' or '→')
local source =  frame:extensionTag{
    local source =  frame:extensionTag{
name = 'syntaxhighlight',
        name = 'syntaxhighlight',
args = {
        args = {
lang = 'wikitext',
            lang = 'wikitext',
inline = true,
            inline = true,
style = args.style or args.estilo
            style = args.style or args.estilo
},
        },
content = show.source
        content = show.source
}
    }
return args.reverse and
    return args.reverse and
show.output .. args.sep .. source or
        show.output .. args.sep .. source or
source .. args.sep .. show.output
        source .. args.sep .. show.output
end
end


--passing of args into other module without preprocessing
-- Passing of args into other module without preprocessing
function p.module(frame)
function p.module(frame)
local orphan, frame = disinherit(frame, {
    local orphan, frame = disinherit(frame, {
'demo_template',
        'demo_template',
'demo_module',
        'demo_module',
'demo_module_func',
        'demo_module_func',
'demo_main',
        'demo_main',
'demo_sep',
        'demo_sep',
'demo_br',
        'demo_br',
'demo_result_arg',
        'demo_result_arg',
'demo_kill_categories',
        'demo_kill_categories',
'nocat'
        'nocat'
})
    })
local template = frame.args.demo_template and 'Template:'..frame.args.demo_template or frame.args.demo_template and 'Predefinição:'..frame.args.demo_template
   
local demoFunc = frame.args.demo_module_func or 'main\n'
    local template = frame.args.demo_template and 'Predefinição:'..frame.args.demo_template
local demoModule = require('Módulo:' .. frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
    -- Correção: Removido o '\n' desnecessário do padrão
frame.args.br, frame.args.result_arg = frame.args.demo_sep or frame.args.demo_br, frame.args.demo_result_arg
    local demoFunc = frame.args.demo_module_func or 'main'
local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
    local demoModule = require('Módulo:' .. frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
if demoModule then
    frame.args.br, frame.args.result_arg = frame.args.demo_sep or frame.args.demo_br, frame.args.demo_result_arg
local named = {insert = function(self, ...) table.insert(self, ...) return self end}
    local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
local source = {insert = named.insert, '{{', frame.args.demo_template or frame.args.demo_module, '\n'}
   
if not template then
    local unpackFunc = table.unpack or unpack
source:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)
 
end
    if demoModule then
local insertNamed = #source + 1
        local named = {insert = function(self, ...) table.insert(self, ...) return self end}
for k, v in pairs(orphan.args) do
        local source = {insert = named.insert, '{{', frame.args.demo_template or frame.args.demo_module, '\n'}
local nan, insert = type(k) ~= 'number', {v}
        if not template then
local target = nan and named or source
            source:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)
target:insert'|'
        end
if nan then
        local insertNamed = #source + 1
target:insert(k):insert'=':insert'\n'
        for k, v in pairs(orphan.args) do
table.insert(insert, 1, #target)
            local nan, insert = type(k) ~= 'number', {v}
end
            local target = nan and named or source
target:insert(unpack(insert))
            target:insert'|'
local nowiki = v:match('nowiki')
            if nan then
if nowiki or v:match('{{.-}}') then
                target:insert(k):insert'=':insert'\n'
orphan.args[k] = frame:preprocess(nowiki and mw.text.unstripNoWiki(v) or v)
                table.insert(insert, 1, #target)
end
            end
end
            target:insert(unpackFunc(insert))
source:insert'}}'
            local nowiki = v:match('nowiki')
table.insert(source, insertNamed, table.concat(named))
            if nowiki or v:match('{{.-}}') then
return p.main(orphan, {
                orphan.args[k] = frame:preprocess(nowiki and mw.text.unstripNoWiki(v) or v)
source = table.concat(source), "<>'|=~",
            end
output = tostring(demoModule(orphan)):gsub(kill_categories and '%[%[Category.-%]%]' or kill_categories and '%[%[Categoria.-%]%]' or '', ''),
        end
frame = frame
        source:insert'}}'
})
        table.insert(source, insertNamed, table.concat(named))
else
       
return "Erro: Função de módulo inválida: "..demoFunc
        -- Correção: Removida a string morta "<>'|=~" de dentro da tabela abaixo
end
        return p.main(orphan, {
            source = table.concat(source),
            output = tostring(demoModule(orphan)):gsub(kill_categories and '%[%[Categor[ia].-%]%]' or '', ''),
            frame = frame
        })
    else
        return "Erro: Função de módulo inválida: " .. demoFunc
    end
end
end


return p
return p

Edição atual tal como às 05h28min de 19 de julho de 2026

local p = {}

-- Creates a frame object that cannot access any of the parent's args -- unless a table containing a list keys of not to inherit is provided function disinherit(frame, onlyTheseKeys)

   local parent = frame:getParent() or frame
   local orphan = parent:newChild{}
   orphan.getParent = parent.getParent
   orphan.args = {}
   if onlyTheseKeys then
       local family = {parent, frame}
       for f = 1, 2 do
           for k, v in pairs(family[f] and family[f].args or {}) do
               orphan.args[k] = orphan.args[k] or v
           end
       end
       parent.args = mw.clone(orphan.args)
       setmetatable(orphan.args, nil)
       for _, k in ipairs(onlyTheseKeys) do
           rawset(orphan.args, k, nil)
       end
   end
   return orphan, parent

end

function getSeparator(args, default)

   local br = tonumber(args.br) and ('
'):rep(args.br) or args.br local sep = args.sep or br or default return #sep > 0 and ' ' .. sep .. ' ' or sep

end

function p.get(frame, arg, passArgs)

   local orphan, frame = disinherit(frame, passArgs and {arg or 1})
   local code = frame.args[arg or 1] or 
   if code:match'UNIQ%-%-nowiki' then
       code = mw.text.unstripNoWiki(code)
           :gsub('<', '<')
           :gsub('>', '>')
           :gsub('"', '"')
           -- Replace `}%-` with `}-` because of some server quirk
           :gsub('}%-', '}-')
           -- The same with `-{`
           :gsub('%-{', '-{')
   end
   local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
   return {
       source = code,
       output = orphan:preprocess(code):gsub(kill_categories and '%[%[Categor[ia].-%]%]' or , ),
       frame = frame
   }

end

function p.main(frame, demoTable)

   local show = demoTable or p.get(frame)
   local args = show.frame.args
   if show[args.result_arg] then
       return show[args.result_arg]
   end
   local yesno = require('Module:Yesno')
   args.reverse = yesno(args.reverse or args.invertido, false)
   args.sep = getSeparator(args, )
   local source = frame:extensionTag{
       name = 'syntaxhighlight',
       args = {
           lang = 'wikitext',
           style = args.style or args.estilo
       },
       content = show.source
   }
   return args.reverse and
       show.output .. args.sep .. source or
       source .. args.sep .. show.output

end

-- Alternate function to return an inline result function p.inline(frame, demoTable)

   local show = demoTable or p.get(frame)
   local args = show.frame.args
   if show[args.result_arg] then
       return show[args.result_arg]
   end
   local yesno = require('Module:Yesno')
   args.reverse = yesno(args.reverse or args.invertido, false)
   args.sep = getSeparator(args, args.reverse and '←' or '→')
   local source =  frame:extensionTag{
       name = 'syntaxhighlight',
       args = {
           lang = 'wikitext',
           inline = true,
           style = args.style or args.estilo
       },
       content = show.source
   }
   return args.reverse and
       show.output .. args.sep .. source or
       source .. args.sep .. show.output

end

-- Passing of args into other module without preprocessing function p.module(frame)

   local orphan, frame = disinherit(frame, {
       'demo_template',
       'demo_module',
       'demo_module_func',
       'demo_main',
       'demo_sep',
       'demo_br',
       'demo_result_arg',
       'demo_kill_categories',
       'nocat'
   })
   
   local template = frame.args.demo_template and 'Predefinição:'..frame.args.demo_template
   -- Correção: Removido o '\n' desnecessário do padrão
   local demoFunc = frame.args.demo_module_func or 'main'
   local demoModule = require('Módulo:' .. frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
   frame.args.br, frame.args.result_arg = frame.args.demo_sep or frame.args.demo_br, frame.args.demo_result_arg
   local kill_categories = frame.args.demo_kill_categories or frame.args.nocat
   
   local unpackFunc = table.unpack or unpack
   if demoModule then
       local named = {insert = function(self, ...) table.insert(self, ...) return self end}
       local source = {insert = named.insert, '{{', frame.args.demo_template or frame.args.demo_module, '\n'}
       if not template then
           source:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)
       end
       local insertNamed = #source + 1
       for k, v in pairs(orphan.args) do
           local nan, insert = type(k) ~= 'number', {v}
           local target = nan and named or source
           target:insert'|'
           if nan then
               target:insert(k):insert'=':insert'\n'
               table.insert(insert, 1, #target)
           end
           target:insert(unpackFunc(insert))
           local nowiki = v:match('nowiki')
           if nowiki or v:match('Predefinição:.-') then
               orphan.args[k] = frame:preprocess(nowiki and mw.text.unstripNoWiki(v) or v)
           end
       end
       source:insert'}}'
       table.insert(source, insertNamed, table.concat(named))
       
       -- Correção: Removida a string morta "<>'|=~" de dentro da tabela abaixo
       return p.main(orphan, {
           source = table.concat(source),
           output = tostring(demoModule(orphan)):gsub(kill_categories and '%[%[Categor[ia].-%]%]' or , ),
           frame = frame
       })
   else
       return "Erro: Função de módulo inválida: " .. demoFunc
   end

end

return p