17,273
edits
mNo edit summary |
(implement table with groups) |
||
Line 1: | Line 1: | ||
local Box = require( 'Module:Box' ).Box | local Box = require( 'Module:Box' ).Box | ||
function getCategoryProps( categoryName ) | |||
local categoryContent = mw.title.new( categoryName, 'Category' ):getContent() | |||
local props = {} | |||
for match in string.gmatch( categoryContent, '{{Prop|([^}]*)}}' ) do | |||
local prop, value = unpack(mw.text.split( match, '|' )) | |||
-- table if multiple values, value if one value, true if no values (i.e. flag) | |||
props[prop] = value and string.match( value, ',' ) and mw.text.split( value, ',' ) or value or true | |||
end | |||
return props | |||
end | |||
function renderHList(parent, pages) | |||
local list = parent:addClass( 'hlist' ) | |||
:tag( 'ul' ) | |||
for _, page in ipairs(pages) do | |||
list:tag( 'li' ) | |||
:wikitext( '[[' .. page.text .. '|' .. (page.sortkey or page.text) .. ']]' ) | |||
end | |||
end | |||
local Navbox = Box.new() | local Navbox = Box.new() | ||
Line 18: | Line 41: | ||
function Navbox:renderContent() | function Navbox:renderContent() | ||
local catprops = getCategoryProps( self.subject ) | |||
-- get list of pages in the category | -- get list of pages in the category | ||
local pages = mw.ext.dpl. | local pages = mw.ext.dpl.getPages{ category = self.subject, ordermethod = 'sortkey', order = 'ascending' } | ||
-- | -- organize them according to metadata | ||
local content = mw.html.create( ' | local pagetree = { default = {} } | ||
: | for _, page in ipairs( pages ) do | ||
-- parse extra | |||
for | local extra = page.extra or '' | ||
page.extra = {} | |||
for prop in mw.text.gsplit( extra, ',' ) do | |||
if prop then | |||
local propvalue = mw.text.split( prop, ':' ) | |||
page.extra[propvalue[1]] = propvalue[2] or true -- for valueless flags use true | |||
end | |||
end | |||
-- add page to group | |||
if not page.extra.group then page.extra.group = 'default' end | |||
if not pagetree[page.extra.group] then pagetree[page.extra.group] = {} end | |||
pagetree[page.extra.group][#pagetree[page.extra.group] + 1] = page | |||
end | |||
-- build the table | |||
local content = mw.html.create( 'table' ) | |||
if #pagetree.default then | |||
local defaultcell = content:tag( 'tr' ) | |||
:tag( 'td' ) | |||
renderHList( defaultcell, pagetree.default ) | |||
end | |||
if catprops.groups then | |||
-- coerce it to a table for easier processing | |||
local groups = type(catprops.groups) == 'table' and catprops.groups or { catprops.groups } | |||
for _, group in ipairs(groups) do | |||
if( pagetree[group] ) then | |||
local row = content:tag( 'tr' ) | |||
row:tag( 'th' ) | |||
:wikitext( group ) | |||
local cell = row:tag( 'td' ) | |||
renderHList( cell, pagetree[group] ) | |||
end | |||
end | |||
end | end | ||
Line 53: | Line 107: | ||
p.debugframe = { | p.debugframe = { | ||
args = { | args = { | ||
[1] = " | [1] = "The Legend of Zelda", | ||
[2] = "Enemies", | [2] = "Enemies", | ||
} | } |