m (1 revision imported)
 
(No difference)

Latest revision as of 16:10, 18 November 2015

Documentation for this module may be created at Module:Documentation/doc

  1. -- This module implements {{documentation}}.
  2.  
  3. -- Get required modules.
  4. local getArgs = require('Module:Arguments').getArgs
  5. local messageBox = require('Module:Message box')
  6.  
  7. -- Get the config table.
  8. local cfg = mw.loadData('Module:Documentation/config')
  9.  
  10. local p = {}
  11.  
  12. -- Often-used functions.
  13. local ugsub = mw.ustring.gsub
  14.  
  15. ----------------------------------------------------------------------------
  16. -- Helper functions
  17. --
  18. -- These are defined as local functions, but are made available in the p
  19. -- table for testing purposes.
  20. ----------------------------------------------------------------------------
  21.  
  22. local function message(cfgKey, valArray, expectType)
  23. --[[
  24. -- Gets a message from the cfg table and formats it if appropriate.
  25. -- The function raises an error if the value from the cfg table is not
  26. -- of the type expectType. The default type for expectType is 'string'.
  27. -- If the table valArray is present, strings such as $1, $2 etc. in the
  28. -- message are substituted with values from the table keys [1], [2] etc.
  29. -- For example, if the message "foo-message" had the value 'Foo $2 bar $1.',
  30. -- message('foo-message', {'baz', 'qux'}) would return "Foo qux bar baz."
  31. --]]
  32. local msg = cfg[cfgKey]
  33. expectType = expectType or 'string'
  34. if type(msg) ~= expectType then
  35. error('message: type error in message cfg.' .. cfgKey .. ' (' .. expectType .. ' expected, got ' .. type(msg) .. ')', 2)
  36. end
  37. if not valArray then
  38. return msg
  39. end
  40.  
  41. local function getMessageVal(match)
  42. match = tonumber(match)
  43. return valArray[match] or error('message: no value found for key $' .. match .. ' in message cfg.' .. cfgKey, 4)
  44. end
  45.  
  46. local ret = ugsub(msg, '$([1-9][0-9]*)', getMessageVal)
  47. return ret
  48. end
  49.  
  50. p.message = message
  51.  
  52. local function makeWikilink(page, display)
  53. if display then
  54. return mw.ustring.format('[[%s|%s]]', page, display)
  55. else
  56. return mw.ustring.format('[[%s]]', page)
  57. end
  58. end
  59.  
  60. p.makeWikilink = makeWikilink
  61.  
  62. local function makeCategoryLink(cat, sort)
  63. local catns = mw.site.namespaces[14].name
  64. return makeWikilink(catns .. ':' .. cat, sort)
  65. end
  66.  
  67. p.makeCategoryLink = makeCategoryLink
  68.  
  69. local function makeUrlLink(url, display)
  70. return mw.ustring.format('[%s %s]', url, display)
  71. end
  72.  
  73. p.makeUrlLink = makeUrlLink
  74.  
  75. local function makeToolbar(...)
  76. local ret = {}
  77. local lim = select('#', ...)
  78. if lim < 1 then
  79. return nil
  80. end
  81. for i = 1, lim do
  82. ret[#ret + 1] = select(i, ...)
  83. end
  84. return '<small style="font-style: normal;">(' .. table.concat(ret, ' &#124; ') .. ')</small>'
  85. end
  86.  
  87. p.makeToolbar = makeToolbar
  88.  
  89. ----------------------------------------------------------------------------
  90. -- Argument processing
  91. ----------------------------------------------------------------------------
  92.  
  93. local function makeInvokeFunc(funcName)
  94. return function (frame)
  95. local args = getArgs(frame, {
  96. valueFunc = function (key, value)
  97. if type(value) == 'string' then
  98. value = value:match('^%s*(.-)%s*$') -- Remove whitespace.
  99. if key == 'heading' or value ~= '' then
  100. return value
  101. else
  102. return nil
  103. end
  104. else
  105. return value
  106. end
  107. end
  108. })
  109. return p[funcName](args)
  110. end
  111. end
  112.  
  113. ----------------------------------------------------------------------------
  114. -- Main function
  115. ----------------------------------------------------------------------------
  116.  
  117. p.main = makeInvokeFunc('_main')
  118.  
  119. function p._main(args)
  120. --[[
  121. -- This function defines logic flow for the module.
  122. -- @args - table of arguments passed by the user
  123. --
  124. -- Messages:
  125. -- 'main-div-id' --> 'template-documentation'
  126. -- 'main-div-classes' --> 'template-documentation iezoomfix'
  127. --]]
  128. local env = p.getEnvironment(args)
  129. local root = mw.html.create()
  130. root
  131. :wikitext(p.protectionTemplate(env))
  132. :wikitext(p.sandboxNotice(args, env))
  133. -- This div tag is from {{documentation/start box}}, but moving it here
  134. -- so that we don't have to worry about unclosed tags.
  135. :tag('div')
  136. :attr('id', message('main-div-id'))
  137. :addClass(message('main-div-classes'))
  138. :newline()
  139. :wikitext(p._startBox(args, env))
  140. :wikitext(p._content(args, env))
  141. :tag('div')
  142. :css('clear', 'both') -- So right or left floating items don't stick out of the doc box.
  143. :newline()
  144. :done()
  145. :done()
  146. :wikitext(p._endBox(args, env))
  147. :wikitext(p.addTrackingCategories(env))
  148. return tostring(root)
  149. end
  150.  
  151. ----------------------------------------------------------------------------
  152. -- Environment settings
  153. ----------------------------------------------------------------------------
  154.  
  155. function p.getEnvironment(args)
  156. --[[
  157. -- Returns a table with information about the environment, including title objects and other namespace- or
  158. -- path-related data.
  159. -- @args - table of arguments passed by the user
  160. --
  161. -- Title objects include:
  162. -- env.title - the page we are making documentation for (usually the current title)
  163. -- env.templateTitle - the template (or module, file, etc.)
  164. -- env.docTitle - the /doc subpage.
  165. -- env.sandboxTitle - the /sandbox subpage.
  166. -- env.testcasesTitle - the /testcases subpage.
  167. -- env.printTitle - the print version of the template, located at the /Print subpage.
  168. --
  169. -- Data includes:
  170. -- env.protectionLevels - the protection levels table of the title object.
  171. -- env.subjectSpace - the number of the title's subject namespace.
  172. -- env.docSpace - the number of the namespace the title puts its documentation in.
  173. -- env.docpageBase - the text of the base page of the /doc, /sandbox and /testcases pages, with namespace.
  174. -- env.compareUrl - URL of the Special:ComparePages page comparing the sandbox with the template.
  175. --
  176. -- All table lookups are passed through pcall so that errors are caught. If an error occurs, the value
  177. -- returned will be nil.
  178. --]]
  179. local env, envFuncs = {}, {}
  180.  
  181. -- Set up the metatable. If triggered we call the corresponding function in the envFuncs table. The value
  182. -- returned by that function is memoized in the env table so that we don't call any of the functions
  183. -- more than once. (Nils won't be memoized.)
  184. setmetatable(env, {
  185. __index = function (t, key)
  186. local envFunc = envFuncs[key]
  187. if envFunc then
  188. local success, val = pcall(envFunc)
  189. if success then
  190. env[key] = val -- Memoise the value.
  191. return val
  192. end
  193. end
  194. return nil
  195. end
  196. })
  197.  
  198. function envFuncs.title()
  199. -- The title object for the current page, or a test page passed with args.page.
  200. local title
  201. local titleArg = args.page
  202. if titleArg then
  203. title = mw.title.new(titleArg)
  204. else
  205. title = mw.title.getCurrentTitle()
  206. end
  207. return title
  208. end
  209.  
  210. function envFuncs.templateTitle()
  211. --[[
  212. -- The template (or module, etc.) title object.
  213. -- Messages:
  214. -- 'sandbox-subpage' --> 'sandbox'
  215. -- 'testcases-subpage' --> 'testcases'
  216. --]]
  217. local subjectSpace = env.subjectSpace
  218. local title = env.title
  219. local subpage = title.subpageText
  220. if subpage == message('sandbox-subpage') or subpage == message('testcases-subpage') then
  221. return mw.title.makeTitle(subjectSpace, title.baseText)
  222. else
  223. return mw.title.makeTitle(subjectSpace, title.text)
  224. end
  225. end
  226.  
  227. function envFuncs.docTitle()
  228. --[[
  229. -- Title object of the /doc subpage.
  230. -- Messages:
  231. -- 'doc-subpage' --> 'doc'
  232. --]]
  233. local title = env.title
  234. local docname = args[1] -- User-specified doc page.
  235. local docpage
  236. if docname then
  237. docpage = docname
  238. else
  239. docpage = env.docpageBase .. '/' .. message('doc-subpage')
  240. end
  241. return mw.title.new(docpage)
  242. end
  243. function envFuncs.sandboxTitle()
  244. --[[
  245. -- Title object for the /sandbox subpage.
  246. -- Messages:
  247. -- 'sandbox-subpage' --> 'sandbox'
  248. --]]
  249. return mw.title.new(env.docpageBase .. '/' .. message('sandbox-subpage'))
  250. end
  251. function envFuncs.testcasesTitle()
  252. --[[
  253. -- Title object for the /testcases subpage.
  254. -- Messages:
  255. -- 'testcases-subpage' --> 'testcases'
  256. --]]
  257. return mw.title.new(env.docpageBase .. '/' .. message('testcases-subpage'))
  258. end
  259. function envFuncs.printTitle()
  260. --[[
  261. -- Title object for the /Print subpage.
  262. -- Messages:
  263. -- 'print-subpage' --> 'Print'
  264. --]]
  265. return env.templateTitle:subPageTitle(message('print-subpage'))
  266. end
  267.  
  268. function envFuncs.protectionLevels()
  269. -- The protection levels table of the title object.
  270. return env.title.protectionLevels
  271. end
  272.  
  273. function envFuncs.subjectSpace()
  274. -- The subject namespace number.
  275. return mw.site.namespaces[env.title.namespace].subject.id
  276. end
  277.  
  278. function envFuncs.docSpace()
  279. -- The documentation namespace number. For most namespaces this is the same as the
  280. -- subject namespace. However, pages in the Article, File, MediaWiki or Category
  281. -- namespaces must have their /doc, /sandbox and /testcases pages in talk space.
  282. local subjectSpace = env.subjectSpace
  283. if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then
  284. return subjectSpace + 1
  285. else
  286. return subjectSpace
  287. end
  288. end
  289.  
  290. function envFuncs.docpageBase()
  291. -- The base page of the /doc, /sandbox, and /testcases subpages.
  292. -- For some namespaces this is the talk page, rather than the template page.
  293. local templateTitle = env.templateTitle
  294. local docSpace = env.docSpace
  295. local docSpaceText = mw.site.namespaces[docSpace].name
  296. -- Assemble the link. docSpace is never the main namespace, so we can hardcode the colon.
  297. return docSpaceText .. ':' .. templateTitle.text
  298. end
  299. function envFuncs.compareUrl()
  300. -- Diff link between the sandbox and the main template using [[Special:ComparePages]].
  301. local templateTitle = env.templateTitle
  302. local sandboxTitle = env.sandboxTitle
  303. if templateTitle.exists and sandboxTitle.exists then
  304. local compareUrl = mw.uri.fullUrl(
  305. 'Special:ComparePages',
  306. {page1 = templateTitle.prefixedText, page2 = sandboxTitle.prefixedText}
  307. )
  308. return tostring(compareUrl)
  309. else
  310. return nil
  311. end
  312. end
  313.  
  314. return env
  315. end
  316.  
  317. ----------------------------------------------------------------------------
  318. -- Auxiliary templates
  319. ----------------------------------------------------------------------------
  320.  
  321. function p.sandboxNotice(args, env)
  322. --[=[
  323. -- Generates a sandbox notice for display above sandbox pages.
  324. -- @args - a table of arguments passed by the user
  325. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  326. --
  327. -- Messages:
  328. -- 'sandbox-notice-image' --> '[[Image:Sandbox.svg|50px|alt=|link=]]'
  329. -- 'sandbox-notice-blurb' --> 'This is the $1 for $2.'
  330. -- 'sandbox-notice-diff-blurb' --> 'This is the $1 for $2 ($3).'
  331. -- 'sandbox-notice-pagetype-template' --> '[[Wikipedia:Template test cases|template sandbox]] page'
  332. -- 'sandbox-notice-pagetype-module' --> '[[Wikipedia:Template test cases|module sandbox]] page'
  333. -- 'sandbox-notice-pagetype-other' --> 'sandbox page'
  334. -- 'sandbox-notice-compare-link-display' --> 'diff'
  335. -- 'sandbox-notice-testcases-blurb' --> 'See also the companion subpage for $1.'
  336. -- 'sandbox-notice-testcases-link-display' --> 'test cases'
  337. -- 'sandbox-category' --> 'Template sandboxes'
  338. --]=]
  339. local title = env.title
  340. local sandboxTitle = env.sandboxTitle
  341. local templateTitle = env.templateTitle
  342. local subjectSpace = env.subjectSpace
  343. if not (subjectSpace and title and sandboxTitle and templateTitle and mw.title.equals(title, sandboxTitle)) then
  344. return nil
  345. end
  346. -- Build the table of arguments to pass to {{ombox}}. We need just two fields, "image" and "text".
  347. local omargs = {}
  348. omargs.image = message('sandbox-notice-image')
  349. -- Get the text. We start with the opening blurb, which is something like
  350. -- "This is the template sandbox for [[Template:Foo]] (diff)."
  351. local text = ''
  352. local frame = mw.getCurrentFrame()
  353. local isPreviewing = frame:preprocess('{{REVISIONID}}') == '' -- True if the page is being previewed.
  354. local pagetype
  355. if subjectSpace == 10 then
  356. pagetype = message('sandbox-notice-pagetype-template')
  357. elseif subjectSpace == 828 then
  358. pagetype = message('sandbox-notice-pagetype-module')
  359. else
  360. pagetype = message('sandbox-notice-pagetype-other')
  361. end
  362. local templateLink = makeWikilink(templateTitle.prefixedText)
  363. local compareUrl = env.compareUrl
  364. if isPreviewing or not compareUrl then
  365. text = text .. message('sandbox-notice-blurb', {pagetype, templateLink})
  366. else
  367. local compareDisplay = message('sandbox-notice-compare-link-display')
  368. local compareLink = makeUrlLink(compareUrl, compareDisplay)
  369. text = text .. message('sandbox-notice-diff-blurb', {pagetype, templateLink, compareLink})
  370. end
  371. -- Get the test cases page blurb if the page exists. This is something like
  372. -- "See also the companion subpage for [[Template:Foo/testcases|test cases]]."
  373. local testcasesTitle = env.testcasesTitle
  374. if testcasesTitle and testcasesTitle.exists then
  375. if testcasesTitle.namespace == mw.site.namespaces.Module.id then
  376. local testcasesLinkDisplay = message('sandbox-notice-testcases-link-display')
  377. local testcasesRunLinkDisplay = message('sandbox-notice-testcases-run-link-display')
  378. local testcasesLink = makeWikilink(testcasesTitle.prefixedText, testcasesLinkDisplay)
  379. local testcasesRunLink = makeWikilink(testcasesTitle.talkPageTitle.prefixedText, testcasesRunLinkDisplay)
  380. text = text .. '<br />' .. message('sandbox-notice-testcases-run-blurb', {testcasesLink, testcasesRunLink})
  381. else
  382. local testcasesLinkDisplay = message('sandbox-notice-testcases-link-display')
  383. local testcasesLink = makeWikilink(testcasesTitle.prefixedText, testcasesLinkDisplay)
  384. text = text .. '<br />' .. message('sandbox-notice-testcases-blurb', {testcasesLink})
  385. end
  386. end
  387. -- Add the sandbox to the sandbox category.
  388. text = text .. makeCategoryLink(message('sandbox-category'))
  389. omargs.text = text
  390. local ret = '<div style="clear: both;"></div>'
  391. ret = ret .. messageBox.main('ombox', omargs)
  392. return ret
  393. end
  394.  
  395. function p.protectionTemplate(env)
  396. -- Generates the padlock icon in the top right.
  397. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  398. -- Messages:
  399. -- 'protection-template' --> 'pp-template'
  400. -- 'protection-template-args' --> {docusage = 'yes'}
  401. local protectionLevels, mProtectionBanner
  402. local title = env.title
  403. if title.namespace ~= 10 and title.namespace ~= 828 then
  404. -- Don't display the protection template if we are not in the template or module namespaces.
  405. return nil
  406. end
  407. protectionLevels = env.protectionLevels
  408. if not protectionLevels then
  409. return nil
  410. end
  411. local editProt = protectionLevels.edit and protectionLevels.edit[1]
  412. local moveProt = protectionLevels.move and protectionLevels.move[1]
  413. if editProt then
  414. -- The page is edit-protected.
  415. mProtectionBanner = require('Module:Protection banner')
  416. local reason = message('protection-reason-edit')
  417. return mProtectionBanner._main{reason, small = true}
  418. elseif moveProt and moveProt ~= 'autoconfirmed' then
  419. -- The page is move-protected but not edit-protected. Exclude move
  420. -- protection with the level "autoconfirmed", as this is equivalent to
  421. -- no move protection at all.
  422. mProtectionBanner = require('Module:Protection banner')
  423. return mProtectionBanner._main{action = 'move', small = true}
  424. else
  425. return nil
  426. end
  427. end
  428.  
  429. ----------------------------------------------------------------------------
  430. -- Start box
  431. ----------------------------------------------------------------------------
  432.  
  433. p.startBox = makeInvokeFunc('_startBox')
  434.  
  435. function p._startBox(args, env)
  436. --[[
  437. -- This function generates the start box.
  438. -- @args - a table of arguments passed by the user
  439. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  440. --
  441. -- The actual work is done by p.makeStartBoxLinksData and p.renderStartBoxLinks which make
  442. -- the [view] [edit] [history] [purge] links, and by p.makeStartBoxData and p.renderStartBox
  443. -- which generate the box HTML.
  444. --]]
  445. env = env or p.getEnvironment(args)
  446. local links
  447. local content = args.content
  448. if not content then
  449. -- No need to include the links if the documentation is on the template page itself.
  450. local linksData = p.makeStartBoxLinksData(args, env)
  451. if linksData then
  452. links = p.renderStartBoxLinks(linksData)
  453. end
  454. end
  455. -- Generate the start box html.
  456. local data = p.makeStartBoxData(args, env, links)
  457. if data then
  458. return p.renderStartBox(data)
  459. else
  460. -- User specified no heading.
  461. return nil
  462. end
  463. end
  464.  
  465. function p.makeStartBoxLinksData(args, env)
  466. --[[
  467. -- Does initial processing of data to make the [view] [edit] [history] [purge] links.
  468. -- @args - a table of arguments passed by the user
  469. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  470. --
  471. -- Messages:
  472. -- 'view-link-display' --> 'view'
  473. -- 'edit-link-display' --> 'edit'
  474. -- 'history-link-display' --> 'history'
  475. -- 'purge-link-display' --> 'purge'
  476. -- 'file-docpage-preload' --> 'Template:Documentation/preload-filespace'
  477. -- 'module-preload' --> 'Template:Documentation/preload-module-doc'
  478. -- 'docpage-preload' --> 'Template:Documentation/preload'
  479. -- 'create-link-display' --> 'create'
  480. --]]
  481. local subjectSpace = env.subjectSpace
  482. local title = env.title
  483. local docTitle = env.docTitle
  484. if not title or not docTitle then
  485. return nil
  486. end
  487.  
  488. local data = {}
  489. data.title = title
  490. data.docTitle = docTitle
  491. -- View, display, edit, and purge links if /doc exists.
  492. data.viewLinkDisplay = message('view-link-display')
  493. data.editLinkDisplay = message('edit-link-display')
  494. data.historyLinkDisplay = message('history-link-display')
  495. data.purgeLinkDisplay = message('purge-link-display')
  496. -- Create link if /doc doesn't exist.
  497. local preload = args.preload
  498. if not preload then
  499. if subjectSpace == 6 then -- File namespace
  500. preload = message('file-docpage-preload')
  501. elseif subjectSpace == 828 then -- Module namespace
  502. preload = message('module-preload')
  503. else
  504. preload = message('docpage-preload')
  505. end
  506. end
  507. data.preload = preload
  508. data.createLinkDisplay = message('create-link-display')
  509. return data
  510. end
  511.  
  512. function p.renderStartBoxLinks(data)
  513. --[[
  514. -- Generates the [view][edit][history][purge] or [create] links from the data table.
  515. -- @data - a table of data generated by p.makeStartBoxLinksData
  516. --]]
  517. local function escapeBrackets(s)
  518. -- Escapes square brackets with HTML entities.
  519. s = s:gsub('%[', '&#91;') -- Replace square brackets with HTML entities.
  520. s = s:gsub('%]', '&#93;')
  521. return s
  522. end
  523.  
  524. local ret
  525. local docTitle = data.docTitle
  526. local title = data.title
  527. if docTitle.exists then
  528. local viewLink = makeWikilink(docTitle.prefixedText, data.viewLinkDisplay)
  529. local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, data.editLinkDisplay)
  530. local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, data.historyLinkDisplay)
  531. local purgeLink = makeUrlLink(title:fullUrl{action = 'purge'}, data.purgeLinkDisplay)
  532. ret = '[%s] [%s] [%s] [%s]'
  533. ret = escapeBrackets(ret)
  534. ret = mw.ustring.format(ret, viewLink, editLink, historyLink, purgeLink)
  535. else
  536. local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay)
  537. ret = '[%s]'
  538. ret = escapeBrackets(ret)
  539. ret = mw.ustring.format(ret, createLink)
  540. end
  541. return ret
  542. end
  543.  
  544. function p.makeStartBoxData(args, env, links)
  545. --[=[
  546. -- Does initial processing of data to pass to the start-box render function, p.renderStartBox.
  547. -- @args - a table of arguments passed by the user
  548. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  549. -- @links - a string containing the [view][edit][history][purge] links - could be nil if there's an error.
  550. --
  551. -- Messages:
  552. -- 'documentation-icon-wikitext' --> '[[File:Test Template Info-Icon - Version (2).svg|50px|link=|alt=Documentation icon]]'
  553. -- 'template-namespace-heading' --> 'Template documentation'
  554. -- 'module-namespace-heading' --> 'Module documentation'
  555. -- 'file-namespace-heading' --> 'Summary'
  556. -- 'other-namespaces-heading' --> 'Documentation'
  557. -- 'start-box-linkclasses' --> 'mw-editsection-like plainlinks'
  558. -- 'start-box-link-id' --> 'doc_editlinks'
  559. -- 'testcases-create-link-display' --> 'create'
  560. --]=]
  561. local subjectSpace = env.subjectSpace
  562. if not subjectSpace then
  563. -- Default to an "other namespaces" namespace, so that we get at least some output
  564. -- if an error occurs.
  565. subjectSpace = 2
  566. end
  567. local data = {}
  568. -- Heading
  569. local heading = args.heading -- Blank values are not removed.
  570. if heading == '' then
  571. -- Don't display the start box if the heading arg is defined but blank.
  572. return nil
  573. end
  574. if heading then
  575. data.heading = heading
  576. elseif subjectSpace == 10 then -- Template namespace
  577. data.heading = message('documentation-icon-wikitext') .. ' ' .. message('template-namespace-heading')
  578. elseif subjectSpace == 828 then -- Module namespace
  579. data.heading = message('documentation-icon-wikitext') .. ' ' .. message('module-namespace-heading')
  580. elseif subjectSpace == 6 then -- File namespace
  581. data.heading = message('file-namespace-heading')
  582. else
  583. data.heading = message('other-namespaces-heading')
  584. end
  585. -- Heading CSS
  586. local headingStyle = args['heading-style']
  587. if headingStyle then
  588. data.headingStyleText = headingStyle
  589. elseif subjectSpace == 10 then
  590. -- We are in the template or template talk namespaces.
  591. data.headingFontWeight = 'bold'
  592. data.headingFontSize = '125%'
  593. else
  594. data.headingFontSize = '150%'
  595. end
  596. -- Data for the [view][edit][history][purge] or [create] links.
  597. if links then
  598. data.linksClass = message('start-box-linkclasses')
  599. data.linksId = message('start-box-link-id')
  600. data.links = links
  601. end
  602. return data
  603. end
  604.  
  605. function p.renderStartBox(data)
  606. -- Renders the start box html.
  607. -- @data - a table of data generated by p.makeStartBoxData.
  608. local sbox = mw.html.create('div')
  609. sbox
  610. :css('padding-bottom', '3px')
  611. :css('border-bottom', '1px solid #aaa')
  612. :css('margin-bottom', '1ex')
  613. :newline()
  614. :tag('span')
  615. :cssText(data.headingStyleText)
  616. :css('font-weight', data.headingFontWeight)
  617. :css('font-size', data.headingFontSize)
  618. :wikitext(data.heading)
  619. local links = data.links
  620. if links then
  621. sbox:tag('span')
  622. :addClass(data.linksClass)
  623. :attr('id', data.linksId)
  624. :wikitext(links)
  625. end
  626. return tostring(sbox)
  627. end
  628.  
  629. ----------------------------------------------------------------------------
  630. -- Documentation content
  631. ----------------------------------------------------------------------------
  632.  
  633. p.content = makeInvokeFunc('_content')
  634.  
  635. function p._content(args, env)
  636. -- Displays the documentation contents
  637. -- @args - a table of arguments passed by the user
  638. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  639. env = env or p.getEnvironment(args)
  640. local docTitle = env.docTitle
  641. local content = args.content
  642. if not content and docTitle and docTitle.exists then
  643. content = args._content or mw.getCurrentFrame():expandTemplate{title = docTitle.prefixedText}
  644. end
  645. -- The line breaks below are necessary so that "=== Headings ===" at the start and end
  646. -- of docs are interpreted correctly.
  647. return '\n' .. (content or '') .. '\n'
  648. end
  649.  
  650. p.contentTitle = makeInvokeFunc('_contentTitle')
  651.  
  652. function p._contentTitle(args, env)
  653. env = env or p.getEnvironment(args)
  654. local docTitle = env.docTitle
  655. if not args.content and docTitle and docTitle.exists then
  656. return docTitle.prefixedText
  657. else
  658. return ''
  659. end
  660. end
  661.  
  662. ----------------------------------------------------------------------------
  663. -- End box
  664. ----------------------------------------------------------------------------
  665.  
  666. p.endBox = makeInvokeFunc('_endBox')
  667.  
  668. function p._endBox(args, env)
  669. --[=[
  670. -- This function generates the end box (also known as the link box).
  671. -- @args - a table of arguments passed by the user
  672. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  673. --
  674. -- Messages:
  675. -- 'fmbox-id' --> 'documentation-meta-data'
  676. -- 'fmbox-style' --> 'background-color: #ecfcf4'
  677. -- 'fmbox-textstyle' --> 'font-style: italic'
  678. --
  679. -- The HTML is generated by the {{fmbox}} template, courtesy of [[Module:Message box]].
  680. --]=]
  681. -- Get environment data.
  682. env = env or p.getEnvironment(args)
  683. local subjectSpace = env.subjectSpace
  684. local docTitle = env.docTitle
  685. if not subjectSpace or not docTitle then
  686. return nil
  687. end
  688. -- Check whether we should output the end box at all. Add the end
  689. -- box by default if the documentation exists or if we are in the
  690. -- user, module or template namespaces.
  691. local linkBox = args['link box']
  692. if linkBox == 'off'
  693. or not (
  694. docTitle.exists
  695. or subjectSpace == 2
  696. or subjectSpace == 828
  697. or subjectSpace == 10
  698. )
  699. then
  700. return nil
  701. end
  702.  
  703. -- Assemble the arguments for {{fmbox}}.
  704. local fmargs = {}
  705. fmargs.id = message('fmbox-id') -- Sets 'documentation-meta-data'
  706. fmargs.image = 'none'
  707. fmargs.style = message('fmbox-style') -- Sets 'background-color: #ecfcf4'
  708. fmargs.textstyle = message('fmbox-textstyle') -- 'font-style: italic;'
  709.  
  710. -- Assemble the fmbox text field.
  711. local text = ''
  712. if linkBox then
  713. text = text .. linkBox
  714. else
  715. text = text .. (p.makeDocPageBlurb(args, env) or '') -- "This documentation is transcluded from [[Foo]]."
  716. if subjectSpace == 2 or subjectSpace == 10 or subjectSpace == 828 then
  717. -- We are in the user, template or module namespaces.
  718. -- Add sandbox and testcases links.
  719. -- "Editors can experiment in this template's sandbox and testcases pages."
  720. text = text .. (p.makeExperimentBlurb(args, env) or '')
  721. text = text .. '<br />'
  722. if not args.content and not args[1] then
  723. -- "Please add categories to the /doc subpage."
  724. -- Don't show this message with inline docs or with an explicitly specified doc page,
  725. -- as then it is unclear where to add the categories.
  726. text = text .. (p.makeCategoriesBlurb(args, env) or '')
  727. end
  728. text = text .. ' ' .. (p.makeSubpagesBlurb(args, env) or '') --"Subpages of this template"
  729. local printBlurb = p.makePrintBlurb(args, env) -- Two-line blurb about print versions of templates.
  730. if printBlurb then
  731. text = text .. '<br />' .. printBlurb
  732. end
  733. end
  734. end
  735. fmargs.text = text
  736.  
  737. return messageBox.main('fmbox', fmargs)
  738. end
  739.  
  740. function p.makeDocPageBlurb(args, env)
  741. --[=[
  742. -- Makes the blurb "This documentation is transcluded from [[Template:Foo]] (edit, history)".
  743. -- @args - a table of arguments passed by the user
  744. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  745. --
  746. -- Messages:
  747. -- 'edit-link-display' --> 'edit'
  748. -- 'history-link-display' --> 'history'
  749. -- 'transcluded-from-blurb' -->
  750. -- 'The above [[Wikipedia:Template documentation|documentation]]
  751. -- is [[Wikipedia:Transclusion|transcluded]] from $1.'
  752. -- 'module-preload' --> 'Template:Documentation/preload-module-doc'
  753. -- 'create-link-display' --> 'create'
  754. -- 'create-module-doc-blurb' -->
  755. -- 'You might want to $1 a documentation page for this [[Wikipedia:Lua|Scribunto module]].'
  756. --]=]
  757. local docTitle = env.docTitle
  758. if not docTitle then
  759. return nil
  760. end
  761. local ret
  762. if docTitle.exists then
  763. -- /doc exists; link to it.
  764. local docLink = makeWikilink(docTitle.prefixedText)
  765. local editUrl = docTitle:fullUrl{action = 'edit'}
  766. local editDisplay = message('edit-link-display')
  767. local editLink = makeUrlLink(editUrl, editDisplay)
  768. local historyUrl = docTitle:fullUrl{action = 'history'}
  769. local historyDisplay = message('history-link-display')
  770. local historyLink = makeUrlLink(historyUrl, historyDisplay)
  771. ret = message('transcluded-from-blurb', {docLink})
  772. .. ' '
  773. .. makeToolbar(editLink, historyLink)
  774. .. '<br />'
  775. elseif env.subjectSpace == 828 then
  776. -- /doc does not exist; ask to create it.
  777. local createUrl = docTitle:fullUrl{action = 'edit', preload = message('module-preload')}
  778. local createDisplay = message('create-link-display')
  779. local createLink = makeUrlLink(createUrl, createDisplay)
  780. ret = message('create-module-doc-blurb', {createLink})
  781. .. '<br />'
  782. end
  783. return ret
  784. end
  785.  
  786. function p.makeExperimentBlurb(args, env)
  787. --[[
  788. -- Renders the text "Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages."
  789. -- @args - a table of arguments passed by the user
  790. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  791. --
  792. -- Messages:
  793. -- 'sandbox-link-display' --> 'sandbox'
  794. -- 'sandbox-edit-link-display' --> 'edit'
  795. -- 'compare-link-display' --> 'diff'
  796. -- 'module-sandbox-preload' --> 'Template:Documentation/preload-module-sandbox'
  797. -- 'template-sandbox-preload' --> 'Template:Documentation/preload-sandbox'
  798. -- 'sandbox-create-link-display' --> 'create'
  799. -- 'mirror-edit-summary' --> 'Create sandbox version of $1'
  800. -- 'mirror-link-display' --> 'mirror'
  801. -- 'mirror-link-preload' --> 'Template:Documentation/mirror'
  802. -- 'sandbox-link-display' --> 'sandbox'
  803. -- 'testcases-link-display' --> 'testcases'
  804. -- 'testcases-edit-link-display'--> 'edit'
  805. -- 'template-sandbox-preload' --> 'Template:Documentation/preload-sandbox'
  806. -- 'testcases-create-link-display' --> 'create'
  807. -- 'testcases-link-display' --> 'testcases'
  808. -- 'testcases-edit-link-display' --> 'edit'
  809. -- 'module-testcases-preload' --> 'Template:Documentation/preload-module-testcases'
  810. -- 'template-testcases-preload' --> 'Template:Documentation/preload-testcases'
  811. -- 'experiment-blurb-module' --> 'Editors can experiment in this module's $1 and $2 pages.'
  812. -- 'experiment-blurb-template' --> 'Editors can experiment in this template's $1 and $2 pages.'
  813. --]]
  814. local subjectSpace = env.subjectSpace
  815. local templateTitle = env.templateTitle
  816. local sandboxTitle = env.sandboxTitle
  817. local testcasesTitle = env.testcasesTitle
  818. local templatePage = templateTitle.prefixedText
  819. if not subjectSpace or not templateTitle or not sandboxTitle or not testcasesTitle then
  820. return nil
  821. end
  822. -- Make links.
  823. local sandboxLinks, testcasesLinks
  824. if sandboxTitle.exists then
  825. local sandboxPage = sandboxTitle.prefixedText
  826. local sandboxDisplay = message('sandbox-link-display')
  827. local sandboxLink = makeWikilink(sandboxPage, sandboxDisplay)
  828. local sandboxEditUrl = sandboxTitle:fullUrl{action = 'edit'}
  829. local sandboxEditDisplay = message('sandbox-edit-link-display')
  830. local sandboxEditLink = makeUrlLink(sandboxEditUrl, sandboxEditDisplay)
  831. local compareUrl = env.compareUrl
  832. local compareLink
  833. if compareUrl then
  834. local compareDisplay = message('compare-link-display')
  835. compareLink = makeUrlLink(compareUrl, compareDisplay)
  836. end
  837. sandboxLinks = sandboxLink .. ' ' .. makeToolbar(sandboxEditLink, compareLink)
  838. else
  839. local sandboxPreload
  840. if subjectSpace == 828 then
  841. sandboxPreload = message('module-sandbox-preload')
  842. else
  843. sandboxPreload = message('template-sandbox-preload')
  844. end
  845. local sandboxCreateUrl = sandboxTitle:fullUrl{action = 'edit', preload = sandboxPreload}
  846. local sandboxCreateDisplay = message('sandbox-create-link-display')
  847. local sandboxCreateLink = makeUrlLink(sandboxCreateUrl, sandboxCreateDisplay)
  848. local mirrorSummary = message('mirror-edit-summary', {makeWikilink(templatePage)})
  849. local mirrorPreload = message('mirror-link-preload')
  850. local mirrorUrl = sandboxTitle:fullUrl{action = 'edit', preload = mirrorPreload, summary = mirrorSummary}
  851. local mirrorDisplay = message('mirror-link-display')
  852. local mirrorLink = makeUrlLink(mirrorUrl, mirrorDisplay)
  853. sandboxLinks = message('sandbox-link-display') .. ' ' .. makeToolbar(sandboxCreateLink, mirrorLink)
  854. end
  855. if testcasesTitle.exists then
  856. local testcasesPage = testcasesTitle.prefixedText
  857. local testcasesDisplay = message('testcases-link-display')
  858. local testcasesLink = makeWikilink(testcasesPage, testcasesDisplay)
  859. local testcasesEditUrl = testcasesTitle:fullUrl{action = 'edit'}
  860. local testcasesEditDisplay = message('testcases-edit-link-display')
  861. local testcasesEditLink = makeUrlLink(testcasesEditUrl, testcasesEditDisplay)
  862. testcasesLinks = testcasesLink .. ' ' .. makeToolbar(testcasesEditLink)
  863. else
  864. local testcasesPreload
  865. if subjectSpace == 828 then
  866. testcasesPreload = message('module-testcases-preload')
  867. else
  868. testcasesPreload = message('template-testcases-preload')
  869. end
  870. local testcasesCreateUrl = testcasesTitle:fullUrl{action = 'edit', preload = testcasesPreload}
  871. local testcasesCreateDisplay = message('testcases-create-link-display')
  872. local testcasesCreateLink = makeUrlLink(testcasesCreateUrl, testcasesCreateDisplay)
  873. testcasesLinks = message('testcases-link-display') .. ' ' .. makeToolbar(testcasesCreateLink)
  874. end
  875. local messageName
  876. if subjectSpace == 828 then
  877. messageName = 'experiment-blurb-module'
  878. else
  879. messageName = 'experiment-blurb-template'
  880. end
  881. return message(messageName, {sandboxLinks, testcasesLinks})
  882. end
  883.  
  884. function p.makeCategoriesBlurb(args, env)
  885. --[[
  886. -- Generates the text "Please add categories to the /doc subpage."
  887. -- @args - a table of arguments passed by the user
  888. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  889. -- Messages:
  890. -- 'doc-link-display' --> '/doc'
  891. -- 'add-categories-blurb' --> 'Please add categories to the $1 subpage.'
  892. --]]
  893. local docTitle = env.docTitle
  894. if not docTitle then
  895. return nil
  896. end
  897. local docPathLink = makeWikilink(docTitle.prefixedText, message('doc-link-display'))
  898. return message('add-categories-blurb', {docPathLink})
  899. end
  900.  
  901. function p.makeSubpagesBlurb(args, env)
  902. --[[
  903. -- Generates the "Subpages of this template" link.
  904. -- @args - a table of arguments passed by the user
  905. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  906. -- Messages:
  907. -- 'template-pagetype' --> 'template'
  908. -- 'module-pagetype' --> 'module'
  909. -- 'default-pagetype' --> 'page'
  910. -- 'subpages-link-display' --> 'Subpages of this $1'
  911. --]]
  912. local subjectSpace = env.subjectSpace
  913. local templateTitle = env.templateTitle
  914. if not subjectSpace or not templateTitle then
  915. return nil
  916. end
  917. local pagetype
  918. if subjectSpace == 10 then
  919. pagetype = message('template-pagetype')
  920. elseif subjectSpace == 828 then
  921. pagetype = message('module-pagetype')
  922. else
  923. pagetype = message('default-pagetype')
  924. end
  925. local subpagesLink = makeWikilink(
  926. 'Special:PrefixIndex/' .. templateTitle.prefixedText .. '/',
  927. message('subpages-link-display', {pagetype})
  928. )
  929. return message('subpages-blurb', {subpagesLink})
  930. end
  931.  
  932. function p.makePrintBlurb(args, env)
  933. --[=[
  934. -- Generates the blurb displayed when there is a print version of the template available.
  935. -- @args - a table of arguments passed by the user
  936. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  937. --
  938. -- Messages:
  939. -- 'print-link-display' --> '/Print'
  940. -- 'print-blurb' --> 'A [[Help:Books/for experts#Improving the book layout|print version]]'
  941. -- .. ' of this template exists at $1.'
  942. -- .. ' If you make a change to this template, please update the print version as well.'
  943. -- 'display-print-category' --> true
  944. -- 'print-category' --> 'Templates with print versions'
  945. --]=]
  946. local printTitle = env.printTitle
  947. if not printTitle then
  948. return nil
  949. end
  950. local ret
  951. if printTitle.exists then
  952. local printLink = makeWikilink(printTitle.prefixedText, message('print-link-display'))
  953. ret = message('print-blurb', {printLink})
  954. local displayPrintCategory = message('display-print-category', nil, 'boolean')
  955. if displayPrintCategory then
  956. ret = ret .. makeCategoryLink(message('print-category'))
  957. end
  958. end
  959. return ret
  960. end
  961.  
  962. ----------------------------------------------------------------------------
  963. -- Tracking categories
  964. ----------------------------------------------------------------------------
  965.  
  966. function p.addTrackingCategories(env)
  967. --[[
  968. -- Check if {{documentation}} is transcluded on a /doc or /testcases page.
  969. -- @env - environment table containing title objects, etc., generated with p.getEnvironment
  970. -- Messages:
  971. -- 'display-strange-usage-category' --> true
  972. -- 'doc-subpage' --> 'doc'
  973. -- 'testcases-subpage' --> 'testcases'
  974. -- 'strange-usage-category' --> 'Wikipedia pages with strange ((documentation)) usage'
  975. --
  976. -- /testcases pages in the module namespace are not categorised, as they may have
  977. -- {{documentation}} transcluded automatically.
  978. --]]
  979. local title = env.title
  980. local subjectSpace = env.subjectSpace
  981. if not title or not subjectSpace then
  982. return nil
  983. end
  984. local subpage = title.subpageText
  985. local ret = ''
  986. if message('display-strange-usage-category', nil, 'boolean')
  987. and (
  988. subpage == message('doc-subpage')
  989. or subjectSpace ~= 828 and subpage == message('testcases-subpage')
  990. )
  991. then
  992. ret = ret .. makeCategoryLink(message('strange-usage-category'))
  993. end
  994. return ret
  995. end
  996.  
  997. return p
