Module:Navbox: Difference between revisions

From PT National Labs
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
-- <nowiki>
--
-- Implements {{Navbox}}
--
local p = {}
local p = {}
local tnavbar = require( 'Module:Tnavbar' )
function p.box( f )
local yesno = require( 'Module:Yesno' )
local args = require( 'Module:ProcessArgs' ).merge( true )
local page_title = mw.title.getCurrentTitle().fullText
local navbox = {}
--
-- Helper for inserting a new row into the navbox
--
-- @param tbl {mw.html table}
-- @return tbl {mw.html table}
--
local function insertRow( tbl )
return tbl:tag( 'tr' )
end
 
--
-- Creates the navbox table
--
-- @param args {table}
-- @return tbl {mw.html table}
--
local function createTbl( args )
 
local tbl = mw.html.create( 'table' )


tbl
if args.title then
:addClass( yesno(args.subgroup, false) and 'navbox-subgroup' or 'navbox <mw:editsection' )
local class = args.class or 'collapsible'
:addClass( 'nowraplinks' )
local bodyStyle = args.bodystyle or ''
 
if bodyStyle ~= '' then
if not yesno(args.subgroup, false) and
bodyStyle = 'style="' .. bodyStyle .. '"'
( args.state == 'collapsed' or
  args.state == 'uncollapsed' or
  args.state == 'autocollapse' or
  -- defaults to autocollapse
  args.state == nil )
then
tbl:addClass( 'mw-collapsible' )
 
if args.state == 'collapsed' then
tbl:addClass( 'mw-collapsed' )
elseif args.state == 'uncollapsed' then
tbl:addClass('navbox-uncollapsed')
else
tbl:addClass( 'mw-collapsed' )
tbl:addClass( 'navbox-autocollapse' )
end
end
end
table.insert( navbox, ' {| class="navbox hlist ' .. class .. '" ' .. bodyStyle )
 
if yesno(args.collapsible, false) then
local titleStyle = args.titlestyle or ''
tbl:addClass( 'navbox-collapsible' )
if titleStyle ~= '' then
end
titleStyle = 'style="' .. titleStyle .. '"'
 
if args.style then
tbl:cssText( args.style )
end
 
-- used by [[MediaWiki:Gadget-navbox-tracking.js]] for tracking purposes
tbl:attr( { ['data-navbox-name'] = args.name } )
 
return tbl
end
 
--
-- Wrapper for [[Module:Tnavbar]]
--
-- @param args {table}
-- @return {mw.html table}
--
local function navbar( args )
 
local div = mw.html.create( 'div' )
:css( {
float = 'left',
['text-align'] = 'left'
} )
:wikitext( tnavbar._navbar( { [1] = args.name, ['mini'] = true, ['talk'] = 'autoconfirmed' } ) )
return div
end
 
--
-- Creates the header (what you see when the navbox is collapsed)
--
-- @param tbl {mw.html table}
-- @param args {table}
-- @return {mw.html table}
--
local function header( tbl, args )
local div = insertRow( tbl )
:tag( 'th' )
:attr( 'colspan', '2' )
:addClass( 'navbox-title' )
:wikitext( args.name and tostring( navbar( args ) ) )
:tag('div')
:addClass( 'navbox-title-name' )
:wikitext( args.title )
 
return div:allDone()
end
 
--
-- Inserts a row into the navbox
--
-- @param tbl {mw.html table}
-- @param gtitle {string}
-- @param group {string}
-- @param gtype {string}
-- @param style {string}
-- @return {mw.html table}
--
local function row( tbl, gtitle, group, gtype, style, _name, subgroup )
local tr = insertRow( tbl )
local td
 
if gtitle then
td = tr
:addClass( 'navbox-group' )
:tag( 'th' )
:addClass( 'navbox-group-title' )
:wikitext( gtitle )
:done()
:tag( 'td' )
else
td = tr
:addClass( 'navbox-group' )
:addClass( 'navbox-group-split' )
:tag( 'td' )
:addClass( 'navbox-group-title-hidden' )
:attr( 'colspan', '0' )
:css( 'display', 'none' )
:done()
:tag( 'td' )
:attr( 'colspan', '2' )
end
 
--[[
  List styling
  This is unlikely to be implemented in the near future due to it requiring extra css to work
  and mobile currently not supporting that css.
  As an example, it lets you do the following instead if using {{*}} all the time
  | group3 =
  * {{plink|foo}}
  * {{plink|bar}}
  * {{plink|baz}}
]]
if mw.ustring.match( group, '^%s*%*' ) then
td:newline()
 
-- trim whitespace on bullets
local spl = mw.text.split( group, '\n' )
 
for i = 1, #spl do
spl[i] = mw.text.trim( spl[i] )
end
end
 
