Module:Disambiguation/Sandbox

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

function p.main(frame) local args = getArgs (frame) local g_type local g_main_name = args['main_name'] local g_main_version local g_main_title local g_main_image local g_main_image_size local g_no_image local g_logo local g_description 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 headers = { 'Alternate Reality Versions', 'Movies', 'Television', 'Video Games', 'Others', 'Related', 'Teams' } local tables = { {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} } local s = '' local s1 = '' local s2 = '' local output = {}

-- 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'] g_type = pagetype.lua_getPageType(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, '&#x20;', '') list = h.explode('@', list) for i = 1, #list do               s = h.explode('~', list[i]) s1, s2 = p.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.lua_concatTables(all_versions, p.lua_include(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 ttype == "Movie" then table.insert(movies, all_versions[i]) else table.insert(comics, all_versions[i]) end end comics = h.lua_concatTables(comics, p.lua_include(g_includeComic, true)) movies = h.lua_concatTables(movies, p.lua_include(g_includeMovie, true)) tv = h.lua_concatTables(tv, p.lua_include(g_includeTV, true)) games = h.lua_concatTables(games, p.lua_include(g_includeGame, true)) tables = { comics, movies, tv, games } for i = 1,4 do                table.insert(output, p.createSection({ s_table = tables[i], header = headers[i], main_name = g_main_name, main_title = g_main_title, noimage = g_noimage, old_format = false }) )           end end

