Module:Documentation: Difference between revisions
create a formatMessage function for formatting cfg messages in an arbitrary order
m>Mr. Stradivarius (fix purge link destination) |
m>Mr. Stradivarius (create a formatMessage function for formatting cfg messages in an arbitrary order) |
||
Line 44: | Line 44: | ||
cfg.fileDocpagePreload = 'Template:Documentation/preload-filespace' -- A preload file for documentation page in the file namespace. | cfg.fileDocpagePreload = 'Template:Documentation/preload-filespace' -- A preload file for documentation page in the file namespace. | ||
cfg.docpagePreload = 'Template:Documentation/preload-filespace' -- A preload file for template documentation pages in all namespaces. | cfg.docpagePreload = 'Template:Documentation/preload-filespace' -- A preload file for template documentation pages in all namespaces. | ||
cfg.modulePreload = 'Template:Documentation/preload-module-doc' -- A preload file for Lua module documentation pages. | |||
-- Settings for the {{fmbox}} template. | -- Settings for the {{fmbox}} template. | ||
Line 59: | Line 60: | ||
-- The following settings configure the values displayed by the module. | -- The following settings configure the values displayed by the module. | ||
-- Text displayed in wikilinks. | |||
cfg.viewLinkDisplay = 'view' -- The text to display for "view" links. | cfg.viewLinkDisplay = 'view' -- The text to display for "view" links. | ||
cfg.editLinkDisplay = 'edit' -- The text to display for "edit" links. | cfg.editLinkDisplay = 'edit' -- The text to display for "edit" links. | ||
Line 72: | Line 74: | ||
cfg.testcasesEditLinkDisplay = 'edit' -- The text to display for test cases "edit" links. | cfg.testcasesEditLinkDisplay = 'edit' -- The text to display for test cases "edit" links. | ||
cfg.testcasesCreateLinkDisplay = 'create' -- The text to display for test cases "create" links. | cfg.testcasesCreateLinkDisplay = 'create' -- The text to display for test cases "create" links. | ||
-- Sentences used in the end box. | |||
cfg.transcludedFrom = 'The above [[Wikipedia:Template documentation|documentation]] is [[Wikipedia:Transclusion|transcluded]] from $1.' -- Notice displayed when the docs are transcluded from another page. $1 is a wikilink to that page. | |||
cfg.createModuleDoc = 'You might want to $1 a documentation page for this [[Wikipedia:Lua|Scribunto module]].' -- Notice displayed in the module namespace when the documentation subpage does not exist. $1 is a link to create the documentation page with the preload cfg.modulePreload and the display cfg.createLinkDisplay. | |||
-- Other display settings | |||
cfg.documentationIconWikitext = '[[File:Template-info.png|50px|link=|alt=Documentation icon]]' -- The wikitext for the icon shown at the top of the template. | cfg.documentationIconWikitext = '[[File:Template-info.png|50px|link=|alt=Documentation icon]]' -- The wikitext for the icon shown at the top of the template. | ||
cfg.templateNamespaceHeading = 'Template documentation' -- The heading shown in the template namespace. | cfg.templateNamespaceHeading = 'Template documentation' -- The heading shown in the template namespace. | ||
Line 86: | Line 94: | ||
local htmlBuilder = require('Module:HtmlBuilder') | local htmlBuilder = require('Module:HtmlBuilder') | ||
local messageBox = require('Module:Message box') | local messageBox = require('Module:Message box') | ||
local libraryUtil = require('libraryUtil') | |||
local p = {} | local p = {} | ||
Line 91: | Line 100: | ||
-- Constants. | -- Constants. | ||
local currentTitle = mw.title.getCurrentTitle() | local currentTitle = mw.title.getCurrentTitle() | ||
local subjectSpace = mw.site.namespaces[currentTitle.namespace].subject.id | local subjectSpace = mw.site.namespaces[currentTitle.namespace].subject.id -- The number of the current subject namespace. | ||
-- Often-used functions | |||
local gsub = mw.ustring.gsub | |||
local checkType = libraryUtil.checkType | |||
---------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ||
-- Helper functions | -- Helper functions | ||
---------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ||
local function formatMessage(msg, valArray) | |||
--[[ | |||
-- Formats a message, usually from the cfg table. | |||
-- 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." | |||
--]] | |||
checkType('formatMessage', 1, msg, 'string') | |||
checkType('formatMessage', 2, valArray, 'table') | |||
local function getMessageVal(match) | |||
match = tonumber(match) | |||
return valArray[match] or error('formatMessage: No value found for key $' .. match, 2) | |||
end | |||
local ret = gsub(msg, '$([1-9][0-9]*)', getMessageVal) | |||
return ret | |||
end | |||
local function makeWikilink(page, display) | local function makeWikilink(page, display) | ||
Line 380: | Line 411: | ||
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 .. | text = text .. formatMessage(cfg.transcludedFrom, {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 = | local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = cfg.modulePreload}, cfg.createLinkDisplay) | ||
text = text .. | text = text .. formatMessage(cfg.createModuleDoc, {createLink}) .. '<br />' | ||
end | end | ||
-- Add links to /sandbox and /testcases when appropriate. | -- Add links to /sandbox and /testcases when appropriate. |