Module:Browse

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Browse/doc

Code

local p = {}
function p.run(frame)
	local title = mw.title.getCurrentTitle().fullText
	local titleWords = mw.text.split(title, " ")
	local lastWord = table.remove(titleWords, #titleWords)
	local titleBase = table.concat(titleWords, " ")
	local previousLink = createPreviousNextLink(
		frame.args.link_last,
		frame.args.text_last,
		lastWord,
		titleBase,
		-1
	)
	local nextLink = createPreviousNextLink(
		frame.args.link_next,
		frame.args.text_next,
		lastWord,
		titleBase,
		1
	)
	out = ""
	if previousLink then
		out = out .. "<< " .. previousLink
		if nextLink then
			out = out .. " | "
		end
	end
	if nextLink then
		out = out .. nextLink .. " >>"
	end
	return out
end

function createPreviousNextLink(target, text, lastWord, titleBase, difference)
	local link
	if target ~= nil and target ~= "" then
		if text == nil or text == "" then
			text = target
		end
		link = createLink(target, text)
	elseif tonumber(lastWord) then
		link = createYearLink(lastWord, difference, titleBase)
	end
	if link then
		return link
	else
		return nil
	end
end

function createYearLink(originalYear, difference, titleBase)
	local year = originalYear + difference
	local title = mw.title.new(titleBase .. " " .. year)
	if title.exists then
		return createLink(title.fullText, year)
	else
		return nil
	end
end

function createLink(target, text)
	return "[[" .. target .. "|" .. text .. "]]"
end

return p