Module:WP: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
mNo edit summary
mNo edit summary
Line 29: Line 29:


-- Main function returns a string value: nutshell text from the {{nutshell}} template on the given page.
-- Main function returns a string value: nutshell text from the {{nutshell}} template on the given page.
local function main(frame)
local function _main(pagename)
local pagename = frame.args[1]
if not pagename then return err("No page names given") end
if not pagename then return err("No page names given") end
Line 51: Line 51:
-- Remove wikilinks.
-- Remove wikilinks.
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[(%w-)%]%]", "%1") -- [[Page]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[([%w ]-)%]%]", "%1") -- [[Page]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww]ikipedia:%w-#%w-|(.-)%]%]", "%1") -- [[Wikipedia:Page#Section|Name]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww]ikipedia:[%w ]-#[%w ]-|(.-)%]%]", "%1") -- [[Wikipedia:Page#Section|Name]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww][Pp]:%w-#%w-|(.-)%]%]", "%1") -- [[WP:Page#Section|Name]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww][Pp]:[%w ]-#[%w ]-|(.-)%]%]", "%1") -- [[WP:Page#Section|Name]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww]ikipedia:(.-)%]%]", "%1") -- [[Wikipedia:Page]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww]ikipedia:(.-)%]%]", "%1") -- [[Wikipedia:Page]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww][Pp]:(.-)%]%]", "%1") -- [[WP:Page]]
nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww][Pp]:(.-)%]%]", "%1") -- [[WP:Page]]
return frame:preprocess(nutshell)
return nutshell
end

local function _invoke(frame)
return main(frame.args[1])
end
end


function p.main(pagename) return main(pagename) end
function p.main(pagename) return _main(pagename) end
function p.invoke(frame) return _invoke(frame) end


return p
return p

Revision as of 14:16, 5 April 2020

local p = {}

local mRedirect = require('Module:Redirect')

-- Return blank text, or an error message if requested
local function err(text)
	if errors then error(text, 2) end
	return ""
end

-- Get a redirect target (or nil if not a redirect) without using the expensive title object property .isRedirect
local function getRedirectTarget(titleObject)
	local content = titleObject:getContent()
	if not content then return nil end
	return mRedirect.getTargetFromText(content)
end

-- Get a page's content, following redirects, and processing file description pages for files.
-- Also returns the page name, or the target page name if a redirect was followed, or false if no page found
local function getContent(page, frame)
	local title = mw.title.new(page) -- Read description page (for :File:Foo rather than File:Foo)
	if not title then return false, false end

	local redir = getRedirectTarget(title)
	if redir then title = mw.title.new(redir) end

	return title:getContent(), redir or title.prefixedText
end

-- Main function returns a string value: nutshell text from the {{nutshell}} template on the given page.
local function _main(pagename)
	
	if not pagename then return err("No page names given") end
    
	local text
    text, normalisedPagename = getContent(pagename)
    
    if not normalisedPagename then
        return err("No title for page name " .. pagename)
    end
    
    if text then
        local isStub = mw.ustring.find(text, "%s*{{[^{|}]*%-[Ss]tub%s*}}")
        if isStub then text = nil end
    end
    
	if not text then return err("Cannot read a valid page: page name is " .. pagename) end
	
	local nutshell = mw.ustring.match(text, "{{[Nn]utshell|(.-)}}")
	if not nutshell then return err("Page has no nutshell text: " .. pagename) end
	
	-- Remove wikilinks.
	nutshell, _ = mw.ustring.gsub(nutshell, "%[%[([%w ]-)%]%]", "%1") -- [[Page]]
	nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww]ikipedia:[%w ]-#[%w ]-|(.-)%]%]", "%1") -- [[Wikipedia:Page#Section|Name]]
	nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww][Pp]:[%w ]-#[%w ]-|(.-)%]%]", "%1") -- [[WP:Page#Section|Name]]
	nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww]ikipedia:(.-)%]%]", "%1") -- [[Wikipedia:Page]]
	nutshell, _ = mw.ustring.gsub(nutshell, "%[%[[Ww][Pp]:(.-)%]%]", "%1") -- [[WP:Page]]
	
	return nutshell
end

local function _invoke(frame)
	return main(frame.args[1])
end

function p.main(pagename) return _main(pagename) end
function p.invoke(frame) return _invoke(frame) end

return p