×
Create a new article
Write your page title here:
We currently have 3,053 articles on YumeWiki. Type your article name above or click on one of the titles below and start writing!



YumeWiki
3,053Articles

Module:SoundtrackTable: Difference between revisions

No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


function splitByPlainSeparator(str, sep, max)
function split(str, sep, max)
     local z = #sep; sep = '^.-'..sep:gsub('[$%%()*+%-.?%[%]^]', '%%%0')
     local z = #sep; sep = '^.-'..sep:gsub('[$%%()*+%-.?%[%]^]', '%%%0')
     local t,n,p, q,r = {},1,1, str:find(sep)
     local t,n,p, q,r = {},1,1, str:find(sep)
Line 17: Line 17:


function toMap(entries)
function toMap(entries)
local fields = splitByPlainSeparator(entries, '$$')
local fields = split(entries, '$$')
local map = {}
local map = {}
for var=1,#fields-1 do
for var=1,#fields-1 do
local tmp = {}
local tmp = {}
local current = fields[var]
local current = fields[var]
for k, s in pairs(splitByPlainSeparator(current, '$')) do
for k, s in pairs(split(current, '$')) do
local tmp2 = splitByPlainSeparator(s, '=')
local tmp2 = split(s, '=')
tmp[tmp2[1]] = tmp2[2]
tmp[tmp2[1]] = tmp2[2]
end
end
Line 59: Line 59:
function p.entry(frame)
function p.entry(frame)
local parent = frame:getParent()
local parent = frame:getParent()
local idsUntrimmed = splitByPlainSeparator(parent.args["id"], ",")
local idsUntrimmed = split(parent.args["id"], ",")
local ids = {}
local ids = {}
for i = 1, #idsUntrimmed do
for i = 1, #idsUntrimmed do
Line 71: Line 71:
local t = {}
local t = {}
for k, value in pairs(parent.args) do
for k, value in pairs(parent.args) do
local split = splitByPlainSeparator(k, '/')
local kSplit = split(k, '/')
local key, override = split[1], split[2]
local key, override = kSplit[1], kSplit[2]
if not t[key] then
if not t[key] then
t[key] = {}
t[key] = {}
Line 79: Line 79:
t[key][idIndexes[override]] = trim(value)
t[key][idIndexes[override]] = trim(value)
elseif value:find(",") then
elseif value:find(",") then
t[key] = splitByPlainSeparator(value, ",")
t[key] = split(value, ",")
for i = 1, #ids do
for i = 1, #ids do
t[key][i] = trim(t[key][i])
t[key][i] = trim(t[key][i])
Line 103: Line 103:
local locationRegex = '%[%[*.-%|*.-%]%]'
local locationRegex = '%[%[*.-%|*.-%]%]'
if value:find(locationRegex) then
if value:find(locationRegex) then
subobject = subobject .. splitByPlainSeparator(splitByPlainSeparator(value:match(locationRegex), '|')[2], ']')[1]
local locations = {}
for locMatch in value:gmatch(locationRegex) do
table.insert(locations, split(split(split(locMatch, '|')[1], ':')[2], '#')[1])
end
mw.logObject(locations)
subobject = subobject .. table.concat(locations, ",") .. "|+sep=,"
else
else
subobject = subobject .. value
subobject = subobject .. value
Line 121: Line 126:
local args = frame.args
local args = frame.args
local entries = toMap(frame.args['entries'])
local entries = toMap(frame.args['entries'])
local columns = splitByPlainSeparator(frame.args['columns'], ',')
local columns = split(frame.args['columns'], ',')
local result = ""
local result = ""

Revision as of 21:31, 15 July 2024

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

local p = {}

function split(str, sep, max)
    local z = #sep; sep = '^.-'..sep:gsub('[$%%()*+%-.?%[%]^]', '%%%0')
    local t,n,p, q,r = {},1,1, str:find(sep)
    while q and n~=max do
        t[n],n,p = str:sub(q,r-z),n+1,r+1
        q,r = str:find(sep,p)
    end
    t[n] = str:sub(p)
    return t
