Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Page: Difference between revisions

From Neodyland Wiki
help library, not invoked
 
m fix
Line 42: Line 42:
     return function()
     return function()
         local pg = title();
         local pg = title();
         return pg and sub(pg, prefix) or nil;
         return pg and mw.ustring.sub(pg, prefix) or nil;
     end
     end
end
end


return _M
return _M

Revision as of 23:22, 29 April 2013

Module documentation[ create · purge ]
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

function _M.subpages(title)
    local title, prefix = _M.clean(title);
    prefix, title = pcall(mw.title.new, title, 0);
    if prefix and title 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="[^"]*">(.-)</a>'
        );
    else
        prefix = nil
        title = function() return nil end
    end
    return function()
        local pg = title();
        return pg and mw.ustring.sub(pg, prefix) or nil;
    end
end

return _M
Cookies help us deliver our services. By using our services, you agree to our use of cookies.