模块:沙盒/SunAfterRain/CGroup-merge

local p = {}
local mError = require('Module:Error')

function processText(text)
	local splits = mw.text.split(text, '\n')
	for i, line in ipairs(splits) do
		if mw.ustring.match(line, '^=.-=$') then
			splits[i] = '=' .. line .. '='
		end
	end
	return mw.text.listToText(splits, '\n', '\n')
end

function p.mergeCGroupContent(groups, locals)
	if type(groups) ~= type({}) then
		groups = {group}
	end
	for _, group in ipairs(groups) do
		local status, module = pcall(require, 'Module:CGroup/' .. group)
		if not status then
			table.insert(locals, { type = 'text', text = mError.error({ '合併[[Module:CGroup/' .. group .. ']]失敗:' .. module, tag = 'div' }) })
		else
			table.insert(locals, { type = 'text', text = '== [[Module:CGroup/' .. group .. ']] ==' })
			for _, line in ipairs(module.content) do 
				if line.type == 'text' then
					table.insert(locals, { type = 'text', text = processText(line.text) })
				else
					table.insert(locals, line)
				end
			end
		end
	end
	return locals
end

return p