Template:HTML lists and Module:Ns has subpages: Difference between pages

From Frontierpedia, the Microsoft Agent encyclopedia
(Difference between pages)
en>DePiep
No edit summary
 
m (1 revision imported)
 
Line 1: Line 1:
| above =
-- This module implements [[Template:Ns has subpages]].
* {{tl|hlist}}
-- While the template is fairly simple, this information is made available to
* {{tl|plainlist}} and {{tl|unbulleted list}}
-- Lua directly, so using a module means that we don't have to update the
* {{tl|bulleted list}}
-- template as new namespaces are added.
* {{tl|ordered list}}
* {{tl|multi-column numbered list}}
* {{tl|nowrap}}
{{navbox
|name=Template:Navbox lists
|title=HTML listings
|state=uncollapsed


|above = [[WP:HLIST]]
local p = {}


| group1=Bulleted
function p._main(ns, frame)
|  list1=
-- Get the current namespace if we were not passed one.
<code><nowiki>{{bulleted list |item1 |item2 |...}}</nowiki></code>
if not ns then
{{bulleted list |item1 |item2 |...}}
ns = mw.title.getCurrentTitle().namespace
end


| group2=Unbulleted
-- Look up the namespace table from mw.site.namespaces. This should work
|  list2=
-- for a majority of cases.
<code><nowiki>{{unbulleted list|first item|second item|third item|...}}</nowiki></code>
local nsTable = mw.site.namespaces[ns]
{{unbulleted list|first item|second item|third item|...}}


|below = See also:
-- Try using string matching to get the namespace from page names.
{{hlist
-- Do a quick and dirty bad title check to try and make sure we do the same
|{{tl|hlist}}
-- thing as {{NAMESPACE}} in most cases.
|{{tl|plainlist}} and {{tl|unbulleted list}}
if not nsTable and type(ns) == 'string' and not ns:find('[<>|%[%]{}]') then
|tl|bulleted list}}
local nsStripped = ns:gsub('^[_%s]*:', '')
|{{tl|ordered list}}
nsStripped = nsStripped:gsub(':.*$', '')
|{{tl|multi-column numbered list}}
nsTable = mw.site.namespaces[nsStripped]
|{{tl|nowrap}}
end
|{{tl|pagelist}}


}}
-- If we still have no match then try the {{NAMESPACE}} parser function,
-- which should catch the remainder of cases. Don't use a mw.title object,
-- as this would increment the expensive function count for each new page
-- tested.
if not nsTable then
frame = frame or mw.getCurrentFrame()
local nsProcessed = frame:callParserFunction('NAMESPACE', ns)
nsTable = nsProcessed and mw.site.namespaces[nsProcessed]
end
return nsTable and nsTable.hasSubpages
end


}}<!--
function p.main(frame)
local ns = frame:getParent().args[1]
if ns then
ns = ns:match('^%s*(.-)%s*$') -- trim whitespace
ns = tonumber(ns) or ns
end
local hasSubpages = p._main(ns, frame)
return hasSubpages and 'yes' or ''
end


--><noinclude>{{documentation}}</noinclude>
return p

Latest revision as of 00:28, 18 October 2022

Documentation for this module may be created at Module:Ns has subpages/doc

-- This module implements [[Template:Ns has subpages]].
-- While the template is fairly simple, this information is made available to
-- Lua directly, so using a module means that we don't have to update the
-- template as new namespaces are added.

local p = {}

function p._main(ns, frame)
	-- Get the current namespace if we were not passed one.
	if not ns then
		ns = mw.title.getCurrentTitle().namespace
	end

	-- Look up the namespace table from mw.site.namespaces. This should work
	-- for a majority of cases.
	local nsTable = mw.site.namespaces[ns]

	-- Try using string matching to get the namespace from page names.
	-- Do a quick and dirty bad title check to try and make sure we do the same
	-- thing as {{NAMESPACE}} in most cases.
	if not nsTable and type(ns) == 'string' and not ns:find('[<>|%[%]{}]') then
		local nsStripped = ns:gsub('^[_%s]*:', '')
		nsStripped = nsStripped:gsub(':.*$', '')
		nsTable = mw.site.namespaces[nsStripped]
	end

	-- If we still have no match then try the {{NAMESPACE}} parser function,
	-- which should catch the remainder of cases. Don't use a mw.title object,
	-- as this would increment the expensive function count for each new page
	-- tested.
	if not nsTable then
		frame = frame or mw.getCurrentFrame()
		local nsProcessed = frame:callParserFunction('NAMESPACE', ns)
		nsTable = nsProcessed and mw.site.namespaces[nsProcessed]
	end
	
	return nsTable and nsTable.hasSubpages
end

function p.main(frame)
	local ns = frame:getParent().args[1]
	if ns then
		ns = ns:match('^%s*(.-)%s*$') -- trim whitespace
		ns = tonumber(ns) or ns
	end
	local hasSubpages = p._main(ns, frame)
	return hasSubpages and 'yes' or ''
end

return p