Module:Template documentation

-- local p = {} function p.docs(frame) local title = mw.title.getCurrentTitle -- THIS IS EXPENSIVE local docs = mw.title.new(title.fullText .. '/doc') local html = mw.html.create('div'):addClass('template-documentation') -- Links local editLink = tostring(docs:fullUrl({ action = 'edit' })) local historyLink = tostring(docs:fullUrl({ action = 'history' })) local purgeLink = tostring(title:fullUrl({ action = 'purge' })) if docs.exists then html:tag('div') :addClass('template-documentation__header') :tag('div') :addClass('template-documentation__title') :wikitext('Template documentation') :done :tag('div') :addClass('template-documentation__buttons') :wikitext(table.concat({ '', editLink, ' edit] • [', historyLink,                       ' history] • [', purgeLink, ' purge' }))               :done :done :tag('div') :addClass('template-documentation__contents') :wikitext(frame:preprocess(docs:getContent)) :done else html:tag('div') :addClass('template-documentation__error') :wikitext(table.concat({ 'This template has no documentation! Click [', editLink, ' here] to create it or [', purgeLink, ' here] to purge the current page after the documentation has been created' }))           :done end return tostring(html:done) end function page_section(t, name, content) local trimmed = content and mw.text.trim(content) or '' if trimmed ~= '' then table.insert(t, '== ') table.insert(t, name) table.insert(t, ' ==\n') table.insert(t, trimmed) table.insert(t, '\n') end end function p.page(frame) local args = frame:getParent.args local content = {} table.insert(content, args.description or 'This template is missing a description!') table.insert(content, '\n') page_section(content, 'Parameters', args.parameters) page_section(content, 'Sample output', args.samples) page_section(content, 'See also', args.see) local title = mw.title.getCurrentTitle if title.namespace == 10 then -- We're in template namespace if title.subpageText == 'doc' then -- We're on a documentation subpage table.insert(content, '') elseif args.category then for _, cat in ipairs(mw.text.split(args.category, ',', true)) do               table.insert(content, '[[Category:')                table.insert(content, cat)                table.insert(content, ' templates]]') end end end table.insert(content, '') return frame:preprocess(table.concat(content)) end function p.parameters(frame) local html = mw.html.create('table') :addClass('wikitable') :addClass('template-documentation__parameters') html:tag('tr') :tag('th') :wikitext('Parameter') :done :tag('th') :wikitext('Description') :done :done for k, v in pairs(frame:getParent.args) do       html:tag('tr') :tag('td') :wikitext(table.concat({' '})) :done :tag('td') :wikitext(v) :done :done end return tostring(html) end function p.samples(frame) local ret = {} local args if frame.args.documentation then -- We can't use the template for purposes of documenting -- that template due to the template loop... args = frame.args else args = frame:getParent.args end for _, value in ipairs(args) do       local sample = mw.text.trim(mw.text.decode(mw.text.unstrip(value))) if mw.ustring.match(sample, '\n') then table.insert(ret, ' ') table.insert(ret, sample) table.insert(ret, ' ') else table.insert(ret, ' ') end table.insert(ret, '') table.insert(ret, wrapped) table.insert(ret, ' gives... ') table.insert(ret, sample) table.insert(ret, '\n') end return frame:preprocess(table.concat(ret)) .. '\n' end return p