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

local p = {}
local list_of_realities = mw.loadData( 'Module:Disambiguation/Realities' )
local h = require("Module:HF")
local pagetype = require("Module:PageType")
local design = require("Module:Design")
local module_reality = require("Module:Reality")
local getArgs = require('Module:Arguments').getArgs


-- main function used to create entire disambiguation page
function p.disambiguation(frame)
	local args = getArgs (frame)
	local g_type
	local g_main_name = args['main_name']
	local g_main_character = args['Main Character'] or ''
	local g_main_image_size = args['Main Image Size'] or '250px'
	local g_main_version
	local g_main_title
	local g_main_image
	local g_no_image
	local g_logo = args['logo'] or ''
	local g_description = args['description'] or ''
	local g_alternative_names = {}
	local g_include = {}
	local g_exclude = {}
	local g_includeComic = {}
	local g_includeMovie = {}
	local g_includeTV = {}
	local g_includeGame = {}
	local g_teams = {}
	local g_others = {}
	local g_related = {}
	local g_new_text = { {}, {}, {}, {}, {}, {}, {}, {}, {} }
	local g_page = mw.title.getCurrentTitle()
	local g_pagename = g_page.text
	
	local list = ''
	local all_versions = {}
	local comics = {}
	local movies = {}
	local tv = {}
	local games = {}
	local headers1 = { 'Alternate Reality Versions', 'Movies', 'Television', 'Video Games' }
	local headers2 = { 'Others', g_pagename..' Related', 'Teams' }
	local tables = {}
	local s = ''
	local s1 = ''
	local s2 = ''
	local output = {}

	table.insert(output, '__NOTOC__')
	table.insert(output, g_logo)
	-- if 'main_name' parameter is not empty, then use DPL to create a list of alternate versions
	if not h.isempty(g_main_name)
		then
			g_main_version = args['main']
			g_main_title = args['main_title'] or g_main_name
			g_main_image = args['main_image']
			table.insert(output, p.createBox_MainVersion({pagename = g_main_version, 
														  image = g_main_image,
														  imageSize = g_main_image_size,
														  description = g_description
														}) 
						)

			g_type = args['type'] or pagetype.get_page_type(g_main_version)
			g_noimage = args['noimage']
			for i = 1,100 do
				if not h.isempty(args['alternative'..i])
					then table.insert(g_alternative_names, args['alternative'..i])
				end
				if not h.isempty(args['include'..i])
					then table.insert(g_include, args['include'..i])
				end
				if not h.isempty(args['includeComic'..i])
					then table.insert(g_includeComic, args['includeComic'..i])
				end
				if not h.isempty(args['includeMovie'..i])
					then table.insert(g_includeMovie, args['includeMovie'..i])
				end
				if not h.isempty(args['includeTV'..i])
					then table.insert(g_includeTV, args['includeTV'..i])
				end
				if not h.isempty(args['includeGame'..i])
					then table.insert(g_includeGame, args['includeGame'..i])
				end
				if not h.isempty(args['exclude'..i])
					then table.insert(g_exclude, args['exclude'..i])
				end	
			end
			list = frame:preprocess( p.lua_dpl_list(g_main_name, g_main_version, g_type, g_alternative_names, g_exclude, 500)..p.lua_dpl_list(g_main_name, g_main_version, g_type, g_alternative_names, g_exclude, 500, 500) )
			list = string.gsub(list, ' ', '')
			list = h.explode('@', list)
			
			for i = 1, #list do
				s = mw.text.split(list[i], '~') --h.explode('~', list[i])
				s1, s2 = module_reality.lua_get_name_and_reality(s[1])
				table.insert( all_versions, { ['page'] = s[1], ['image'] = s[2], ['title'] = s[3], ['name'] = s1, ['reality'] = s2 } )
			end
			all_versions = h.join_tables(all_versions, p.lua_transform_from_old_format(g_include, true))

			for i = 1, #all_versions do
				s = all_versions[i].reality.type
				if s == 'Game'
					then table.insert(games, all_versions[i])
					elseif s == "TV" 
						then table.insert(tv, all_versions[i])
					elseif s == "Movie" 
						then table.insert(movies, all_versions[i])
					else table.insert(comics, all_versions[i])
				end
			end
			
			comics = h.join_tables(comics, p.lua_transform_from_old_format(g_includeComic, false))
			movies = h.join_tables(movies, p.lua_transform_from_old_format(g_includeMovie, false))
			tv =  h.join_tables(tv, p.lua_transform_from_old_format(g_includeTV, false))
			games = h.join_tables(games, p.lua_transform_from_old_format(g_includeGame, false))

			tables = { comics, movies, tv, games }
			table.insert( output, p.lua_createTOC(tables, headers1, g_pagename, g_main_title) )
			for i = 1,4 do
				 table.insert(output, p.createSection({
							s_table = tables[i], 
							header = headers1[i],
							main_name = g_main_name,
							main_title = g_main_title,
							disambiguation_pagename = g_pagename,
							noimage = g_noimage,
							old_format = false
					}) )
			end
		elseif not h.isempty(g_main_character)
			then
				g_main_character = mw.text.split(h.trim(g_main_character), ';', true)
				table.insert(output, p.createBox_MainVersion({
					pagename = h.break_link(g_main_character[1], 1), 
					text = h.break_link(g_main_character[1], 2), 
					image = h.trim(g_main_character[2]) or 'No Image Available.png',
					imageSize = g_main_image_size,
					description = g_description
				})
				)
	end

	for i = 1,9 do
		table.insert(headers2, args['New Header'..i] or '')
	end

	for i = 1,200 do
		table.insert(g_teams, args['Teams'..i])
		table.insert(g_others, args['Others'..i])
		table.insert(g_related, args['Related'..i])
		for j = 1,9 do
			table.insert(g_new_text[j], args['New Header'..j..'_'..i])
		end
	end
	g_teams = p.lua_transform_from_old_format(g_teams, false)
	g_others = p.lua_transform_from_old_format(g_others, false)
	g_related = p.lua_transform_from_old_format(g_related, false)

	for j = 1,9 do
		g_new_text[j] = p.lua_transform_from_old_format(g_new_text[j], false)
	end
	
	tables = { g_others, g_related, g_teams, 
			   g_new_text[1], g_new_text[2], g_new_text[3], g_new_text[4], g_new_text[5],
			   g_new_text[6], g_new_text[7], g_new_text[8], g_new_text[9]}

	for i = 1,#tables do
		table.insert(output, p.createSection({
							s_table = tables[i], 
							header = headers2[i],
							main_name = g_main_name,
							main_title = g_main_title,
							disambiguation_pagename = g_pagename,
							noimage = g_noimage,
							old_format = true
			}) )
	end

	--return '<div valign="top">'..table.concat(output)..'</div>'
	return frame:preprocess( table.concat(output) )
