Module:Reality

local p = {} local h = require("Module:HF") local getArgs = require('Dev:Arguments').getArgs local list_of_realities = mw.loadData( 'Module:Disambiguation/Realities' ) --local special_names = { "Mojoverse", "Ideaverse", "Brilliant City", "Otherworld", "Weirdworld", "Unknown Reality", "Unknown" }

function p.sort_by_reality(frame) local function sort_name(reality_number) if tonumber(reality_number) ~= nil then output = string.rep('0', 15-#reality_number)..reality_number else output = reality_number end return output end local args = getArgs (frame) local value = args[1] local name local reality local info local output = '' if not h.isempty(value) then name, reality = p.lua_get_name_and_reality(value) if reality ~= '' then if name ~= '' then output = name..' ('..reality.padded_number..')' else output = reality.padded_number end else output = value 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.get_reality_info({s_reality}) if not h.isempty(info) then s_reality = info else s_reality = '' end else info = p.get_reality_info({pagename}) if not h.isempty(info) then s_name = '' s_reality = info else s_name = pagename s_reality = '' end end end return s_name, s_reality end

function p.get_reality_info(frame) local args = getArgs (frame) local reality = args[1] local part = args[2] 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], ['description'] = info[1], ['type'] = info[3], ['number'] = reality, ['padded_number'] = p.padded_reality_number(reality), }               elseif tonumber(reality) ~= nil or string.match(reality, '^TRN%d+') ~= nil then output = { ['name'] = "Earth-"..reality, ['description'] = "Earth-"..reality, ['type'] = "Comic", ['number'] = reality, ['padded_number'] = p.padded_reality_number(reality), }                   --else output = { ['name'] = reality, ['description'] = reality, ['type'] = "Comic" } end end if not h.isempty(part) then part = tonumber(part) if part == 3 then output = output.type elseif part == 2 then output = output.description else output = output.name end end return output end

function p.padded_reality_number(reality_number) local trn = string.match(reality_number, 'TRN(.+)') local output = '' if tonumber(reality_number) ~= nil then output = 'Earth-'..string.rep('0', 15-#reality_number)..reality_number elseif trn ~= nil then output = 'Earth-TRN'..string.rep('0', 5-#trn)..trn else output = reality_number end return output end

return p