Difference between revisions of "Module:For related page"

From blackwiki
Jump to navigation Jump to search
blackwiki>Nihiltres
(Reversed order of "titles" array to prefer initial-lowercased titles first)
blackwiki>Pppery
(Syncing merge -- step 1; i'm doing this in two steps to not break Template:for outline while I convert it)
Line 38: Line 38:
 
return mFor._For({'a topical guide to this subject', target})
 
return mFor._For({'a topical guide to this subject', target})
 
end
 
end
 +
function p.forFoo (frame)
 +
local args = mArguments.getArgs(frame,
 +
{parentOnly = true}
 +
)
 +
local target
 +
if args[1] then
 +
target = args[1]
 +
else
 +
local title = mw.title.getCurrentTitle().text
 +
local titles = {
 +
mw.ustring.lower(mw.ustring.sub(title, 1, 1)) .. mw.ustring.sub(title, 2),
 +
title
 +
}
 +
local forms = frame.args
 +
for k, v in ipairs(forms) do
 +
for i, j in pairs(titles) do
 +
local lookup = string.format(v, j)
 +
if mw.title.new(lookup, 0).exists then
 +
target = lookup
 +
break
 +
end
 +
end
 +
if target then break end
 +
end
 +
target = target or string.format(forms[1], title)
 +
end
  
 +
return mFor._For({frame.args.what, target})
 +
end
 
return p
 
return p

Revision as of 10:44, 8 July 2018


Implements {{for outline}}, {{for timeline}}, and {{for glossary}}


local mFor = require('Module:For')
local mArguments = require('Module:Arguments')
local p = {}

function p.forOutline (frame)
	local args = mArguments.getArgs(frame)
	local target
	if args[1] then
		target = args[1]
	else
		local title = mw.title.getCurrentTitle().text
		local titles = {
			mw.ustring.lower(mw.ustring.sub(title, 1, 1)) .. mw.ustring.sub(title, 2),
			title
		}
		local outlineForms = {
			'Outline of %s',
			'Outline of %ss',
			'Outline of %s topics',
			'Outline of %s-related topics',
			'List of %s topics',
			'List of %s-related topics',
			'List of topics related to the %s'
		}
		for k, v in pairs(outlineForms) do
			for i, j in pairs(titles) do
				local lookup = string.format(v, j)
				if mw.title.new(lookup, 0).exists then
					target = lookup
					break
				end
			end
			if target then break end
		end
		target = target or string.format(outlineForms[1], title)
	end

	return mFor._For({'a topical guide to this subject', target})
end
function p.forFoo (frame)
	local args = mArguments.getArgs(frame, 
		{parentOnly = true}
	)
	local target
	if args[1] then
		target = args[1]
	else
		local title = mw.title.getCurrentTitle().text
		local titles = {
			mw.ustring.lower(mw.ustring.sub(title, 1, 1)) .. mw.ustring.sub(title, 2),
			title
		}
		local forms = frame.args
		for k, v in ipairs(forms) do
			for i, j in pairs(titles) do
				local lookup = string.format(v, j)
				if mw.title.new(lookup, 0).exists then
					target = lookup
					break
				end
			end
			if target then break end
		end
		target = target or string.format(forms[1], title)
	end

	return mFor._For({frame.args.what, target})
end
return p