end


function p.lua_sort_by_reality(a, b)
	local reality1 = a.reality.number
	local reality2 = b.reality.number
	local earth33 = {'33 1/3', '33⅓'}
	local output
	
	if h.isempty(reality1) or h.isempty(reality2)
		then output = false
		else
			if h.in_list(earth33, reality1)
				then reality1 = 33.3
			end
			if h.in_list(earth33, reality2)
				then reality2 = 33.3
			end
			if tonumber(reality1) ~= nil
				then
					if tonumber(reality2) ~= nil
						then output = tonumber(reality1)<tonumber(reality2)
						else output = true
					end
				elseif tonumber(reality2) ~= nil
					then output = false
					else output = reality1<reality2
			end
	end

	return output
end


function p.lua_dpl_list(main_name, main_version, main_version_type, alternative_names, exclude, count, offset)
	local template = '{Marvel Database:'..main_version_type..' Template}'
	local image = template..':Image, '
	local title = ''
	local s = ''
	local output = {}
	
	if not h.isempty(alternative_names)
		then alternative_names = mw.text.listToText(alternative_names, '%¦', '%¦')..'%'
		else alternative_names = ''
	end
	if not h.isempty(exclude)
		then exclude = mw.text.listToText(exclude, '%¦', '%¦')..'%'
		else exclude = ''
	end

	main_version_type = string.lower(main_version_type)
	if  main_version_type == 'character' or main_version_type == 'characters'
		then title = template..':CurrentAlias'
		else title = template..':OfficialName'
	end
	
	table.insert(output, '{{#dpl:')
	table.insert(output, '|category = '..main_version_type..'s')
	table.insert(output, '|namespace = ')
	table.insert(output, '|titlematch = '..main_name..' (%¦'..alternative_names)
	table.insert(output, '|nottitlematch = '..main_version..'¦'..exclude)
	table.insert(output, '|include = '..image..title)
	table.insert(output, '|mode = userformat')
	table.insert(output, '|secseparators = %PAGE%~,~,,@')
	table.insert(output, '|noresultsheader = &#x20;')
	--table.insert(output, '|eliminate = templates')
	table.insert(output, '|allowcachedresults = true')
	if not h.isempty(count)
		then table.insert(output, '|count ='..count)
	end
	if not h.isempty(offset)
		then table.insert(output, '|offset ='..offset)
	end
	table.insert(output, '}}')
	
	return table.concat(output)
