Module:Navbox: Difference between revisions
Jump to navigation
Jump to search
navbox name in table tag
m (1 revision imported) |
(navbox name in table tag) Tag: Reverted |
||
Line 1: | Line 1: | ||
-- <nowiki> | |||
-- | -- | ||
-- | -- Implements {{navbox}} | ||
-- | -- | ||
local p = {} | local p = {} | ||
local tnavbar = require( 'Module:Tnavbar' ) | |||
local yesno = require( 'Module:Yesno' ) | |||
local onmain = require('Module:Mainonly').on_main() | |||
local page_title = mw.title.getCurrentTitle().fullText | |||
-- | |||
-- 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 | local tbl = mw.html.create( 'table' ) | ||
tbl | |||
:addClass( yesno( args.subgroup ) and 'navbox-subgroup' or 'navbox' ) | |||
:addClass( 'nowraplinks' ) | |||
if not yesno( args.subgroup ) and | |||
( 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. | |||
if args.state == 'collapsed' then | |||
tbl:addClass( 'mw-collapsed' ) | |||
elseif args.state == 'uncollapsed' then | |||
tbl:addClass('navbox-uncollapsed') | |||
end | end | ||
end | end | ||
if | if yesno( args.collapsible ) then | ||
tbl:addClass( 'navbox-collapsible' ) | |||
end | end | ||
if args.style then | |||
if | tbl:cssText( args.style ) | ||
end | end | ||
-- manually set collapse/expand messages | |||
-- bug causing the default database messages to be used | |||
tbl | |||
:attr( { | |||
['cellspacing'] = '0', | |||
['data-expandtext'] = 'show', | |||
['data-collapsetext'] = 'hide', | |||
['data-navbox-name'] = args.name | |||
} ) | |||
return tbl | |||
end | |||
-- | |||
-- Wrapper for [[Module:Tnavbar]] | |||
-- | |||
-- @param args {table} | |||
-- @return {string} | |||
-- | |||
local function navbar( args ) | |||
return tnavbar._collapsible( { [1] = '<span style="display:none;">RS3 </span>'..args.title, [2] = args.name } ) | |||
end | 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 | local function header( tbl, args ) | ||
local div = insertRow( tbl ) | |||
:tag( 'th' ) | |||
:attr( 'colspan', '2' ) | |||
:addClass( 'navbox-title' ) | |||
:attr( 'id' , 'navbox-title' ) | |||
:tag( 'div' ) | |||
-- @todo move this to site css so we can simplify this (hook off a class) | |||
-- to something like div:wikitext( args.name and navbar( args ) or args.title ) | |||
if args. | -- which can be appended to the above and returned straight away | ||
if args.name then | |||
: | div | ||
:css( 'padding-right', args.state == 'plain' and '6em' or '0' ) | |||
:wikitext( navbar( args ) ) | |||
else | |||
div | |||
:css( 'padding-left', args.state == 'plain' and '0' or '6em' ) | |||
:wikitext( '<span style="display:none;">RS3 </span>'..args.title ) | |||
end | end | ||
return div:allDone() | |||
end | end | ||
-- | -- | ||
-- | -- Inserts a row into the navbox | ||
-- | |||
-- @param tbl {mw.html table} | |||
-- @param gtitle {string} | |||
-- @param group {string} | |||
-- @param gtype {string} | |||
-- @param gcats {table} | |||
-- @param style {string} | |||
-- @return {mw.html table} | |||
-- | -- | ||
local function row( tbl, gtitle, group, gtype, gcats, style, _name, subgroup ) | |||
local tr = insertRow( tbl ) | |||
local td | |||
if gtitle then | |||
td = tr | |||
:addClass( 'navbox-group' ) | |||
:tag( 'td' ) | |||
: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 | |||
if | 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() | |||
local | -- 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 | |||
group = '\n' .. table.concat( spl, '\n' ) | |||
end | |||
--local group2 = group | |||
--local group3 = group2 | |||
-- analytics | |||
-- | --if _name then | ||
-- | -- local name = mw.ustring.gsub(_name,' ','_') | ||
-- | -- for v in mw.ustring.gmatch(group,'%[%[[^%]]+%]%]') do | ||
local | -- if mw.ustring.match(v,'%[%[File:.+|link=') then | ||
local | -- local link = mw.ustring.match(v,'|link=([^%]|]+)') | ||
-- 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://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 = '' | |||
if | -- black links if current page | ||
local | -- if link == page_title then | ||
-- newlink = string.format('<b>%s</b>',txt) | |||
-- else | |||
-- local _link = mw.ustring.gsub(link or '',' ','_') | |||
-- newlink = string.format('[https://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://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 | |||
end | |||
td | |||
:addClass( 'navbox-list' ) | |||
:wikitext( group ) --group3 | |||
if | if gtype and mw.ustring.lower( gtype ) == 'subgroup' then | ||
td | |||
: | :addClass( 'navbox-parent' ) | ||
:css(' | :css( { | ||
padding = '0', | |||
['border-bottom'] = '0' | |||
} ) | |||
end | end | ||
if | if style then | ||
td:cssText( style ) | |||
end | end | ||
-- add subgroup categories | |||
if | if next(gcats) and onmain then | ||
first_char = page_title:sub(1,1) | |||
title_pattern = "[" .. first_char:upper() .. first_char:lower() .. "]" .. page_title:sub(2) | |||
title_pattern = title_pattern :gsub("%(","%%(") | |||
:gsub("%)","%%)") | |||
:gsub("_"," ") | |||
:gsub(" ","[_ ]") | |||
:gsub("%-","%%-") | |||
local link_patterns = { | |||
"%[%["..title_pattern.."%]%]", "%[%["..title_pattern.."%|", "{{[Pp]link%|"..title_pattern.."}}", | |||
"{{[Pp]link%|"..title_pattern.."%|", "{{[Pp]linkp%|"..title_pattern.."}}", | |||
"{{[Pp]linkp%|"..title_pattern.."%|", "{{[Ii]linkp%|"..title_pattern.."}}", | |||
"{{[Ii]linkp%|"..title_pattern.."%|", "{{[Cc]hatl%|"..title_pattern.."}}", | |||
"{{[Cc]hatl%|"..title_pattern.."%|" | |||
} | |||
for _,v in ipairs(link_patterns) do | |||
if group:match(v) then | |||
for _,cat in ipairs(gcats) do | |||
td:wikitext('[[Category:'..cat..']]') | |||
end | |||
break | |||
end | |||
end | |||
end | end | ||
return td:allDone() | |||
end | 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 | |||
if | th:cssText( args.fstyle ) | ||
end | 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 | ||
args.footer = table.concat( spl, '\n' ) | |||
th:addClass( 'navbox-list' ) | |||
end | end | ||
th:wikitext( args.footer ) | |||
return th:allDone() | |||
end | end | ||
local function | -- | ||
-- Adds [[Category:Navigational templates]] to navbox template pages | |||
-- | |||
-- @return {string} | |||
-- | |||
local function categories() | |||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
local page = title.text | |||
local | local ns = title.nsText | ||
if ns == 'Template' then | |||
-- sort in category by pagename | |||
return '[[Category:Navigational templates|' .. page .. ']]' | |||
else | |||
return '' | |||
end | end | ||
end | end | ||
-- | -- | ||
-- | -- Adds [[Template:Navbox/doc]] to navbox template pages | ||
-- | -- | ||
local function | -- @param args {table} | ||
local | -- @return {string} | ||
-- | |||
local function docs( args ) | |||
local frame = mw.getCurrentFrame() | |||
local title = mw.title.getCurrentTitle() | |||
local base = title.baseText | |||
local ns = title.nsText | |||
if | -- 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 ) and | |||
-- not a collapsible navbox within a navbox | |||
not yesno( args.collapsible ) and | |||
-- not if the doc argument is not set to "yes" | |||
yesno( args.doc, false ) | |||
then | |||
return frame:expandTemplate{ title = 'Navbox/doc' } | |||
else | |||
return '' | |||
end | end | ||
end | end | ||
function p._navbox( | -- | ||
args = | -- Navbox method to allow it to be called by other modules | ||
-- | |||
-- @param _args {table} | |||
for k, | -- @return {string} | ||
if | -- | ||
function p._navbox( _args ) | |||
local args = {} | |||
local wkCss = '' | |||
local wkDiv = '' | |||
local j | |||
-- preserves parser function behaviour where an empty string is considered undefined | |||
-- or nil in lua's case | |||
for k, v in pairs( _args ) do | |||
if v ~= '' then | |||
args[k] = v | |||
end | end | ||
end | end | ||
local tbl = createTbl( args ) | |||
if | |||
if not yesno( args.subgroup ) then | |||
tbl = header( tbl, args ) | |||
end | end | ||
-- | -- insert up to 20 rows | ||
-- | |||
-- 20 is a limit inherited from wikipedia when we copied this over | |||
-- and we've never had a reason to extend it | |||
for i = 1, 20 do | |||
j = tostring( i ) | |||
if args['group' .. j] then | |||
local gcats = {} | |||
for p = 1, 20 do | |||
local q = tostring ( p ) | |||
if args['g' .. j .. 'cat' .. q] then | |||
table.insert(gcats, args['g' .. j .. 'cat' .. q]) | |||
else | |||
break | |||
end | |||
end | |||
tbl = row( tbl, args['gtitle' .. j], args['group' .. j], args['gtype' .. j], gcats, args['style' .. j], args.name, args.subgroup ) | |||
else | else | ||
break | |||
end | end | ||
end | end | ||
if | if args.footer then | ||
tbl = footer( tbl, args ) | |||
end | end | ||
tbl = tostring( tbl ) | |||
if not | |||
local cats = '' | |||
if not yesno(args.subgroup) and not yesno(args.hidecat) then | |||
cats = categories() | |||
end | end | ||
local docs = docs( args ) | |||
return tbl .. cats .. docs | |||
end | |||
return p._navbox(args) | -- | ||
-- 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 | end | ||
return p | return p | ||
-- </nowiki> |