Module:Forumlink: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
(create)
 
No edit summary
Line 2: Line 2:
local largs
local largs


--Keys must be lowercase
local presets = {
local presets = {
npo = { domain = 'npowned.net', type = 'ips' }
npo = { domain = 'npowned.net', type = 'ips' }
}
}


--Keys must be lowercase
local types = {
local types = {
ips = {
ips = {
Line 26: Line 28:
end
end


local function finalize(url, arg, ftype)
local function geturl(url, arg, ftype)
paths = types[ftype]
if arg == 'default' then
if arg == 'default' then
return url .. paths[arg]
return url .. ftype[arg]
end
end
local text = mw.ustring.lower(largs[arg])
local text = mw.ustring.lower(largs[arg])
if paths.sep ~= ' ' then
if ftype.sep ~= ' ' then
text = mw.ustring.gsub(text, '%s', paths.sep)
text = mw.ustring.gsub(text, '%s', ftype.sep)
end
end
return url .. mw.ustring.format(paths[arg], text)
return url .. mw.ustring.format(ftype[arg], text)
end
end


Line 43: Line 44:


function p.call(args)
function p.call(args)
largs = args
largs = args-- lazy shortcut for local args
local ftype, url, domain
local ftype, url, domain, display
if argDefault('http') then
if argDefault('http') then
url = 'http://'
url = 'http://'
Line 52: Line 53:
local preset = presets[mw.ustring.lower(argDefault(1, ''))]
local preset = presets[mw.ustring.lower(argDefault(1, ''))]
if not preset then
if not preset then
domain = argDefault('site', '{{{site}}}')
domain = argDefault(1)
ftype = argDefault('type')
ftype = argDefault('type')
else
else
Line 58: Line 59:
ftype = preset.type
ftype = preset.type
end
end
if not domain then
return '<span class="error">missing site address</span>'
end
if not ftype then
return '<span class="error">missing forum type</span>'
end
ftype = types[mw.ustring.lower(ftype)]
if not ftype then
if not ftype then
return '<span class="error">missing type</span>'
return '<span class="error">type is invalid or unrecognized</span>'
end
end
url = url .. domain .. '/'
url = url .. domain .. '/'
if argDefault('topic') then
local params = {'topic', 'profile', 'forum', 'news', 'announcement'}
return finalize(url, 'topic', ftype)
local component = 'default'
elseif argDefault('profile') then
for i, para in ipairs(params) do
return finalize(url, 'profile', ftype)
if argDefault(para) then
elseif argDefault('forum') then
component = para
return finalize(url, 'forum', ftype)
break
elseif argDefault('news') then
end
return finalize(url, 'news', ftype)
end
elseif argDefault('announcement') then
url = geturl(url, component, ftype)
return finalize(url, 'announcement', ftype)
else
if argDefault('numbered') then
return finalize(url, 'default', ftype)
return '[' .. url .. ']'
end
display = argDefault(2)
if not display then
if component == 'default' then
display = args[1] .. ' forums'
else
display = args[component]
end
end
end
return '[' .. url .. ' ' .. display .. ']'
end
end


return p
return p

Revision as of 02:04, 11 July 2020

Documentation [edit]

This page is a stub - it only covers the very basics of its subject. More information should be added to make the page informative and useful. If no more information is available, the page should be considered for a merge, redirect, or deletion.

Details: documentation to be written

If you can correct the issue, please edit the page to do so, then remove this notice.
Please see Template:Forumlink for usage information. Use the template rather than invoking the module.

The above documentation is transcluded from Module:Forumlink/doc.

local p = {}
local largs

--Keys must be lowercase
local presets = {
	npo = { domain = 'npowned.net', type = 'ips' }
}

--Keys must be lowercase
local types = {
	ips = {
		sep = '-',
		announcement = 'announcement/%s/',
		forum = 'forums/forum/%s/',
		news = 'news/community/%s/',
		profile = 'profile/%s/',
		topic = 'forums/topic/%s/',
		default = 'forums/'
	}
}

local function argDefault(arg, default)
	if mw.text.trim(largs[arg] or '') == '' then
		return default
	else
		return largs[arg]
	end
end

local function geturl(url, arg, ftype)
	if arg == 'default' then
		return url .. ftype[arg]
	end
	local text = mw.ustring.lower(largs[arg])
	if ftype.sep ~= ' ' then
		text = mw.ustring.gsub(text, '%s', ftype.sep)
	end
	return url .. mw.ustring.format(ftype[arg], text)
end

function p.main(frame)
	return p.call(frame:getParent().args)
end

function p.call(args)
	largs = args-- lazy shortcut for local args
	local ftype, url, domain, display
	if argDefault('http') then
		url = 'http://'
	else
		url = 'https://'
	end
	local preset = presets[mw.ustring.lower(argDefault(1, ''))]
	if not preset then
		domain = argDefault(1)
		ftype = argDefault('type')
	else
		domain = preset.domain
		ftype = preset.type
	end
	if not domain then
		return '<span class="error">missing site address</span>'
	end
	if not ftype then
		return '<span class="error">missing forum type</span>'
	end
	ftype = types[mw.ustring.lower(ftype)]
	if not ftype then
		return '<span class="error">type is invalid or unrecognized</span>'
	end
	url = url .. domain .. '/'
	
	local params = {'topic', 'profile', 'forum', 'news', 'announcement'}
	local component = 'default'
	for i, para in ipairs(params) do
		if argDefault(para) then
			component = para
			break
		end
	end
	url = geturl(url, component, ftype)
	
	if argDefault('numbered') then
		return '[' .. url .. ']'
	end
	
	display = argDefault(2)
	if not display then
		if component == 'default' then
			display = args[1] .. ' forums'
		else
			display = args[component]
		end
	end
	return '[' .. url .. ' ' .. display .. ']'
end

return p