Module:Documentation: Difference between revisions
From Test Wiki
Content deleted Content added
per edit request on talk page - make the doc environment behave correctly, giving the actual template name on the doc page rather than the name of the "doc" subpage |
Importing from https://publictestwiki.com/wiki/Module:Documentation. |
||
| Line 11: | Line 11: | ||
-- Often-used functions. |
-- Often-used functions. |
||
local ugsub = mw.ustring.gsub |
local ugsub = mw.ustring.gsub |
||
local format = mw.ustring.format |
|||
---------------------------------------------------------------------------- |
---------------------------------------------------------------------------- |
||
| Line 51: | Line 50: | ||
local function makeWikilink(page, display) |
local function makeWikilink(page, display) |
||
if display then |
if display then |
||
return format('[[%s|%s]]', page, display) |
return mw.ustring.format('[[%s|%s]]', page, display) |
||
else |
else |
||
return format('[[%s]]', page) |
return mw.ustring.format('[[%s]]', page) |
||
end |
end |
||
end |
end |
||
| Line 67: | Line 66: | ||
local function makeUrlLink(url, display) |
local function makeUrlLink(url, display) |
||
return format('[%s %s]', url, display) |
return mw.ustring.format('[%s %s]', url, display) |
||
end |
end |
||
| Line 82: | Line 81: | ||
end |
end |
||
-- 'documentation-toolbar' |
-- 'documentation-toolbar' |
||
return '<span class="' .. message('toolbar-class') .. '">(' |
|||
return format( |
|||
.. table.concat(ret, ' | ') .. ')</span>' |
|||
message('toolbar-class'), |
|||
table.concat(ret, ' | ') |
|||
) |
|||
end |
end |
||
| Line 137: | Line 133: | ||
local root = mw.html.create() |
local root = mw.html.create() |
||
root |
root |
||
:wikitext(p._getModuleWikitext(args, env)) |
|||
:wikitext(p.protectionTemplate(env)) |
|||
:wikitext(p.sandboxNotice(args, env)) |
|||
:tag('div') |
:tag('div') |
||
-- 'documentation-container' |
-- 'documentation-container' |
||
| Line 186: | Line 179: | ||
-- |
-- |
||
-- Data includes: |
-- Data includes: |
||
-- env.protectionLevels - the protection levels table of the title object. |
|||
-- env.subjectSpace - the number of the title's subject namespace. |
-- env.subjectSpace - the number of the title's subject namespace. |
||
-- env.docSpace - the number of the namespace the title puts its documentation in. |
-- env.docSpace - the number of the namespace the title puts its documentation in. |
||
| Line 237: | Line 229: | ||
local title = env.title |
local title = env.title |
||
local subpage = title.subpageText |
local subpage = title.subpageText |
||
if subpage == message('sandbox-subpage') or subpage == message('testcases-subpage' |
if subpage == message('sandbox-subpage') or subpage == message('testcases-subpage') then |
||
return mw.title.makeTitle(subjectSpace, title.baseText) |
return mw.title.makeTitle(subjectSpace, title.baseText) |
||
else |
else |
||
| Line 279: | Line 271: | ||
end |
end |
||
function envFuncs.protectionLevels() |
|||
-- The protection levels table of the title object. |
|||
return env.title.protectionLevels |
|||
end |
|||
function envFuncs.subjectSpace() |
function envFuncs.subjectSpace() |
||
| Line 317: | Line 305: | ||
local sandboxTitle = env.sandboxTitle |
local sandboxTitle = env.sandboxTitle |
||
if templateTitle.exists and sandboxTitle.exists then |
if templateTitle.exists and sandboxTitle.exists then |
||
local compareUrl = mw.uri. |
local compareUrl = mw.uri.fullUrl( |
||
'Special:ComparePages', |
'Special:ComparePages', |
||
{ page1 = templateTitle.prefixedText, page2 = sandboxTitle.prefixedText} |
{ page1 = templateTitle.prefixedText, page2 = sandboxTitle.prefixedText} |
||
| Line 329: | Line 317: | ||
return env |
return env |
||
end |
end |
||
---------------------------------------------------------------------------- |
|||
-- Auxiliary templates |
|||
---------------------------------------------------------------------------- |
|||
p.getModuleWikitext = makeInvokeFunc('_getModuleWikitext') |
|||
function p._getModuleWikitext(args, env) |
|||
local currentTitle = mw.title.getCurrentTitle() |
|||
if currentTitle.contentModel ~= 'Scribunto' then return end |
|||
pcall(require, currentTitle.prefixedText) -- if it fails, we don't care |
|||
local moduleWikitext = package.loaded["Module:Module wikitext"] |
|||
if moduleWikitext then |
|||
return moduleWikitext.main() |
|||
end |
|||
end |
|||
function p.sandboxNotice(args, env) |
|||
--[=[ |
|||
-- Generates a sandbox notice for display above sandbox pages. |
|||
-- @args - a table of arguments passed by the user |
|||
-- @env - environment table containing title objects, etc., generated with p.getEnvironment |
|||
-- |
|||
-- Messages: |
|||
-- 'sandbox-notice-image' --> '[[File:Sandbox.svg|50px|alt=|link=]]' |
|||
-- 'sandbox-notice-blurb' --> 'This is the $1 for $2.' |
|||
-- 'sandbox-notice-diff-blurb' --> 'This is the $1 for $2 ($3).' |
|||
-- 'sandbox-notice-pagetype-template' --> '[[Wikipedia:Template test cases|template sandbox]] page' |
|||
-- 'sandbox-notice-pagetype-module' --> '[[Wikipedia:Template test cases|module sandbox]] page' |
|||
-- 'sandbox-notice-pagetype-other' --> 'sandbox page' |
|||
-- 'sandbox-notice-compare-link-display' --> 'diff' |
|||
-- 'sandbox-notice-testcases-blurb' --> 'See also the companion subpage for $1.' |
|||
-- 'sandbox-notice-testcases-link-display' --> 'test cases' |
|||
-- 'sandbox-category' --> 'Template sandboxes' |
|||
-- 'module-sandbox-category' --> 'Module sandboxes' |
|||
-- 'other-sandbox-category' --> 'Sandboxes outside of template or module namespace' |
|||
--]=] |
|||
local title = env.title |
|||
local sandboxTitle = env.sandboxTitle |
|||
local templateTitle = env.templateTitle |
|||
local subjectSpace = env.subjectSpace |
|||
if not (subjectSpace and title and sandboxTitle and templateTitle |
|||
and mw.title.equals(title, sandboxTitle)) then |
|||
return nil |
|||
end |
|||
-- Build the table of arguments to pass to {{ombox}}. We need just two fields, "image" and "text". |
|||
local omargs = {} |
|||
omargs.image = message('sandbox-notice-image') |
|||
-- Get the text. We start with the opening blurb, which is something like |
|||
-- "This is the template sandbox for [[Template:Foo]] (diff)." |
|||
local text = '__EXPECTUNUSEDTEMPLATE__' |
|||
local pagetype, sandboxCat |
|||
if subjectSpace == 10 then |
|||
pagetype = message('sandbox-notice-pagetype-template') |
|||
sandboxCat = message('sandbox-category') |
|||
elseif subjectSpace == 828 then |
|||
pagetype = message('sandbox-notice-pagetype-module') |
|||
sandboxCat = message('module-sandbox-category') |
|||
else |
|||
pagetype = message('sandbox-notice-pagetype-other') |
|||
sandboxCat = message('other-sandbox-category') |
|||
end |
|||
local templateLink = makeWikilink(templateTitle.prefixedText) |
|||
local compareUrl = env.compareUrl |
|||
if compareUrl then |
|||
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 |
|||
-- "See also the companion subpage for [[Template:Foo/testcases|test cases]]." |
|||
local testcasesTitle = env.testcasesTitle |
|||
if testcasesTitle and testcasesTitle.exists then |
|||
if testcasesTitle.contentModel == "Scribunto" then |
|||
local testcasesLinkDisplay = message('sandbox-notice-testcases-link-display') |
|||
local testcasesRunLinkDisplay = message('sandbox-notice-testcases-run-link-display') |
|||
local testcasesLink = makeWikilink(testcasesTitle.prefixedText, testcasesLinkDisplay) |
|||
local testcasesRunLink = makeWikilink(testcasesTitle.talkPageTitle.prefixedText, testcasesRunLinkDisplay) |
|||
text = text .. '<br />' .. message('sandbox-notice-testcases-run-blurb', {testcasesLink, testcasesRunLink}) |
|||
else |
|||
local testcasesLinkDisplay = message('sandbox-notice-testcases-link-display') |
|||
local testcasesLink = makeWikilink(testcasesTitle.prefixedText, testcasesLinkDisplay) |
|||
text = text .. '<br />' .. message('sandbox-notice-testcases-blurb', {testcasesLink}) |
|||
end |
|||
end |
|||
-- Add the sandbox to the sandbox category. |
|||
omargs.text = text .. makeCategoryLink(sandboxCat) |
|||
-- 'documentation-clear' |
|||
return '<div class="' .. message('clear') .. '"></div>' |
|||
.. require('Module:Message box').main('ombox', omargs) |
|||
end |
|||
function p.protectionTemplate(env) |
|||
-- Generates the padlock icon in the top right. |
|||
-- @env - environment table containing title objects, etc., generated with p.getEnvironment |
|||
-- Messages: |
|||
-- 'protection-template' --> 'pp-template' |
|||
-- 'protection-template-args' --> {docusage = 'yes'} |
|||
local protectionLevels = env.protectionLevels |
|||
if not protectionLevels then |
|||
return nil |
|||
end |
|||
local editProt = protectionLevels.edit and protectionLevels.edit[1] |
|||
local moveProt = protectionLevels.move and protectionLevels.move[1] |
|||
if editProt then |
|||
-- The page is edit-protected. |
|||
return require('Module:Protection banner')._main{ |
|||
message('protection-reason-edit'), 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. |
|||
return require('Module:Protection banner')._main{ |
|||
action = 'move', small = true |
|||
} |
|||
else |
|||
return nil |
|||
end |
|||
end |
|||
---------------------------------------------------------------------------- |
---------------------------------------------------------------------------- |
||
| Line 515: | Line 379: | ||
end |
end |
||
local data = {} |
|||
data.title = title |
|||
data.docTitle = docTitle |
|||
-- View, display, edit, and purge links if /doc exists. |
|||
data.viewLinkDisplay = message('view-link-display') |
|||
data.editLinkDisplay = message('edit-link-display') |
|||
data.historyLinkDisplay = message('history-link-display') |
|||
data.purgeLinkDisplay = message('purge-link-display') |
|||
-- Create link if /doc doesn't exist. |
-- Create link if /doc doesn't exist. |
||
local preload = args.preload |
local preload = args.preload |
||
| Line 524: | Line 396: | ||
end |
end |
||
end |
end |
||
data.preload = preload |
|||
data.createLinkDisplay = message('create-link-display') |
|||
return { |
|||
return data |
|||
title = title, |
|||
docTitle = docTitle, |
|||
-- View, display, edit, and purge links if /doc exists. |
|||
viewLinkDisplay = message('view-link-display'), |
|||
editLinkDisplay = message('edit-link-display'), |
|||
historyLinkDisplay = message('history-link-display'), |
|||
purgeLinkDisplay = message('purge-link-display'), |
|||
preload = preload, |
|||
createLinkDisplay = message('create-link-display') |
|||
} |
|||
end |
end |
||
| Line 543: | Line 406: | ||
-- @data - a table of data generated by p.makeStartBoxLinksData |
-- @data - a table of data generated by p.makeStartBoxLinksData |
||
--]] |
--]] |
||
local docTitle = data.docTitle |
|||
-- yes, we do intend to purge the template page on which the documentation appears |
|||
local purgeLink = makeWikilink("Special:Purge/" .. data.title.prefixedText, data.purgeLinkDisplay) |
|||
local function escapeBrackets(s) |
|||
-- Escapes square brackets with HTML entities. |
|||
s = s:gsub('%[', '[') -- Replace square brackets with HTML entities. |
|||
s = s:gsub('%]', ']') |
|||
return s |
|||
end |
|||
local ret |
|||
local docTitle = data.docTitle |
|||
local title = data.title |
|||
local purgeLink = makeUrlLink(title:fullUrl{action = 'purge'}, data.purgeLinkDisplay) |
|||
if docTitle.exists then |
if docTitle.exists then |
||
local viewLink = makeWikilink(docTitle.prefixedText, data.viewLinkDisplay) |
local viewLink = makeWikilink(docTitle.prefixedText, data.viewLinkDisplay) |
||
local editLink = |
local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, data.editLinkDisplay) |
||
local historyLink = |
local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, data.historyLinkDisplay) |
||
ret = '[%s] [%s] [%s] [%s]' |
|||
return "[" .. viewLink .. "] [" .. editLink .. "] [" .. historyLink .. "] [" .. purgeLink .. "]" |
|||
ret = escapeBrackets(ret) |
|||
ret = mw.ustring.format(ret, viewLink, editLink, historyLink, purgeLink) |
|||
else |
else |
||
local createLink = makeUrlLink(docTitle: |
local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay) |
||
ret = '[%s] [%s]' |
|||
return "[" .. createLink .. "] [" .. purgeLink .. "]" |
|||
ret = escapeBrackets(ret) |
|||
ret = mw.ustring.format(ret, createLink, purgeLink) |
|||
end |
end |
||
return ret |
return ret |
||
| Line 657: | Line 532: | ||
if not content and docTitle and docTitle.exists then |
if not content and docTitle and docTitle.exists then |
||
content = args._content or mw.getCurrentFrame():expandTemplate{title = docTitle.prefixedText} |
content = args._content or mw.getCurrentFrame():expandTemplate{title = docTitle.prefixedText} |
||
end |
|||
if mw.site.siteName == "Miraheze Developers Wiki" and args.noexportinstructions ~= "yes" then |
|||
-- commoncss decides whether a message about copy-pasting MediaWiki:Common.css should be printed |
|||
local export = mw.getCurrentFrame():expandTemplate{title="How to export", args = {commoncss=args.commoncss or ""}} |
|||
content = export .. "\n" .. (content or '') |
|||
end |
end |
||
-- The line breaks below are necessary so that "=== Headings ===" at the start and end |
-- The line breaks below are necessary so that "=== Headings ===" at the start and end |
||
| Line 766: | Line 646: | ||
return nil |
return nil |
||
end |
end |
||
local ret |
|||
if docTitle.exists then |
if docTitle.exists then |
||
-- /doc exists; link to it. |
-- /doc exists; link to it. |
||
local docLink = makeWikilink(docTitle.prefixedText) |
local docLink = makeWikilink(docTitle.prefixedText) |
||
local editUrl = docTitle:fullUrl{action = 'edit'} |
|||
local editDisplay = message('edit-link-display') |
local editDisplay = message('edit-link-display') |
||
local editLink = |
local editLink = makeUrlLink(editUrl, editDisplay) |
||
local historyUrl = docTitle:fullUrl{action = 'history'} |
|||
local historyDisplay = message('history-link-display') |
local historyDisplay = message('history-link-display') |
||
local historyLink = |
local historyLink = makeUrlLink(historyUrl, historyDisplay) |
||
ret = message('transcluded-from-blurb', {docLink}) |
|||
.. ' ' |
.. ' ' |
||
.. makeToolbar(editLink, historyLink) |
.. makeToolbar(editLink, historyLink) |
||
| Line 779: | Line 662: | ||
elseif env.subjectSpace == 828 then |
elseif env.subjectSpace == 828 then |
||
-- /doc does not exist; ask to create it. |
-- /doc does not exist; ask to create it. |
||
local createUrl = docTitle: |
local createUrl = docTitle:fullUrl{action = 'edit', preload = message('module-preload')} |
||
local createDisplay = message('create-link-display') |
local createDisplay = message('create-link-display') |
||
local createLink = makeUrlLink(createUrl, createDisplay) |
local createLink = makeUrlLink(createUrl, createDisplay) |
||
ret = message('create-module-doc-blurb', {createLink}) |
|||
.. '<br />' |
.. '<br />' |
||
end |
end |
||
return ret |
|||
end |
end |
||
| Line 829: | Line 713: | ||
local sandboxDisplay = message('sandbox-link-display') |
local sandboxDisplay = message('sandbox-link-display') |
||
local sandboxLink = makeWikilink(sandboxPage, sandboxDisplay) |
local sandboxLink = makeWikilink(sandboxPage, sandboxDisplay) |
||
local sandboxEditUrl = sandboxTitle:fullUrl{action = 'edit'} |
|||
local sandboxEditDisplay = message('sandbox-edit-link-display') |
local sandboxEditDisplay = message('sandbox-edit-link-display') |
||
local sandboxEditLink = |
local sandboxEditLink = makeUrlLink(sandboxEditUrl, sandboxEditDisplay) |
||
local compareUrl = env.compareUrl |
local compareUrl = env.compareUrl |
||
local compareLink |
local compareLink |
||
| Line 845: | Line 730: | ||
sandboxPreload = message('template-sandbox-preload') |
sandboxPreload = message('template-sandbox-preload') |
||
end |
end |
||
local sandboxCreateUrl = sandboxTitle: |
local sandboxCreateUrl = sandboxTitle:fullUrl{action = 'edit', preload = sandboxPreload} |
||
local sandboxCreateDisplay = message('sandbox-create-link-display') |
local sandboxCreateDisplay = message('sandbox-create-link-display') |
||
local sandboxCreateLink = makeUrlLink(sandboxCreateUrl, sandboxCreateDisplay) |
local sandboxCreateLink = makeUrlLink(sandboxCreateUrl, sandboxCreateDisplay) |
||
local mirrorSummary = message('mirror-edit-summary', {makeWikilink(templatePage)}) |
local mirrorSummary = message('mirror-edit-summary', {makeWikilink(templatePage)}) |
||
local mirrorPreload = message('mirror-link-preload') |
local mirrorPreload = message('mirror-link-preload') |
||
local mirrorUrl = sandboxTitle: |
local mirrorUrl = sandboxTitle:fullUrl{action = 'edit', preload = mirrorPreload, summary = mirrorSummary} |
||
if subjectSpace == 828 then |
if subjectSpace == 828 then |
||
mirrorUrl = sandboxTitle: |
mirrorUrl = sandboxTitle:fullUrl{action = 'edit', preload = templateTitle.prefixedText, summary = mirrorSummary} |
||
end |
end |
||
local mirrorDisplay = message('mirror-link-display') |
local mirrorDisplay = message('mirror-link-display') |
||
| Line 862: | Line 747: | ||
local testcasesDisplay = message('testcases-link-display') |
local testcasesDisplay = message('testcases-link-display') |
||
local testcasesLink = makeWikilink(testcasesPage, testcasesDisplay) |
local testcasesLink = makeWikilink(testcasesPage, testcasesDisplay) |
||
local testcasesEditUrl = testcasesTitle: |
local testcasesEditUrl = testcasesTitle:fullUrl{action = 'edit'} |
||
local testcasesEditDisplay = message('testcases-edit-link-display') |
local testcasesEditDisplay = message('testcases-edit-link-display') |
||
local testcasesEditLink = |
local testcasesEditLink = makeUrlLink(testcasesEditUrl, testcasesEditDisplay) |
||
-- for Modules, add testcases run link if exists |
-- for Modules, add testcases run link if exists |
||
if testcasesTitle.contentModel == "Scribunto" and testcasesTitle.talkPageTitle and testcasesTitle.talkPageTitle.exists then |
if testcasesTitle.contentModel == "Scribunto" and testcasesTitle.talkPageTitle and testcasesTitle.talkPageTitle.exists then |
||
| Line 880: | Line 765: | ||
testcasesPreload = message('template-testcases-preload') |
testcasesPreload = message('template-testcases-preload') |
||
end |
end |
||
local testcasesCreateUrl = testcasesTitle: |
local testcasesCreateUrl = testcasesTitle:fullUrl{action = 'edit', preload = testcasesPreload} |
||
local testcasesCreateDisplay = message('testcases-create-link-display') |
local testcasesCreateDisplay = message('testcases-create-link-display') |
||
local testcasesCreateLink = makeUrlLink(testcasesCreateUrl, testcasesCreateDisplay) |
local testcasesCreateLink = makeUrlLink(testcasesCreateUrl, testcasesCreateDisplay) |
||
| Line 967: | Line 852: | ||
end |
end |
||
local subpage = title.subpageText |
local subpage = title.subpageText |
||
local ret = '' |
|||
if message('display-strange-usage-category', nil, 'boolean') |
if message('display-strange-usage-category', nil, 'boolean') |
||
and ( |
and ( |
||
| Line 973: | Line 859: | ||
) |
) |
||
then |
then |
||
ret = ret .. makeCategoryLink(message('strange-usage-category')) |
|||
end |
end |
||
return |
return ret |
||
end |
end |
||