end


-- transform string in 'page; image' format into table
function p.lua_transform_from_old_format(list, redlinks)
	local i
	local s_page = ''
	local s_title = ''
	local s_image = ''
	local s_reality = ''
	local s_name = ''
	local s = ''
	local output = {}

	if not h.isempty(list)
		then
			for i = 1, #list do
				s = list[i]
				if not h.isempty(s)
					then
						s = mw.text.split(h.trim(s), ';', true)
						s_page = h.break_link(s[1], 1)
						s_title = h.break_link(s[1], 2)
						s_image = h.trim(s[2] or '')
						s_name, s_reality = module_reality.lua_get_name_and_reality(s_page)
						if not h.isempty(s_reality) and s_title == s_page
							then s_title = s_name
						end
						if redlinks == true
							then s_title = ' '..s_title
						end

						table.insert( output, { 
												['page'] = s_page, 
												['image'] = s_image, 
												['title'] = s_title, 
												['name'] = s_name, 
												['reality'] = s_reality } 
									)
				end
			end
	end
	
	return output
end


-- creates box with image, link to page (if needed) and link to reality (if stated)
function p.lua_createBox(s_table, main_name, main_title, disambiguation_pagename, noimage, old_format)
	local image = ''
	local name = ''
	local reality = ''
	local output = ''

	if not h.isempty(s_table)
		then
			image = p.createBox_image( { 
							image = s_table.image, 
							page = s_table.page, 
							noimage = noimage } )
			name = p.createBox_link( {
							page = s_table.page, 
							name = s_table.name, 
							title = s_table.title, 
							main_name = main_name, 
							main_title = main_title,
							disambiguation_pagename = disambiguation_pagename,
							old_format = old_format } )
			reality = p.createBox_reality( {s_table.reality} )

			output = tostring( mw.html.create( 'div' )
					:css( 'width', '150px' )
					:css( 'float', 'left' )
					:css( 'text-align', 'center' )
					:css( 'margin-left', '10px' )
					:wikitext( image..name..reality )
					:done() )
	end
	
	return output
end


function p.createBox_image(frame)
	local args = getArgs (frame)
	local image = args['image']
	local imageSize = args['imageSize'] or '150px'
	local page = args['page']
	local noimage = args['noimage'] or 'No Image Available.png'
	local link = ''
	local output
	
	if not h.isempty(page)
		then 
			link = '|link='..page
			if (string.find(page, ' Vol ') ~= nil and pagetype.get_page_type(page) == 'Volume') or
			   (string.find(page, '(Story Arc)') ~= nil and pagetype.get_page_type(page) == 'Story Arc') 
				then imageSize = '100px'
			end
	end
	
	if h.isempty(image) or image == "No Image Available At All.png"
		then image = noimage
	end
	
	output = mw.html.create( 'div' )
			 :css( 'height', '150px' )
			 :css( 'overflow', 'hidden' )
			 :wikitext( '[[File:'..image..'|'..imageSize..link..']]' )
			 :done() 

	return tostring(output)
end


