Module:Sandbox/User:Fjara/Sandbox/Sieve

From Illerai
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/User:Fjara/Sandbox/Sieve/doc

local p = {}

--{{Transcript|link=Rune Mysteries|historical=yes}}
--{{#if:{{{historical|}}}|historical}}

local contains = require('Module:Array').contains
local unique = require('Module:Array').unique
local yesNo = require('Module:Yesno')

local split = mw.text.split
local title = mw.title

local CurrentTitle = title.getCurrentTitle().text

local TranscriptTypes = {
	"npc",
	"pet",
	"quest",
	"miniquest",
	"event",
	"item",
	"scenery",
}

local Phrases = {
	['npc'] = 'on an NPC',
	['pet'] = 'on a pet',
	['quest'] = 'on a quest',
	['miniquest'] = 'on a miniquest',
	['event'] = 'on an event',
	['item'] = 'on an item',
	['scenery'] = 'on a scenery object',
}

local RemoveTemplates = {
	'transcript',
	'fromgame',
	'god letter',
	'incomplete',
	'inuse',
	'construction',
	'lore',
	'postbag',
	'transcript list',
}

-- Unused
local IgnoreTemplates = {
	'!',
	'*',
	'anchortext',
	'clear',
	'color',
	'colour',
	'kg',
	'nbsp',
	'qact',
	'quote',
	'mes',
	'sic',
	'titleanchor',
}

local headerSize = {
	['=='] = 1.25,
	['==='] = 1.2,
	['===='] = 1,
	['====='] = 0.95,
	['======'] = 0.9,
}

function transclusion(pageContent)
	local pageLines = split(pageContent, '\n')
	local ret = mw.html.create('div'):addClass('transcript')
	for i, line in ipairs(pageLines) do
		local addLine = true
		-- Don't include lines that are just a template on the RemoveTemplates list
		if(line:find('^{{.*}}$')) then
			local template = line:lower():gsub('template:',''):match('{{(.-)[|}]')
			if(contains(RemoveTemplates, template)) then
				addLine = false
			end
		end
		
		-- Format headers
		if(addLine) then
			local headingType, headingTitle = line:match('(=+)(.-)(=+)')
			if(headingType and headingTitle and headerSize[headingType]) then
				addLine = false
				ret:tag('div'):css({ ['font-size'] = headerSize[headingType] .. 'em', ['font-weight'] = 'bold', ['padding-top'] = '0.5em' }):wikitext(headingTitle):done()
					:tag('hr'):done()
			end

			-- Remove categories and default sorting
			line = line:gsub('%[%[[Cc]ategory:.-%]%]',''):gsub('{{DEFAULTSORT.+}}','')
		end
		if(addLine) then
			ret:newline()
			ret:wikitext(line)
		end
	end
	return tostring(ret)
end

function transcludePages(pageList)
	local ret = {}
	for i, page in ipairs(pageList) do
		local pageInfo = title.new('Transcript:' .. page)
		if(pageInfo.exists) then -- Add styling
			table.insert(ret, 'The following text is transcluded from [[Transcript:' .. page .. ']].' .. transclusion(pageInfo:getContent()))
		else --Add styling
			table.insert(ret, '[[Transcript:' .. page .. ']] does not exist. Create this page or correct the query.')
		end
	end
	return ret
end

function transcriptList(pageList)
	return '<br/>*' .. table.concat(pageList, '<br/>*')
end

-- Add link overrides? or at least remove Transcript: from links?
function getTranscripts(pages, cat)
	query = {}
	table.insert(query, cat)
	query.offset = 0
	query.limit = 500
	local t1 = os.clock()
	local smwData = mw.smw.ask(query)
	local t2 = os.clock()
	if(smwData == nil or #smwData == 0) then
		return pages
	end
	mw.log(string.format('SMW: entries %d, time elapsed: %.3f ms.', #smwData, (t2 - t1) * 1000))
	for i,v in ipairs(smwData) do
		table.insert(pages, v)
	end
	return pages
end

-- Should quest/miniquest keep the messagebox?
function p._main(frame, args)
	transclude = yesNo(args.transclude or '', false)
	
	if(not transclude) then
		local pageType = args[1]:lower() or 'none'
		local pageList = {}
		table.insert(pageList, args[2] or CurrentTitle)
	
		if(pageType == 'npc') then
			-- Test multiple getTranscript calls versus getting print out of the category true/false
			cat = '[[Transcript NPCs::' .. CurrentTitle .. ']]'
			pageList = getTranscripts(pageList, cat .. '[[Category:NPC dialogue]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:Item transcript]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:Scenery transcript]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:Miscellaneous transcript]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:Quest transcript]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:Miniquest transcript]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:Event transcript]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:Postbag from the Hedge transcripts]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:God letters transcripts]]')
			pageList = unique(pageList) -- assuming this will keep the order
		elseif(pageType == 'quest') then
			cat = '[[Category::' .. CurrentTitle .. ']]'
			pageList = getTranscripts(pageList, cat .. '[[Category:Quest journal transcript]]')
			pageList = getTranscripts(pageList, cat .. '[[Category:Historical transcript]]')
		end
		
		local phrase = ''
		if(#pageList == 1) then
			phrase = 'This article ' .. Phrases[pageType] .. ' has an associated dialogue page:'
		else
			phrase = 'This article ' .. Phrases[pageType] .. ' has the associated dialogue pages:'
		end
		 
		return phrase .. transcriptList(pageList)
	elseif(transclude) then
		local pageList = {}
		for _, page in ipairs(args) do
			table.insert(pageList, page)
		end
		
		if(#pageList == 0) then
			table.insert(pageList, CurrentTitle)
		end
		
		return frame:preprocess(table.concat(transcludePages(pageList), '\n'))
	end
end

function p.main(frame)
	local args = frame:getParent().args
	--mw.logObject(args)
	return p._main(frame, args)
end

return p