Module:Page: Difference between revisions
From Neodyland Wiki
More actions
m fix |
m extending M.subpages |
||
| Line 24: | Line 24: | ||
-- subpages = require('Module:Page').subpages | -- subpages = require('Module:Page').subpages | ||
-- for page in subpages('Page') do ... end | -- for page in subpages('Page') do ... end | ||
-- Extended: redirects now reported and if namespace has subpages disabled, return zero results | |||
function _M.subpages(title) | function _M.subpages(title, redirects) | ||
local title, prefix = _M.clean(title); | local title, redirects, prefix = _M.clean(title), redirects and ' class="mw%-redirect"' or ''; | ||
local returnZeroResults = function() | |||
prefix = nil | |||
title = function() return nil end | |||
end | |||
prefix, title = pcall(mw.title.new, title, 0); | prefix, title = pcall(mw.title.new, title, 0); | ||
if prefix and title then | if prefix and title then | ||
local string, frame, expand = mw.ustring, mw:getCurrentFrame(), mw.text.unstrip; | local ns = mw.site.namespaces[title.namespace] | ||
if ns.hasSubpages then | |||
local string, frame, expand = mw.ustring, mw:getCurrentFrame(), mw.text.unstrip; | |||
local path = '{{Special:PrefixIndex/%s/}}'; | |||
prefix = string.len( title.text ) + 2; | |||
title = string.gmatch( | |||
) | expand( frame:preprocess( string.format(path, title.prefixedText ) ) ), | ||
'<a href="[^"]*" title="[^"]*"' .. redirects .. '>(.-)</a>' | |||
); | |||
else | |||
returnZeroResults() | |||
end | |||
else | else | ||
returnZeroResults() | |||
end | end | ||
return function() | return function() | ||
Revision as of 15:04, 15 May 2013
This documentation is transcluded from Module:Page/doc.
local _M = {};
function _M.clean(title)
local string = mw.ustring;
title = string.gsub( title, '^%s*%[*%[%[(.-)%]%]%]*%s*$', function(l)
return string.gsub( l, '^([^|]*)|.*$', '%1', 1 )
end, 1 )
:gsub( '[%s_]+', ' ' )
:gsub( '/+', '/' )
:gsub( '^%s', '', 1 )
:gsub( '%s$', '', 1 )
:gsub( '/$', '', 1 );
if title == '' then
return tostring( mw.title.getCurrentTitle() );
elseif string.sub( title, 1, 1) == '/' then
return table.concat{ tostring( mw.title.getCurrentTitle() ), title };
else
return title;
end
end
-- subpages = require('Module:Page').subpages
-- for page in subpages('Page') do ... end
-- Extended: redirects now reported and if namespace has subpages disabled, return zero results
function _M.subpages(title, redirects)
local title, redirects, prefix = _M.clean(title), redirects and ' class="mw%-redirect"' or '';
local returnZeroResults = function()
prefix = nil
title = function() return nil end
end
prefix, title = pcall(mw.title.new, title, 0);
if prefix and title then
local ns = mw.site.namespaces[title.namespace]
if ns.hasSubpages then
local string, frame, expand = mw.ustring, mw:getCurrentFrame(), mw.text.unstrip;
local path = '{{Special:PrefixIndex/%s/}}';
prefix = string.len( title.text ) + 2;
title = string.gmatch(
expand( frame:preprocess( string.format(path, title.prefixedText ) ) ),
'<a href="[^"]*" title="[^"]*"' .. redirects .. '>(.-)</a>'
);
else
returnZeroResults()
end
else
returnZeroResults()
end
return function()
local pg = title();
return pg and mw.ustring.sub(pg, prefix) or nil;
end
end
return _M