-- This module implements
Documentation[create]
.

-- Get required modules. local getArgs = require('Module:Arguments').getArgs 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 -- -- These are defined as local functions, but are made available in the p -- table for testing purposes.


local function message(cfgKey, valArray, expectType) --[[ -- Gets a message from the cfg table and formats it if appropriate. -- The function raises an error if the value from the cfg table is not -- of the type expectType. The default type for expectType is 'string'. -- If the table valArray is present, strings such as $1, $2 etc. in the -- message are substituted with values from the table keys [1], [2] etc. -- For example, if the message "foo-message" had the value 'Foo $2 bar $1.', -- message('foo-message', {'baz', 'qux'}) would return "Foo qux bar baz." --]] local msg = cfg[cfgKey] expectType = expectType or 'string' if type(msg) ~= expectType then error('message: type error in message cfg.' .. cfgKey .. ' (' .. expectType .. ' expected, got ' .. type(msg) .. ')', 2) end if not valArray then 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 return mw.ustring.format('%s', page, display) else return mw.ustring.format('%s', page) 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 = {} local lim = select('#', ...) if lim < 1 then return nil end for i = 1, lim do ret[#ret + 1] = select(i, ...) end return '(' .. table.concat(ret, ' | ') .. ')' end

p.makeToolbar = makeToolbar


-- Argument processing


local function makeInvokeFunc(funcName) return function (frame) local args = getArgs(frame, { valueFunc = function (key, value) if type(value) == 'string' then value = value:match('^%s*(.-)%s*$') -- Remove whitespace. if key == 'heading' or value ~= then return value else return nil end else return value end end }) return p[funcName](args) end end


-- Main function


p.main = makeInvokeFunc('_main')

function p._main(args) --[[ -- This function defines logic flow for the module. -- @args - table of arguments passed by the user -- -- Messages: -- 'main-div-id' --> 'template-documentation' -- 'main-div-classes' --> 'template-documentation iezoomfix' --]] local env = p.getEnvironment(args) local root = mw.html.create() root :wikitext(p.protectionTemplate(env)) :wikitext(p.sandboxNotice(args, env)) -- This div tag is from Template: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)) return tostring(root) end


