Jump to content

Module:Documentation: Difference between revisions

convert env table to use a metatable so we only process things when we need to; add a grab method and an err function for dealing with errors (main functions need to be converted to use these)
m>Mr. Stradivarius
(finish converting existing values to use env table)
m>Mr. Stradivarius
(convert env table to use a metatable so we only process things when we need to; add a grab method and an err function for dealing with errors (main functions need to be converted to use these))
Line 73: Line 73:
return '<small style="font-style: normal;">(' .. table.concat(ret, ' &#124; ') .. ')</small>'
return '<small style="font-style: normal;">(' .. table.concat(ret, ' &#124; ') .. ')</small>'
end
end
local function err(msg)
return string.format(
'<strong class="error">[[Module:Documentation]] error: %s</strong>%s',
msg,
makeCategoryLink('Documentation template invocations with errors')
)
end


----------------------------------------------------------------------------
----------------------------------------------------------------------------
Line 106: Line 114:


function p._main(args)
function p._main(args)
-- Get environment data, using pcall in case we get any errors.
local env = p.getEnvironment(args)
local success, env = pcall(p.getEnv, args)
if not success then
return string.format('<strong class="error">[[Module:Documentation]] error: %s</strong>', env) -- If there's an error, env is the error message.
end
-- Build the documentation.
local root = htmlBuilder.create()
local root = htmlBuilder.create()
root
root
Line 139: Line 142:
----------------------------------------------------------------------------
----------------------------------------------------------------------------


function p.getEnv(args)
function p.getEnvironment(args)
-- Returns a table with information about the environment, including the title to use, the subject namespace, etc.
-- Returns a table with information about the environment, including the title to use, the subject namespace, etc.
-- This is called from p._main using pcall in case we get any errors from exceeding the expensive function count
-- This is called from p._main using pcall in case we get any errors from exceeding the expensive function count
Line 149: Line 152:
-- env.docspace - the name of the namespace the title puts its documentation in.
-- env.docspace - the name of the namespace the title puts its documentation in.
-- env.templatePage - the name of the template page with no namespace or interwiki prefixes.
-- env.templatePage - the name of the template page with no namespace or interwiki prefixes.
local env = {}
local env, envFuncs = {}, {}
 
-- Set up the metatable. If a nil value is called, we call that function in the envFuncs table and memoize it
-- in the env table so we don't have to call any of the functions more than once.
setmetatable(env, {
__index = function (t, key)
local envFunc = envFuncs[key]
if envFunc then
local val = envFunc()
env[key] = val
return val
else
return nil
end
end
})


-- Get the title.
-- Get the title.
local title
function envFuncs.title()
local titleArg = args[message('titleArg', 'string')]
local title
if titleArg then
local titleArg = args[message('titleArg', 'string')]
title = mw.title.new(titleArg)
if titleArg then
if not title then
title = mw.title.new(titleArg)
error(message('titleArgError', 'string', {titleArg}))
if not title then
error(message('titleArgError', 'string', {titleArg}))
end
else
title = mw.title.getCurrentTitle()
end
end
else
return title
title = mw.title.getCurrentTitle()
end
end
env.title = title


-- Get the subject namespace number.
-- Get the subject namespace number.
local subjectSpace = mw.site.namespaces[title.namespace].subject.id
function envFuncs.subjectSpace()
env.subjectSpace = subjectSpace
return mw.site.namespaces[env.title.namespace].subject.id
end
-- Get the name of the documentation namespace.
-- Get the name of the documentation namespace.
local docspace
function envFuncs.docspace()
if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then
local subjectSpace = env.subjectSpace
-- Pages in the Article, File, MediaWiki or Category namespaces must have their
if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then
-- /doc, /sandbox and /testcases pages in talk space.
-- Pages in the Article, File, MediaWiki or Category namespaces must have their
docspace = mw.site.namespaces[subjectSpace].talk.name  
-- /doc, /sandbox and /testcases pages in talk space.
else
return mw.site.namespaces[subjectSpace].talk.name  
docspace = title.subjectNsText
else
return env.title.subjectNsText
end
end
end
env.docspace = docspace
-- Get the template page with no namespace or interwiki prefixes.
-- Get the template page with no namespace or interwiki prefixes.
local templatePage
function envFuncs.templatePage()
local subpage = title.subpageText
local title = env.title
if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
local subpage = title.subpageText
templatePage = title.baseText
if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
else
return title.baseText
templatePage = title.text
else
return title.text
end
end
 
function env:grab(key)
local success, val = pcall(function() return self[key] end)
return success, val
end
end
env.templatePage = templatePage


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