Module:Disambiguation

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

function p.test(frame) local l = mw.text.unstrip( frame.args[1] ) local a,b,i,v l = mw.text.split(l,'\n') for i,v in ipairs(l) do       v = mw.ustring.gsub(v, '%*%s-(.-)', '* %2 %1%3' )..'\n' end return frame:preprocess( table.concat(l) ) end

function p.comic_issue(frame) local monthData = mw.loadData( 'Module:Month/data' ) local monthAliases = monthData[1] local l = mw.text.split(frame.args["list"],"@@@",true) local parts = { frame.args["part1"], frame.args["part2"], frame.args["part3"], frame.args["part4"], frame.args["part5"], frame.args["part6"], frame.args["part7"], frame.args["part8"], frame.args["part9"]} local above = { frame.args["part1_above"], frame.args["part2_above"], frame.args["part3_above"], frame.args["part4_above"], frame.args["part5_above"], frame.args["part6_above"], frame.args["part7_above"], frame.args["part8_above"], frame.args["part9_above"] } local below = { frame.args["part1_below"], frame.args["part2_below"], frame.args["part3_below"], frame.args["part4_below"], frame.args["part5_below"], frame.args["part6_below"], frame.args["part7_below"], frame.args["part8_below"], frame.args["part9_below"] } local part local a   local b    local s_page local s_image local s_title local s_year local s_month local s_volume local s_issue local i = 1 local v   local s    local t = {} local t2 = {} local cover local textless local j   local k    local e

while next(l,i) ~= nil do       v = mw.text.split(l[i],"*",true) i = i + 1 s_page = v[1] s_image = v[2] s_title = v[3] s_year = v[4] s_month = monthAliases[string.lower( v[5] )] or v[5] s = s_page s = string.gsub(s,'/',' ') s = string.gsub(s,':',' ') cover = s..".jpg" textless = s.." Textless.jpg" if s_image == nil or s_image == "" then if mw.title.new("File:"..cover).exists == true then s_image = cover elseif mw.title.new("File:"..textless).exists == true then s_image = textless else s_image = "No Image Cover.jpg" end end if s_title == nil or s_title == "" then s_title = "" else s_title = ' "'..s_title..'"' end

e,j,k = string.find(s_page," Vol (%d+) ") s_volume = string.sub(s_page, 1, e)       s_issue = string.sub(s_page, j+1, string.len(s_page)) if tonumber(k) == 1 then s = ""..s_volume.." #"..s_issue.."" else s = ""..s_volume.." Vol "..k.." #"..s_issue.."" end j = string.find(s_issue,"-",1,true) -- -1 issues k = string.find(s_issue,".",1,true) -- point one issues if j == nil then if k ~= nil then s_issue = string.sub(s_issue,1,k-1).."9" s_issue = string.rep("0", 10-#s_issue)..s_issue else s_issue = s_issue.."0" s_issue = string.rep("0", 10-#s_issue)..s_issue end end s = s_issue..'@@@'..s_image..'|'....s....s_title..' ('..s_month..' ,'..s_year..')\n' table.insert(t,s) end table.sort(t) i = 1 while i <= #t do       v = mw.text.split(t[i],"@@@",true) i = i + 1 s = v[2] table.insert(t2,s) end

if tonumber(parts[1]) == nil or tonumber(parts[1]) == 0 then return frame:preprocess('') else t = {} i = 1 while i <=#parts do               if i == 1 then j = 1 else j = parts[i-1]+1 end part = tonumber(parts[i]) if part == nil or part == 0 then break else a = above[i] b = below[i] i = i + 1 if part>#t2 then part = #t2 end table.insert(t, a..frame:preprocess('')..b)               end end return table.concat(t) end 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.lua_in_list(list, text) local b   for i, v in ipairs( list ) do        if v == text then b = true break else b = false end end return b end

function p.lua_include(list,redlinks) 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        if redlinks == true            then s = s..s_page.."*"..s_image.."* "..s_title.."@"            else s = s..s_page.."*"..s_image.."*"..s_title.."@"        end    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] elseif tonumber(r) ~= nil then return "Earth-"..r, "Earth-"..r, "Comic" else return r, r, "Comic" end end

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

if e == true or e == "true" then r = string.reverse( string.sub(string.reverse(string.sub(r, 1, #r-1)), 1, string.find(string.reverse(string.sub(r, 1, #r-1)),"( ",1,true)-1) )   end

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

function p.lua_image(i,p,noimage) if i == nil or i == "" or i == "No Image Available At All.png" then if noimage ~= nil and noimage ~= "" then i = noimage else i = "No Image Available.png" end 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, others) local s = "" local i   local j    local s_name = ""

if others == true then s = ""..s_title.." " else 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)                    --if s_title ~= s_main_title and s_title ~= s_main_name                    if p.lua_in_list({s_main_title, s_main_name}, s_title) == false                        then                            if p.lua_in_list({s_main_title, s_main_name, s_title}, s_name) == false                            --if s_name ~= s_main_name and s_name ~= s_title and s_name ~= s_main_title                                 then s = ""..s_title.." ("..s_name..") "                                else s = ""..s_title.." "                            end                        elseif p.lua_in_list({s_main_title, s_main_name}, s_name) == false --elseif s_name ~= s_main_name and s_name ~= s_main_title then s = ""..s_name.." " end elseif s_name ~= s_main_name and s_name ~= s_main_title then s = ""..s_name.." " end end return s end

function p.name(frame) local s_page = frame.args["page"] or frame.args[1] local i = string.find(s_page," (",1,true)   local s_name = string.sub(s_page, 1, i-1)    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)   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, 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 s_reality ~= nil and s_reality ~= "" then desc,earth,ttype = p.lua_reality(s_reality) 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..' ' 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 s_include ~= nil and s_include ~= "" 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 includes[j] ~= nil and includes[j] ~= "" 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 sections[j] ~= nil and sections[j] ~= "" 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 s_main_title ~= nil and s_main_title ~= "" and mw.title.new(s_main_title.." Comic Books").exists == true then d = d.." · Comics" elseif s_main_name ~= nil and s_main_name ~= "" 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