Module:Documentation

local p = {} local h = {}

local list = require("Module:List").unorderedList local utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable") local utilsMarkup = require("Module:UtilsMarkup")

local frame = mw.getCurrentFrame function p.Main(frame) local args = frame:getParent.args return p.template(args) end

function p.Module(frame) local docPage = mw.title.new(frame:getParent:getTitle) local modulePage = "Module:" .. docPage.baseText local module = require(modulePage) if not module.Documentation then error("No `Documentation` property exists on module " .. modulePage) end return p.module(module, frame.args) end

function p.template(templateExamples) if not templateExamples.vertical then local tableData = { hideEmptyColumns = true, rows = { { "Input", "Output", "Categories added", header = true } }		}		for _, example in ipairs(templateExamples) do			local input, output, categoryList = h.templateExample(example) input = utilsMarkup.format(input, { code = true }) table.insert(tableData.rows, {input, output, categoryList}) end return utilsMarkup.wikitable(tableData) end local result = "" for _, example in ipairs(templateExamples) do		local input, output, categoryList = h.templateExample(example) input = utilsMarkup.format(input, {pre = true}) local headerStyles = { ["width"] = "5rem" -- for alignment. See Template:Letters/Documentation for an example of why this is needed }		result = result .. utilsMarkup.wikitable({			hideEmptyRows = true,			rows = {				{					{ header = true, content= "Input", styles = headerStyles}, 					input,				},				{					{ header = true, content = "Output", styles = headerStyles}, 					output				},				{					{ header = true, content = "Categories\nAdded", styles = headerStyles },					categoryList				},			}		}) .. "\n" end return result end

function h.templateExample(example) local input = mw.text.unstripNoWiki(example) local output = frame:preprocess(input) input = mw.text.trim(input) input = mw.text.nowiki(input) local output, categories = h.removeCategories(output) local output = h.killBackLinks(output) local categoryList = list(categories) return input, output, categoryList end

function p.module(module, options) local output = '' for functionName, functionDoc in pairs(module.Documentation) do output = output .. ("==%s=="):format(functionName) .. "\n" output = output .. h.printFunctionSyntax(functionName, functionDoc.params) output = output .. h.printParamsDescription(functionDoc.params) output = output .. h.printReturnsDescription(functionDoc.returns) output = output .. h.printFunctionCases(module[functionName], functionName, functionDoc.cases, options) end return output end

function h.printFunctionSyntax(functionName, params) local paramSyntax = "" if params then local paramNames = utilsTable.map(h.printParamSyntax, params) paramSyntax = table.concat(paramNames, ", ") end local output = (" "):format(functionName, paramSyntax) return output .. "\n" end

function h.printParamSyntax(param) local name = param.name if param.optional then name = "[" .. name .. "]"	end return name end

function h.printParamsDescription(params) if not params or #params == 0 then return "" end local output = ";Parameters\n" for _, param in ipairs(params) do output = output .. mw.getCurrentFrame:expandTemplate({ title = "Parameter", args = { 			param.name,			param.description or "",			param.optional and "Optional" or "Required" 		}}) end return output .. "\n" end

function h.printReturnsDescription(returns) if not returns then return "" end local output = ";Returns\n" output = output .. "* " .. mw.getCurrentFrame:preprocess(returns) return output .. "\n" end

function h.printFunctionCases(fn, fnName, fnExamples, options) local output = ";Examples\n" local showWikitext = (options and options.wikitext) or nil local tableData = { rows = { {				header = true, cells = { "Input", "Output", showWikitext and "Wikitext"}, }		},	}	for _, example in ipairs(fnExamples) do		local argsText = utilsTable.map(utilsTable.print, example.args) argsText = table.concat(argsText, ", ") argsText = ("%s(%s)"):format(fnName, argsText) local input = utilsMarkup.format(argsText, {syntaxHighlight = "lua"}) local output = fn(unpack(example.args)) if example.description then table.insert(tableData.rows, {				header = true,				{					colspan = -1,					content = example.description,				}			}) end local wikitext = showWikitext and utilsMarkup.format(output, {pre = true}) table.insert(tableData.rows, {input, output, wikitext }) end output = output .. utilsMarkup.wikitable(tableData) .. "\n" return output end

local categoryPattern = "%[%[Category:[^%]]*%]%]" function h.removeCategories(wikitext) local categories = {} for category in string.gmatch(wikitext, categoryPattern) do		local categoryWithoutSortKey = string.gsub(category, "|[^%]]*%]%]", "]]") local categoryLink = string.gsub(categoryWithoutSortKey, "Category", ":Category") table.insert(categories, categoryLink) end return string.gsub(wikitext, categoryPattern, ""), categories end

function h.killBackLinks(wikitext) local result = wikitext for link in string.gmatch(wikitext, "%[%^%*%]%]") do		local startPos, endPos = string.find(wikitext, link, 1, true) local before = string.sub(wikitext, 1, startPos -1) local after = string.sub(wikitext, endPos + 1) local externalLink = h.killBackLink(link) result = before .. externalLink .. after end return result end

function h.killBackLink(link) if utilsString.startsWith("[[Category:", link) then		return ""	end	if utilsString.startsWith("[[File:", link) then		return link -- not dealing with this now, but it's doable	end	link = mw.text.trim(link, "][")	link = string.gsub(link, "^:", "")	local pipe = string.find(link, "|")	local page = string.sub(link, 1, pipe and pipe - 1)	local display = pipe and string.sub(link, pipe + 1)	return utilsMarkup.link(page, display, true) end

return p