local navbar = args[1] or ''
group = '\n' .. table.concat( spl, '\n' )
if navbar ~= '' then
end
local mini = ''
 
if navbar:match( 'navbar%-mini' ) then
--local group2 = group
mini = '1'
--local group3 = group2
end
-- analytics
navbar = '<div class="navbox-navbar">' .. f:expandTemplate( {
 
title = 'navbar',
--if _name then
args = {
-- local name = mw.ustring.gsub(_name,' ','_')
args.name,
-- for v in mw.ustring.gmatch(group,'%[%[[^%]]+%]%]') do
mini = mini
-- if mw.ustring.match(v,'%[%[File:.+|link=') then
}
-- local link = mw.ustring.match(v,'|link=([^%]|]+)')
} ) .. '</div>'
-- if link then
-- local linkrep = mw.ustring.gsub(link,'([%%%]%[%-^$*()+?])','%%%1')
-- local _link = mw.ustring.gsub(link,' ','_')
-- local newfile = mw.ustring.gsub(v,'|link='..linkrep,string.format('|link=https://oldschool.runescape.wiki/w/%s?f=%s',_link,name))
-- local w = mw.ustring.gsub(v,'([%%%]%[%-^$*()+?])','%%%1')
-- group2 = mw.ustring.gsub(group2,w,newfile)
-- end
-- elseif mw.ustring.match(v,'%[%[Category:') then
-- nothing
-- else
-- local link = mw.ustring.match(v,'%[%[([^%]|]+)')
-- local txt = mw.ustring.match(v,'%|([^%]|]+)') or link
 
-- local newlink = ''
 
-- black links if current page
-- if link == page_title then
-- newlink = string.format('<b>%s</b>',txt)
-- else
-- local _link = mw.ustring.gsub(link or '',' ','_')
-- newlink = string.format('[https://oldschool.runescape.wiki.com/w/%s?n=%s %s]',_link,name,txt)
-- end
-- local w = mw.ustring.gsub(v,'([%%%]%[%-^$*()+?])','%%%1')
-- group2 = mw.ustring.gsub(group2,w,newlink)
-- end
-- end
 
--[==[
fix [[these kind]]s of [[link]]s post analytics parse
]==]
-- group3 = group2
 
-- for v in mw.ustring.gmatch(group2,'%[https://oldschool.runescape.wiki.com/w[^%]]-%]%a') do
-- local rep = mw.ustring.gsub(v,'%]','')
-- rep = rep..']'
-- local w = mw.ustring.gsub(v,'([%%%]%[%-^$*()+?])','%%%1')
 
-- group3 = mw.ustring.gsub(group2,w,rep)
-- end
--end
 
td
:addClass( 'navbox-list' )
:wikitext( group ) --group3
 
if gtype and mw.ustring.lower( gtype ) == 'subgroup' then
td
:addClass( 'navbox-parent' )
:css( {
padding = '0'
} )
end
 
if style then
td:cssText( style )
end
 
return td:allDone()
end
 
--
-- Inserts a footer into the navbox
--
-- @param tbl {mw.html table}
-- @param args {table}
-- @return {mw.html table}
--
local function footer( tbl, args )
local th = insertRow( tbl )
:tag( 'th' )
:attr( 'colspan', '2' )
:addClass( 'navbox-footer' )
 
if args.fstyle then
th:cssText( args.fstyle )
end
 
if mw.ustring.match( args.footer, '^%s*%*' ) then
th:newline()
 
-- trim whitespace on bullets
local spl = mw.text.split( args.footer, '\n' )
 
for i = 1, #spl do
spl[i] = mw.text.trim( spl[i] )
end
end
 
table.insert( navbox, '! class="navbox-top" colspan="2" ' .. titleStyle .. ' | ' .. navbar .. '<span class="navbox-title">' .. args.title .. '</span>' )
args.footer = table.concat( spl, '\n' )
 
th:addClass( 'navbox-list' )
end
 
th:wikitext( args.footer )
 
return th:allDone()
end
 
--
-- Adds [[Category:Navbox templates]] to navbox template pages
--
-- @return {string}
--
local function categories()
local title = mw.title.getCurrentTitle()
local page = title.text
local ns = title.nsText
 
if ns == 'Template' then
-- sort in category by pagename
return '[[Category:Navbox templates| ' .. page .. ']]'
else
return ''
end
 
end
 
--
-- Adds [[Template:Navbox/doc]] to navbox template pages
--
-- @param args {table}
-- @return {string}
--
local function docs( args )
local frame = mw.getCurrentFrame()
local title = mw.title.getCurrentTitle()
local base = title.baseText
local ns = title.nsText
 
