Module:Disambiguation

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

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.lua_include(list) local l = mw.text.split(list,"@",true) local i   local v    local j    local k    local s_page local s_title local s_image local page_s local s=""

i = 1 while next(l,i) ~= nil do       v = l[i] i = i + 1 s_page = "" s_title = "" s_image = "" page_s = "" j = string.find(v,";",1,true) if j ~= nil then s_image = mw.text.trim(string.sub(v,j+1,string.len(v))) s_page = string.sub(v,1,j-1) else s_page = v       end s_title = p.lua_breaklink(s_page,2) s_page = p.lua_breaklink(s_page,1) page_s = string.reverse(s_page) j = string.find(page_s,"( ",1,true)       if j ~= nil and s_title == s_page            then s_title = string.reverse(string.sub(page_s, j+2, string.len(page_s)))        end        s = s..s_page.."*"..s_image.."*"..s_title.."@"    end    return s end

function p.include (frame) return p.lua_include(frame.args[1]) end

function p.lua_reality (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.reality (frame) local r = frame.args[1] local i = tonumber(frame.args[2]) local s1   local s2    local s3    s1, s2, s3 = p.lua_reality(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.lua_image(i,p) if i == nil or i == "" or i == "No Image Available At All.png" then i = "No Image Available.png" end return '' end

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

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

i = string.find(s_page," (",1,true)   if i ~= nil         then s_name = string.sub(s_page, 1, i-1)        else s_name = s_page    end    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.name(frame) return p.lua_name(frame.args["page"], frame.args["title"], frame.args["main_title"], frame.args["main_name"]) end

function p.lua_box(v, s_main_title, s_main_name) 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)   if j ~= nil         then s_reality = string.reverse(string.sub(page_s, 2, 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.lua_image(string.sub(v, part+1, j-1), s_page)..' '    s_title = p.lua_name(s_page, tostring(string.sub(v, j+1, string.len(v))), s_main_title, s_main_name) desc,earth,ttype = p.lua_reality(s_reality) if desc == earth then s_reality = ' ('..earth..')' else s_reality = desc..' ('..earth..')' 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 ~= nil then s = ' '..header..' ' end return s end

function p.sortlist(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_others = frame.args["others"] local s_related = frame.args["related"] local s_teams = frame.args["teams"] local s   local v    local i    local ttype local part local comic = {} local movie = {} local game = {} local tv = {} local c = "" local m = "" local g = "" local t = "" local others = {} local related = {} local teams = {} local o = "" if s_include ~= nil and s_include ~= "" then l = l..p.lua_include(s_include) 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) if ttype == "TV" then table.insert(tv, s)           elseif ttype == "Movie" then table.insert(movie, s)            elseif ttype == "Game" then table.insert(game, s)            else table.insert(comic, s)        end end table.sort(comic) table.sort(movie) table.sort(tv) table.sort(game) c = p.lua_table(comic, 'Alternate Reality Versions') m = p.lua_table(movie, 'Movies') t = p.lua_table(tv, 'Television') g = p.lua_table(game, 'Video Games') if s_others ~= nil and s_others ~= "" then l = mw.text.split(p.lua_include(s_others),"@",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) table.insert(others, s)           end o = p.lua_table(others, 'Others') end if s_related ~= nil and s_related ~= "" then related = p.lua_include(s_related) end if s_teams ~= nil and s_teams ~= "" then teams = p.lua_include(s_teams) end return c,m,t,g,o end

return p