模块:Sandbox/HistoryM

来自Minecraft Wiki
跳转到导航 跳转到搜索
[ 创建 | 刷新 ]文档页面
此模块没有文档页面。如果你知道此模块的使用方法,请帮助为其创建文档页面。
local p = {}
local mode = nil
local tableHead
local currentVersion = nil
local currentConsoleVersions = {
    xb360 = nil,
    xbone = nil,
    ps = nil,
    wiiu = nil,
    switch = nil
}
local consoleRowspanIndexes
local consoleRowspans
local rowspanIndex
local rowspans
local versionLink = require( 'Module:Version link' ).main

local editionAlias = {
	['pre-classic'] = "java pre-Classic", ['java pre-classic'] = "java pre-Classic",
	classic = "java Classic", ['java classic'] = "java Classic",
	['classic server'] = "java Classic server", ['java classic server'] = "java Classic server",
	indev = "java Indev", ['java indev'] = "java Indev",
	infdev = "java Infdev", ['java infdev'] = "java Infdev",
	alpha = "java Alpha", ['java alpha'] = "java Alpha", ['java edition alpha'] = "java Alpha",
	['alpha server'] = "java Alpha server", ['java alpha server'] = "java Alpha server", ['java edition alpha server'] = "java Alpha server",
	beta = "java Beta", ['java beta'] = "java Beta", ['java edition beta'] = "java Beta",
	je = "java", java = "java", ['java edition'] = "java",
	['java edition upcoming'] = "java upcoming", ['java upcoming'] = "java upcoming", ['upcoming java'] = "java upcoming",
	['pe alpha'] = "pocket Alpha", ['pocket alpha'] = "pocket Alpha", ['pocket edition alpha'] = "pocket Alpha",
	pe = "pocket", pocket = "pocket", ['pocket edition'] = "pocket",
	ce = "console", lce = "console", console = "console", ['legacy console'] = "console", ['console edition'] = "console", ['legacy console edition'] = "console",
	be = "bedrock", bedrock = "bedrock", ['bedrock edition'] = "bedrock",
	['be upcoming'] = "bedrock upcoming", ['bedrock upcoming'] = "bedrock upcoming", ['bedrock edition upcoming'] = "bedrock upcoming", ['upcoming bedrock'] = "bedrock upcoming",
	['3ds'] = "3ds", new3ds = "3ds", ['new 3ds'] = "3ds",
	realms = "realms",
	edu = "edu", minecraftedu = "edu",
	education = "education", ['education edition'] = "education",
	['ee upcoming'] = "education upcoming", ['education upcoming'] = "education upcoming", ['education edition upcoming'] = "education upcoming", ['upcoming education'] = "education upcoming",
	ps4 = "ps4", playstation4 = "ps4", ['playstation 4'] = "ps4",
	earth = "earth", ['minecraft earth'] = "earth",
	dungeons = "dungeons", ['minecraft dungeons'] = "dungeons",
	['?'] = "unknown", unknown = "unknown"
}

