跳转到内容

模組:Navbox/div

维基百科,自由的百科全书

这是本页的一个历史版本,由Dabao qian留言 | 贡献2024年8月8日 (四) 09:26编辑。这可能和当前版本存在着巨大的差异。

--
-- This module implements {{Navbox}}
--

local p = {}

local navbar = require('Module:Navbar')._navbar
local getArgs -- lazily initialized

local args
local border
local listnums
local ODD_EVEN_MARKER = '\127_ODDEVEN_\127'
local RESTART_MARKER = '\127_ODDEVEN0_\127'
local REGEX_MARKER = '\127_ODDEVEN(%d?)_\127'

local lists = {
	plainlist_t = {
		patterns = {
			'^plainlist$',
			'%splainlist$',
			'^plainlist%s',
			'%splainlist%s'
		},
		found = false,
		styles = 'Plainlist/styles.css'
	},
	hlist_t = {
		patterns = {
			'^hlist$',
			'%shlist$',
			'^hlist%s',
			'%shlist%s'
		},
		found = false,
		styles = 'Hlist/styles.css'
	}
}

local function has_list_class(args_to_check)
	for _, list in pairs(lists) do
		if not list.found then
			for _, arg in pairs(args_to_check) do
				for _, pattern in ipairs(list.patterns) do
					if mw.ustring.find(arg or '', pattern) then
						list.found = true
						break
					end
				end
				if list.found then break end
			end
		end
	end
end

local function striped(wikitext)
	-- Return wikitext with markers replaced for odd/even striping.
	-- Child (subgroup) navboxes are flagged with a category that is removed
	-- by parent navboxes. The result is that the category shows all pages
	-- where a child navbox is not contained in a parent navbox.
	local orphanCat = '[[Category:没有链接的导航框]]'
	if border == 'subgroup' and args.orphan ~= 'yes' then
		-- No change; striping occurs in outermost navbox.
		return wikitext .. orphanCat
	end
	local first, second = 'odd', 'even'
	if args.evenodd then
		if args.evenodd == 'swap' then
			first, second = second, first
		else
			first = args.evenodd
			second = first
		end
	end
	local changer
	if first == second then
		changer = first
	else
		local index = 0
		changer = function (code)
			if code == '0' then
				-- Current occurrence is for a group before a nested table.
				-- Set it to first as a valid although pointless class.
				-- The next occurrence will be the first row after a title
				-- in a subgroup and will also be first.
				index = 0
				return first
			end
			index = index + 1
			return index % 2 == 1 and first or second
		end
	end
	local regex = orphanCat:gsub('([%[%]])', '%%%1')
	return (wikitext:gsub(regex, ''):gsub(REGEX_MARKER, changer))  -- () omits gsub count
end

local function processItem(item, nowrapitems)
	if item:sub(1, 2) == '{|' then
		-- Applying nowrap to lines in a table does not make sense.
		-- Add newlines to compensate for trim of x in |parm=x in a template.
		return '\n' .. item ..'\n'
	end
	if nowrapitems == 'yes' then
		local lines = {}
		for line in (item .. '\n'):gmatch('([^\n]*)\n') do
			local prefix, content = line:match('^([*:;#]+)%s*(.*)')
			if prefix and not content:match('^<span class="nowrap">') then
				line = prefix .. '<span class="nowrap">' .. content .. '</span>'
			end
			table.insert(lines, line)
		end
		item = table.concat(lines, '\n')
	end
	if item:match('^[*:;#]') then
		return '\n' .. item ..'\n'
	end
	return item
end

local function renderNavBar(titleCell)
	-- Depending on the presence of the navbar and/or show/hide link, we may need to add a spacer div on the left
	-- or right to keep the title centered.
	local spacerSide = nil

	if args.navbar == 'off' then
		-- No navbar, and client wants no spacer, i.e. wants the title to be shifted to the left. If there's
		-- also no show/hide link, then we need a spacer on the right to achieve the left shift.
		if args.state == 'plain' then spacerSide = 'right' end
	elseif args.navbar == 'plain' or (not args.name and mw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$', '') == 'Template:Navbox') then
		-- No navbar. Need a spacer on the left to balance out the width of the show/hide link.
		if args.state ~= 'plain' then spacerSide = 'left' end
	else
			-- Will render navbar (or error message). If there's no show/hide link, need a spacer on the right
			-- to balance out the width of the navbar.
			if args.state == 'plain' then spacerSide = 'right' end

			titleCell:wikitext(navbar{
				args.name,
				mini = 1,
				fontstyle = 'color:inherit;' .. (args.basestyle or '') .. ';' .. (args.titlestyle or '') ..  ';background:none transparent;border:none;'
			})
	end

	-- Render the spacer div.
	if spacerSide then
		titleCell
			:tag('span')
				:css('float', spacerSide)
				:css('position', 'absolute')
				:css(spacerSide, '1em')
				:css('margin-' .. (spacerSide == 'left' and 'right' or 'left'), '0.5em')
				:css('padding-' .. spacerSide, '0.2em')
				:wikitext('&nbsp;')
	end
end