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) return require(frame:getParent:getTitle .. "/Examples").Main(frame) 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 = utilsMarkup.format(example, { code = true }) local output, categoryList = h.templateExample(example) table.insert(tableData.rows, {input, output, categoryList}) end return utilsMarkup.wikitable(tableData) end local result = "" for _, example in ipairs(templateExamples) do		local input = utilsMarkup.format(example, {pre = true}) local output, categoryList = h.templateExample(example) result = result .. utilsMarkup.wikitable({			hideEmptyRows = true,			rows = {				{					{ header = true, content= "Input"}, 					input,				},				{					{ header = true, content = "Output"}, 					output				},				{					{ header = true, content = "Categories added" },					categoryList				},			}		}) .. "\n" end return result end

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

function p.module(module, moduleExamples, options) local output = '' for fn, fnExamples in pairs(moduleExamples) do output = output .. ("== ==\n"):format(fn) output = output .. h.fn(module[fn], fn, fnExamples, options) .. "\n" end return output end

function h.fn(fn, fnName, fnExamples, options) local showWikitext = (options and options.showOutputWikitext) or nil local tableData = { rows = { {				header = true, cells = { "Input", "Output", showWikitext and "Wikitext"}, }		},	}	for _, example in ipairs(fnExamples) do		local argsText = utilsTable.mapSafe(example.args, utilsTable.inspect) 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 return utilsMarkup.wikitable(tableData) 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, false) end

return p