local headerText = {
    ['java pre-Classic']    = '[[Java版pre-Classic]]',
    ['java Classic']        = '[[Java版Classic]]',
    ['java Classic server'] = '[[Java版Classic|Java版Classic服务器]]',
    ['java Indev']          = '[[Java版Indev]]',
    ['java Infdev']         = '[[Java版Infdev]]',
    ['java Alpha']          = '[[Java版Alpha]]',
    ['java Alpha server']   = '[[Java版Alpha|Java版Alpha服务器]]',
    ['java Beta']           = '[[Java版Beta]]',
    ['java']                = '[[Java版版本记录|Java版]]',
    ['java upcoming']       = '[[计划版本#Java版|Java版(即将到来)]][[Category:Java版即将到来]]',
    ['pocket Alpha']        = '[[携带版Alpha]]',
    ['pocket']              = '[[携带版]]',
    ['bedrock']             = '[[基岩版版本记录|基岩版]]',
    ['bedrock upcoming']    = '[[计划版本#基岩版|基岩版(即将到来)]][[Category:基岩版即将到来]]',
    ['edu']                 = '[[MinecraftEdu]]',
    ['education']           = '[[教育版版本记录|教育版]]',
    ['education upcoming']  = '[[计划版本#教育版|教育版(即将到来)]][[Category:教育版即将到来]]',
    ['console']             = '[[原主机版版本记录|原主机版]]',
    ['realms']              = '[[Realms#历史|Realms]]',
    ['3ds']                 = '[[New Nintendo 3DS版版本记录|New Nintendo 3DS版]]',
    ['ps4']                 = '[[原主机版版本记录|PlayStation 4版]]',
    ['earth']               = '[[Minecraft Earth]]',
    ['dungeons']            = '[[Minecraft Dungeons]]',
    ['unknown']             = '未知[[Category:有未知版本的History模板的页面]]'
}

local versionLinks = {
    ['java upcoming'] = 'java',
    ['bedrock upcoming'] = 'bedrock',
    ['education upcoming'] = 'education',
    ps4 = 'ps4',
    earth = 'Earth',
    dungeons = 'Dungeons'
}

local function resetConsole()
    currentConsoleVersions.xb360 = nil
    currentConsoleVersions.xbone = nil
    currentConsoleVersions.ps = nil
    currentConsoleVersions.wiiu = nil
    currentConsoleVersions.switch = nil
end

local function reset()
    currentVersion = nil
    resetConsole()
end

local function footReset()
    mode = nil
    reset()
end

local function getVersionLink(link,text,isSnap)
    local cat = ''
    if(not isSnap) then
        -- upcoming categories
        if(mode == 'java upcoming') then cat = '[[Category:Java版即将到来/'..text..']]'
        elseif(mode == 'bedrock upcoming') then cat = '[[Category:基岩版即将到来/'..text..']]'
        elseif(mode == 'education upcoming') then cat = '[[Category:教育版即将到来/'..text..']]'
        end
    end
    if(link == 'none') then 
      	if(text == 'unknown' or text == '?') then return '?[[Category:有未知版本的History模板的页面]]' end
		if(isSnap and text == 'java') then return '' end --不知道有什么用
      	return text .. cat
    end
    if(link == 'ver') then 
      	if(text == 'unknown' or text == '?') then return '?[[Category:有未知版本的History模板的页面]]' end
		if(isSnap and text == 'java') then return '' end --不知道有什么用
      	if((not isSnap) and (mode == 'realms')) then return text end
      	local c
			if(not isSnap) then
				if(text == 'Pre-release' and mode == 'pocket Alpha') then c = 'pocket Pre-release'
				else c = (versionLinks[mode] or mode) .. ' ' .. text
				end
			else
                c = text:lower()
                if(c:match('^pre') or c:match('^rc') or c:match('^exp')) then c = currentVersion .. '-' .. text
                elseif(c:match('^test build') or c:match('^pre%-release') or c:match('^build') or c:match('^20[01]')) then c = currentVersion .. ' ' .. text
                elseif(c:match('^release')) then c = currentVersion
                else c = text
                end
				c = (versionLinks[mode] or mode) .. ' ' .. c
			end
      	return versionLink({c,text:gsub( '^%((.*)%)$', '%1')}) .. cat
    end
    if(text == 'unknown' or text == '?') then cat = '[[Category:有未知版本的History模板的页面]]' end
    if(link:find('//',1,true)) then return '['..link..' '..text..']'..cat else return '[['..link..'|'..text..']]'..cat end
      
end


local function consoleVersionLink(platform,ver)
    if(ver == 'none') then return ' ' end
    if(ver == 'unknown' or ver=='?') then return '?[[Category:有未知版本的History模板的页面]]' end
    local full = platform..' '.. ver
    if(platform == 'ps') then ver = ver:gsub('%w*%(.*%)$','')
    end
    return versionLink({full,ver})
