Модуль:Песочница/Pok: различия между версиями

Материал из Space Station 14 Вики
Нет описания правки
Нет описания правки
Строка 6: Строка 6:
end
end


local function split(s, sep)
if s == nil then return {} end
local parts = {}
sep = sep or "%."
for part in string.gmatch(s, "([^" .. sep .. "]+)") do
table.insert(parts, part)
end
return parts
end


local function parse_keys_from_template(content)
 
local function parse_keys_from_template(content, switches)
if not content then return {} end
if not content then return {} end
local keys = {}
local lower = content:lower()
local lower = content:lower()
local titlePos = lower:find("|%s*title%s*=")
local result = {}
if not titlePos then
for _, sw in ipairs(switches) do
return keys
result[sw] = {}
end
local swLower = sw:lower()
local pos = lower:find("|%s*" .. swLower .. "%s*=")
local startBrace = content:find("{{", titlePos)
if not pos then
local region = nil
-- no keys for this switch
if startBrace then
else
local len = #content
local startBrace = content:find("{{", pos)
local i = startBrace
local region = nil
local depth = 0
if startBrace then
while i <= len - 1 do
local len = #content
local two = content:sub(i, i+1)
local i = startBrace
if two == "{{" then
local depth = 0
depth = depth + 1
while i <= len - 1 do
i = i + 2
local two = content:sub(i, i+1)
elseif two == "}}" then
if two == "{{" then
depth = depth - 1
depth = depth + 1
i = i + 2
i = i + 2
if depth == 0 then
elseif two == "}}" then
region = content:sub(startBrace, i-1)
depth = depth - 1
break
i = i + 2
if depth == 0 then
region = content:sub(startBrace, i-1)
break
end
else
i = i + 1
end
end
end
if not region then
local substr = content:sub(pos)
local endPos = substr:find("}}")
if endPos then
region = substr:sub(1, endPos)
else
region = substr
end
end
for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
local k = trim(key)
if k ~= "" then
table.insert(result[sw], k)
end
end
else
i = i + 1
end
end
end
end
end
end
if not region then
return result
local substr = content:sub(titlePos)
local endPos = substr:find("}}")
if endPos then
region = substr:sub(1, endPos)
else
region = substr
end
end
for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
local k = trim(key)
if k ~= "" then
table.insert(keys, k)
end
end
return keys
end
end


Строка 130: Строка 126:
local out = {}
local out = {}
local errors = {}
local errors = {}
local keyOrder = {}
local switches = { "card", "title" }
local keyToTemplates = {}
local switchKeyOrder = {}
local switchKeyToTemplates = {}
for _, sw in ipairs(switches) do
switchKeyOrder[sw] = {}
switchKeyToTemplates[sw] = {}
end
local switchConfigs = {
card = {
wrapper = function(key, tplCalls)
if not tplCalls or #tplCalls == 0 then return "" end
return "{{card|" .. mw.text.encode(key) .. "|" .. table.concat(tplCalls, " ") .. "}}"
end
},
title = {
wrapper = function(key, tplCalls)
local parts = {}
table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>")
if tplCalls and #tplCalls > 0 then
for _, tpl in ipairs(tplCalls) do
table.insert(parts, tpl)
end
end
return table.concat(parts, "\n")
end
}
}


local function lcfirst(s)
local function lcfirst(s)
Строка 138: Строка 159:
end
end


