Module:Disambiguation

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('Dev:Arguments').getArgs local g_noimage

- function p.main(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_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_includeLiveAction = {} local g_includeAnimated = {} 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 live_action = {} local animated = {} local games = {} local headers1 = { 'Comic Versions', 'Live-Action Versions', 'Animated Versions', 'Video Game Versions' } local headers2 = { 'Others', g_pagename..' Related', 'Teams' } local tables = {} local s = '' local s1 = '' local s2 = '' local output = {}

g_noimage = args['noimage'] or 'No Image Available.png' table.insert(output, '') 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) 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['includeLiveAction'..i]) then table.insert(g_includeLiveAction, args['includeLiveAction'..i]) end if not h.isempty(args['includeAnimated'..i]) then table.insert(g_includeAnimated, args['includeAnimated'..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 if g_type == 'Team' or g_type == 'Organization' then list = p.lua_dpl_list(g_main_name, g_main_version, 'Team', g_alternative_names, g_exclude, 500)..p.lua_dpl_list(g_main_name, g_main_version, 'Team', g_alternative_names, g_exclude, 500, 500) list = list..p.lua_dpl_list(g_main_name, g_main_version, 'Organization', g_alternative_names, g_exclude, 500)..p.lua_dpl_list(g_main_name, g_main_version, 'Organization', g_alternative_names, g_exclude, 500, 500) list = frame:preprocess(list) else 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) ) end list = string.gsub(list, '&#x20;', '') 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 == "Animated" then table.insert(animated, all_versions[i]) elseif s == "Live Action" then table.insert(live_action, 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)) live_action = h.join_tables(live_action, p.lua_transform_from_old_format(g_includeLiveAction, false)) animated = h.join_tables(animated, p.lua_transform_from_old_format(g_includeAnimated, false)) games = h.join_tables(games, p.lua_transform_from_old_format(g_includeGame, false))

tables = { comics, live_action, animated, 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, 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,9 do		table.insert(headers2, args['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 for j = 1,9 do			table.insert(g_new_text[j], args['Box'..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, old_format = true }) )	end

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, '') 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, old_format) local image = '' local name = '' local reality = '' local output = ''

if not h.isempty(s_table) then image = p.createBox_image(s_table.image, s_table.page) 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 = '\n'..image..'|'..name..reality..'|link='..s_table.page end return output end

- function p.createBox_image(image, page) --local link = '' local output = image --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 output = g_noimage end

return output end

- function p.createBox_link(frame) local function bold_link(text1, text2) return design.span( h.Link(text1, text2) ).bold 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 = '' 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 = ' ('..h.trim(i)..') ('..subname..')' else name = ' ('..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)..' ('..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)..' ('..subname..')' end output = bold_link(page, name) end end

if not h.isempty(output) then output = output..' '	end

return output end

- function p.createBox_reality(reality) 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 = h.Link(info.name, h.break_link(info.description, 2)..' ('..info.name..')') else output = h.Link(info.name, '('..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 = ' '..p.createBox_link({page = pagename, title = s_name, old_format = true})	image = ''	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( " &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 old_format = frame['old_format'] local div = mw.html.create('div'):attr('class', 'navfaces') local output = ''

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				table.insert(output, p.lua_createBox(s_table[i], main_name, main_title, disambiguation_pagename, old_format) ) end output = '' output = tostring( div:wikitext(output) ) output = header..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]..' ('..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 = ' '..reality[1]..' ('..h.Link(reality[2])..')'							end 						else							link = s[1]							text = s[1]					end					output = design.span(h.Link(link,text)..comment).bold..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