Module:Navbox

Iz Medžuviki, svobodnoj enciklopedije
Jump to navigation Jump to search

Nema teksta dokumentacije.

--
-- This module implements {{Navbox}}
--
require('strict')

local p = {}

local stylesPage = 'Module:Navbox/styles.css'
local getArgs -- lazily initialized

local args
local border
local listnums

-- ↓ Convert to Cyrillic if needed
local isv_cyrl = require('Module:Alphabet')._global_cyrl

-- ↓ Only convert visible link text
local function cyrlLinkText( text )
	if not text:match('%[%[') then
		return isv_cyrl(text)
	end
	
	if isv_cyrl(text) == text then
		return text
	end
	
	return ( text:gsub(
		'%[%[([^%]|]+)|([^%]]+)',
		function(s1, s2)
			return string.format('[[%s|%s', s1, isv_cyrl( s2 ))
		end
	):gsub(
		'%[%[([^%]|]+)%]%]',
		function(s1)
			local cyrl = isv_cyrl(s1)
			if s1 ~= cyrl then
				return string.format('[[%s|%s]]', s1, cyrl)
			end
			
			return string.format('[[%s]]', s1)
		end
	) )
end

-- ↓ Repurposed for Cyrillic support
local function processItem(item)
	local ignoreClass = '<span class="ext-gadget-alphabet-disable">'
	ignoreClass = ignoreClass:gsub("([^%w])", "%%%1")
	
	-- Handle lists
	local lines = {}
	for line in (item .. '\n'):gmatch('([^\n]*)\n') do
		local prefix, content = mw.ustring.match(line, '^([*:;#]+)%s*(.*)')
		if prefix and not mw.ustring.match(content, '^' .. ignoreClass) then
			-- Ignore lines likely in Latin
			line = prefix .. cyrlLinkText(content)
		end
		table.insert(lines, line)
	end
	item = table.concat(lines, '\n')
	
	if item:match('^[*:;#]') then
		return '\n' .. item .. '\n'
	end
	
	-- Handle simple text
	if mw.ustring.match(item, '^' .. ignoreClass) then
		return item
	end

	return isv_cyrl(item)
end

-- ↓ Custom navbar
local function renderNavBar(titleCell)
	if not args.name then
		return
	end
	
	local navbarTitle = 'Template:' .. args.name
	local success, navbar = pcall(require('Module:Navbar')._navbar, {
		navbarTitle,
		'view',
		'talk',
		'edit',
		['class'] = 'navbox-edit',
	})

	if success then
		titleCell:node(navbar)
	end
end

-- ↓ Add hlist styles automatically
local function renderHlistStyles(class)
	local hlistStylesPage = 'Template:Flatlist/styles.css'
	local ignoreClass = 'nohlist'
	
	if class == nil then class = '' end
	local styles = ''
	if not mw.ustring.find(class, ignoreClass) then
		class = 'hlist ' .. class
		local frame = mw.getCurrentFrame()
		styles = frame:extensionTag{
			name = 'templatestyles',
			args = { src = hlistStylesPage }
		}
	else
		class = mw.ustring.gsub(listClass, ignoreClass, '')
	end
	
	return class, styles
end

--
--   ↓ (Custom) Title row
--
local function renderTitleRow(tbl)
	-- Make title row required
	if not args.title then
		args.title = '{{{title}}}'
	end

	local titleCell = tbl:tag('caption'):addClass('navbox-title')

	titleCell
		:tag('strong')
			-- id for aria-labelledby attribute
			:attr('id', 'navbox-' .. mw.uri.anchorEncode(args.title))
			:addClass(args.titleclass)
			:wikitext(processItem(args.title))
	
	-- Toggle placement
	if (args.state ~= 'plain' and args.state ~= 'off') then
		titleCell
			:wikitext(' ')
			:tag('span'):addClass('navbox-icon mw-collapsible-toggle')
				:attr('role', 'button')
				:tag('span'):addClass('mw-collapsible-text')
					:wikitext(mw.message.new('collapsible-collapse'):plain())
	end

	renderNavBar(titleCell)
end