function p.createBox_link(frame)
	local function bold_link(text1, text2) 
		return tostring( mw.html.create( 'span' )
							:css( 'font-weight', 'bold' )
							:wikitext( h.Link(text1, text2) )
							:done() 
						)
	end
	local args = getArgs (frame, {trim = false})
	local old_format = args['old_format']
	local page = args['page']
	local name = args['name']
	local title = args['title']
	local main_name = args['main_name']
	local main_title = args['main_title']
	local disambiguation_pagename = args['disambiguation_pagename']
	local output = ''
	local subname = ''
	local i = ''

	if old_format == true -- check for old disambiguation pages
		then 
			if (string.find(page, ' Vol ') ~= nil and pagetype.get_page_type(page) == 'Volume')
				then output = '{{cvd|'..page..'}}'
				else output = bold_link(page, title)
			end
		else
			i, subname = string.match(name, '(.+)%((.+)%)')
			if not h.isempty(title)
				then
					title = h.break_link (title, 2)
					if not h.in_list({main_title, main_name, disambiguation_pagename}, title)
						then 
							title = mw.text.trim(title)
							if not h.in_list({main_title, main_name, title, disambiguation_pagename}, name)
								then 
									if not h.isempty(subname)
										then name = '<br />('..h.trim(i)..')<br />('..subname..')'
										else name = '<br />('..name..')'
									end
									output = bold_link(page, title..name)
								else output = bold_link(page, title)
							end
						elseif not h.in_list({main_title, main_name}, name)
							then 
								if not h.isempty(subname)
									then name = h.trim(i)..'<br />('..subname..')'
								end
								output = bold_link(page, name)  
					end
				elseif name ~= main_name and name ~= main_title
					then 
						if not h.isempty(subname)
							then name = h.trim(i)..'<br />('..subname..')'
						end
						output = bold_link(page, name)
			end
	end
	
	if not h.isempty(output)
		then output = output..'<br />'
	end
	
	return output
end
	

function p.createBox_reality(frame)
	local args = getArgs (frame)
	local reality = args[1]
	local info
	local output = ''
	
	if not h.isempty(reality)
		then
			if type(reality) == 'table'
				then info = reality
				else info = module_reality.get_reality_info({reality})
			end
			if info.name ~= info.description
				then output = info.description..'<br />([['..info.name..']])'
				else output = '([['..info.name..']])'
			end
	end
	
	return output
end


function p.createBox_MainVersion(frame)
	local args = getArgs (frame)
	local pagename = args['pagename']
	local text = args['text'] or nil
	local image = args['image']
	local imageSize = args['imageSize']
	local description = args['description']
	local s_name
	local s_reality
	local div_box
	local div_clear = tostring(mw.html.create( 'div' ):css( 'clear', 'left' ):done() )
	local output = ''

	s_name, s_reality = module_reality.lua_get_name_and_reality(pagename)
	s_name = string.match(s_name, '(.+) %(') or s_name
	s_name = text or s_name
	s_reality = p.createBox_reality({s_reality})
	s_name = '<br />'..p.createBox_link({page = pagename, title = s_name, old_format = true})
	image = '[[File:'..image..'|'..imageSize..'|link='..pagename..']]'
	
	if not h.isempty(description)
		then
			div_box = tostring(mw.html.create( 'div' )
						:css( 'font-size', '125%' )
						:css( 'text-align', 'center' )
						:css( 'float', 'left' )
						:css( 'margin-right', '10px')
						:wikitext(image..s_name..s_reality)
						:done() 
					  )
			output = div_box..description
		else
			output = tostring(mw.html.create( 'div' )
						:css( 'font-size', '125%' )
						:css( 'text-align', 'center' )
						:attr('align', 'center')
						:wikitext(image..s_name..s_reality)
						:done() 
					  )
	end
	
	return output..div_clear
end


function p.lua_createTOC(versions, headers, pagename, main_title)
	local output = {}
	local bold_middot = tostring( mw.html.create( 'span' ) 
				:css( 'font-weight', 'bold' )
				:wikitext( "&nbsp;&middot; " )
				:done() 
			)
	local div_toc = mw.html.create( 'div' )
					:attr( 'align', 'center' )
					:css( 'width', '100%' )
					:css( 'text-align', 'center' )
					:css( 'border-top', '1px solid grey' )
					:css( 'border-bottom', '1px solid grey' )
					:css( 'margin-top', '1em')
					:done()

	for i = 1, #versions do
		if not h.isempty(versions[i])
			then table.insert(output, h.Link(pagename..'#'..headers[i], headers[i]) )
		end
	end
	
	if h.exists(pagename..' Comic Books')
		then table.insert(output, h.Link(pagename..' Comic Books', pagename..' Comics') )
		elseif not h.isempty(main_title) and h.exists(main_title..' Comic Books')
			then table.insert(output, h.Link(main_title..' Comic Books', main_title..' Comics') )
	end
	
	if not h.isempty(output)
		then 
			div_toc = div_toc:wikitext( mw.text.listToText(output, bold_middot, bold_middot) ):done()
			output = tostring(div_toc)
		else output = ''
	end
				
	return output