-- Environment settings


function p.getEnvironment(args) --[[ -- Returns a table with information about the environment, including title objects and other namespace- or -- path-related data. -- @args - table of arguments passed by the user -- -- Title objects include: -- env.title - the page we are making documentation for (usually the current title) -- env.templateTitle - the template (or module, file, etc.) -- env.docTitle - the /doc subpage. -- env.sandboxTitle - the /sandbox subpage. -- env.testcasesTitle - the /testcases subpage. -- env.printTitle - the print version of the template, located at the /Print subpage. -- -- Data includes: -- env.protectionLevels - the protection levels table of the title object. -- env.subjectSpace - the number of the title's subject namespace. -- env.docSpace - the number of the namespace the title puts its documentation in. -- env.docpageBase - the text of the base page of the /doc, /sandbox and /testcases pages, with namespace. -- env.compareUrl - URL of the Special:ComparePages page comparing the sandbox with the template. -- -- All table lookups are passed through pcall so that errors are caught. If an error occurs, the value -- 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 -- more than once. (Nils won't be memoized.) setmetatable(env, { __index = function (t, key) local envFunc = envFuncs[key] if envFunc then local success, val = pcall(envFunc) if success then env[key] = val -- Memoise the value. return val end end return nil end })

function envFuncs.title() -- The title object for the current page, or a test page passed with args.page. local title local titleArg = args.page if titleArg then title = mw.title.new(titleArg) else title = mw.title.getCurrentTitle() end return title end

function envFuncs.templateTitle() --[[ -- The template (or module, etc.) title object. -- Messages: -- 'sandbox-subpage' --> 'sandbox' -- 'testcases-subpage' --> 'testcases' --]] local subjectSpace = env.subjectSpace local title = env.title local subpage = title.subpageText if subpage == message('sandbox-subpage') or subpage == message('testcases-subpage') then return mw.title.makeTitle(subjectSpace, title.baseText) else return mw.title.makeTitle(subjectSpace, title.text) end end

function envFuncs.docTitle() --[[ -- Title object of the /doc subpage. -- Messages: -- 'doc-subpage' --> 'doc' --]] local title = env.title local docname = args[1] -- User-specified doc page. local docpage if docname then docpage = docname else docpage = env.docpageBase .. '/' .. message('doc-subpage') end return mw.title.new(docpage) end

function envFuncs.sandboxTitle() --[[ -- Title object for the /sandbox subpage. -- Messages: -- 'sandbox-subpage' --> 'sandbox' --]] return mw.title.new(env.docpageBase .. '/' .. message('sandbox-subpage')) end

function envFuncs.testcasesTitle() --[[ -- Title object for the /testcases subpage. -- Messages: -- 'testcases-subpage' --> 'testcases' --]] return mw.title.new(env.docpageBase .. '/' .. message('testcases-subpage')) end

function envFuncs.printTitle() --[[ -- Title object for the /Print subpage. -- Messages: -- 'print-subpage' --> 'Print' --]] 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 -- subject namespace. However, pages in the Article, File, MediaWiki or Category -- namespaces must have their /doc, /sandbox and /testcases pages in talk space. local subjectSpace = env.subjectSpace if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then return subjectSpace + 1 else return subjectSpace end end

function envFuncs.docpageBase() -- The base page of the /doc, /sandbox, and /testcases subpages. -- For some namespaces this is the talk page, rather than the template page. local templateTitle = env.templateTitle local docSpace = env.docSpace local docSpaceText = mw.site.namespaces[docSpace].name -- Assemble the link. docSpace is never the main namespace, so we can hardcode the colon. return docSpaceText .. ':' .. templateTitle.text end

function envFuncs.compareUrl() -- Diff link between the sandbox and the main template using Special:ComparePages. local templateTitle = env.templateTitle local sandboxTitle = env.sandboxTitle if templateTitle.exists and sandboxTitle.exists then local compareUrl = mw.uri.fullUrl( 'Special:ComparePages', {page1 = templateTitle.prefixedText, page2 = sandboxTitle.prefixedText} ) return tostring(compareUrl) else return nil end end

return env end


-- Auxiliary templates


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' --> '' -- 'sandbox-notice-blurb' --> 'This is the $1 for $2.' -- 'sandbox-notice-diff-blurb' --> 'This is the $1 for $2 ($3).' -- 'sandbox-notice-pagetype-template' --> 'template sandbox page' -- 'sandbox-notice-pagetype-module' --> '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' --]=] 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 . 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 = local frame = mw.getCurrentFrame() local isPreviewing = frame:preprocess() == -- True if the page is being previewed. local pagetype if subjectSpace == 10 then pagetype = message('sandbox-notice-pagetype-template') elseif subjectSpace == 828 then pagetype = message('sandbox-notice-pagetype-module') else pagetype = message('sandbox-notice-pagetype-other') end 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}) end -- Get the test cases page blurb if the page exists. This is something like -- "See also the companion subpage for test cases." local testcasesTitle = env.testcasesTitle if testcasesTitle and testcasesTitle.exists then if testcasesTitle.namespace == mw.site.namespaces.Module.id 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 .. '
' .. 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 .. '
' .. message('sandbox-notice-testcases-blurb', {testcasesLink}) end end -- Add the sandbox to the sandbox category. text = text .. makeCategoryLink(message('sandbox-category')) omargs.text = text

local ret = '
'

ret = ret .. messageBox.main('ombox', omargs) return ret 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, mProtectionBanner local title = env.title if title.namespace ~= 10 and title.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 editProt = protectionLevels.edit and protectionLevels.edit[1] local moveProt = protectionLevels.move and protectionLevels.move[1] if editProt then -- The page is edit-protected. mProtectionBanner = require('Module:Protection banner') local reason = message('protection-reason-edit') 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) --[[ -- This function generates the start box. -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment -- -- The actual work is done by p.makeStartBoxLinksData and p.renderStartBoxLinks which make -- the [view] [edit] [history] [purge] links, and by p.makeStartBoxData and p.renderStartBox -- which generate the box HTML. --]] env = env or p.getEnvironment(args) local links local content = args.content if not content then -- No need to include the links if the documentation is on the template page itself. local linksData = p.makeStartBoxLinksData(args, env) if linksData then links = p.renderStartBoxLinks(linksData) end end -- Generate the start box html. local data = p.makeStartBoxData(args, env, links) if data then return p.renderStartBox(data) else -- User specified no heading. return nil end end

