Moduł:Narzędzia licencyjne: Różnice pomiędzy wersjami
Z Nonsensopedii, polskiej encyklopedii humoru
M |
M |
||
Linia 80: | Linia 80: | ||
-- przypadek samych nazw szablonów licencji |
-- przypadek samych nazw szablonów licencji |
||
if string:find("{{", 1, true) == nil then |
if string:find("{{", 1, true) == nil then |
||
return string |
return nil, string |
||
end |
end |
||
local template, arguments = string |
local template, arguments = string |
Wersja z 11:21, 10 cze 2020
local p = {}
function p.makeTemplate(name, args, runFunction)
local code = "<nowiki>{{"
if runFunction ~= nil then
-- invoke module
code = code .. "#invoke:" .. name .. "|" .. runFunction
else
-- template
code = code .. name .. "|"
end
for name, value in pairs(args) do
code = code .. "|" .. name .. "=" .. value
end
return "" .. code .. "}}</nowiki>"
end
-- returns name, value from parameter of form abc=def or nil and parameter if name is null
local function parseParameter(param)
local equalsPos = param:find("=", 1, true)
if equalsPos == nil then
return nil, param
else
return param:sub(1, equalsPos - 1), param:sub(equalsPos + 1)
end
end
-- returns template name, table with arguments
function p.stripTemplate(string)
local arguments = {}
local startingBracePos, endingBracePos = string:find("\{\{.*\}\}")
if startingBracePos == nil or endingBracePos == nil then
return nil, nil
end
local templateNameBegin, templateNameEnd = string:find("%w+", startingBracePos + 2)
local templateName = string:sub(templateNameBegin, templateNameEnd)
local i = templateNameEnd + 2
local begins = 0
local argumentString = ""
local unnamedParameterCounter = 1
while i < endingBracePos - 1 do
if string:sub(i, i) == "{" and string:sub(i + 1, i + 1) == "{" then
begins = begins + 1
elseif string:sub(i, i) == "}" and string:sub(i + 1, i + 1) == "}" then
begins = begins - 1
elseif string:sub(i, i) == "|" and begins == 0 then -- not | inside other template
-- get argument
local name, value = parseParameter(argumentString)
if name == nil then
name = unnamedParameterCounter
unnamedParameterCounter = unnamedParameterCounter + 1
end
arguments[name] = value
argumentString = ""
end
if not (string:sub(i, i) == "|" and begins == 0) then
argumentString = argumentString .. string:sub(i, i)
end
i = i + 1
end
if argumentString ~= "" then
local name, value = parseParameter(argumentString)
if name == nil then
name = unnamedParameterCounter
end
arguments[name] = value
end
return templateName, arguments
end
-- {{domniemanie|{{self|{{PD}}}}}}
-- {{name|param|param2=test|{{test2}}}}
-- {{test|{{test2|{{test3}}}}}}
-- {{test|{{test2|a}}|{{test3|b}}}}
-- zwraca modyfikatory i kod szablonu wewnętrznego
function p.stripLicenseModifierTemplates(string)
-- przypadek samych nazw szablonów licencji
if string:find("{{", 1, true) == nil then
return nil, string
end
local template, arguments = string
local modifiers = {}
local strippedTemplates = {}
repeat
template, arguments = p.stripTemplate(template)
strippedTemplates[template] = arguments
until next(arguments) ~= nil -- until table is not empty
for name, args in pairs(strippedTemplates) do
if name == "domniemanie" or name == "copydown" then
modifiers[name] = args
end
end
return modifiers, template
end
return p