Module:Transcript

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search

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

-- <pre>
local p = {}

local remove_templates = {
	'dialogue',
	'transcript character',
}

local ignore_templates = {
	'!',
	'*',
	'anchortext',
	'clear',
	'color',
	'colour',
	'kg',
	'nbsp',
	'qact',
	'quote',
	'sic',
	'titleanchor',
}

local headerSize = {
	['=='] = 18,
	['==='] = 17,
	['===='] = 15,
	['====='] = 12,
	['======'] = 11
}

function p.main(frame)
	local args = frame:getParent().args
	local ret = {}

        -- for handling multiple transcript transclusions on single page
	for _, v in ipairs(args) do
		table.insert(ret, p._main(v))
	end

	return frame:preprocess(table.concat(ret, '\n'))
end

function p._main(title)
	local ret = mw.html.create('div')

	if title == nil then
		ret:addClass('seealso')
			:wikitext(string.format(
				'[[Transcript:%s]] does not exist. Please create this page or correct this query.',
				title
			))
		return tostring(ret)
        end

	local _t = mw.title.new('Transcript:' .. title)

	if not _t.exists then
		ret:addClass('seealso')
			:wikitext(string.format(
				'[[Transcript:%s]] does not exist. Please create this page or correct this query.',
				title
				))
		return tostring(ret)
	end

	local _tsplit = mw.text.split(_t:getContent(),'\n')

	
	ret	:tag('div')
			:addClass('seealso')
			:wikitext(string.format(
				'The following text is transcluded from [[Transcript:%s]].',
				title
				))
		:done()

	local txt = ret:tag('div')
			:addClass('transcript')

	for _, _v in ipairs(_tsplit) do
		local v = _v:lower()

		local lineToAdd = _v

		local addLine = true
		-- look for lines that are a single template
		if v:find('^{{.*}}$') then
			-- filter template name
			local t_name = v:gsub('template:',''):match('{{(.-)[|}]')

			-- check the kill list
			for _, w in ipairs(remove_templates) do
				if w == t_name then
					addLine = false
					break
				end
			end

			-- other templates
			-- we'll see later
		end

		-- formatting for headers changes
		if addLine then
			local headingtype, headingtitle = _v:match('(=+)(.-)(=+)')

			if headingtype and headingtitle and headerSize[headingtype] then
				addLine = false
				txt	:tag('div')
						:css({ ['font-size'] = headerSize[headingtype] .. 'px', ['font-weight'] = 'bold', ['padding-top'] = '10px' })
						:wikitext(headingtitle)
					:done()
					:tag('hr')
					:done()
			end

			-- remove category transclusion
			lineToAdd = lineToAdd:gsub('%[%[[Cc]ategory:.-%]%]','')
						-- remove default sorting
						:gsub('{{DEFAULTSORT.+}}','')
		end

		if addLine then
			txt:newline()
			txt:wikitext(lineToAdd)
		end

	end
				
	return tostring(ret)
end

return p