Module:Documentation: Difference between revisions

m
160 revisions imported from templatewiki:Module:Documentation
No edit summary
m (160 revisions imported from templatewiki:Module:Documentation)
 
(43 intermediate revisions by 25 users not shown)
Line 1:
-- This module implements {{documentation}}.
 
-- Get required modules.
local getArgs = require('Module:Arguments').getArgs
local htmlBuilder = require('Module:HtmlBuilder')
local messageBox = require('Module:Message box')
 
-- Get the config table.
local cfg = mw.loadData('Module:Documentation/config')
 
local p = {}
 
-- Often-used functions.
local ugsub = mw.ustring.gsub
 
----------------------------------------------------------------------------
-- Helper functions
Line 20 ⟶ 19:
-- table for testing purposes.
----------------------------------------------------------------------------
 
local function message(cfgKey, valArray, expectType)
--[[
Line 39 ⟶ 38:
return msg
end
 
local function getMessageVal(match)
match = tonumber(match)
return valArray[match] or error('message: no value found for key $' .. match .. ' in message cfg.' .. cfgKey, 4)
end
 
local ret = ugsub(msg, '$([1-9][0-9]*)', getMessageVal)
return ret
end
 
p.message = message
 
local function makeWikilink(page, display)
if display then
Line 58 ⟶ 57:
end
end
 
p.makeWikilink = makeWikilink
 
local function makeCategoryLink(cat, sort)
local catns = mw.site.namespaces[14].name
return makeWikilink(catns .. ':' .. cat, sort)
end
 
p.makeCategoryLink = makeCategoryLink
 
local function makeUrlLink(url, display)
return mw.ustring.format('[%s %s]', url, display)
end
 
p.makeUrlLink = makeUrlLink
 
local function makeToolbar(...)
local ret = {}
Line 85 ⟶ 84:
return '<small style="font-style: normal;">(' .. table.concat(ret, ' &#124; ') .. ')</small>'
end
 
p.makeToolbar = makeToolbar
 
----------------------------------------------------------------------------
-- Argument processing
----------------------------------------------------------------------------
 
local function makeInvokeFunc(funcName)
return function (frame)
Line 111 ⟶ 110:
end
end
 
----------------------------------------------------------------------------
-- Main function
----------------------------------------------------------------------------
 
p.main = makeInvokeFunc('_main')
 
function p._main(args)
--[[
Line 128 ⟶ 127:
--]]
local env = p.getEnvironment(args)
local root = htmlBuildermw.html.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.
.:tag('div')
.:attr('id', message('main-div-id'))
.:addClass(message('main-div-classes'))
.: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.
.:newline()
.:done()
.:done()
.:wikitext(p._endBox(args, env))
:wikitext(p.addTrackingCategories(env))
.newline()
.wikitext(p.addTrackingCategories(env))
return tostring(root)
end
 
----------------------------------------------------------------------------
-- Environment settings
----------------------------------------------------------------------------
 
function p.getEnvironment(args)
--[[
Line 179 ⟶ 177:
-- returned will be nil.
--]]
local env, envFuncs = {}, {}
 
-- Set up the metatable. If triggered we call the corresponding function in the envFuncs table. The value
-- returned by that function is memoized in the env table so that we don't call any of the functions
Line 198 ⟶ 196:
end
})
 
function envFuncs.title()
-- The title object for the current page, or a test page passed with args.page.
Line 210 ⟶ 208:
return title
end
 
