Module:Documentation: Difference between revisions

get rid of the env:grab function
m>Mr. Stradivarius
(use the err function for all the errors)
m>Mr. Stradivarius
(get rid of the env:grab function)
Line 175: Line 175:
local envFunc = envFuncs[key]
local envFunc = envFuncs[key]
if envFunc then
if envFunc then
local val = envFunc()
local success, val = pcall(envFunc)
env[key] = val
if success then
return val
env[key] = val -- Memoise the value.
else
return val
return nil
end
end
end
return nil
end
end
})
})
Line 231: Line 232:
local title = env.title
local title = env.title
local subpage = title.subpageText
local subpage = title.subpageText
local ret
if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
ret = title.basePageTitle
return title.basePageTitle
else
else
ret = title
return title
end
end
if not ret then
error(message('titleArgError', 'string', {titleArg}))
end
return ret
end
end


Line 265: Line 261:
function envFuncs.sandboxTitle()
function envFuncs.sandboxTitle()
-- Title object for the /sandbox subpage.
-- Title object for the /sandbox subpage.
local titleArg = env.docpageRoot .. '/' .. message('sandboxSubpage', 'string')
return mw.title.new(env.docpageRoot .. '/' .. message('sandboxSubpage', 'string'))
local title = mw.title.new(titleArg)
if not title then
error(message('titleArgError', 'string', {titleArg}))
end
return title
end
end
function envFuncs.testcasesTitle()
function envFuncs.testcasesTitle()
-- Title object for the /testcases subpage.
-- Title object for the /testcases subpage.
local titleArg = env.docpageRoot .. '/' .. message('testcasesSubpage', 'string')
return mw.title.new(env.docpageRoot .. '/' .. message('testcasesSubpage', 'string'))
local title = mw.title.new(titleArg)
if not title then
error(message('titleArgError', 'string', {titleArg}))
end
return title
end
end
function envFuncs.printTitle()
function envFuncs.printTitle()
-- Title object for the /Print subpage.
-- Title object for the /Print subpage.
local titleArg = env.templatePage .. '/' .. message('printSubpage', 'string')
return mw.title.new(env.templatePage .. '/' .. message('printSubpage', 'string'))
local title = mw.title.new(titleArg)
if not title then
error(message('titleArgError', 'string', {titleArg}))
end
return title
end
function env:grab(key)
local success, val = pcall(function() return self[key] end)
return success, val
end
end


Line 381: Line 357:
local data = {}
local data = {}
-- Get title objects.
-- Get title objects.
local titleSuccess, title = env:grab('title')
local title = env.title
if titleSuccess then
local docTitle = env.docTitle
data.title = title
if not title or not docTitle then
else
return nil
return err(title)
end
local docTitleSuccess, docTitle = env:grab('docTitle')
if docTitleSuccess then
data.docTitle = docTitle
else
return err(docTitle)
end
end
-- View, display, edit, and purge links if /doc exists.
-- View, display, edit, and purge links if /doc exists.
Line 433: Line 402:
function p.makeStartBoxData(args, env, links)
function p.makeStartBoxData(args, env, links)
local subjectSpace = env.subjectSpace
local subjectSpace = env.subjectSpace
if not subjectSpace then
return nil
end
local data = {}
local data = {}
Line 506: Line 478:
function p._content(args, env)
function p._content(args, env)
-- Get the /doc title object
-- Get the /doc title object
local success, docTitle = env:grab('docTitle')
local docTitle = env.docTitle
if not success then
if not docTitle then
return err(docTitle) -- docTitle is an error message
return nil
end
end
-- Get the documentation content.
-- Get the documentation content.
Line 532: Line 504:
-- Get environment data.
-- Get environment data.
local subjectSpace = env.subjectSpace
local subjectSpace = env.subjectSpace
local success, docTitle = env:grab('docTitle')
local docTitle = env.docTitle
if not success then
if not subjectSpace or not docTitle then
return err(docTitle) -- Error message
return nil
end
end
Line 594: Line 566:
function p.makePrintBlurb(args, env)
function p.makePrintBlurb(args, env)
-- Get the /Print title object
-- Get the /Print title object
local success, printTitle = env:grab('printTitle')
local printTitle = env.printTitle
if not success then
if not printTitle then
return err(printTitle) -- Error message
return nil
end
end
-- Make the print blurb.
-- Make the print blurb.
Line 613: Line 585:
function p.makeSubpagesBlurb(args, env)
function p.makeSubpagesBlurb(args, env)
-- Get the template title object
-- Get the template title object
local success, templateTitle = env:grab('templateTitle')
local templateTitle = env.templateTitle
if not success then
if not templateTitle then
return err(templateTitle) -- Error message.
return nil
end
end
-- Make the subpages blurb.
-- Make the subpages blurb.
Line 634: Line 606:
function p.makeCategoriesBlurb(args, env)
function p.makeCategoriesBlurb(args, env)
-- Get the title object.
-- Get the title object.
local success, docTitle = env:grab('docTitle')
local docTitle = env.docTitle
if not success then
if not docTitle then
return err(docTitle) -- Error message
return nil
end
end
-- Make the blurb.
-- Make the blurb.
Line 645: Line 617:
function p.makeDocPageBlurb(args, env)
function p.makeDocPageBlurb(args, env)
-- Get the title object.
-- Get the title object.
local success, docTitle = env:grab('docTitle')
local docTitle = env.docTitle
if not success then
if not docTitle then
return err(docTitle) -- Error message
return nil
end
end
-- Make the blurb.
-- Make the blurb.
Line 680: Line 652:
local templatePage = env.templatePage
local templatePage = env.templatePage
-- Get title objects.
-- Get title objects.
local templateSuccess, templateTitle = env:grab('templateTitle')
local templateTitle = env.templateTitle
if not templateSuccess then
local sandboxTitle = env.sandboxTitle
return err(templateTitle)
local testcasesTitle = env.testcasesTitle
end
if not templateTitle or not sandboxTitle or not testcasesTitle then
local sandboxSuccess, sandboxTitle = env:grab('sandboxTitle')
return nil
if not sandboxSuccess then
return err(sandboxTitle)
end
local testcasesSuccess, testcasesTitle = env:grab('testcasesTitle')
if not testcasesSuccess then
return err(testcasesTitle)
end
end
-- Make links.
-- Make links.
Anonymous user