Module:Section Index

From Coral Island Wiki
Jump to navigation Jump to search

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

local p = {}
local lib = require('Module:Feature/Draft')
local search = require('Module:TableTools').includes

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrappers = {'Template:Section Index'}
	})
	return p._main(args)
end

function p._main(args)
	args[2] = args[2]:gsub('_',' ')
	local title = mw.title.makeTitle('',args[1],args[2])
	local pageContent = title:getContent():gsub('<!%-%-.-%-%->',''):gsub('^.-(==+)%s*','',1):gsub('\n.-(==+)%s*','%1'):gsub('%s*(==+)\n.-$',''):gsub('%s*==+%s*','//')
	--mw.logObject(pageContent)
	assert(pageContent,'Page does not exist')
	args.sections = lib.split(pageContent, '//' )
	-- mw.logObject(args.sections,'Before')
	if #args.sections > 1 then
		for i,s in ipairs(args.sections) do
			local repeated = 2
			for I = i+1,#args.sections do
				if args.sections[I] == s then
					args.sections[I] = args.sections[I] .. ' ' .. tostring(repeated)
					repeated = repeated + 1
				end
			end
		end
	end
	-- mw.logObject(args.sections,'After')
	for k,v in ipairs(args.sections) do
		if v == args[2] then
			return tostring(k)
		end
	end
	return ''
end


return p