Module:Ep ref/sandbox

local p = {} -- function to create and format the reference function p.epRef(frame)

-- receiving 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

-- convert the input into the standardized epCode local epMatcher = require('Module:Ep/Matcher') local epCode = epMatcher.matchCode(ep) -- error message if code doesn't exist; otherwise, get the full episode string if epCode == '0x00' and not isnotempty(vod) then return frame:preprocess(" ") elseif isnotempty(vod) then -- ignore epCode when it's a custom video link epValue = ep   else epValue = p.epForRef(epCode) 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[epCode .. part] else url = ytURL[epCode] or ytURL["default"] end --add timestamp to youtube URL local timedURL local longURL = false

if isnotempty(url) then longURL = string.find(url, "?") end -- create linked startTime, if available if isnotempty(startTime) then startTime = p.formatTimestamp(startTime) timedURL = p.buildLinkedTimestamp(url, startTime) end if isnotempty(endTime) then endTime = p.formatTimestamp(endTime) end if isnotempty(timedURL) then startTime = timedURL end local refText = 'See ' .. epValue

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 = "" end if isnotempty(intro) then refText = intro .. " " .. refText end if isnotempty(cite) then refText = refText .. tostring(supCite) .. citeCategory end if isnotempty(more) then refText = refText .. " " .. more end

-- create wikitext reference local reference = "" .. refText .. " "   return frame:preprocess(reference) end function p.epForRef(epCode) -- get the title and pagename for that epCode local epInfo = require('Module:Ep/Info') local epName = epInfo.title(epCode) local pageName = epInfo.pagename(epCode) -- if there is no epCode code, then just return the episode name without any further formatting if not isnotempty(epCode) then return epName end -- (epCode) local small = mw.html.create('small') small :wikitext("(" .. epCode .. ")")   if epCode == '0x00' or not isnotempty(epName) then local pageName = 'Unknown episode' local epName = pageName end local epValue if pageName ~= epName then epValue = '"' .. epName .. '"' elseif isnotempty(pageName) then epValue = '"' .. pageName .. '"' else epValue = '' end

local fullEpValue = epValue .. " " .. tostring(small) if isnotempty(hide) then return epValue else return fullEpValue end

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

-- convert either MM:SS or HH:MM:SS to seconds function timestampToSeconds(s) local total_seconds local t = split(s, ':') if #t == 3 then total_seconds = 3600*t[1] + 60*t[2] + t[3] elseif #t == 2 then total_seconds = 60*t[1] + t[2] end return total_seconds end

-- convert seconds to either MM:SS or HH:MM:SS (depending on length) function secondsToTimestamp(seconds) local formatted if tonumber(seconds) and tonumber(seconds) >= 3600 then formatted = string.format("%d:%02d:%02d",           math.floor(seconds / 3600) % 60,            math.floor(seconds / 60) % 60,            math.floor(seconds) % 60) elseif tonumber(seconds) then formatted = string.format("%d:%02d",       math.floor(seconds / 60) % 60,        math.floor(seconds) % 60) end return formatted end

-- zeropad a timestamp, or convert seconds to timestamp function p.formatTimestamp(timeValue) local seconds if timeValue:find(':') then seconds = timestampToSeconds(timeValue) elseif tonumber(timeValue) then seconds = timeValue end timestamp = secondsToTimestamp(seconds) return timestamp end

-- create suffix for appending to YT URL function timestampToYtSuffix(timeValue) local suffix local timestamp = p.formatTimestamp(timeValue) timeSnippet = split(timestamp, ':') if #timeSnippet == 3 then suffix = string.format("%dh%02dm%02ds", timeSnippet[1], timeSnippet[2], timeSnippet[3]) elseif #timeSnippet == 2 then suffix = string.format("%dm%02ds", timeSnippet[1], timeSnippet[2]) end suffix = 't=' .. suffix return suffix end

-- combine YT url and url-formatted timestamp function buildYouTubeLink(videoLink, timestamp) local fullLink local suffix = timestampToYtSuffix(timestamp) if videoLink:find('youtube.com/%w+') then fullLink = videoLink .. '&' .. suffix elseif videoLink:find('youtu.be/%w+') then fullLink = videoLink .. '?' .. suffix end return fullLink end

function p.buildLinkedTimestamp(videoLink, timestamp) local url local linkedTimestamp timestamp = p.formatTimestamp(timestamp) url = buildYouTubeLink(videoLink, timestamp) linkedTimestamp = '[' .. url .. ' ' .. timestamp .. "] "	return linkedTimestamp end

return p