Moduł:Szablony CC

Z Nonsensopedii, polskiej encyklopedii humoru

local longNames = 
{
	BY = "Uznanie autorstwa",
	SA = "Na tych samych warunkach",
	NC = "Użycie niekomercyjne",
	ND = "Bez utworów zależnych"
}

local iconNames =
{
	BY = "Cc-by new white.svg",
	SA = "Cc-sa white.svg",
	NC = "Cc-nc white.svg",
	ND = "Cc-nd white.svg"
}

local versionNames = 
{
	["1.0"] = "Ogólny",
	["2.0"] = "Ogólny",
	["2.5"] = "Ogólny",
	["3.0"] = "Unported",
	["4.0"] = "Międzynarodowe"
}

local modifiers = {"BY", "ND", "NC", "SA"}

-- ikony
-- nazwa długa
-- kategorie

local function getLongName(license, noVersionName)
	local name = "";
	
	for _, modifier in pairs(modifiers) do
		if license[modifier] ~= nil then
			name = name .. (longNames[modifier] or "") .. "-"
		end
	end
	
	name = name:sub(1, name:len() - 1) -- remove last hyphen
	if type(license.version) ~= "table" then
		
		name = name .. " " .. license.version .. (noVersionName and (" " .. (versionNames[license.version] or "Ogólny")) or "")
	elseif license.version[1] ~= "3.0" and license.version[1] ~= "4.0" and not noVersionName then
		-- need to add "ogólny" if max license <= 2.5, ugh
		name = name .. " Ogólny"
	end
	
	return name
end

local function getIcons(license)
	local icons = "[[Plik:Cc-white.svg|32px|center]]"
	for _, modifier in pairs(modifiers) do
		if license[modifier] == true and iconNames[modifier] ~= nil then
			icons = icons .. "[[Plik:" .. iconNames[modifier] .. "|32px|center]]"
		end
	end
	
	return icons
end

local function getCategories(license)
	local noincludeCats = {"Szablony licencji| "}
	local includeonly = "{{#if:{{{mini|}}}|"
	
	local nc = license["NC"]
	local nd = license["ND"]
	
	local modifierString = ""
	if nc then
		modifierString = " do użytku niekomercyjnego"
		if nd then
			modifierString = modifierString .. " i"
		end
	end
	if nd then
		modifierString = modifierString .. " bez możliwości modyfikacji"
	end
	
	table.insert(noincludeCats, "Pliki Creative Commons" .. modifierString .. "| ")
	table.insert(noincludeCats, "Pliki zawierające elementy Creative Commons" .. modifierString .. "| ")
	
	includeonly = includeonly .. "[[Kategoria:Pliki zawierające elementy Creative Commons" .. modifierString .. "]]|[[Kategoria:" .. "Pliki Creative Commons" .. modifierString .. "]]}}"
	
	local noinclude = "";
	for _, cat in pairs(noincludeCats) do
		noinclude = noinclude .. "[[Kategoria:" .. cat .. "]]"
	end
	
	return noinclude, includeonly
end

local function getLink(lic, versionOverride)
    local license = {}
    for k, v in pairs(lic) do
    	license[k] = v
    end
    if versionOverride ~= nil then
       license.version = versionOverride
    end
    
    local link = "http://creativecommons.org/licenses/"
    
    for _, modifier in pairs(modifiers) do
		if license[modifier] == true then
			link = link .. modifier:lower() .. "-"
		end
	end
    
    link = link:sub(1, link:len() - 1) -- remove last hyphen
    local versionText = license.version
    if type(versionText) == "table" then
    	versionText = table.concat(versionText, ",")
    end
    link = link .. "/" .. versionText .. "/deed.pl"
    
    return link
end

local function getMultipleVersionString(license, doLinks)
    local string = ""
    for i, version in pairs(license.version) do
        if doLinks == true then
        	string = string .. "[" .. getLink(license, version) .. " " .. version .. "]"
        else
        	string = string .. version
        end
        
        if i < #license.version - 1 then
            string = string .. ", "
        elseif i == #license.version - 1 then
            string = string .. " i "
        end
    end
    
    return string
end

local function getCode(args, license)
    local licbox = require("Moduł:Licbox")
    local licboxArgs = {}
    licboxArgs["1"] = license.name
    local prefix = "Ten plik jest udostępniony na licencji Creative Commons. Dokładniej, jest on udostępniony na licencji Creative Commons "
  
    local longNameNoLink = getLongName(license)
    local longName = longNameNoLink
    if type(license.version) ~= "table" then
        longName = "[" .. getLink(license) .. " " .. longName .. "]"
    end
    local versionString = ", potocznie znanej jako „" .. license.name:upper() .. "”"
    local versionStringNoLink = versionString
    if type(license.version) == "table" then -- multiple versions, so strings should be different
        versionString = " w wersjach " .. getMultipleVersionString(license, true)
        versionStringNoLink = " w wersjach " .. getMultipleVersionString(license, false)
    end
    
    licboxArgs["2"] = prefix .. longName .. versionString .. "."
    licboxArgs["3"] = prefix .. longNameNoLink .. versionStringNoLink .. "."
    licboxArgs["typ"] = "cc"
    licboxArgs["grafika"] = getIcons(license)
    licboxArgs["mini"] = args["mini"]
    licboxArgs["demo"] = args["demo"]
	licboxArgs["domniemanie"] = args["domniemanie"]
	licboxArgs["copydown"] = args["copydown"]
	
	return licbox._getCode(licboxArgs, args["templatename"])
end

-- returns license object from its name
local function licenseAsObject(name)
	name = name:upper()
	local license = {name = name}
	for _, modifier in pairs(modifiers) do
		if name:find(modifier) ~= nil then
			license[modifier] = true
		end
	end
	
	license.version = name:sub(name:find("%d.*"))
    if license.version:find(",") ~= nil then
        license.version = mw.text.split(license.version, ",", true)
    end
	return license
end

local p = {}
local tools = require("Moduł:Narzędzia")
function p.run(frame)
    local args = tools.getArgs(frame)
    local license = licenseAsObject(args["1"])
    
    local code = getCode(args, license)
    
    if args["demo"] ~= nil then
        return frame:preprocess(code)
    else
        return code
    end
end

function p.getMetadata(licname, attributionText)
	local ret = {metadata = {}}
	local metadata = ret.metadata
	
	local license = licenseAsObject(licname)
	metadata["licensetpl_short"] = license.name
	metadata["licensetpl_long"] = "Creative Commons " .. getLongName(license, true) .. (type(license.version) == "table" and (" " .. table.concat(license.version, ",")) or "")
	metadata["licensetpl_attr_req"] = "true"
	if attributionText then
		metadata["licensetpl_attr_req"] = attributionText
	end
	metadata["licensetpl_link_req"] = "true"
	
	-- na commons jak jest kilka wersji w jednej licencji, dają niedziałający link xd
	metadata["licensetpl_link"] = getLink(license)
	metadata["licensetpl_nonfree"] = "false"
	return ret
end

return p