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 local noshow = frame.args["noshow"] 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 isempty(vod)) or isempty(ep) then return frame:preprocess(" ") elseif not isempty(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 not isempty(vod) then url = vod elseif not isempty(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 not isempty(url) then longURL = string.find(url, "?") end -- create linked startTime, if available if not isempty(startTime) then startTime = p.formatTimestamp(startTime) timedURL = p.buildLinkedTimestamp(url, startTime) end if not isempty(endTime) then endTime = p.formatTimestamp(endTime) end if not isempty(timedURL) then startTime = timedURL end local refText = 'See ' .. epValue

if not isempty(part) then refText = refText .. ", part " .. part end if not isempty(part) and not isempty(startTime) then refText = refText .. "," end if not isempty(startTime) and not isempty(endTime) then refText = refText .. " from " .. startTime .. " through " .. endTime .. "."   elseif not isempty(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 not isempty(cite) then citeCategory = "" end if not isempty(intro) then refText = intro .. " " .. refText end if not isempty(cite) then refText = refText .. tostring(supCite) .. citeCategory end if not isempty(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 isempty(epCode) then return epName end -- (epCode) local small = mw.html.create('small') small :wikitext("(" .. epCode .. ")")   if epCode == '0x00' or isempty(epName) then local pageName = 'Unknown episode' local epName = pageName end local epValue if pageName:find("Talks Machina Special") and not isempty(noshow) then epName = split(pageName, ': ')[2] elseif pageName:find("Talks Machina") and not isempty(noshow) then epName = pageName:gsub('Talks Machina ', '#') elseif not isempty(pageName) or not isempty(noshow) then epValue = pageName end if pageName ~= epName and not epValue then epValue = pageName .. '|' .. epName end if epValue then epValue = '"' .. epValue .. '"' else epValue = '' end

local fullEpValue = epValue .. " " .. tostring(small) if not isempty(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 if a string is empty function isempty(s) return s == nil or s == '' end

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