end

local function addConsoleVersions(platform,ver,prnt)
    if((ver ~= nil and ver == currentConsoleVersions[platform]) or (ver == nil and currentConsoleVersions[platform] ~= nil))
    then
        return
    end
    prnt('<th class="nowrap" rowspan="')
    currentConsoleVersions[platform] = ver or "none"
    consoleRowspanIndexes[platform] = consoleRowspanIndexes[platform] + 1
    prnt(consoleRowspans[platform][consoleRowspanIndexes[platform]]..'">')
    prnt(consoleVersionLink(platform,ver or 'none'))
    prnt('</th>')
end


local function processLine(args, prnt)
    local snapRows = 1
    for i,_ in ipairs(args) do
    	if(i > 3) then snapRows = snapRows + 1
    	end
    end

    --Header or Foot
    if(args[1] ~= nil and args[1] ~= '') then
        local temp = (args[1]):lower()
        if (temp == 'foot') then
            prnt('</table>')
            footReset()
            tableHead = 0
            return
        end
        mode = editionAlias[temp] or temp
        reset()
        prnt('<tr><th colspan="6">')
        prnt(headerText[mode] or ('[['.. mode ..']]'))
        prnt('</th></tr>')
        return
    end

    --Content rows

    prnt('<tr>')

    if((mode == 'console') and (args.xbox or args.xbone or args.ps or args.wiiu or args.switch )) then
        currentVersion = nil

        if(args[3] ~= nil and args[3] ~= '') then
            snapRows = snapRows + 1
        end
  
        addConsoleVersions("xb360",args.xbox,prnt)
        addConsoleVersions("xbone",args.xbone,prnt)
        addConsoleVersions("ps",args.ps,prnt)
        addConsoleVersions("wiiu",args.wiiu,prnt)
        addConsoleVersions("switch",args.switch,prnt)
  
        prnt('<td>'..args[2]..'</td></tr>')
  
        for k,v in ipairs(args) do
        	if(k > 2) then prnt('<tr><td>' .. v .. '</td></tr>')
        		end
        end

        return
    end

    --Other versions:

    resetConsole()
    if((args.snap == nil) or (args[2] and args[2]~='' and (args[2] ~= currentVersion))) then
  	    currentVersion = nil
        prnt('<th ')
        if( not (args.link or args[2]:find('(', 1, true))) then prnt('class="nowrap"') end
        prnt(' rowspan="')
        if( args.snap ~= nil) then
            currentVersion = args[2]
            rowspanIndex = rowspanIndex + 1
            prnt(rowspans[rowspanIndex]..'" colspan="2">')
        else
            prnt(snapRows .. '" colspan="5">')
        end

        prnt(getVersionLink(args.link or 'ver', args[2], false))
        prnt('</th>')
    end

    if(args.snap ~= nil) then
        prnt('<th colspan="3" rowspan="' .. snapRows .. '">')
        prnt(getVersionLink(args.slink or 'ver', args.snap, true))
        prnt('</th>')
    end

    prnt('<td>' .. args[3] .. '</td></tr>')
    for k,v in ipairs(args) do if(k>3) then prnt('<tr><td>'..v..'</td></tr>') end end

    return
end


function p.getLines(f)
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Module:ProcessArgs' ).merge( true )
	end
	assert(mw.text.trim(args[1]):sub(-14)=='<!--LineEnd-->')
	local stringLines = mw.text.split(mw.text.trim(args[1]):sub(1,-15),'<!--LineEnd-->',true)
	local tableLines = {}
	local stringArgs
	local temp,temp1,temp2
	for i,l in ipairs(stringLines) do
		tableLines[i] = {}
		temp = mw.text.trim(l) or ''
		if(temp ~= '') then
			stringArgs = mw.text.split(temp,'<!--ArgSep-->',true)
			for m,n in ipairs(stringArgs) do
				if(n ~= '') then
					temp = n:find("=", 1, true)
					temp1 = mw.text.trim(n:sub(1, temp - 1))
					temp1 = tonumber(temp1) or temp1
					temp2 = mw.text.trim(n:sub(temp + 1))
					tableLines[i][temp1] = temp2
				end
			end
		end
	end
	return tableLines
