Module:Disambiguation

local p = {}

local list_of_realities = mw.loadData( 'Module:Disambiguation/Realities' )

function p.lua_realities (r) local j   local k    local s    local s2    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] else return r, r, "Comic" end end

function p.realities (frame) local r = frame.args[1] local i = tonumber(frame.args[2]) local s1   local s2    local s3    s1, s2, s3 = p.lua_realities(r) if i == nil then if i == 3 then return s3               elseif i == 2 then return s2                else return s1            end end end

function p.l_d (t) local div = mw.html.create( 'div' ) div :css( 'height', '150px' ) :css( 'overflow', 'hidden' ) :wikitext( t ) return tostring( div ) end

function p.d (frame) return p.l_d(frame.args[1]) end

function p.lua_breaklink (link, part) --Check if 'link' is a wikilink. If yes, then check if it has | inside. If yes, then return 'part' part of it. If 'link' is not a wikilink return 'link'. local i   local j    i = string.find(link, "]]",1,true) if i ~= nil then j = string.find(link, "|",1,true) if j == nil then return string.sub(link, 3, i-1) else if part == 2 then return string.sub(link, j+1, i-1) else return string.sub(link, 3, j-1) end end else return link end end

function p.breaklink (frame) return p.lua_breaklink(frame.args[1], tonumber(frame.args[2])) end

function p.l_image(i,p) if i == nil or i == "No Image Available At All.png" then i = "No Image Available.png" end return '' end

function p.l_name(s_page, s_title, s_main_title, s_main_name) local s = "" local i   local j

i = string.find(s_page," (",1,true)   s_name = string.sub(s_page, 1, i-1)    if s_title ~= nil and s_title ~= ""         then s_title = p.lua_breaklink (s_title, 2)    end                if s_title ~= nil and s_title ~= "" and s_title ~= s_main_title and s_title ~= s_main_name        then             if s_name ~= s_main_name                then s = ""..s_title.." ("..s_name..") "                else s = ""..s_title.." "            end        else             if s_name ~= s_main_name                then s = ""..s_name.." "            end    end        return s end

function p.image(frame) return p.l_image(frame.args[1], frame.args[2]) end

function p.name(frame) return p.l_name(frame.args["page"], frame.args["title"], frame.args["main_title"], frame.args["main_name"]) end

function p.reality(frame) local s_page = frame.args[1] local i   local j    local s    i = string.find(s_page,"(",1,true)    j = string.len(s_page)    s = string.sub(s_page, i+1, j-1)	return frame:expandTemplate{ title = 'Number to Name', args = { s } }..'('..s..')' end

function p.box(frame) local s_page = frame.args["page"] local s_image = frame.args["image"] local s_title = tostring(frame.args["title"]) local s_main_title = frame.args["main_title"] local s_main_name = frame.args["main_name"] local s_name local s_reality local i   local j    local si    local sn = "" local sr   i = string.find(s_page,"(",1,true)    j = string.len(s_page)    s_reality = string.sub(s_page, i+1, j-1)    s_name = string.sub(s_page, 1, i-2)    sr = frame:expandTemplate{ title = 'Number to Name', args = { s_reality } }..'('..s_reality..')'    if s_image == nil or s_image == "No Image Available At All.png"        then s_image = "No Image Available.png"           end	si = '   '     if s_name ~= s_main_name        then sn = ""..s_name.." "        else if s_title ~= nil and s_title ~= ""            then                 j = string.find(s_title, "]]",1,true)                if j ~= nil                     then                        i = string.find(s_title, "|",1,true)                        if i == nil                             then s_title = string.sub(s_title, 3, j-1)                             else s_title = string.sub(s_title, i+1, j-1)                        end end if s_title ~= s_main_title and s_title ~= s_main_name then sn = ""..s_title.." " end end end

return si..sn..sr..' ' end

function p.sortlist(frame) local l = mw.text.split(frame.args["list"],"@",true) local s_main_title = frame.args["main_title"] local s_main_name = frame.args["main_name"] local s_page local s_image local s_title local s_name local s_reality local r = {} local k   local j      local s    local s1    local s2    local s_pad local v   local i    local part local r2 = {} local comic = {} local movie = {} local game = {} local tv = {} local desc local earth local ttype i = 1 while next(l,i) ~= nil do       v = l[i] i = i + 1 part = string.find(v,"*",1,true) s_page = string.sub(v, 1, part-1) s_reality = string.reverse(string.sub(string.reverse(s_page), 1, string.find(s_page,") ",1,true)))       j, k = string.find(s_page," (Earth-TRN",1,true) if j == nil then 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)                        s2 = string.sub(s_page, j+2, k)                        s_pad = string.rep("0", 20-string.len(s1))                        s = s2..s_pad..s1                    else                        s = s_reality                end        end        j = string.find(v,"*",part+1,true)               s_image = '  '..p.l_image(string.sub(v, part+1, j-1), s_page)..' '        s_title = p.l_name(s_page, tostring(string.sub(v, j+1, string.len(v))), s_main_title, s_main_name)        desc,earth,ttype = p.lua_realities(s_reality)        s_reality = desc..' ('..earth..')'        table.insert(comic, s_reality)        --        if ttype == "TV"            then table.insert(tv, s..'*'..s_image..s_title..s_reality)            elseif ttype == "Movie" then table.insert(movie, s..'*'..s_image..s_title..s_reality)            elseif ttype == "Game" then table.insert(game, s..'*'..s_image..s_title..s_reality)            else table.insert(comic, s..'*'..s_image..s_title..s_reality..'  ')        end        -- --       j, k = string.find(s_page," (Earth-TRN",1,true)        if j == nil            then j, k = string.find(s_page," (Earth-",1,true)        end        s1 = string.sub(s_page, k+1, string.len(s_page)-1)        s2 = string.sub(s_page, j+2, k)        s_pad = string.rep("0", 20-string.len(s1))        table.insert(r, s2..s_pad..s1..'*'..v)        -- end --table.sort(r) --table.sort(comic) table.sort(movie) table.sort(tv) table.sort(game)

--   while i <= -- return table.concat( comic ) end

return p