SiIvaGunner Wiki
Advertisement
SiIvaGunner Wiki

Documentation for this module may be created at Module:PlaylistParam/doc

-- Implements the playlist parameter of Template:Rip

local p = {}

--checks if a string has any link syntax
local function hasLinkSyntax(str)
    if string.match(str, "%[") or string.match(str,"%]") then
        return true
    else
        return false
    end
end

local function trim(s)
  -- from PiL2 20.4
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end
 
--ref from https://stackoverflow.com/questions/1426954/split-string-in-lua
local function split(inputstr, sep)
    if sep == nil then
       sep = ";" --for the purposes of this wiki, we assume that the delimiter is a ;
    end
    local t={};
    if string.find(inputstr,sep) ~= nil then
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            table.insert(t,trim(str))
        end
    else 
        table.insert(t,inputstr)
    end
    return t
end

-- format category links
local function link_label(str)
    assert (str, 'str must exist')
 
    str = tostring(str)
 
    if (not str or str == "") then
        return str
    end
 
    local label_map = {
        ["Kingdom Hearts Union χ (CROSS)"] = "Kingdom Hearts Union χ (CROSS)|Kingdom Hearts Union χ [CROSS]",
        ["Kingdom Hearts Union χ [CROSS]"] = "Kingdom Hearts Union χ (CROSS)|Kingdom Hearts Union χ [CROSS]",
        ["PriPara Mezase! Idol Grand Prix 1"] = "PriPara Mezase! Idol Grand Prix 1|PriPara Mezase! Idol Grand Prix #1",
        ["PriPara Mezase! Idol Grand Prix #1"] = "PriPara Mezase! Idol Grand Prix 1|PriPara Mezase! Idol Grand Prix #1",
        ["Steins Gate"] = "Steins;Gate|Steins;Gate",
        ["Steins Gate 0"] = "Steins;Gate 0|Steins;Gate 0",
        ["UNDER NIGHT IN-BIRTH Exe:Late(st)"] = "UNDER NIGHT IN-BIRTH Exe:Late(st)|UNDER NIGHT IN-BIRTH Exe:Late[st]",
        ["UNDER NIGHT IN-BIRTH Exe:Late[st]"] = "UNDER NIGHT IN-BIRTH Exe:Late(st)|UNDER NIGHT IN-BIRTH Exe:Late[st]",
    }
 
    if label_map[str] then
        str = label_map[str]
    else
        str = str .. "|" .. str
    end
    
    str = "Category:" .. str
 
    return str
end

-- format playlist URL
local function format_playlist(str)
    assert(str, 'str must exist')

    if string.match(str, "[^0-9a-zA-Z_-]") then
        return str
    end

    str = "https://www.youtube.com/playlist?list=" .. str
    return str
end

-- formats playlist IDs and titles into something like:
-- ''[[Playlist_name]]'' ({{Icon|<site>|2={{{playlist_id}}}}}),
local function format_param(ids, titles)
    assert (ids, 'ids must exist')
    assert (titles, 'titles must exist')
 
    local result = ""
    if (ids == "") or (titles == "") then
        return titles
    end
    ids = split(ids, ";")
    if ids[2] then -- only split titles if there is more than one link
        titles = split(titles, ";")
    else
        titles = {trim(titles)}
    end
    for index,value in ipairs(ids) do
        if (index > 1) then -- if not the first playlist
            result = result .. ", "
        end
        local url = format_playlist(value)
        if string.match(url, "youtube%.com/") and (string.match(url, "^https?://") or string.match(url, "^//")) then
            result = result .. string.format("''[[:%s]]'' ({{Icon|YouTube|2=%s }})",link_label(titles[index] or ""),url)
        else -- if the URL is not a link to YouTube, then return without icon formatting
            result = result .. string.format("''[[:%s]]'' %s",link_label(titles[index] or ""),url)
        end
    end
    return result
end

-- implements automatic categorization
local function categorize(str)
    assert (str, "str must exist")
    t = split(str,";")
    local result = ""
    if (str == "") then
        return str
    end
    for index, value in ipairs(t) do
       result = result .. string.format("[[%s]]",string.match(link_label(value), "^[^|]+"))
    end
    return result
end

-- puts categories on the bottom of the page. assumes delimiter is ";".
function p.categorize(frame)
    local str = frame.args[1]
        --if there's any link syntax, then we override the transform function
    if hasLinkSyntax(str) then
        return ""
    elseif pcall(function () result = categorize(str) end) then 
        return frame:preprocess(result)
    else
        return frame:preprocess(str .. "[[Category:Pages with script errors]]")
    end
end

function p.main(frame)
    local ids = frame.args[1] or ""
    local titles = frame.args[2] or ""
    --if there are no IDs, then we override the main function
    if ids == "" then
        if hasLinkSyntax(titles) or titles == "" then
            return titles
        else
            return string.format("''[[:%s]]''",link_label(titles))
        end
    elseif pcall(function () result = format_param(ids, titles) end) then
        return frame:preprocess(result)
    else
        return frame:preprocess(str .. "[[Category:Pages with script errors]]")
    end
end

return p
Advertisement