end


function p.printArgs(f)
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Module:ProcessArgs' ).merge( false )
	end
	local result = ''
	for k,v in pairs(args) do
		result = result .. k .. '=' .. v .. '<!--ArgSep-->'
	end
	result = result .. '<!--LineEnd-->'
	return result
end

local function countConsoleRowspans(platform,ver,snapRows)
    if((ver ~= nil and ver == currentConsoleVersions[platform]) or (ver == nil and currentConsoleVersions[platform] ~= nil))
    then
        consoleRowspans[platform][consoleRowspanIndexes[platform]] = consoleRowspans[platform][consoleRowspanIndexes[platform]] + snapRows
        return
    end
    currentConsoleVersions[platform] = ver or "none"
    consoleRowspanIndexes[platform] = consoleRowspanIndexes[platform] + 1
    consoleRowspans[platform][consoleRowspanIndexes[platform]] = (consoleRowspans[platform][consoleRowspanIndexes[platform]] or 0) + snapRows
end

local function countRowspans(args)
    local snapRows = 1
    for i,_ in ipairs(args) do
    	if(i>3) then snapRows = snapRows + 1
    	end
    end

    --Header or Foot
    if(args[1] ~= nil and args[1] ~= '') then
        local temp = (args[1]):lower()
        if (temp == 'foot') then
            footReset()
            return
        end
        mode = editionAlias[temp] or temp
        reset()
        return
    end

    --Content rows

    if((mode == 'console') and (args.xbox or args.xbone or args.ps or args.wiiu or args.switch )) then
        currentVersion = nil

        if(args[3] ~= nil and args[3] ~= '') then
            snapRows = snapRows + 1
        end
  
        countConsoleRowspans("xb360",args.xbox,snapRows)
        countConsoleRowspans("xbone",args.xbone,snapRows)
        countConsoleRowspans("ps",args.ps,snapRows)
        countConsoleRowspans("wiiu",args.wiiu,snapRows)
        countConsoleRowspans("switch",args.switch,snapRows)
  
        return
    end

    --Other versions:

    resetConsole()
    if((args.snap == nil) or (args[2] and args[2] ~= '' and (args[2] ~= currentVersion))) then
  	    currentVersion = nil
  	    if( args.snap ~= nil) then
            currentVersion = args[2]
            rowspanIndex = rowspanIndex + 1
        end
    end

    if(args.snap ~= nil) then
        rowspans[rowspanIndex] = (rowspans[rowspanIndex] or 0) + snapRows
    end
    return
end

local function initRowspans()
    rowspans = {}
    consoleRowspans = {
        xb360 = {},
        xbone = {},
        ps = {},
        wiiu = {},
        switch = {}
    }
end

local function resetRowspanIndexes()
	rowspanIndex = 0
	consoleRowspanIndexes = {
		xb360 = 0,
		xbone = 0,
		ps = 0,
		wiiu = 0,
		switch = 0
	}
end

function p.main(f)
    local lines = p.getLines(f)

    initRowspans()
    resetRowspanIndexes()
	for _,v in ipairs(lines) do
		countRowspans(v)
	end

	resetRowspanIndexes()
    local result = {}
    tableHead=0
	for _,v in ipairs(lines) do
		if(tableHead~=1) then
        	table.insert(result, '<table class="wikitable" data-description="历史">')
        	tableHead = 1
		end
		processLine(v, function(a) table.insert(result, a) end)
	end
	return table.concat(result)
end

return p