local items = {}
for compName,_ in pairs(foundComponents) do
for compName,_ in pairs(foundComponents) do
local compPathName = lcfirst(compName)
table.insert(items, { kind = "component", name = compName })
local tplPath = "component/" .. compPathName
end
local content = load_template_content(tplPath)
for protoName,_ in pairs(foundPrototypes) do
if not content then
table.insert(items, { kind = "prototype", name = protoName })
table.insert(errors, "Ошибка: не найден шаблон component/" .. compPathName)
else
local keys = parse_keys_from_template(content)
for _, key in ipairs(keys) do
if not keyToTemplates[key] then
keyToTemplates[key] = {}
table.insert(keyOrder, key)
end
table.insert(keyToTemplates[key], "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
end


for protoName,_ in pairs(foundPrototypes) do
for _, item in ipairs(items) do
local protoPathName = lcfirst(protoName)
local kind = item.kind
local tplPath = "prototype/" .. protoPathName
local name = item.name
local pathName = lcfirst(name)
local tplPath = kind .. "/" .. pathName
local content = load_template_content(tplPath)
local content = load_template_content(tplPath)
if not content then
if not content then
table.insert(errors, "Ошибка: не найден шаблон prototype/" .. protoPathName)
table.insert(errors, "Ошибка: не найден шаблон " .. kind .. "/" .. pathName)
else
else
local keys = parse_keys_from_template(content)
local parsed = parse_keys_from_template(content, switches)
for _, key in ipairs(keys) do
for _, sw in ipairs(switches) do
if not keyToTemplates[key] then
local keys = parsed[sw] or {}
keyToTemplates[key] = {}
for _, key in ipairs(keys) do
table.insert(keyOrder, key)
if not switchKeyToTemplates[sw][key] then
switchKeyToTemplates[sw][key] = {}
table.insert(switchKeyOrder[sw], key)
end
table.insert(switchKeyToTemplates[sw][key], "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
table.insert(keyToTemplates[key], "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
end
Строка 178: Строка 194:
end
end


for _, key in ipairs(keyOrder) do
for _, sw in ipairs(switches) do
table.insert(out, "<h2>" .. mw.text.encode(key) .. "</h2>")
local cfg = switchConfigs[sw] or {}
for _, tplCall in ipairs(keyToTemplates[key]) do
for _, key in ipairs(switchKeyOrder[sw]) do
table.insert(out, tplCall)
local tplCalls = switchKeyToTemplates[sw][key] or {}
if cfg.wrapper then
local outStr = cfg.wrapper(key, tplCalls)
if outStr and outStr ~= "" then
table.insert(out, outStr)
end
end
end
end
end
end

Версия от 12:56, 21 января 2026

Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc

local p = {}

local function trim(s)
	if not s then return s end
	return (s:gsub("^%s*(.-)%s*$", "%1"))
end



local function parse_keys_from_template(content, switches)
	if not content then return {} end
	local lower = content:lower()
	local result = {}
	for _, sw in ipairs(switches) do
		result[sw] = {}
		local swLower = sw:lower()
		local pos = lower:find("|%s*" .. swLower .. "%s*=")
		if not pos then
			-- no keys for this switch
		else
			local startBrace = content:find("{{", pos)
			local region = nil
			if startBrace then
				local len = #content
				local i = startBrace
				local depth = 0
				while i <= len - 1 do
					local two = content:sub(i, i+1)
					if two == "{{" then
						depth = depth + 1
						i = i + 2
					elseif two == "}}" then
						depth = depth - 1
						i = i + 2
						if depth == 0 then
							region = content:sub(startBrace, i-1)
							break
						end
					else
						i = i + 1
					end
				end
			end
			if not region then
				local substr = content:sub(pos)
				local endPos = substr:find("}}")
				if endPos then
					region = substr:sub(1, endPos)
				else
					region = substr
				end
			end
			for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
				local k = trim(key)
				if k ~= "" then
					table.insert(result[sw], k)
				end
			end
		end
	end
	return result
end

local function load_module_data(page)
	local baseUser = "IanComradeBot/"
	local moduleName = "Module:" .. baseUser .. page .. "/data"
	local ok, data = pcall(mw.loadData, moduleName)
	if not ok then return nil end
	return data
end

local function load_template_content(path)
	local title = mw.title.new("Template:" .. path)
	if not title then return nil end
	local ok, content = pcall(function() return title:getContent() end)
	if not ok then return nil end
	return content
end

function p.get(frame)
	local args = frame.args or {}
	local id = args[1] or ""
	if id == "" then return "" end

	local componentDefs = load_module_data("component.json")
	local prototypeDefs = load_module_data("prototype.json")
	if not componentDefs or not prototypeDefs then
		return ""
	end

	local foundComponents = {}
	local foundPrototypes = {}
	local compList = componentDefs[id]
	if type(compList) == "table" then
		for _, v in ipairs(compList) do
			if type(v) == "string" then
				foundComponents[v] = true
			end
		end
	end

	local protoList = prototypeDefs[id]
	if type(protoList) == "table" then
		for _, v in ipairs(protoList) do
			if type(v) == "string" then
				foundPrototypes[v] = true
			end
		end
	end

	for name in string.gmatch(id, "[^,]+") do
		local n = trim(name)
		if n ~= "" then
			if componentDefs[n] ~= nil then
				foundComponents[n] = true
			end
			if prototypeDefs[n] ~= nil then
				foundPrototypes[n] = true
			end
			if componentDefs[n] == nil and prototypeDefs[n] == nil then
				foundComponents[n] = true
			end
		end
	end

	local out = {}
	local errors = {}
	local switches = { "card", "title" }
	local switchKeyOrder = {}
	local switchKeyToTemplates = {}
	for _, sw in ipairs(switches) do
		switchKeyOrder[sw] = {}
		switchKeyToTemplates[sw] = {}
	end
	local switchConfigs = {
		card = {
			wrapper = function(key, tplCalls)
				if not tplCalls or #tplCalls == 0 then return "" end
				return "{{card|" .. mw.text.encode(key) .. "|" .. table.concat(tplCalls, " ") .. "}}"
			end
		},
		title = {
			wrapper = function(key, tplCalls)
				local parts = {}
				table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>")
				if tplCalls and #tplCalls > 0 then
					for _, tpl in ipairs(tplCalls) do
						table.insert(parts, tpl)
					end
				end
				return table.concat(parts, "\n")
			end
		}
	}

	local function lcfirst(s)
		if not s or s == "" then return s end
		return string.lower(s:sub(1,1)) .. (s:sub(2) or "")
	end

	local items = {}
	for compName,_ in pairs(foundComponents) do
		table.insert(items, { kind = "component", name = compName })
	end
	for protoName,_ in pairs(foundPrototypes) do
		table.insert(items, { kind = "prototype", name = protoName })
	end

	for _, item in ipairs(items) do
		local kind = item.kind
		local name = item.name
		local pathName = lcfirst(name)
		local tplPath = kind .. "/" .. pathName
		local content = load_template_content(tplPath)
		if not content then
			table.insert(errors, "Ошибка: не найден шаблон " .. kind .. "/" .. pathName)
		else
			local parsed = parse_keys_from_template(content, switches)
			for _, sw in ipairs(switches) do
				local keys = parsed[sw] or {}
				for _, key in ipairs(keys) do
					if not switchKeyToTemplates[sw][key] then
						switchKeyToTemplates[sw][key] = {}
						table.insert(switchKeyOrder[sw], key)
					end
					table.insert(switchKeyToTemplates[sw][key], "{{" .. tplPath .. "|title|" .. key .. "}}")
				end
			end
		end
	end

	for _, e in ipairs(errors) do
		table.insert(out, "<div class=\"error\">" .. mw.text.encode(e) .. "</div>")
	end

	for _, sw in ipairs(switches) do
		local cfg = switchConfigs[sw] or {}
		for _, key in ipairs(switchKeyOrder[sw]) do
			local tplCalls = switchKeyToTemplates[sw][key] or {}
			if cfg.wrapper then
				local outStr = cfg.wrapper(key, tplCalls)
				if outStr and outStr ~= "" then
					table.insert(out, outStr)
				end
			end
		end
	end

	return frame:preprocess(table.concat(out, "\n\n"))
end

return p