end


function p.createSection(frame)
	local args = getArgs (frame)
	local s_table = frame['s_table']
	local header = frame['header']
	local main_name = frame['main_name']
	local main_title = frame['main_title']
	local disambiguation_pagename = frame['disambiguation_pagename']
	local noimage = frame['noimage']
	local old_format = frame['old_format']
	local row4 = {}
	local output = ''
	local function div_row(row)
		local output = mw.html.create( 'div' )
					:css( 'display', 'table' )
					:css( 'margin', '0 auto' )
					:wikitext( row )
					:done()
		return tostring(output)
	end
	local div_clear = tostring(mw.html.create( 'div' ):css( 'clear', 'left' ):done() )
	
	if not h.isempty(s_table) 
		then
			if not old_format
				then table.sort(s_table, p.lua_sort_by_reality)
			end
			
			output = {}
			if not h.isempty(header)
				then header = design.add_header(header, 2, 'center')
				else header = ''
			end
			for i = 1, #s_table do
				if #row4 < 4
					then table.insert(row4, p.lua_createBox(s_table[i], main_name, main_title, disambiguation_pagename, noimage, old_format) )
				end
				if #row4 == 4 or i == #s_table
					then 
						table.insert( output, div_row(table.concat(row4)) )
						table.insert( output, div_clear )
						row4 = {}
				end
			end
			output = header..table.concat(output)
	end
	
	return output
end


------------------------------------------------------------------------------------------------
-- used in Template:Disambiguation Text
function p.disambiguation_text(frame)
	local s = frame.args[1]
	local reality = ''
	local link
	local text
	local comment = ''
	local i
	local output = ''

	if not h.isempty(s)
		then
			s = h.explode(';', s)
			if tonumber(s[1]) ~= nil or string.match(s[1], '^TRN%d+') ~= nil or list_of_realities[s[1]] ~= nil
				then
					reality = { p.lua_reality(s[1]) }
					output = reality[1]..'<br />('..h.Link(reality[2])..')'
				else
					if h.is_link(s[1])
						then
							i = string.find(s[1], ']]', 1, true)
							link = h.break_link(s[1],1)
							text = h.break_link(s[1],2)
							if #s[1] > i+2
								then comment = string.sub(s[1], i+2, #s[1])
							end
							if not h.isempty( string.find(link, '(',1,true) )
								then 
									reality = { p.lua_reality(link, true) }
									reality = '<br />'..reality[1]..'<br />('..h.Link(reality[2])..')'
							end 
						else
							link = s[1]
							text = s[1]
					end
					output = tostring( mw.html.create( 'span' )
								:css( 'font-weight', 'bold' )
								:wikitext( h.Link(link,text)..comment )
								:done() )..reality
			end
	end
			
	return output
end

function p.lua_reality(r, e)
	local j
	local k
	local s
	local s1
	local s2
	local s3
	
	if e == true or e == "true"
		then 
			s1 = string.reverse(r)
			s2 = string.find(s1, "( ",1,true) or string.find(s1, "(_",1,true)
			s3 = string.find(s1, ")",1,true)
			r = string.reverse(string.sub(s1, s3+1, s2-1))
	end
	
	j,k = string.find(string.lower( r ),"earth-",1,true)
	if j ~= nil
		then s = string.sub(r,k+1,string.len(r))
		else s = r
	end
	s2 = list_of_realities[s]
	if s2 ~= nil
		then return s2[1], s2[2], s2[3]
		elseif tonumber(r) ~= nil or string.match(r, '^TRN%d+') ~= nil
			then return "Earth-"..r, "Earth-"..r, "Comic"
			else return r, r, "Comic"   
	end
end

function p.reality (frame)
	local r = h.break_link(frame.args[1], 1)
	local i = tonumber(frame.args[2])
	local e = frame.args[3] or frame.args["extract"]
	local s1
	local s2
	local s3

	s1, s2, s3 = p.lua_reality(r, e)
	if i == 3
		then return s3
		elseif i == 2 
			then return s2
			else return s1
	end
end

return p
Community content is available under CC-BY-SA unless otherwise noted.