for i = 1,5 do       table.insert(headers, 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,5 do           table.insert(g_new_text[j], args['New Header'..j..'_'..i]) end end g_teams = p.lua_include(g_teams, false) g_others = p.lua_include(g_others, false) g_related = p.lua_include(g_related, false)

for j = 1, 5 do       g_new_text[j] = p.lua_include(g_new_text[j], false) end tables = { {}, {}, {}, {}, g_teams, g_others, g_related, g_new_text[1], g_new_text[2], g_new_text[3], g_new_text[4], g_new_text[5] } for i = 5,12 do       table.insert(output, p.createSection({ s_table = tables[i], header = headers[i], main_name = g_main_name, main_title = g_main_title, noimage = g_noimage, old_format = true }) )   end --return ' '..table.concat(output)..' ' return table.concat(output) end

function p.lua_sort_by_reality(a, b)   local reality1 = a.reality.number --p.getRealityInfo({a.reality}).number local reality2 = b.reality.number --p.getRealityInfo({b.reality}).number local output if h.isempty(reality1) or h.isempty(reality2) then output = false else 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 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

if main_version_type == 'Character' 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_include(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.lua_breaklink(s[1], 1) s_title = h.lua_breaklink(s[1], 2) s_image = h.trim(s[2] or '') s_name, s_reality = p.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 lnk to reality (if stated) function p.lua_createBox(s_table, main_name, main_title, 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,                            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( 'padding', '10px' )                    :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 page = args['page'] local noimage = args['noimage'] or 'No Image Available.png' local link = '' if not h.isempty(page) then link = '|link='..page end if h.isempty(image) or image == "No Image Available At All.png" then image = noimage end local output = mw.html.create( 'div' ) :css( 'height', '150px' ) :css( 'overflow', 'hidden' ) :wikitext( '' ) :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 output = '' local subname = '' local i = '' i, subname = string.match(name, '(.+)%((.+)%)')

if old_format == true -- check for old disambiguation pages then output = bold_link(page, title) else if not h.isempty(title) then title = h.lua_breaklink (title, 2) if not h.inlist({main_title, main_name}, title) then title = mw.text.trim(title) if not h.inlist({main_title, main_name, title}, 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.inlist({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(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 = p.getRealityInfo({reality}) end if info.name ~= info.desc then output = info.desc..' ('..info.name..')' else output = '('..info.name..')' end end return output end -- separate 'pagename' into name and reality. If reality is not empty, then information about it is returned from 'list_of_realities' function p.lua_get_name_and_reality(pagename) local s_reverse local s_name = pagename local info = {} local s_reality = {} local i   if not h.isempty(pagename) then pagename = string.gsub(pagename,'_',' ') s_reverse = string.reverse(pagename) i = string.find(s_reverse,'( ',1,true)           j = string.find(s_reverse,')',1,true) if not h.isempty(i) then s_name = string.reverse( string.sub(s_reverse, i+2, #s_reverse) ) s_reality = string.reverse( string.sub(s_reverse, j+1, i-1) ) info = p.getRealityInfo({s_reality}) if not h.isempty(info) then s_reality = info end end end return s_name, s_reality end

function p.getRealityInfo(frame) local args = getArgs (frame) local reality = args[1] local info local s2   local s3    local output = '' if not h.isempty(reality) then reality = string.gsub(reality, 'Earth%-', '') info = list_of_realities[reality] if info ~= nil then output = { ['name'] = info[2], ['desc'] = info[1], ['type'] = info[3], ['number'] = reality } elseif tonumber(reality) ~= nil or string.match(reality, '^TRN%d+') ~= nil then output = { ['name'] = "Earth-"..reality, ['desc'] = "Earth-"..reality, ['type'] = "Comic", ['number'] = reality } --else output = { ['name'] = reality, ['desc'] = reality, ['type'] = "Comic" } end 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 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', 'flex' ) :css( 'justify-content', 'center' ) :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.lua_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, noimage, old_format) ) end if #row4 == 4 or #row4 == #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

--

function p.lua_getReality(reality, link) local j   local k    local s    local s1    local s2    local s3    local output = '' if not h.isempty(reality) then reality = h.lua_breaklink(reality, 1) if link == true or link == "true" then _, reality = p.lua_get_name_and_reality(reality) end j,k = string.find(string.lower( reality ),"earth-",1,true) if j ~= nil then s = string.sub(reality,k+1,#reality) else s = reality end s2 = list_of_realities[s] if s2 ~= nil then output = { ['name'] = s2[2], ['desc'] = s2[1], ['type'] = s2[3] } elseif tonumber(reality) ~= nil or string.match(reality, '^TRN%d+') ~= nil then output = { ['name'] = "Earth-"..reality, ['desc'] = "Earth-"..reality, ['type'] = "Comic" } else output = { ['name'] = reality, ['desc'] = reality, ['type'] = "Comic" } end end return output end

function p.lua_name(s_page, s_title, s_main_title, s_main_name, others) local output = '' local s_name = ''

if others == true then output = bold_span(s_page, s_title) else s_name = p.lua_get_name_and_reality(s_page) if not h.isempty(s_title) then s_title = h.lua_breaklink (s_title, 2) if h.inlist({s_main_title, s_main_name}, s_title) == false then s_title = mw.text.trim(s_title) if not h.inlist({s_main_title, s_main_name, s_title}, s_name) then output = bold_span(s_page, s_title..' ('..s_name..')') else output = bold_span( s_page, s_title) end elseif not h.inlist({s_main_title, s_main_name}, s_name) then output = bold_span(s_page, s_name) end elseif s_name ~= s_main_name and s_name ~= s_main_title then output = bold_span(s_page, s_name) end end if not h.isempty(output) then output = output..' '   end return output end

function p.name(frame) local s_page = frame.args["page"] or frame.args[1] s_page = h.lua_breaklink(s_page, 1) local i = string.find(s_page," (",1,true)   local s_name     if i == nil         then s_name = s_page        else s_name = string.sub(s_page, 1, i-1)    end    local s_title = frame.args["title"] or s_name    local s_main_title = frame.args["main_title"] or s_title    local s_main_name = frame.args["main_name"] or s_name    local s_others = frame.args["others"] or true	return p.lua_name(s_page, s_title, s_main_title, s_main_name, s_others) end

function p.lua_box(v, s_main_title, s_main_name, others, noimage) local s_page local s_image local s_title local s_reality = "" local part local j   local k    local s = "" local page_s local s1   local s2    local s_pad local desc local earth local ttype

part = string.find(v,"~",1,true) s_page = string.sub(v, 1, part-1) page_s = string.reverse(s_page) j = string.find(page_s,"( ",1,true)   k = string.find(page_s, ")",1,true) if j ~= nil and k ~= nil then s_reality = string.reverse(string.sub(page_s, k+1, j-1 ) ) end j, k = string.find(s_page,"Earth-TRN",1,true) if j ~= nil then s1 = string.sub(s_page, k+1, string.len(s_page)-1) s_pad = string.rep("0", 20-string.len(s1)-string.len("Earth-TRN")) s = "Earth-TRN"..s_pad..s1       else j, k = string.find(s_page,"Earth-",1,true) if j ~= nil then s1 = string.sub(s_page, k+1, string.len(s_page)-1) s_pad = string.rep("0", 20-string.len(s1)-string.len("Earth-")) s = "Earth-"..s_pad..s1               else s = s_reality end end j = string.find(v,"~",part+1,true) s_image = ' '..p.createBox_image( { image = string.sub(v, part+1, j-1), page = s_page, noimage = noimage } ) s_title = p.lua_name(s_page, tostring(string.sub(v, j+1, string.len(v))), s_main_title, s_main_name,others) if h.isempty(s_reality) == false then desc,earth,ttype = p.lua_getReality(s_reality, false) if desc ~= "" and earth ~= "" and desc == earth then s_reality = ' ('..earth..')' else s_reality = desc..' ('..earth..')' end end s = s..'~'..s_image..s_title..s_reality..' ' return s, ttype end

function p.lua_table(t, header) local i = 1 local s = "" local v   if #t ~= 0 then s = ' '..header..' ' --for i,v in ipairs(t) do       while i <= #t do            v = t[i] s = s..string.sub(v, string.find(v,"~",1,true)+1, string.len(v)) if math.fmod(i, 4) == 0 then s = s..'   ' end i = i + 1 end s = s..' '   end return s end

function p.disambiguation(frame) local l = frame.args["list"] local s_main_title = frame.args["main_title"] local s_main_name = frame.args["main_name"] local s_include = frame.args["include"] local s_noimage = frame.args["noimage"] local headers = { "Alternate Reality Versions", "Movies", "Television", "Video Games", "Others", "Related", "Teams", frame.args["header1"], frame.args["header2"], frame.args["header3"], frame.args["header4"], frame.args["header5"] } local sections = { "", "", "", "", frame.args["others"], frame.args["related"], frame.args["teams"], frame.args["section1"], frame.args["section2"], frame.args["section3"], frame.args["section4"], frame.args["section5"] } local tables = { {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} } local strings= { "", "", "", "", "", "", "", "", "", "", "", "" } local includes= { frame.args["includeComic"], frame.args["includeMovie"], frame.args["includeTV"], frame.args["includeGame"] } local s   local v    local i    local j    local ttype local d = "" l = mw.ustring.gsub(l,"&#x20;","") if h.isempty(s_include) == false then l = l..p.lua_include(s_include,true) end l = mw.text.split(l,"@",true) i = 1 while next(l,i) ~= nil do       v = l[i] i = i + 1 s, ttype = p.lua_box(v, s_main_title, s_main_name, false, s_noimage) if ttype == "Game" then table.insert(tables[4], s)           elseif ttype == "TV" then table.insert(tables[3], s)            elseif ttype == "Movie" then table.insert(tables[2], s)            else table.insert(tables[1], s)        end end for j=1,4 do       if h.isempty(includes[j]) == false then l = p.lua_include(includes[j]) l = mw.text.split(l,"@",true) i = 1 while next(l,i) ~= nil do                   v = l[i] i = i + 1 s = p.lua_box(v, s_main_title, s_main_name, false, s_noimage) table.insert(tables[j], s)               end end end for j=1,4 do       table.sort(tables[j]) strings[j] = p.lua_table(tables[j], headers[j]) end

for j=5,12 do       if h.isempty(sections[j]) == false then l = mw.text.split(p.lua_include(sections[j]),"@",true) i = 1 while next(l,i) ~= nil do                   v = l[i] i = i + 1 s = p.lua_box(v, s_main_title, s_main_name, true, s_noimage) table.insert(tables[j], s)               end strings[j] = p.lua_table(tables[j], headers[j]) end end s = frame:preprocess( "" ) d = d.."Alternate Reality Versions" if h.isempty(s_main_title) == false and mw.title.new(s_main_title.." Comic Books").exists == true then d = d.." · Comics" elseif h.isempty(s_main_name) == false and mw.title.new(s_main_name.." Comic Books").exists == true then d = d.." · Comics" end for j=2,7 do       if strings[j] ~= "" then d = d.." · "..headers[j].."" end end d = ' ' return d,table.concat(strings) end

return p