Moduł:Narzędzia licencyjne: Różnice pomiędzy wersjami

Z Nonsensopedii, polskiej encyklopedii humoru
Linia 76: Linia 76:
-- {{test|{{test2|a}}|{{test3|b}}}}
-- {{test|{{test2|a}}|{{test3|b}}}}


-- zwraca kod szablonu wewnętrznego
-- zwraca modyfikatory i kod szablonu wewnętrznego
function p.stripLicenseModifierTemplates(string)
function p.stripLicenseModifierTemplates(string)
-- przypadek samych nazw szablonów licencji
-- przypadek samych nazw szablonów licencji
Linia 83: Linia 83:
end
end
local template, arguments = string
local template, arguments = string
local modifiers = {}
local strippedTemplates = {}
repeat
repeat
template, arguments = p.stripTemplate(template)
template, arguments = p.stripTemplate(template)
strippedTemplates[template] = arguments
until next(arguments) ~= nil -- until table is not empty
until next(arguments) ~= nil -- until table is not empty
return template
for name, args in pairs(strippedTemplates) do
if name == "domniemanie" or name == "copydown" then
modifiers[name] = value
end
end
return modifiers, template
end
end
return p
return p

Wersja z 11:17, 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 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] = value
		end
	end
	return modifiers, template
end
return p