Module:History

From Vintage Story Wiki
Revision as of 19:46, 27 August 2022 by Sana (talk | contribs)

Documentation for this module may be created at Module:History/doc

local p = {}

function newAutotable(dim)
    local MT = {};
    for i=1, dim do
        MT[i] = {__index = function(t, k)
            if i < dim then
                t[k] = setmetatable({}, MT[i+1])
                return t[k];
            end
        end}
    end

    return setmetatable({}, MT[1]);
end

function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

function p.create(f)
	local args = require( 'Module:ProcessArgs' ).merge( true )
		
	local header = {
		'{|class="wikitable" style="width:90%"',
		'!colspan="3"|History',
		'|-'
	}
	local footer = '|}'
	
	local changes = {}
	for i,v in pairs(args) do
	    local versionChanges = {}
		local version = string.match(v, '(.*);')
		local notes
		local parent
		if string.match(version, '-(.*)') ~= nil then
		    parent = string.match(version, '(.*)-')
		    notes = string.match(v, '-(.*)')
		else
		    parent = version
		    notes = 'base;' .. string.match(v, ';(.*)')
		end
		
		if #changes == 0 then
		    changes[1] = {parent, notes}
		else
    		for i=1, #changes, 1 do
    		    if changes[i][1] == parent then 
    		       changes[i][#changes[i] + 1] = notes 
    		    else
    		        changes[i + 1] = {parent, notes}
    		    end
    		    break
    		end
    	end
	end
	
	local body = table.concat(header, '\n') .. '\n'
	for i,t in pairs(changes) do
	    local verName = t[1]
	    local subVerName = string.match(t[2], '(.*);')
	    local notes = string.match(t[2], ';(.*)')
		body = body .. '|rowspan="' .. (#changes[i] - 1) .. '" style="width: 200px; text-align:center"|\'\'\'[[' .. verName .. ']]\'\'\'\n'
		for k = 2, #changes[i], 1 do
		    local subVer = string.match(changes[i][k], '(.*);')
		    local notes = string.match(changes[i][k], ';(.*)')
		    if subVer == 'base' then
		        body = body .. '|' .. changes[i][k] .. '\n|-\n'
		    else
		        body = body .. '|style="width: 200px; text-align:center"|\'\'\'[['.. verName .. '-' .. subVer .. '|' .. subVer .. ']]\'\'\'||' .. notes .. '\n|-\n'
		    end
		end
	end
	return body .. '\n' .. footer
end
return p