Jump to content

Module:Page: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Remove out-of-date content fork of /doc
Tags: Mobile edit Mobile web edit
clean for style; fix error per talk
Line 2: Line 2:
local result = { func(...) }
local result = { func(...) }
if not result[1] then
if not result[1] then
-- Return a string with error class detectable by #iferror.
error(mw.ustring.format('%s(%s) failed', funcName, join(', ', ...)), 0)
return string.format(
'<span class="error">Error: %s(%s) failed</span>',
funcName,
table.concat({...}, ', ')
)
end
end
return unpack(result)
return unpack(result)
end
end


function main(frame, field)
local function main(frame, field)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
local makeTitle=args.makeTitle or pargs.makeTitle or "";
local makeTitle=args.makeTitle or pargs.makeTitle or ""
local namespace=args.namespace or pargs.namespace or "";
local namespace=args.namespace or pargs.namespace or ""
local fragment=args.fragment or pargs.fragment or "";
local fragment=args.fragment or pargs.fragment or ""
local interwiki=args.interwiki or pargs.interwiki or "";
local interwiki=args.interwiki or pargs.interwiki or ""
local page=args.page or args[1] or pargs.page or pargs[1] or "";
local page=args.page or args[1] or pargs.page or pargs[1] or ""
local id= tonumber( args.id or pargs.id );
local id= tonumber( args.id or pargs.id )
local pn = {};
local pn = {}
local title -- holds the result of the mw.title.xxx call
local title -- holds the result of the mw.title.xxx call

for i = 1,9 do pn[i] = args['p'..i] or pargs['p'..i]; end
for i = 1,9 do pn[i] = args['p'..i] or pargs['p'..i] end
if not id and not mw.ustring.match( page, '%S' ) then page = nil; end
if not id and not mw.ustring.match( page, '%S' ) then page = nil end

if id then
if id then
title = callAssert(mw.title.new, 'mw.title.new', id);
title = callAssert(mw.title.new, 'mw.title.new', id)
elseif not page then
elseif not page then
title = callAssert(mw.title.getCurrentTitle, 'getCurrentTitle');
title = callAssert(mw.title.getCurrentTitle, 'getCurrentTitle')
elseif makeTitle then
elseif makeTitle then
title = callAssert(mw.title.makeTitle, 'makeTitle', namespace, page, fragment, interwiki);
title = callAssert(mw.title.makeTitle, 'makeTitle', namespace, page, fragment, interwiki)
else
else
title = callAssert(mw.title.new, 'mw.title.new', page, namespace);
title = callAssert(mw.title.new, 'mw.title.new', page, namespace)
end
end
if type(title) ~= 'table' then
return tostring(title)
local result = title[field];
end
if type(result) == "function" then

local success;
success, result = pcall( result, title, unpack(pn) );
local result = title[field]
if type(result) == "function" then
if not success then
local success
error(result, 0);
success, result = pcall( result, title, unpack(pn) )
end
if not success then
end
error(result, 0)
end
return tostring(result or "");
end

return tostring(result or "")
end
end


local p = {};
local p = {}


-- main function does all the work
-- main function does all the work
local meta = {};
local meta = {}
function meta.__index(table, key)
function meta.__index(self, key)
return function(frame)
return function(frame)
return main(frame, key)
return main(frame, key)
Line 55: Line 63:


function p.getContent(frame)
function p.getContent(frame)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
local fmt = args.as or pargs.as or false
local fmt = args.as or pargs.as or false
local text = main(frame, "getContent")
local text = main(frame, "getContent")

if not fmt then
if not fmt then
return frame:preprocess( "<pre>" .. text .. "</pre>" )
return frame:preprocess( "<pre>" .. text .. "</pre>" )
end
end

fmt = mw.text.split( fmt, ", ?" )
fmt = mw.text.split( fmt, ", ?" )

for _, how in ipairs( fmt ) do
for _, how in ipairs( fmt ) do
if how == "pre" then
if how == "pre" then

Revision as of 02:15, 18 August 2018

local function callAssert(func, funcName, ...)
	local result = { func(...) }
	if not result[1] then
		-- Return a string with error class detectable by #iferror.
		return string.format(
			'<span class="error">Error: %s(%s) failed</span>',
			funcName,
			table.concat({...}, ', ')
		)
	end
	return unpack(result)
end

local function main(frame, field)
	local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
	local makeTitle=args.makeTitle or pargs.makeTitle or ""
	local namespace=args.namespace or pargs.namespace or ""
	local fragment=args.fragment or pargs.fragment or ""
	local interwiki=args.interwiki or pargs.interwiki or ""
	local page=args.page or args[1] or pargs.page or pargs[1] or ""
	local id= tonumber( args.id or pargs.id )
	local pn = {}
	local title -- holds the result of the mw.title.xxx call

	for i = 1,9 do pn[i] = args['p'..i] or pargs['p'..i] end
	if not id and not mw.ustring.match( page, '%S' ) then page = nil end

	if id then
		title = callAssert(mw.title.new, 'mw.title.new', id)
	elseif not page then
		title = callAssert(mw.title.getCurrentTitle, 'getCurrentTitle')
	elseif makeTitle then
		title = callAssert(mw.title.makeTitle, 'makeTitle', namespace, page, fragment, interwiki)
	else
		title = callAssert(mw.title.new, 'mw.title.new', page, namespace)
	end
	if type(title) ~= 'table' then
		return tostring(title)
	end

	local result = title[field]
	if type(result) == "function" then
		local success
		success, result = pcall( result, title, unpack(pn) )
		if not success then
			error(result, 0)
		end
	end

	return tostring(result or "")
end

local p = {}

-- main function does all the work
local meta = {}
function meta.__index(self, key)
	return function(frame)
		return main(frame, key)
	end
end
setmetatable(p, meta)

function p.getContent(frame)
	local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
	local fmt = args.as or pargs.as or false
	local text = main(frame, "getContent")

	if not fmt then
		return frame:preprocess( "<pre>" .. text .. "</pre>" )
	end

	fmt = mw.text.split( fmt, ", ?" )

	for _, how in ipairs( fmt ) do
		if how == "pre" then
			text = table.concat{ "<pre>", text, "</pre>" }
		elseif how == "expand" then
			text = frame:preprocess(text)
		elseif how == "nowiki" then
			text = mw.text.nowiki(text)
		end
	end

	return text
end

return p