function p.makeStartBoxLinksData(args, env) --[[ -- Does initial processing of data to make the [view] [edit] [history] [purge] links. -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment -- -- Messages: -- 'view-link-display' --> 'view' -- 'edit-link-display' --> 'edit' -- 'history-link-display' --> 'history' -- 'purge-link-display' --> 'purge' -- 'file-docpage-preload' --> 'Template:Documentation/preload-filespace' -- 'module-preload' --> 'Template:Documentation/preload-module-doc' -- 'docpage-preload' --> 'Template:Documentation/preload' -- 'create-link-display' --> 'create' --]] local subjectSpace = env.subjectSpace local title = env.title local docTitle = env.docTitle if not title or not docTitle then return nil 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. local preload = args.preload if not preload then if subjectSpace == 6 then -- File namespace preload = message('file-docpage-preload') elseif subjectSpace == 828 then -- Module namespace preload = message('module-preload') else preload = message('docpage-preload') end end data.preload = preload data.createLinkDisplay = message('create-link-display') return data end

function p.renderStartBoxLinks(data) --[[ -- Generates the [view][edit][history][purge] or [create] links from the data table. -- @data - a table of data generated by p.makeStartBoxLinksData --]]

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 if docTitle.exists then local viewLink = makeWikilink(docTitle.prefixedText, data.viewLinkDisplay) local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, data.editLinkDisplay) local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, data.historyLinkDisplay) local purgeLink = makeUrlLink(title:fullUrl{action = 'purge'}, data.purgeLinkDisplay) ret = '[%s] [%s] [%s] [%s]' ret = escapeBrackets(ret) ret = mw.ustring.format(ret, viewLink, editLink, historyLink, purgeLink) else local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = data.preload}, data.createLinkDisplay) ret = '[%s]' ret = escapeBrackets(ret) ret = mw.ustring.format(ret, createLink) end return ret end

