Module:Page: Difference between revisions

Content deleted Content added
clean for style; fix error per talk
Add support for tables
 
(7 intermediate revisions by 3 users not shown)
Line 1:
local function callAssert(func, funcName,= ...require('Module:CallAssert')
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 ""
Line 34 ⟶ 23:
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
success, result = pcallresult( result, title, unpack(pn) )
local success
ifelseif type(titleresult) ~== '"table'" then
success, result = pcall( result, title, unpack(pn) )
result = mw.text.listToText(result)
if not success then
error(result, 0)
end
end
 
return tostring(result or "")
end
 
-- handle all errors in main
main = require('Module:Protect')(main)
 
local p = {}
Line 64 ⟶ 51:
function p.getContent(frame)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
local fmt = args.as or pargs.as or false"pre"
local text = main(frame, "getContent")
 
if not fmt then
return frame:preprocess( "<pre>" .. text .. "</pre>" )
end
 
fmt = mw.text.split( fmt, ", ?" )