Modul


--[=[ SoftwareDoku 2022-10-14
Module for software documentation templates
Author: Vollbracht

template functions
* functionDoc(frame)	standard description of a function
]=]

--Module globals
local p = {}

local _, Parser = pcall(require, "Modul:SimpleStruct")

local tcDef = {
	td = '<td style="text-align:left; vertical-align:top; padding:0.2em;">',
	th = '<th style="text-align:left; vertical-align:top; padding:0.2em;">'
}

--[[
	functionDoc(frame)
	standard description of a function
	parameters:
		frame.args.name			name of function
		frame.args.short		short description
		frame.args.parameters	parsable parameters list for en table header
		frame.args.Parameter	parsable parameters list for de table header
		frame.args.returns		wikitext return description for en table header
		frame.args.Rueckgabe	wikitext return description for de table header
		frame.args.example		code for en intro
		frame.args.Beispiel		code for de intro
]]
p.functionDoc = function(frame)
	if not frame.args.name then return '' end
	if not frame.args.short then return '' end
	local result =	'<h3>' .. frame.args.name .. '</h3><p>' .. frame.args.short
				..	'</p><table style="width:100%;"><tr>'
				..	'<th colspan="3" style="text-align:left;">'
	local pars = frame.args.parameters
	if pars then
		result = result .. 'parameters:</th></tr><tr>'
	else
		pars = frame.args.Parameter
		if pars then
			result = result .. 'Parameter:</th></tr><tr>'
		else return '' end
	end
	if pars == 'none' then
		result = result .. '<td></td><td colspan="2">no parameters</td></tr>'
	else
		local pList = Parser.altParse(pars)
		result = result .. '<td rowspan="' .. #pList .. '"></td>'
		local pRows = {}
		for _, v in ipairs(pList) do
			table.insert(pRows,
				tcDef.th .. v[1] .. '</th><td>' .. v[2] .. '</td>')
		end
		result =	result .. table.concat(pRows, '</tr><tr>')
				..	'</tr><tr>' .. tcDef.th
	end
	local ret = frame.args.returns
	if ret then
		result = result .. 'returns'
	else
		ret = frame.args.Rueckgabe
		if ret then
			result = result .. 'Rückgabewert'
		else return '' end
	end
	result = result .. ':</th><td colspan="2">' .. ret .. '</td></tr></table>'
	local ex = frame.args.example
	if ex then
		ex = frame:preprocess('<pre>' .. ex .. '</pre>')
		result = result .. '<h4>Example</h4>' .. ex
	else
		ex = frame.args.Beispiel
		if ex then
			ex = frame:preprocess('<pre>' .. ex .. '</pre>')
			result = result .. '<h4>Beispiel</h4>' .. ex
		end
	end
	return result
end

--[[
	moduleName()
	name of module based on site name
]]
p.moduleName = function(frame)
	local siteName = mw.title.getCurrentTitle()
	local full = siteName:match('Modul:%.*')
	if not full then
		full = siteName:match('Modul/%.*')
	end
	if full then
		full = full:sub(8)
		local subNP = full:find('/')
		if subNP then return full:sub(1, subNP) end
		return full
	end
end

--[[
	linkBoxDeEn()
	link box for German and English LUA links
]]
p.linkBoxDeEn = function(frame)
	local siteName = mw.title.getCurrentTitle().fullText
	if not string.match(siteName, 'Modul%.*') then
		return 'nothing about module'
	else
		return string.match(siteName, 'Modul%.*')
	end
	local full = string.match(siteName, 'Modul%.*'):sub(7)
	local function lb(name)
		return '<table class="wikitable" style="margin: 0 0 1em auto;"><tr>'
			.. '<td rowspan="2">[[Wikipedia:Lua/Modul/' .. name .. '|Vorlagenpr'
			.. 'ogrammierung]]</td><td rowspan="2">[[Wikipedia Diskussion:Lua/M'
			.. 'odul/' .. name .. '|Diskussion]]</td><th colspan="3">[[Hilfe:Lu'
			.. 'a|Lua]]</th><td colspan="2">Unterseiten mit Präfix</td></tr><tr'
			.. '><td>[[Modul:' .. name .. '|Modul]]</td><td>[[Wikipedia:Lua/Mod'
			.. 'ul/' .. name .. '/de|Deutsch]]</td><td>[[Wikipedia:Lua/Modul/'
			.. name .. '/en|Englisch]]</td><td>[https://de.wikipedia.org/w/inde'
			.. 'x.php?title=Spezial:Prefixindex&prefix=' .. name .. '&namespace'
			.. '=828 Modul:]</td><td>[https://de.wikipedia.org/w/index.php?titl'
			.. 'e=Spezial:Prefixindex&prefix=' .. name .. '&namespace=4 Dokuman'
			.. 'tation:]</td></tr></table>'
	end
	
	if not full then return "" end
	local c = full:sub(1,1)
	full = full:sub(2)
	local suffix = full:find('/')
	if c == ' ' then
		if siteName:match('Diskussion:') then
			full = full:sub(12)
			if suffix then
				return '#Weiterleitung [[Wikipedia Diskussion:Lua/Modul/'
					.. full:sub(1, suffix) .. ']]'
			end
			return lb(full)
		end
		return lb(full)
	elseif c == '/' then
		local test = full
		local front = ""
		while suffix do
			local s1 = test:sub(suffix + 1)
			front = front .. test:sub(1, suffix - 1)
			if s1 == 'de' or s1 == 'en' then
				return lb(front)
			end
			test = s1
			suffix = s1:find('/')
		end
		return lb(full)
	elseif c == ':' then
		local test = full
		local front = ""
		while suffix do
			local s1 = test:sub(suffix + 1)
			front = front .. test:sub(1, suffix - 1)
			if s1 == 'Doku' then
				return lb(front)
			end
			test = s1
			suffix = s1:find('/')
		end
		return ""
	else return "" end
end

return p