function p.makeStartBoxData(args, env, links) --[=[ -- Does initial processing of data to pass to the start-box render function, p.renderStartBox. -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment -- @links - a string containing the [view][edit][history][purge] links - could be nil if there's an error. -- -- Messages: -- 'documentation-icon-wikitext' --> 'Documentation icon' -- 'template-namespace-heading' --> 'Template documentation' -- 'module-namespace-heading' --> 'Module documentation' -- 'file-namespace-heading' --> 'Summary' -- 'other-namespaces-heading' --> 'Documentation' -- 'start-box-linkclasses' --> 'mw-editsection-like plainlinks' -- 'start-box-link-id' --> 'doc_editlinks' -- 'testcases-create-link-display' --> 'create' --]=] local subjectSpace = env.subjectSpace if not subjectSpace then -- Default to an "other namespaces" namespace, so that we get at least some output -- if an error occurs. subjectSpace = 2 end local data = {}

-- Heading local heading = args.heading -- Blank values are not removed. if heading == then -- Don't display the start box if the heading arg is defined but blank. return nil end if heading then data.heading = heading elseif subjectSpace == 10 then -- Template namespace data.heading = message('documentation-icon-wikitext') .. ' ' .. message('template-namespace-heading') elseif subjectSpace == 828 then -- Module namespace data.heading = message('documentation-icon-wikitext') .. ' ' .. message('module-namespace-heading') elseif subjectSpace == 6 then -- File namespace data.heading = message('file-namespace-heading') else data.heading = message('other-namespaces-heading') end

-- Heading CSS local headingStyle = args['heading-style'] if headingStyle then data.headingStyleText = headingStyle elseif subjectSpace == 10 then -- We are in the template or template talk namespaces. data.headingFontWeight = 'bold' data.headingFontSize = '125%' else data.headingFontSize = '150%' end

-- Data for the [view][edit][history][purge] or [create] links. if links then data.linksClass = message('start-box-linkclasses') data.linksId = message('start-box-link-id') 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 = mw.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 -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment env = env or p.getEnvironment(args) local docTitle = env.docTitle local content = args.content if not content and docTitle and docTitle.exists then content = args._content or mw.getCurrentFrame():expandTemplate{title = docTitle.prefixedText} end -- The line breaks below are necessary so that "=== Headings ===" at the start and end -- of docs are interpreted correctly. return '\n' .. (content or ) .. '\n' end

p.contentTitle = makeInvokeFunc('_contentTitle')

function p._contentTitle(args, env) env = env or p.getEnvironment(args) local docTitle = env.docTitle if not args.content and docTitle and docTitle.exists then return docTitle.prefixedText else return end end


-- End box


p.endBox = makeInvokeFunc('_endBox')

function p._endBox(args, env) --[=[ -- This function generates the end box (also known as the link box). -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment -- -- Messages: -- 'fmbox-id' --> 'documentation-meta-data' -- 'fmbox-style' --> 'background-color: #ecfcf4' -- 'fmbox-textstyle' --> 'font-style: italic' -- -- The HTML is generated by the Template:Fmbox template, courtesy of Module:Message box. --]=]

-- Get environment data. env = env or p.getEnvironment(args) local subjectSpace = env.subjectSpace local docTitle = env.docTitle if not subjectSpace or not docTitle then 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 -- user, module or template namespaces. local linkBox = args['link box'] if linkBox == 'off' or not ( docTitle.exists or subjectSpace == 2 or subjectSpace == 828 or subjectSpace == 10 ) then return nil end

-- Assemble the arguments for Template:Fmbox. local fmargs = {} fmargs.id = message('fmbox-id') -- Sets 'documentation-meta-data' fmargs.image = 'none' fmargs.style = message('fmbox-style') -- Sets 'background-color: #ecfcf4' fmargs.textstyle = message('fmbox-textstyle') -- 'font-style: italic;'

-- Assemble the fmbox text field. local text = if linkBox then text = text .. linkBox else text = text .. (p.makeDocPageBlurb(args, env) or ) -- "This documentation is transcluded from Foo." if subjectSpace == 2 or subjectSpace == 10 or subjectSpace == 828 then -- We are in the user, template or module namespaces. -- 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 .. '
' if not args.content and not args[1] then -- "Please add categories to the /doc subpage." -- Don't show this message with inline docs or with an explicitly specified doc page, -- as then it is unclear where to add the categories. text = text .. (p.makeCategoriesBlurb(args, env) or ) end text = text .. ' ' .. (p.makeSubpagesBlurb(args, env) or ) --"Subpages of this template" local printBlurb = p.makePrintBlurb(args, env) -- Two-line blurb about print versions of templates. if printBlurb then text = text .. '
' .. printBlurb end end end fmargs.text = text

return messageBox.main('fmbox', fmargs) end

function p.makeDocPageBlurb(args, env) --[=[ -- Makes the blurb "This documentation is transcluded from Template:Foo (edit, history)". -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment -- -- Messages: -- 'edit-link-display' --> 'edit' -- 'history-link-display' --> 'history' -- 'transcluded-from-blurb' --> -- 'The above documentation -- is transcluded from $1.' -- 'module-preload' --> 'Template:Documentation/preload-module-doc' -- 'create-link-display' --> 'create' -- 'create-module-doc-blurb' --> -- 'You might want to $1 a documentation page for this Scribunto module.' --]=] local docTitle = env.docTitle if not docTitle then return nil end local ret if docTitle.exists then -- /doc exists; link to it. local docLink = makeWikilink(docTitle.prefixedText) local editUrl = docTitle:fullUrl{action = 'edit'} local editDisplay = message('edit-link-display') local editLink = makeUrlLink(editUrl, editDisplay) local historyUrl = docTitle:fullUrl{action = 'history'} local historyDisplay = message('history-link-display') local historyLink = makeUrlLink(historyUrl, historyDisplay) ret = message('transcluded-from-blurb', {docLink}) .. ' ' .. makeToolbar(editLink, historyLink) .. '
' elseif env.subjectSpace == 828 then -- /doc does not exist; ask to create it. local createUrl = docTitle:fullUrl{action = 'edit', preload = message('module-preload')} local createDisplay = message('create-link-display') local createLink = makeUrlLink(createUrl, createDisplay) ret = message('create-module-doc-blurb', {createLink}) .. '
' end return ret end

function p.makeExperimentBlurb(args, env) --[[ -- Renders the text "Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages." -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment -- -- Messages: -- 'sandbox-link-display' --> 'sandbox' -- 'sandbox-edit-link-display' --> 'edit' -- 'compare-link-display' --> 'diff' -- 'module-sandbox-preload' --> 'Template:Documentation/preload-module-sandbox' -- 'template-sandbox-preload' --> 'Template:Documentation/preload-sandbox' -- 'sandbox-create-link-display' --> 'create' -- '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' -- 'template-sandbox-preload' --> 'Template:Documentation/preload-sandbox' -- 'testcases-create-link-display' --> 'create' -- 'testcases-link-display' --> 'testcases' -- 'testcases-edit-link-display' --> 'edit' -- 'module-testcases-preload' --> 'Template:Documentation/preload-module-testcases' -- 'template-testcases-preload' --> 'Template:Documentation/preload-testcases' -- 'experiment-blurb-module' --> 'Editors can experiment in this module's $1 and $2 pages.' -- 'experiment-blurb-template' --> 'Editors can experiment in this template's $1 and $2 pages.' --]] local subjectSpace = env.subjectSpace local templateTitle = env.templateTitle local sandboxTitle = env.sandboxTitle local testcasesTitle = env.testcasesTitle local templatePage = templateTitle.prefixedText if not subjectSpace or not templateTitle or not sandboxTitle or not testcasesTitle then return nil end -- Make links. local sandboxLinks, testcasesLinks if sandboxTitle.exists then local sandboxPage = sandboxTitle.prefixedText local sandboxDisplay = message('sandbox-link-display') local sandboxLink = makeWikilink(sandboxPage, sandboxDisplay) local sandboxEditUrl = sandboxTitle:fullUrl{action = 'edit'} local sandboxEditDisplay = message('sandbox-edit-link-display') local sandboxEditLink = makeUrlLink(sandboxEditUrl, sandboxEditDisplay) local compareUrl = env.compareUrl local compareLink if compareUrl then local compareDisplay = message('compare-link-display') compareLink = makeUrlLink(compareUrl, compareDisplay) end sandboxLinks = sandboxLink .. ' ' .. makeToolbar(sandboxEditLink, compareLink) else local sandboxPreload if subjectSpace == 828 then sandboxPreload = message('module-sandbox-preload') else sandboxPreload = message('template-sandbox-preload') end local sandboxCreateUrl = sandboxTitle:fullUrl{action = 'edit', preload = sandboxPreload} local sandboxCreateDisplay = message('sandbox-create-link-display') 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 = mirrorPreload, summary = mirrorSummary} local mirrorDisplay = message('mirror-link-display') local mirrorLink = makeUrlLink(mirrorUrl, mirrorDisplay) sandboxLinks = message('sandbox-link-display') .. ' ' .. makeToolbar(sandboxCreateLink, mirrorLink) end if testcasesTitle.exists then local testcasesPage = testcasesTitle.prefixedText local testcasesDisplay = message('testcases-link-display') local testcasesLink = makeWikilink(testcasesPage, testcasesDisplay) local testcasesEditUrl = testcasesTitle:fullUrl{action = 'edit'} local testcasesEditDisplay = message('testcases-edit-link-display') local testcasesEditLink = makeUrlLink(testcasesEditUrl, testcasesEditDisplay) testcasesLinks = testcasesLink .. ' ' .. makeToolbar(testcasesEditLink) else local testcasesPreload if subjectSpace == 828 then testcasesPreload = message('module-testcases-preload') else testcasesPreload = message('template-testcases-preload') end local testcasesCreateUrl = testcasesTitle:fullUrl{action = 'edit', preload = testcasesPreload} local testcasesCreateDisplay = message('testcases-create-link-display') local testcasesCreateLink = makeUrlLink(testcasesCreateUrl, testcasesCreateDisplay) testcasesLinks = message('testcases-link-display') .. ' ' .. makeToolbar(testcasesCreateLink) end local messageName if subjectSpace == 828 then messageName = 'experiment-blurb-module' else messageName = 'experiment-blurb-template' end return message(messageName, {sandboxLinks, testcasesLinks}) end

function p.makeCategoriesBlurb(args, env) --[[ -- Generates the text "Please add categories to the /doc subpage." -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment -- Messages: -- 'doc-link-display' --> '/doc' -- 'add-categories-blurb' --> 'Please add categories to the $1 subpage.' --]] local docTitle = env.docTitle if not docTitle then return nil end local docPathLink = makeWikilink(docTitle.prefixedText, message('doc-link-display')) return message('add-categories-blurb', {docPathLink}) end

function p.makeSubpagesBlurb(args, env) --[[ -- Generates the "Subpages of this template" link. -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment

-- Messages: -- 'template-pagetype' --> 'template' -- 'module-pagetype' --> 'module' -- 'default-pagetype' --> 'page' -- 'subpages-link-display' --> 'Subpages of this $1' --]] local subjectSpace = env.subjectSpace local templateTitle = env.templateTitle if not subjectSpace or not templateTitle then return nil end local pagetype if subjectSpace == 10 then pagetype = message('template-pagetype') elseif subjectSpace == 828 then pagetype = message('module-pagetype') else pagetype = message('default-pagetype') end local subpagesLink = makeWikilink( 'Special:PrefixIndex/' .. templateTitle.prefixedText .. '/', message('subpages-link-display', {pagetype}) ) return message('subpages-blurb', {subpagesLink}) end

function p.makePrintBlurb(args, env) --[=[ -- Generates the blurb displayed when there is a print version of the template available. -- @args - a table of arguments passed by the user -- @env - environment table containing title objects, etc., generated with p.getEnvironment -- -- Messages: -- 'print-link-display' --> '/Print' -- 'print-blurb' --> 'A print version' -- .. ' of this template exists at $1.' -- .. ' If you make a change to this template, please update the print version as well.' -- 'display-print-category' --> true -- 'print-category' --> 'Templates with print versions' --]=] local printTitle = env.printTitle if not printTitle then return nil end local ret if printTitle.exists then local printLink = makeWikilink(printTitle.prefixedText, message('print-link-display')) ret = message('print-blurb', {printLink}) local displayPrintCategory = message('display-print-category', nil, 'boolean') if displayPrintCategory then ret = ret .. makeCategoryLink(message('print-category')) end end return ret end


-- Tracking categories


function p.addTrackingCategories(env) --[[

-- Check if
Documentation[create]
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 -- 'doc-subpage' --> 'doc' -- 'testcases-subpage' --> 'testcases' -- 'strange-usage-category' --> 'Wikipedia pages with strange ((documentation)) usage' -- -- /testcases pages in the module namespace are not categorised, as they may have

--
Documentation[create]
transcluded automatically.

--]] local title = env.title local subjectSpace = env.subjectSpace if not title or not subjectSpace then return nil end local subpage = title.subpageText local ret = if message('display-strange-usage-category', nil, 'boolean') and ( subpage == message('doc-subpage') or subjectSpace ~= 828 and subpage == message('testcases-subpage') ) then ret = ret .. makeCategoryLink(message('strange-usage-category')) end return ret end

return p