Module:Documentation: Difference between revisions

use the cfg key only with formatMessage in order to generate more descriptive error messages
m>Mr. Stradivarius
(make the formatMessage error message more informative, at the risk of including long cfg messages in the error message)
m>Mr. Stradivarius
(use the cfg key only with formatMessage in order to generate more descriptive error messages)
Line 24: Line 24:
----------------------------------------------------------------------------
----------------------------------------------------------------------------


local function formatMessage(msg, valArray)
local function formatMessage(cfgKey, valArray)
--[[
--[[
-- Formats a message, usually from the cfg table.
-- Formats a message from the cfg table.
-- Values from valArray can be specified in the message by using $1 for [1], $2 for [2], etc.
-- Values from valArray can be specified in the message by using $1 for [1], $2 for [2], etc.
-- So formatMessage('Foo $2 bar $1.', {'baz', 'qux'}) will return "Foo qux bar baz."
-- For example, if the message cfg.fooMessage had the value 'Foo $2 bar $1.',
-- formatMessage('fooMessage', {'baz', 'qux'}) would return "Foo qux bar baz."
--]]
--]]
checkType('formatMessage', 1, msg, 'string')
checkType('formatMessage', 1, cfgKey, 'string')
checkType('formatMessage', 2, valArray, 'table')
checkType('formatMessage', 2, valArray, 'table')
local msg = cfg[cfgKey] or error('formatMessage: no message found for cfg key "' .. cfgKey .. '"', 2)


local function getMessageVal(match)
local function getMessageVal(match)
match = tonumber(match)
match = tonumber(match)
return valArray[match] or error('formatMessage: No value found for key $' .. match .. '. Message was "' .. msg .. '"', 4)
return valArray[match] or error('formatMessage: no value found for key $' .. match .. ' in message cfg.' .. cfgKey, 2)
end
end


Line 330: Line 332:
local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, cfg.editLinkDisplay)
local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, cfg.editLinkDisplay)
local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, cfg.historyLinkDisplay)
local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, cfg.historyLinkDisplay)
text = text .. formatMessage(cfg.transcludedFromBlurb, {docLink}) .. ' ' .. makeToolbar(editLink, historyLink) .. '<br />'
text = text .. formatMessage('transcludedFromBlurb', {docLink}) .. ' ' .. makeToolbar(editLink, historyLink) .. '<br />'
elseif subjectSpace == 828 then
elseif subjectSpace == 828 then
-- /doc does not exist; ask to create it.
-- /doc does not exist; ask to create it.
local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = cfg.modulePreload}, cfg.createLinkDisplay)
local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = cfg.modulePreload}, cfg.createLinkDisplay)
text = text .. formatMessage(cfg.createModuleDocBlurb, {createLink}) .. '<br />'
text = text .. formatMessage('createModuleDocBlurb', {createLink}) .. '<br />'
end
end
-- Add links to /sandbox and /testcases when appropriate.
-- Add links to /sandbox and /testcases when appropriate.
Line 350: Line 352:
local sandboxPreload = subjectSpace == 828 and cfg.moduleSandboxPreload or cfg.templateSandboxPreload
local sandboxPreload = subjectSpace == 828 and cfg.moduleSandboxPreload or cfg.templateSandboxPreload
local sandboxCreateLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit', preload = sandboxPreload}, cfg.sandboxCreateLinkDisplay)
local sandboxCreateLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit', preload = sandboxPreload}, cfg.sandboxCreateLinkDisplay)
local mirrorSummary = formatMessage(cfg.mirrorEditSummary, {makeWikilink(templatePage)})
local mirrorSummary = formatMessage('mirrorEditSummary', {makeWikilink(templatePage)})
local mirrorLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit', preload = templatePage, summary = mirrorSummary}, cfg.mirrorLinkDisplay)
local mirrorLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit', preload = templatePage, summary = mirrorSummary}, cfg.mirrorLinkDisplay)
sandboxLinks = cfg.sandboxLinkDisplay .. ' ' .. makeToolbar(sandboxCreateLink, mirrorLink)
sandboxLinks = cfg.sandboxLinkDisplay .. ' ' .. makeToolbar(sandboxCreateLink, mirrorLink)
Line 364: Line 366:
testcasesLinks = cfg.testcasesLinkDisplay .. ' ' .. makeToolbar(testcasesCreateLink)
testcasesLinks = cfg.testcasesLinkDisplay .. ' ' .. makeToolbar(testcasesCreateLink)
end
end
text = text .. formatMessage(cfg.experimentBlurb, {pagePossessive, sandboxLinks, testcasesLinks}) .. '<br />'
text = text .. formatMessage('experimentBlurb', {pagePossessive, sandboxLinks, testcasesLinks}) .. '<br />'
-- Show the categories text, but not if "content" fed or "docname fed" since then it is unclear where to add the categories.
-- Show the categories text, but not if "content" fed or "docname fed" since then it is unclear where to add the categories.
if not content and not docnameFed then
if not content and not docnameFed then
local docPathLink = makeWikilink(docpage, cfg.docLinkDisplay)
local docPathLink = makeWikilink(docpage, cfg.docLinkDisplay)
text = text .. formatMessage(cfg.addCategoriesBlurb, {docPathLink})
text = text .. formatMessage('addCategoriesBlurb', {docPathLink})
end
end
-- Show the "subpages" link.
-- Show the "subpages" link.
Line 380: Line 382:
pagetype = cfg.defaultPagetype
pagetype = cfg.defaultPagetype
end
end
text = text .. ' ' .. makeWikilink('Special:PrefixIndex/' .. templatePage .. '/', formatMessage(cfg.subpagesLinkDisplay, {pagetype}))
text = text .. ' ' .. makeWikilink('Special:PrefixIndex/' .. templatePage .. '/', formatMessage('subpagesLinkDisplay', {pagetype}))
end
end
-- Show the "print" link if it exists.
-- Show the "print" link if it exists.
Line 387: Line 389:
if printTitle.exists then
if printTitle.exists then
local printLink = makeWikilink(printPage, cfg.printLinkDisplay)
local printLink = makeWikilink(printPage, cfg.printLinkDisplay)
text = text .. '<br />' .. formatMessage(cfg.printBlurb, {printLink})
text = text .. '<br />' .. formatMessage('printBlurb', {printLink})
.. (cfg.displayPrintCategory and makeCategoryLink(cfg.printCategory) or '')
.. (cfg.displayPrintCategory and makeCategoryLink(cfg.printCategory) or '')
end
end
Anonymous user