Module:SectionIndex

From Puella Magi Wiki
Jump to navigation Jump to search

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

local p = {}

function p.main(frame)
	local page, section = frame.args[1], frame.args[2]
	if not page then
		return 'no-page'
	end
	if not section then
		return 'no-section'
	end
	if section:match('^#') then
		section = section:sub(2)
	end
	if section == '' then
		return 0
	end
	local title = mw.title.new(page)
	if not title then
		return 'no-title'
	end
	local wikitext = title:getContent()
	if not wikitext then
		return 'no-content'
	end
	local found = false
	local i = 0
	for next_section in wikitext:gmatch('\n=+%s*(.-)%s*=+') do
		i = i + 1
		if section == next_section then
			found = true
			break
		end
	end
	if not found then
		return 'section-not-found'
	end
	return i
end

return p