FANDOM


-- Implements [[Template:Transcript]]
-- <nowiki>
 
-- 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.->(.-)</[ ]*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("{{ ?[Hh]r ?}}",''); -- {{ hr }} 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 = '<nowiki/>'
                                .. (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 &nbsp;]") or '')
                            .. (hr > 0 and "\n\n{{ hr }}" 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 [[Help:Importing YouTube subtitles#Creating subtitles|contributing closed captions]] for this episode in the meantime![[Category:Episode pages without transcripts]]";
        end
 
        return frame:preprocess(compiledTrans);
    end
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.