Module:Transcript

-- Implements Template:Transcript --

-- todo: Consider partitioning main function? It's quite large.

function parseMetadata(data_str) local trim = mw.text.trim; local metadata = {}; for k,v in data_str:gmatch("[\*](.-)\n[\*][\*](.-)\n") do       metadata[trim(k):lower] = trim(v); end return metadata; end

return { main = function(frame) local page   = frame.args.source or ("Transcript:" .. tostring( mw.title.getCurrentTitle )); local content = mw.title.new(page):getContent; local compiledTrans = ''; if (content) then -- Transcript page exists local metadata,transcript = content:match "^(.-)<[ ]*pre.->(.-)"; metadata = parseMetadata(metadata); -- Line parsing status local firstLineReached = false; -- indicates whether the line number counter should count local currLine = 0;            -- line number counter local realLine = 0;            -- for debugging purposes local dialTime = {             -- hold dialog timestamp info start = {h=0, m=0, s=0, r=0}, finish = {h=0, m=0, s=0, r=0} }           local stab,etab = dialTime.start,dialTime.finish; -- tab aliases -- Parse .sbv lines local success,err = pcall(function               transcript:gsub("(.-)\n", function(linetxt) local linetxt,hr = linetxt:gsub("",''); -- support local linetype = ( {[0]='timestamp', 'dialog', 'spacer'} )[currLine % 3]; if linetype == "timestamp" and (linetxt:gsub("%s",'') ~= "") then firstLineReached = true; local startTime,endTime = linetxt:match "^([^,]+),(.+)"; -- Convert timestamp to time table stab.h, stab.m, stab.s = startTime:match "^(%d):(%d%d):(%d%d)\.%d%d%d"; etab.h, etab.m, etab.s = endTime :match "^(%d):(%d%d):(%d%d)\.%d%d%d"; for _,t in pairs(dialTime) do                           -- Change to number t.h, t.m, t.s = tonumber(t.h), tonumber(t.m), tonumber(t.s); t.r = t.h * 3600 + t.m * 60 + t.s;                       end elseif linetype == "dialog" or (linetype == "spacer" and linetxt:gsub('%s','') ~= "") then local spkr,dial = linetxt:match "^[-–]?(.-):[ ]*(.+)"; if (spkr) then spkr = tostring(mw.html.create("span"):wikitext(spkr..': ')); else linetxt = ' ' .. (linetxt:match("^([\[*-]).+(%1)$") and ("''" .. linetxt .. "''") or linetxt); end compiledTrans = compiledTrans .. ( spkr and ("'" .. spkr .. "'" .. dial) or (":" .. linetxt) ) .. (metadata.url and (' [' .. metadata.url .. "#t=" .. stab.r .. "s ]") or '') .. (hr > 0 and "\n\n" or '') .. '\n\n'; firstLineReached = (linetype ~= "spacer"); elseif linetype == "spacer" then firstLineReached = true; end currLine = firstLineReached and (currLine + 1) or currLine; realLine = realLine + 1; return ''; end)           end) if not success then compiledTrans = tostring(mw.html.create("span")                   :addClass("error")                    :wikitext("An error occurred parsing line " .. tostring(realLine) .. " of the transcript.")                   :attr("title", err)) end else -- No transcript compiledTrans = "This transcript should be imported a few days after this episode's premiere on YouTube." .. "\n\nTry contributing closed captions for this episode in the meantime!"; end return frame:preprocess(compiledTrans); end }