Changes
start conversion to a p.getEnv function rather than using globals; I've only got as far as the start box, so the module currently won't run
local p = {}
-- Often-used functions.
----------------------------------------------------------------------------
-- Main functionsfunction
----------------------------------------------------------------------------
function p._main(args)
-- Get environment data, using pcall in case we get any errors.
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()
root
.wikitext(p.protectionTemplate(env)) .wikitext(p.sandboxNotice(args, env))
-- This div tag is from {{documentation/start box}}, but moving it here
-- so that we don't have to worry about unclosed tags.
.addClass(message('mainDivClasses', 'string'))
.newline()
.wikitext(p._startBox(args, env)) .wikitext(p._content(args, env))
.tag('div')
.css('clear', 'both') -- So right or left floating items don't stick out of the doc box.
.done()
.done()
.wikitext(p._endBox(args, env))
.newline()
.wikitext(p.addTrackingCategories(env))
return tostring(root)
end
------------------------------------------------------------------------------ Environment settings---------------------------------------------------------------------------- function p.getEnv(args) -- 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 -- limit, or other perils unknown. -- -- Data includes: -- env.title - the title object of the page we are making documentation for (usually the current title) -- env.subjectSpace - the number of the title's subject namespace. -- 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. local env = {} -- Get the title. local title local titleArg = args[message('titleArg', 'string')] if titleArg then title = mw.title.new(titleArg) if not title then error(message('titleArgError', 'string', {titleArg})) end else title = mw.title.getCurrentTitle() end env.title = title -- Get the subject namespace number. local subjectSpace = mw.site.namespaces[title.namespace].subject.id env.subjectSpace = subjectSpace -- Get the name of the documentation namespace. local docspace if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then -- Pages in the Article, File, MediaWiki or Category namespaces must have their -- /doc, /sandbox and /testcases pages in talk space. docspace = mw.site.namespaces[subjectSpace].talk.name else docspace = title.subjectNsText end env.docspace = docspace -- Get the template page with no namespace or interwiki prefixes. local templatePage local subpage = title.subpageText if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then templatePage = title.baseText else templatePage = title.text end env.templatePage = templatePage return envend ------------------------------------------------------------------------------ Auxiliary templates---------------------------------------------------------------------------- function p.sandboxNotice(args, env)
local sandboxNoticeTemplate = message('sandboxNoticeTemplate', 'string')
if not (sandboxNoticeTemplate and currentTitleenv.title.subpageText == message('sandboxSubpage', 'string')) then
return nil
end
end
function p.protectionTemplate(env) local title = env.title
local protectionTemplate = message('protectionTemplate', 'string')
if not (protectionTemplate and currentTitletitle.namespace == 10) then
-- Don't display the protection template if we are not in the template namespace.
return nil
end
local frame = mw.getCurrentFrame()
local function getProtectionLevel(protectionType, page) -- Gets the protection level for page, or for the current pageif page is not specified. local level = frame:callParserFunction('PROTECTIONLEVEL', protectionType, page)
if level ~= '' then
return level
end
end
local prefixedTitle = title.prefixedText if getProtectionLevel('move', prefixedTitle) == 'sysop' or getProtectionLevel('edit', prefixedTitle) then
-- The page is full-move protected, or full, template, or semi-protected.
return frame:expandTemplate{title = protectionTemplate, args = message('protectionTemplateArgs', 'table')}
return nil
end
----------------------------------------------------------------------------
-- Start box
----------------------------------------------------------------------------
p.startBox = makeInvokeFunc('_startBox')
function p._startBox(args, env)
-- Arg processing from {{documentation}}.
local preload = args[message('preloadArg', 'string')] -- Allow custom preloads.
local headingStyle = args[message('headingStyleArg', 'string')]
local content = args[message('contentArg', 'string')]
local docspace = penv.docspace()
local docname = args[1] -- Other docname, if fed.
local templatePage = penv.templatePage()
-- Arg processing from {{documentation/start box2}}.
return tostring(sbox)
end
----------------------------------------------------------------------------
-- Documentation content
----------------------------------------------------------------------------
p.content = makeInvokeFunc('_content')
return '\n' .. (content or '') .. '\n'
end
----------------------------------------------------------------------------
-- End box
----------------------------------------------------------------------------
p.endBox = makeInvokeFunc('_endBox')
return messageBox.main('fmbox', fmargs)
end
----------------------------------------------------------------------------
-- Tracking categories
----------------------------------------------------------------------------
function p.addTrackingCategories()
end
return ret
end
return p