Module:Auto compact TOC/sandbox

From Wikipedia, the free encyclopedia
local p = {}
local horizontal = require('Module:List').horizontal

local function make_TOC_item_linked(anchor, text)
	text = text or anchor
--	if (frame.args.auto == "")
	return ("[[#%s|%s]]"):format(anchor, text)
end
local function make_TOC_item_unlinked(anchor, text)
	text = text or anchor
	return ("%s"):format(anchor, text)
end
local Array_mt = { __index = table }
local function Array()
	return setmetatable({}, Array_mt)
end

function p.make_TOC(frame)
	local content = mw.title.getCurrentTitle():getContent()
	
	if not content then
		error "The current page has no content"
	end
	
	local letters = Array()
	-- Find uppermost headers containing a single ASCII letter.
	if frame.args.auto:upper() == "LINK" then -- If we wanted them linked, do that
		for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^=]" do
		letter = letter:upper()
		letters:insert(make_TOC_item_linked(letter))
		end
	else -- f we do not want them linked, don't
		for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^=]" do
		letter = letter:upper()
		letters:insert(make_TOC_item_unlinked(letter))
		end
	end
	
	
	return   horizontal(letters) .. horizontal(rest) 
end

return p