function envFuncs.templateTitle()
--[[
Line 227 ⟶ 225:
end
end
 
function envFuncs.docTitle()
--[[
Line 244 ⟶ 242:
return mw.title.new(docpage)
end
function envFuncs.sandboxTitle()
--[[
Line 253 ⟶ 251:
return mw.title.new(env.docpageBase .. '/' .. message('sandbox-subpage'))
end
function envFuncs.testcasesTitle()
--[[
Line 262 ⟶ 260:
return mw.title.new(env.docpageBase .. '/' .. message('testcases-subpage'))
end
function envFuncs.printTitle()
--[[
Line 271 ⟶ 269:
return env.templateTitle:subPageTitle(message('print-subpage'))
end
 
function envFuncs.protectionLevels()
-- The protection levels table of the title object.
return env.title.protectionLevels
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
Line 293 ⟶ 291:
end
end
 
function envFuncs.docpageBase()
-- The base page of the /doc, /sandbox, and /testcases subpages.
Line 303 ⟶ 301:
return docSpaceText .. ':' .. templateTitle.text
end
function envFuncs.compareUrl()
-- Diff link between the sandbox and the main template using [[Special:ComparePages]].
Line 318 ⟶ 316:
end
end
 
return env
end
 
----------------------------------------------------------------------------
-- Auxiliary templates
----------------------------------------------------------------------------
 
function p.sandboxNotice(args, env)
--[=[
Line 333 ⟶ 331:
--
-- Messages:
-- 'sandbox-notice-image' --> '[[Image:Sandbox.pngsvg|50px|alt=|link=]]'
-- 'sandbox-notice-blurb' --> 'This is the $1 for $2.'
-- 'sandbox-notice-diff-blurb' --> 'This is the $1 for $2 ($3).'
Line 357 ⟶ 355:
-- "This is the template sandbox for [[Template:Foo]] (diff)."
local text = ''
local frame = mw.getCurrentFrame()
local isPreviewing = frame:preprocess('{{REVISIONID}}') == '' -- True if the page is being previewed.
local pagetype
if subjectSpace == 10 then
Line 369 ⟶ 365:
local templateLink = makeWikilink(templateTitle.prefixedText)
local compareUrl = env.compareUrl
if isPreviewing or not compareUrl then
text = text .. message('sandbox-notice-blurb', {pagetype, templateLink})
else
local compareDisplay = message('sandbox-notice-compare-link-display')
local compareLink = makeUrlLink(compareUrl, compareDisplay)
text = text .. message('sandbox-notice-diff-blurb', {pagetype, templateLink, compareLink})
else
text = text .. message('sandbox-notice-blurb', {pagetype, templateLink})
end
-- Get the test cases page blurb if the page exists. This is something like
Line 380 ⟶ 376:
local testcasesTitle = env.testcasesTitle
if testcasesTitle and testcasesTitle.exists then
if testcasesTitle.namespacecontentModel == mw.site.namespaces.Module.id"Scribunto" then
local testcasesLinkDisplay = message('sandbox-notice-testcases-link-display')
local testcasesRunLinkDisplay = message('sandbox-notice-testcases-run-link-display')
Line 399 ⟶ 395:
return ret
end
 
function p.protectionTemplate(env)
-- Generates the padlock icon in the top right.
Line 406 ⟶ 402:
-- 'protection-template' --> 'pp-template'
-- 'protection-template-args' --> {docusage = 'yes'}
local protectionLevels, mProtectionBanner
local title = env.title
local protectionLevels
local protectionTemplate = message('protection-template')
local namespace = title.namespace
if not (protectionTemplate and (namespace == 10 or namespace == 828)) then
-- Don't display the protection template if we are not in the template or module namespaces.
return nil
end
protectionLevels = env.protectionLevels
if not protectionLevels then
return nil
end
local editLevelseditProt = protectionLevels.edit and protectionLevels.edit[1]
local moveLevelsmoveProt = protectionLevels.move and protectionLevels.move[1]
if editProt then
if moveLevels and moveLevels[1] == 'sysop' or editLevels and editLevels[1] then
-- The page is full-move protected, or full, template, or semiedit-protected.
mProtectionBanner = require('Module:Protection banner')
local frame = mw.getCurrentFrame()
returnlocal frame:expandTemplate{title = protectionTemplate, argsreason = message('protection-templatereason-args', nil, 'tableedit')}
return mProtectionBanner._main{reason, small = true}
elseif moveProt and moveProt ~= 'autoconfirmed' then
-- The page is move-protected but not edit-protected. Exclude move
-- protection with the level "autoconfirmed", as this is equivalent to
-- no move protection at all.
mProtectionBanner = require('Module:Protection banner')
return mProtectionBanner._main{action = 'move', small = true}
else
return nil
end
end
 
----------------------------------------------------------------------------
-- Start box
----------------------------------------------------------------------------
 
p.startBox = makeInvokeFunc('_startBox')
 
function p._startBox(args, env)
--[[
Line 448 ⟶ 445:
local links
local content = args.content
if not content or args[1] then
-- No need to include the links if the documentation is on the template page itself.
local linksData = p.makeStartBoxLinksData(args, env)
Line 464 ⟶ 461:
end
end
 
function p.makeStartBoxLinksData(args, env)
--[[
Line 487 ⟶ 484:
return nil
end
if docTitle.isRedirect then
docTitle = docTitle.redirectTarget
end
 
local data = {}
data.title = title
Line 511:
return data
end
 
function p.renderStartBoxLinks(data)
--[[
Line 517:
-- @data - a table of data generated by p.makeStartBoxLinksData
--]]
local function escapeBrackets(s)
-- Escapes square brackets with HTML entities.
Line 524:
return s
end
 
local ret
local docTitle = data.docTitle
Line 544:
return ret
end
 
function p.makeStartBoxData(args, env, links)
--[=[
Line 553:
--
-- Messages:
-- 'documentation-icon-wikitext' --> '[[File:Test Template Info-infoIcon - Version (2).pngsvg|50px|link=|alt=Documentation icon]]'
-- 'template-namespace-heading' --> 'Template documentation'
-- 'module-namespace-heading' --> 'Module documentation'
Line 569:
end
local data = {}
-- Heading
local heading = args.heading -- Blank values are not removed.
Line 587:
data.heading = message('other-namespaces-heading')
end
-- Heading CSS
local headingStyle = args['heading-style']
Line 599:
data.headingFontSize = '150%'
end
-- Data for the [view][edit][history][purge] or [create] links.
if links then
Line 606:
data.links = links
end
return data
end
 
function p.renderStartBox(data)
-- Renders the start box html.
-- @data - a table of data generated by p.makeStartBoxData.
local sbox = htmlBuildermw.html.create('div')
sbox
.:css('padding-bottom', '3px')
.:css('border-bottom', '1px solid #aaa')
.:css('margin-bottom', '1ex')
.:newline()
.:tag('span')
.:cssText(data.headingStyleText)
.:css('font-weight', data.headingFontWeight)
.:css('font-size', data.headingFontSize)
.:wikitext(data.heading)
local links = data.links
if links then
sbox.:tag('span')
.:addClass(data.linksClass)
.:attr('id', data.linksId)
.:wikitext(links)
end
return tostring(sbox)
end
 
----------------------------------------------------------------------------
-- Documentation content
----------------------------------------------------------------------------
 
p.content = makeInvokeFunc('_content')
 
function p._content(args, env)
-- Displays the documentation contents
Line 646:
env = env or p.getEnvironment(args)
local docTitle = env.docTitle
local content = args.content or args._content
if not content and docTitle and docTitle.exists then
local framecontent = args._content or mw.getCurrentFrame():expandTemplate{title = docTitle.prefixedText}
content = frame:expandTemplate{title = docTitle.prefixedText}
end
-- The line breaks below are necessary so that "=== Headings ===" at the start and end
Line 655 ⟶ 654:
return '\n' .. (content or '') .. '\n'
end
 
p.contentTitle = makeInvokeFunc('_contentTitle')
 
function p._contentTitle(args, env)
env = env or p.getEnvironment(args)
Line 667 ⟶ 666:
end
end
 
----------------------------------------------------------------------------
-- End box
----------------------------------------------------------------------------
 
p.endBox = makeInvokeFunc('_endBox')
 
function p._endBox(args, env)
--[=[
Line 687 ⟶ 686:
-- The HTML is generated by the {{fmbox}} template, courtesy of [[Module:Message box]].
--]=]
-- Get environment data.
env = env or p.getEnvironment(args)
Line 695 ⟶ 694:
return nil
end
-- Check whether we should output the end box at all. Add the end
-- box by default if the documentation exists or if we are in the
Line 710 ⟶ 709:
return nil
end
 
-- Assemble the arguments for {{fmbox}}.
local fmargs = {}
Line 717 ⟶ 716:
fmargs.style = message('fmbox-style') -- Sets 'background-color: #ecfcf4'
fmargs.textstyle = message('fmbox-textstyle') -- 'font-style: italic;'
 
-- Assemble the fmbox text field.
local text = ''
Line 728 ⟶ 727:
-- Add sandbox and testcases links.
-- "Editors can experiment in this template's sandbox and testcases pages."
text = text .. (p.makeExperimentBlurb(args, env) or '')
text = text .. '<br />'
if not args.content and not args[1] then
Line 744 ⟶ 743:
end
fmargs.text = text
 
return messageBox.main('fmbox', fmargs)
end
 
function p.makeDocPageBlurb(args, env)
--[=[
Line 793 ⟶ 792:
return ret
end
 
function p.makeExperimentBlurb(args, env)
--[[
Line 809 ⟶ 808:
-- 'mirror-edit-summary' --> 'Create sandbox version of $1'
-- 'mirror-link-display' --> 'mirror'
-- 'mirror-link-preload' --> 'Template:Documentation/mirror'
-- 'sandbox-link-display' --> 'sandbox'
-- 'testcases-link-display' --> 'testcases'
-- 'testcases-edit-link-display'--> 'edit'
-- 'module-testcases-preload' --> 'Template:Documentation/preload-module-testcases'
-- 'template-sandbox-preload' --> 'Template:Documentation/preload-sandbox'
-- 'testcases-create-link-display' --> 'create'
Line 857 ⟶ 856:
local sandboxCreateLink = makeUrlLink(sandboxCreateUrl, sandboxCreateDisplay)
local mirrorSummary = message('mirror-edit-summary', {makeWikilink(templatePage)})
local mirrorPreload = message('mirror-link-preload')
local mirrorUrl = sandboxTitle:fullUrl{action = 'edit', preload = templatePage, summary = mirrorSummary}
local mirrorUrl = sandboxTitle:fullUrl{action = 'edit', preload = mirrorPreload, summary = mirrorSummary}
if subjectSpace == 828 then
mirrorUrl = sandboxTitle:fullUrl{action = 'edit', preload = templateTitle.prefixedText, summary = mirrorSummary}
end
local mirrorDisplay = message('mirror-link-display')
local mirrorLink = makeUrlLink(mirrorUrl, mirrorDisplay)
Line 869 ⟶ 872:
local testcasesEditDisplay = message('testcases-edit-link-display')
local testcasesEditLink = makeUrlLink(testcasesEditUrl, testcasesEditDisplay)
-- for Modules, add testcases run link if exists
testcasesLinks = testcasesLink .. ' ' .. makeToolbar(testcasesEditLink)
if testcasesTitle.contentModel == "Scribunto" and testcasesTitle.talkPageTitle and testcasesTitle.talkPageTitle.exists then
local testcasesRunLinkDisplay = message('testcases-run-link-display')
local testcasesRunLink = makeWikilink(testcasesTitle.talkPageTitle.prefixedText, testcasesRunLinkDisplay)
testcasesLinks = testcasesLink .. ' ' .. makeToolbar(testcasesEditLink, testcasesRunLink)
else
testcasesLinks = testcasesLink .. ' ' .. makeToolbar(testcasesEditLink)
end
else
local testcasesPreload
Line 890 ⟶ 900:
return message(messageName, {sandboxLinks, testcasesLinks})
end
 
function p.makeCategoriesBlurb(args, env)
--[[
Line 907 ⟶ 917:
return message('add-categories-blurb', {docPathLink})
end
 
function p.makeSubpagesBlurb(args, env)
--[[
Line 913 ⟶ 923:
-- @args - a table of arguments passed by the user
-- @env - environment table containing title objects, etc., generated with p.getEnvironment
-- Messages:
-- 'template-pagetype' --> 'template'
Line 939 ⟶ 949:
return message('subpages-blurb', {subpagesLink})
end
 
function p.makePrintBlurb(args, env)
--[=[
Line 969 ⟶ 979:
return ret
end
 
----------------------------------------------------------------------------
-- Tracking categories
----------------------------------------------------------------------------
 
function p.addTrackingCategories(env)
--[[
-- Check if {{documentation}} is transcluded on a /doc or /testcases page.
-- @env - environment table containing title objects, etc., generated with p.getEnvironment
-- Messages:
-- 'display-strange-usage-category' --> true
Line 1,005 ⟶ 1,015:
return ret
end
 
return p