Module:Documentation: Difference between revisions
clean up the env table functions and the comments
m>Mr. Stradivarius (fix revision id code) |
m>Mr. Stradivarius (clean up the env table functions and the comments) |
||
Line 158: | Line 158: | ||
function p.getEnvironment(args) | function p.getEnvironment(args) | ||
-- Returns a table with information about the environment, including the title | --[[ | ||
-- | -- Returns a table with information about the environment, including title objects and other namespace- or | ||
-- | -- path-related data. | ||
-- | |||
-- Title objects include: | |||
-- env.title - the page we are making documentation for (usually the current title) | |||
-- env.templateTitle - the template (or module, file, etc.) | |||
-- env.docTitle - the /doc subpage. | |||
-- env.sandboxTitle - the /sandbox subpage. | |||
-- env.testcasesTitle - the /testcases subpage. | |||
-- env.printTitle - the print version of the template, located at the /Print subpage. | |||
-- | -- | ||
-- Data includes: | -- Data includes: | ||
-- env.subjectSpace - the number of the title's subject namespace. | -- env.subjectSpace - the number of the title's subject namespace. | ||
-- env. | -- env.docSpace - the number of the namespace the title puts its documentation in. | ||
-- env. | -- env.docpageRoot - the text of the base page of the /doc, /sandbox and /testcases pages, with namespace. | ||
-- env.compareLink - a URL link of the Special:ComparePages page comparing the sandbox with the template. | |||
-- | |||
-- All table lookups are passed through pcall so that errors are caught. If an error occurs, the value | |||
-- returned will be nil. | |||
--]] | |||
local env, envFuncs = {}, {} | local env, envFuncs = {}, {} | ||
-- Set up the metatable. If | -- Set up the metatable. If triggered we call the corresponding function in the envFuncs table. The value | ||
-- in the env table so we don't | -- returned by that function is memoized in the env table so that we don't call any of the functions | ||
-- more than once. (Nils won't be memoized.) | |||
setmetatable(env, { | setmetatable(env, { | ||
__index = function (t, key) | __index = function (t, key) | ||
Line 198: | Line 212: | ||
end | end | ||
return title | return title | ||
end | end | ||
Line 250: | Line 236: | ||
end | end | ||
return mw.title.new(docpage) | return mw.title.new(docpage) | ||
end | end | ||
Line 271: | Line 250: | ||
function envFuncs.printTitle() | function envFuncs.printTitle() | ||
-- Title object for the /Print subpage. | -- Title object for the /Print subpage. | ||
return mw.title. | return env.templateTitle:subPageTitle(message('printSubpage', 'string')) | ||
end | |||
function envFuncs.subjectSpace() | |||
-- The subject namespace number. | |||
return mw.site.namespaces[env.title.namespace].subject.id | |||
end | |||
function envFuncs.docSpace() | |||
-- The documentation namespace number. For most namespaces this is the same as the | |||
-- subject namespace. However, pages in the Article, File, MediaWiki or Category | |||
-- namespaces must have their /doc, /sandbox and /testcases pages in talk space. | |||
local subjectSpace = env.subjectSpace | |||
if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then | |||
return subjectSpace + 1 | |||
else | |||
return subjectSpace | |||
end | |||
end | |||
function envFuncs.docpageRoot() | |||
-- The base page of the /doc, /sandbox, and /testcases subpages. | |||
-- For some namespaces this is the talk page, rather than the template page. | |||
local templateTitle = env.templateTitle | |||
local docSpace = env.docSpace | |||
local docSpaceText = mw.site.namespaces[docSpace].name | |||
-- Assemble the link. docSpace is never the main namespace, so we can hardcode the colon. | |||
return docSpaceText .. ':' .. templateTitle.text | |||
end | end | ||
Line 685: | Line 691: | ||
function p.makeEndBoxExperimentBlurb(args, env) | function p.makeEndBoxExperimentBlurb(args, env) | ||
-- Renders the text "Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages." | -- Renders the text "Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages." | ||
-- Get environment data. | |||
local subjectSpace = env.subjectSpace | local subjectSpace = env.subjectSpace | ||
local templateTitle = env.templateTitle | local templateTitle = env.templateTitle | ||
local sandboxTitle = env.sandboxTitle | local sandboxTitle = env.sandboxTitle | ||
local testcasesTitle = env.testcasesTitle | local testcasesTitle = env.testcasesTitle | ||
if not templateTitle or not sandboxTitle or not testcasesTitle then | local templatePage = templateTitle.prefixedText | ||
if not subjectSpace or not templateTitle or not sandboxTitle or not testcasesTitle then | |||
return nil | return nil | ||
end | end |