end

function trim(s)
   return s:match( "^%s*(.-)%s*$" )
end

function toMap(entries)
	local fields = split(entries, '$$')
	local map = {}
	for var=1,#fields-1 do
		local tmp = {}
		local current = fields[var]
		for k, s in pairs(split(current, '$')) do
			local tmp2 = split(s, '=')
			tmp[tmp2[1]] = tmp2[2]
		end
		map[var] = tmp
	end
	return map
end

function value(entries, i, j, args)
	if not entries[i] then
		return 'Out Of Range'
	else
		if not entries[i][j] or entries[i][j] == '' then
			return args['default/' .. j] or ''
		else
			return entries[i][j]
		end
	end
end

function uniq(entries, i, j, args, columns)
	if not entries[i] then
		return 'Out Of Range'
	end
	if not args['unique/' .. j] then
		return value(entries, i, j, args)
	end
	local ret = args['unique/' .. j]
	for k, column in pairs(columns) do
		ret = ret:gsub(column, value(entries, i, column, args))
	end
	return ret
end

function p.entry(frame)
	local parent = frame:getParent()
	local idsUntrimmed = split(parent.args["id"], ",")
	local ids = {}
	for i = 1, #idsUntrimmed do
		ids[i] = trim(idsUntrimmed[i])
	end
	local idIndexes = {}
	for k, v in pairs(ids) do
		idIndexes[v] = k
	end
	
	local t = {}
	for k, value in pairs(parent.args) do
		local kSplit = split(k, '/')
		local key, override = kSplit[1], kSplit[2]
		if not t[key] then
			t[key] = {}
		end
		if override then
			t[key][idIndexes[override]] = trim(value)
		elseif value:find(",") then
			t[key] = split(value, ",")
			for i = 1, #ids do
				t[key][i] = trim(t[key][i])
			end
		else
			for i = 1, #ids do
				if not t[key][i] then
					t[key][i] = trim(value)
				end
			end
		end
	end
	
	local result = ""
	for i = 1, #ids do
		local id = t["id"][i]
		local subobject = "{{#subobject:BGM/" .. id
		for key, tvalue in pairs(t) do
			local value = tvalue[i] or ""
			result = result .. "$" .. key .. "=" .. value
			subobject = subobject .. "\n|BGM/" .. key .. "="
			if key == "location" then
				local locationRegex = '%[%[*.-%|*.-%]%]'
				if value:find(locationRegex) then
					local locations = {}
					for locMatch in value:gmatch(locationRegex) do
						table.insert(locations, split(split(split(locMatch, '|')[1], ':')[2], '#')[1])
					end
					mw.logObject(locations)
					subobject = subobject .. table.concat(locations, ",") .. "|+sep=,"
				else
					subobject = subobject .. value
				end
			else
				subobject = subobject .. value
			end
		end
		result = result .. "$$"
		subobject = subobject .. "\n}}"
		frame:preprocess(subobject)
	end
	return result
end

function p.generate(frame)
	local args = frame.args
	local entries = toMap(frame.args['entries'])
	local columns = split(frame.args['columns'], ',')
	
	local result = ""
	for var=1,#entries do
		for k, column in pairs(columns) do
			local id = uniq(entries, var, column, args, columns)
			if (id ~= uniq(entries, var - 1, column, args, columns)) then
				local rowspan = 1
				while (id == uniq(entries, var + rowspan, column, args, columns)) do
					rowspan = rowspan + 1
				end
				local value = value(entries, var, column, args)
				if value ~= '' and frame.args['display/' .. column] then
					value = frame.args['display/' .. column]:gsub('value', value)
				end
				result = result .. '|rowspan="' .. tostring(rowspan) .. '"|' .. value .. "\n"
			end
		end
		if (var ~= #entries) then
			result = result .. "|-\n"
		end
	end
	return result
end

return p