Module:Navbox Filesystem

require('strict') local getArgs = require('Module:Arguments').getArgs local data = mw.loadData('Module:Navbox Filesystem/data') local frame = mw.getCurrentFrame

local function endsWith(str, suffix) return string.sub(str, -#suffix) == suffix end

local p = {}

function p._getSubpages(frame, title) local result = frame:callParserFunction('#ask', {		'Filesystem:~' .. title .. '/* Filesystem:!~' .. title .. '/*/*',		'searchlabel=',		'format=plainlist',		'link=none',		'sep=;;;'	}) if result == '' then return {} end

local splits = mw.text.split(result, ';;;') -- For silly reasons, SMW can give us dupe results. Deduplicate them here. local cleaned = {} local seen = {} for _, item in ipairs(splits) do		if string.find(item, '#') then item = string.sub(item, 1, string.find(item, '#') - 1) end if not seen[item] then seen[item] = true table.insert(cleaned, item) end end return cleaned end

function p._file(frame, title, parentTitle, isRoot) local subpageTitle = mw.title.new(title) if not subpageTitle then subpageTitle = mw.title.new('Filesystem:/') end

local subpageText = subpageTitle.subpageText if isRoot then subpageText = '/' .. subpageText end if subpageTitle.text == parentTitle then subpageText = "'''" .. subpageText .. "'''"	end

local icon for key, value in pairs(data.iconMap) do		if endsWith(subpageTitle.subpageText, key) then icon = value break end end if not icon and table.maxn(p._getSubpages(frame, subpageTitle.text)) > 0 then icon = data.iconMap['/'] end if not icon then icon = data.iconMap[''] end

local li = mw.html.create('li') :wikitext('') :wikitext( .. subpageText .. ) return tostring(li) end

function p._main(frame, args) local title = mw.title.new(args['title']) local body = mw.html.create('table') :addClass('wikitable') :tag('tr')

local splits = mw.text.split(title.text, '/') for i, item in ipairs(splits) do		local parentTitle = '/' if i < table.maxn(splits) then parentTitle = table.concat(splits, '/', 1, i + 1) end local splitTitle = table.concat(splits, '/', 1, i)		local subpages = p._getSubpages(frame, splitTitle)

local ul = body :tag('td') :attr('width', '200') :tag('ul') for _, subpage in ipairs(subpages) do			ul:wikitext(p._file(frame, subpage, parentTitle, i == 1)) end end

local div = mw.html.create('div') :attr('class', 'navbox-filesystem') :wikitext(frame:extensionTag('templatestyles', '', { src = 'Navbox Filesystem/styles.css' })) :wikitext(frame:expandTemplate {			title = 'Navbox',			args = {				title = 'Filesystem',				name = 'Navbox Filesystem',				list1 = tostring(body:done)			}		})

if mw.title.getCurrentTitle.namespace == data.nsFilesystem then div:wikitext('') end

return tostring(div) end

function p.main(frame) return p._main(frame, getArgs(frame)) end

return p