-- not if a subpage of [[Template:Navbox]]
if base ~= 'Navbox' and
-- in template ns
ns == 'Template' and
-- not a navbox group within a navbox
not yesno(args.subgroup, false) and
-- not a collapsible navbox within a navbox
not yesno(args.collapsible, false) and
-- not if the doc argument is not set to "yes"
yesno(args.doc, false)
then
return frame:expandTemplate{ title = 'Navbox/doc' }
else
else
return ''
table.insert( navbox, ' {| class="navbox-child"' )
end
end
end
--
-- Navbox method to allow it to be called by other modules
--
-- @param _args {table}
-- @return {string}
--
function p._navbox( _args )
local args = {}
local wkCss = ''
local wkDiv = ''
local j
-- preserves parser function behaviour where an empty string is considered undefined
local groupNums = {}
-- or nil in lua's case
for k, v in pairs( args ) do
for k, v in pairs( _args ) do
if type( k ) == 'string' then
if v ~= '' then
local groupNum = k:match( 'group(%d+)' )
args[k] = v
if groupNum and v then
table.insert( groupNums, tonumber( groupNum ) )
end
end
end
end
end
 
table.sort( groupNums )
local tbl = createTbl( args )
 
local groupStyle = args.groupstyle or ''
if not yesno(args.subgroup, false) then
local listStyle = args.liststyle or ''
tbl = header( tbl, args )
for _, v in ipairs( groupNums ) do
end
local list = args['list' .. v]
 
if list then
-- insert up to 25 rows
table.insert( navbox, '|-\n! class="navbox-group" style="' .. groupStyle .. '" | ' .. args['group' .. v] )
for i = 1, 25 do
table.insert( navbox, '| class="navbox-list" style="' .. listStyle .. '" | ' .. list:gsub( '^([*#:{])', '\n%1' ) )
j = tostring( i )
 
if args['group' .. j] then
tbl = row( tbl, args['gtitle' .. j], args['group' .. j], args['gtype' .. j], args['style' .. j], args.name, args.subgroup )
else
break
end
end
end
end
 
if args.footer then
table.insert( navbox, '|}' )
tbl = footer( tbl, args )
end
navbox = table.concat( navbox, '\n' ):gsub( ' style=""', '' )
 
return navbox
tbl = tostring( tbl )
 
local cats = ''
if not yesno(args.subgroup, false) and not yesno(args.hidecat, false) then
cats = categories()
end
local docs = docs( args )
 
return tbl .. cats .. docs
end
end
--
-- Main navbox method accessed through #invoke
--
-- @param frame {table}
-- @return {string}
--
function p.navbox( frame )
local args = frame:getParent().args
return p._navbox( args )
end
return p
return p
-- </nowiki>

Revision as of 10:48, 26 June 2025

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

local p = {}
function p.box( f )
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local navbox = {}

	if args.title then
		local class = args.class or 'collapsible'
		local bodyStyle = args.bodystyle or ''
		if bodyStyle ~= '' then
			bodyStyle = 'style="' .. bodyStyle .. '"'
		end
		table.insert( navbox, ' {| class="navbox hlist ' .. class .. '" ' .. bodyStyle )
		
		local titleStyle = args.titlestyle or ''
		if titleStyle ~= '' then
			titleStyle = 'style="' .. titleStyle .. '"'
		end
		local navbar = args[1] or ''
		if navbar ~= '' then
			local mini = ''
			if navbar:match( 'navbar%-mini' ) then
				mini = '1'
			end
			navbar = '<div class="navbox-navbar">' .. f:expandTemplate( {
				title = 'navbar',
				args = {
					args.name,
					mini = mini
				}
			} ) .. '</div>'
		end
		table.insert( navbox, '! class="navbox-top" colspan="2" ' .. titleStyle .. ' | ' .. navbar .. '<span class="navbox-title">' .. args.title .. '</span>' )
	else
		table.insert( navbox, ' {| class="navbox-child"' )
	end
	
	local groupNums = {}
	for k, v in pairs( args ) do
		if type( k ) == 'string' then
			local groupNum = k:match( 'group(%d+)' )
			if groupNum and v then
				table.insert( groupNums, tonumber( groupNum ) )
			end
		end
	end
	table.sort( groupNums )
	
	local groupStyle = args.groupstyle or ''
	local listStyle = args.liststyle or ''
	for _, v in ipairs( groupNums ) do
		local list = args['list' .. v]
		if list then
			table.insert( navbox, '|-\n! class="navbox-group" style="' .. groupStyle .. '" | ' .. args['group' .. v] )
			table.insert( navbox, '| class="navbox-list" style="' .. listStyle .. '" | ' .. list:gsub( '^([*#:{])', '\n%1' ) )
		end
	end
	
	table.insert( navbox, '|}' )
	
	navbox = table.concat( navbox, '\n' ):gsub( ' style=""', '' )
	return navbox
end
return p