Module:Ep ref

From Itora Wiki

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

local p = {}
 
-- function to create and format the reference
function p.epRef(frame)
    local reference = true
    local epSwitcher = require('Module:Ep/Switcher')
 
    -- receiving {{ep ref}} parameters
    local ep = frame.args["ep"] or ""
    local part = frame.args["part"] or nil
    local intro = frame.args["intro"] or nil
    local more = frame.args["more"] or nil
    local vod = frame.args["vod"] or nil
    local group = frame.args["group"] or nil
    local name = frame.args["name"] or nil
    local startTime = frame.args[1] or nil
    local endTime = frame.args[2] or nil
    local cite = frame.args["cite"] or nil
    local nsfw = frame.args["nsfw"] or nil
 
    local epName, CxEE = epSwitcher.epSwitcher(frame, reference)
 
    if isnotempty(CxEE) then epName = "\"[[" .. epName .. "]]\"" end
 
    -- <small>(CxEE)</small>
    local small = mw.html.create('small')
    small
        :wikitext("(" .. CxEE .. ")")
 
    if isnotempty(CxEE) then fullEpName = epName .. " " .. tostring(small)
    else fullEpName = epName end
 
    -- Set empty CxEE value to 0x00 for the purposes of the "Articles needing citations/0x00" category
    if not isnotempty(CxEE) then CxEE = "0x00" end
 
 
    -- if part was used to define a Trial of the Take episode, remove part from final reference
    if CxEE == "1x18" or CxEE == "1x19" or CxEE == "1x20" or CxEE == "1x21" then part = "" end
 
    -- get YouTube URL from Module:Ep/YTURLSwitcher/URLs
    local ytURL = mw.loadData('Module:Ep/YTURLSwitcher/URLs')
    local url
    if isnotempty(vod) then url = vod
    elseif isnotempty(part) then url = ytURL[CxEE .. part] 
    else url = ytURL[CxEE] or ytURL["default"] end
 
    --add timestamp to youtube URL
    local timeSnippet
    local timedURL
    local longURL = false
    local timeLength
 
    if isnotempty(url) then longURL = string.find(url, "?") end
 
    -- split timestamp at colons
    if isnotempty(startTime) then timeSnippet = split(startTime, ":") end
    
    if isnotempty(timeSnippet) then timeLength = #timeSnippet end
    
    -- adding time to standard ("long") YouTube URLs
    if isnotempty(timeSnippet) and longURL and timeLength == 3 then timedURL = "<span class=\"plainlinks\">[" .. url .. "&amp;t=0" .. timeSnippet[1] .. "h0" .. timeSnippet[2] .. "m0" .. timeSnippet[3] .. "s " .. startTime .. "]</span>"
    elseif isnotempty(timeSnippet) and longURL and timeLength == 2 then timedURL = "<span class=\"plainlinks\">[" .. url .. "&amp;t=0" .. timeSnippet[1] .. "m0" .. timeSnippet[2] .. "s " .. startTime .. "]</span>"
    elseif isnotempty(timeSnippet) and longURL and timeLength == 1 then timedURL = "<span class=\"plainlinks\">[" .. url .. "&amp;t=0" .. timeSnippet[1] .. "s " .. startTime .. "]</span>" end
        
    -- adding time to shortened URLS
    if isnotempty(url) and isnotempty(timeSnippet) and not longURL and timeLength == 3 then timedURL = "<span class=\"plainlinks\">[" .. url .. "?t=0" .. timeSnippet[1] .. "h0" .. timeSnippet[2] .. "m0" .. timeSnippet[3] .. "s " .. startTime .. "]</span>"
    elseif isnotempty(url) and isnotempty(timeSnippet) and not longURL and timeLength == 2 then timedURL = "<span class=\"plainlinks\">[" .. url .. "?t=0" .. timeSnippet[1] .. "m0" .. timeSnippet[2] .. "s " .. startTime .. "]</span>"
    elseif isnotempty(url) and isnotempty(timeSnippet) and not longURL and timeLength == 1 then timedURL = "<span class=\"plainlinks\">[" .. url .. "?t=0" .. timeSnippet[1] .. "s " .. startTime .. "]</span>" end
 
    if isnotempty(timedURL) then startTime = timedURL end
 
    local refText = 'See ' .. fullEpName
 
 
    if isnotempty(part) then refText = refText .. ", part " .. part end
 
    if isnotempty(part) and isnotempty(startTime) then refText = refText .. "," end
 
    if isnotempty(startTime) and isnotempty(endTime) then refText = refText .. " from " .. startTime .. " through " .. endTime .. "."
    elseif isnotempty(startTime) then refText = refText .. " at " .. startTime .. "."
    else refText = refText .. "." end
 
 
    -- citation format
    local supCite = mw.html.create('sup')
    supCite
        :wikitext("[''citation needed'']")
 
    local citeCategory
    if isnotempty(cite) then citeCategory = "[[Category:Articles needing citations/" .. CxEE .. "]]" end
 
    if isnotempty(intro) then refText = intro .. "&nbsp; " .. refText end
    if isnotempty(cite) then refText = refText .. tostring(supCite) .. citeCategory end
    if isnotempty(more) then refText = refText .. "&nbsp; " .. more end
    if isnotempty(nsfw) then nsfw = "&nbsp; <span style=\"color: red;\"><abbr style=\"cursor:help;border-bottom:1px dotted red;\" title=\"Not Safe For Work:&nbsp; May include inappropriate content.\">NSFW</abbr></span>." end
    if isnotempty(nsfw) then refText = refText .. nsfw end
 
    -- create wikitext reference
    local reference = "<ref"
    if isnotempty(group) then reference = reference .. " group=" .. group end
    if isnotempty(name) then reference = reference .. " name=" .. name end
    reference = reference .. ">" .. refText .. "</ref>"
 
    return frame:preprocess(reference)
 
 
end
 
-- function to split a string at a specified character
function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end
 
-- function to determine that a string is not empty
function isnotempty(s)
    return s ~= nil and s~= ''
end
 
 
return p