模組:CompactTOC

出自維基百科,自由嘅百科全書
模組解[]
-- vi:set sw=4 ts=4 ai sm:
---- This module attempts to serve as a replacement for Template:CompactTOC8
---- but doesn't actually replicate its behaviour

require ('Module:No globals');
local p = {};



--- Auxiliaries ---------------------------------------------------------------

local function parse_title (s)
	local it = nil
	local head
	local x
	local target, class
	if type(s) == 'table' then
		target = s.target
		class = s.class
		s = s.label
	end
	x = mw.ustring.match(s, '^%d+$')
	if x then
		head = x .. '劃'
	else
		head = s
	end
	it = '<li class="'
	if class == nil then
		it = it .. head
	else
		it = it .. class
	end
	it = it .. '">[[#'
	if target == nil then
		it = it .. head
	else
		it = it .. target
	end
	it = it .. '|'
	it = it .. head
	it = it .. ']]'
	return it
end

--- Exported, invocable functions ---------------------------------------------

-- Entry point
p.NewCompactTOC8 = function( frame )
	local parent = frame:getParent();
	local it;
	local styles = '模組:CompactTOC/styles.css';

	local parts = {};
	local top;
	local seealso;
	local side_p = false;
	it = ''
	for k, v in pairs(frame.args) do
		if type(k) == 'number' then
			table.insert(parts, parse_title(v));
		elseif k == 'seealso' then
			if v == 'yes' then
				seealso = parse_title({['label'] = '睇埋', ['class'] = 'seealso'})
			end
		elseif k == 'side' then
			if v == 'yes' then
				side_p = true
			end
		elseif k == 'top' then
			if v == 'yes' then
				top = parse_title({['label'] = '最頂', ['target'] = 'top', ['class'] = 'top'})
			end
		else
			error('NewCompactTOC8 模遇到不明參數 「' .. k .. '」');
		end
	end
	if top then
		table.insert(parts, 1, top)
	end
	if seealso then
		table.insert(parts, seealso)
	end
	if #parts == 0 then
		error('NewCompactTOC8 模出錯,搵唔到要加入目錄嘅標題');
	end

	local title = '目錄'
	
	-- build it
	it = '<div class=NewCompactTOC role=navigation>'
	if side_p then
		it = it .. '<div class="side toc">'
	else
		it = it .. '<div class=toc>'
	end
	it = it .. '<div role=heading>' .. title .. '</div><ul>'
	for i, v in pairs(parts) do
		it = it .. v;
	end
	it = it .. '</ul></div></div>'

	-- request our style sheet
	it = table.concat ({
			frame:extensionTag ('templatestyles', '', {src=styles}),
			it
		});
	return it;
end

--- Non-invocable internal functions exported for other modules to use --------

return p;