--
--   Above/Below rows
--
local function renderAboveRow(tbl)
	if not args.above then return end

	-- ↓ Removed inline CSS, added default hlist
	local listClass, listStyles = renderHlistStyles(args.aboveclass)
	tbl:tag('tr')
		:tag('td')
			:addClass('navbox-abovebelow')
			:addClass(listClass)
			-- ↓ No need for image support
			:attr('colspan', 2)
			:wikitext(listStyles)
			:wikitext(processItem(args.above, args.nowrapitems))
end

local function renderBelowRow(tbl)
	if not args.below then return end

	-- ↓ Removed inline CSS / div, added default hlist
	local listClass, listStyles = renderHlistStyles(args.belowclass)
	tbl:tag('tr')
		:tag('td')
			:addClass('navbox-abovebelow')
			:addClass(listClass)
			-- ↓ No need for image support
			:attr('colspan', 2)
			:wikitext(listStyles)
			:wikitext(processItem(args.below, args.nowrapitems))
end

--
--   List rows
--
local function renderListRow(tbl, index, listnum)
	local row = tbl:tag('tr')

	-- ↓ Removed imageleft/image support
	if args['group' .. listnum] then
		local groupCell = row:tag('th')

		-- ↓ Removed aria-labelledby
		groupCell
			:attr('scope', 'row')
			:addClass('navbox-group')
			:addClass(args.groupclass)
			:css('width', args.groupwidth) -- If groupwidth not specified, minimize width

		groupCell
			:wikitext(processItem(args['group' .. listnum]))
	end

	local listCell = row:tag('td')

	-- ↓ Removed inline CSS
	if not args['group' .. listnum] then
		listCell:attr('colspan', 2)
	end

	-- ↓ Removed CSS and even-odd, added default hlist
	local listText = args['list' .. listnum]
	local listClass = table.concat({
		args.listclass,
		args['list' .. listnum .. 'class'],
	}, ' ')
	local listClass, listStyles = renderHlistStyles(listClass)
	listCell
		:addClass('navbox-list')
		:addClass(listClass)
		:wikitext(listStyles)
		:wikitext(processItem(listText, args.nowrapitems))
end

-- ↓ Removed tracking categories

--
--   Main navbox tables
--
local function renderMainTable()
	local tbl = mw.html.create('table')
		-- ↓ No nowraplinks
		:addClass(args.bodyclass)

	-- ↓ Ignore state parameter for most cases
	if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
		tbl
			:addClass('mw-collapsible autocollapse')
	end

	-- ↓ Removed inline CSS and subgroups
	tbl
		:addClass('navbox-inner')

	renderTitleRow(tbl)
	renderAboveRow(tbl)
	for i, listnum in ipairs(listnums) do
		renderListRow(tbl, i, listnum)
	end
	renderBelowRow(tbl)

	return tbl
end

function p._navbox(navboxArgs)
	args = navboxArgs
	listnums = {}

	for k, _ in pairs(args) do
		if type(k) == 'string' then
			local listnum = k:match('^list(%d+)$')
			if listnum then table.insert(listnums, tonumber(listnum)) end
		end
	end
	table.sort(listnums)

	-- render the main body of the navbox
	local tbl = renderMainTable()

	-- ↓ Removed subgroup/border parameters
	-- render the appropriate wrapper around the navbox, depending on the border param
	local res = mw.html.create()
	local nav = res:tag('div')
		:attr('role', 'navigation')
		-- ↓ Changed class to avoid MobileFrontend
		:addClass('navbox-outer')
		:addClass(args.navboxclass)
		:node(tbl)
	-- ↓ Always aria-labelledby based on title
	if args.title ~= '{{{title}}}' then
		nav:attr('aria-labelledby', 'navbox-' .. mw.uri.anchorEncode(args.title))
	else
		nav:attr('aria-label', 'Navigacija')
	end
	
	local frame = mw.getCurrentFrame()
	-- ↓ Add TemplateStyles
	local styles = frame:extensionTag{
		name = 'templatestyles',
		args = { src = stylesPage }
	}
	return styles .. tostring(res)
end

function p.navbox(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	args = getArgs(frame, {wrappers = {
		-- ↓ More template names
		'Template:Navbox',
		'Template:Navigacija',
	}})

	-- Read the arguments in the order they'll be output in, to make references number in the right order.
	local _
	_ = args.title
	_ = args.above
	for i = 1, 20 do
		_ = args["group" .. tostring(i)]
		_ = args["list" .. tostring(i)]
	end
	_ = args.below

	return p._navbox(args)
end

return p