<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://159.13.49.45/index.php?action=history&amp;feed=atom&amp;title=Module%3AFormat_link</id>
	<title>Module:Format link - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://159.13.49.45/index.php?action=history&amp;feed=atom&amp;title=Module%3AFormat_link"/>
	<link rel="alternate" type="text/html" href="http://159.13.49.45/index.php?title=Module:Format_link&amp;action=history"/>
	<updated>2026-04-03T22:01:24Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>http://159.13.49.45/index.php?title=Module:Format_link&amp;diff=365&amp;oldid=prev</id>
		<title>Nat: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="http://159.13.49.45/index.php?title=Module:Format_link&amp;diff=365&amp;oldid=prev"/>
		<updated>2025-04-07T11:03:02Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 11:03, 7 April 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key mediawiki:diff:1.41:old-364:rev-365 --&gt;
&lt;/table&gt;</summary>
		<author><name>Nat</name></author>
	</entry>
	<entry>
		<id>http://159.13.49.45/index.php?title=Module:Format_link&amp;diff=364&amp;oldid=prev</id>
		<title>wikipedia&gt;Pppery: Avoid Lua erroring when we run out of expensive parser function calls</title>
		<link rel="alternate" type="text/html" href="http://159.13.49.45/index.php?title=Module:Format_link&amp;diff=364&amp;oldid=prev"/>
		<updated>2022-10-04T13:37:11Z</updated>

		<summary type="html">&lt;p&gt;Avoid Lua erroring when we run out of expensive parser function calls&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--------------------------------------------------------------------------------&lt;br /&gt;
-- Format link&lt;br /&gt;
--&lt;br /&gt;
-- Makes a wikilink from the given link and display values. Links are escaped&lt;br /&gt;
-- with colons if necessary, and links to sections are detected and displayed&lt;br /&gt;
-- with &amp;quot; § &amp;quot; as a separator rather than the standard MediaWiki &amp;quot;#&amp;quot;. Used in&lt;br /&gt;
-- the {{format link}} template.&lt;br /&gt;
--------------------------------------------------------------------------------&lt;br /&gt;
local libraryUtil = require(&amp;#039;libraryUtil&amp;#039;)&lt;br /&gt;
local checkType = libraryUtil.checkType&lt;br /&gt;
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg&lt;br /&gt;
local mArguments -- lazily initialise [[Module:Arguments]]&lt;br /&gt;
local mError -- lazily initialise [[Module:Error]]&lt;br /&gt;
local yesno -- lazily initialise [[Module:Yesno]]&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
--------------------------------------------------------------------------------&lt;br /&gt;
-- Helper functions&lt;br /&gt;
--------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local function getArgs(frame)&lt;br /&gt;
	-- Fetches the arguments from the parent frame. Whitespace is trimmed and&lt;br /&gt;
	-- blanks are removed.&lt;br /&gt;
	mArguments = require(&amp;#039;Module:Arguments&amp;#039;)&lt;br /&gt;
	return mArguments.getArgs(frame, {parentOnly = true})&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function removeInitialColon(s)&lt;br /&gt;
	-- Removes the initial colon from a string, if present.&lt;br /&gt;
	return s:match(&amp;#039;^:?(.*)&amp;#039;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function maybeItalicize(s, shouldItalicize)&lt;br /&gt;
	-- Italicize s if s is a string and the shouldItalicize parameter is true.&lt;br /&gt;
	if s and shouldItalicize then&lt;br /&gt;
		return &amp;#039;&amp;lt;i&amp;gt;&amp;#039; .. s .. &amp;#039;&amp;lt;/i&amp;gt;&amp;#039;&lt;br /&gt;
	else&lt;br /&gt;
		return s&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function parseLink(link)&lt;br /&gt;
	-- Parse a link and return a table with the link&amp;#039;s components.&lt;br /&gt;
	-- These components are:&lt;br /&gt;
	-- - link: the link, stripped of any initial colon (always present)&lt;br /&gt;
	-- - page: the page name (always present)&lt;br /&gt;
	-- - section: the page name (may be nil)&lt;br /&gt;
	-- - display: the display text, if manually entered after a pipe (may be nil)&lt;br /&gt;
	link = removeInitialColon(link)&lt;br /&gt;
&lt;br /&gt;
	-- Find whether a faux display value has been added with the {{!}} magic&lt;br /&gt;
	-- word.&lt;br /&gt;
	local prePipe, display = link:match(&amp;#039;^(.-)|(.*)$&amp;#039;)&lt;br /&gt;
	link = prePipe or link&lt;br /&gt;
&lt;br /&gt;
	-- Find the page, if it exists.&lt;br /&gt;
	-- For links like [[#Bar]], the page will be nil.&lt;br /&gt;
	local preHash, postHash = link:match(&amp;#039;^(.-)#(.*)$&amp;#039;)&lt;br /&gt;
	local page&lt;br /&gt;
	if not preHash then&lt;br /&gt;
		-- We have a link like [[Foo]].&lt;br /&gt;
		page = link&lt;br /&gt;
	elseif preHash ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		-- We have a link like [[Foo#Bar]].&lt;br /&gt;
		page = preHash&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- Find the section, if it exists.&lt;br /&gt;
	local section&lt;br /&gt;
	if postHash and postHash ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		section = postHash&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	return {&lt;br /&gt;
		link = link,&lt;br /&gt;
		page = page,&lt;br /&gt;
		section = section,&lt;br /&gt;
		display = display,&lt;br /&gt;
	}&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function formatDisplay(parsed, options)&lt;br /&gt;
	-- Formats a display string based on a parsed link table (matching the&lt;br /&gt;
	-- output of parseLink) and an options table (matching the input options for&lt;br /&gt;
	-- _formatLink).&lt;br /&gt;
	local page = maybeItalicize(parsed.page, options.italicizePage)&lt;br /&gt;
	local section = maybeItalicize(parsed.section, options.italicizeSection)&lt;br /&gt;
	if (not section) then&lt;br /&gt;
		return page&lt;br /&gt;
	elseif (not page) then&lt;br /&gt;
		return mw.ustring.format(&amp;#039;§&amp;amp;nbsp;%s&amp;#039;, section)&lt;br /&gt;
	else&lt;br /&gt;
		return mw.ustring.format(&amp;#039;%s §&amp;amp;nbsp;%s&amp;#039;, page, section)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function missingArgError(target)&lt;br /&gt;
	mError = require(&amp;#039;Module:Error&amp;#039;)&lt;br /&gt;
	return mError.error{message =&lt;br /&gt;
		&amp;#039;Error: no link or target specified! ([[&amp;#039; .. target .. &amp;#039;#Errors|help]])&amp;#039;&lt;br /&gt;
	}&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--------------------------------------------------------------------------------&lt;br /&gt;
-- Main functions&lt;br /&gt;
--------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
function p.formatLink(frame)&lt;br /&gt;
	-- The formatLink export function, for use in templates.&lt;br /&gt;
	yesno = require(&amp;#039;Module:Yesno&amp;#039;)&lt;br /&gt;
	local args = getArgs(frame)&lt;br /&gt;
	local link = args[1] or args.link&lt;br /&gt;
	local target = args[3] or args.target&lt;br /&gt;
	if not (link or target) then&lt;br /&gt;
		return missingArgError(&amp;#039;Template:Format link&amp;#039;)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return p._formatLink{&lt;br /&gt;
		link = link,&lt;br /&gt;
		display = args[2] or args.display,&lt;br /&gt;
		target = target,&lt;br /&gt;
		italicizePage = yesno(args.italicizepage),&lt;br /&gt;
		italicizeSection = yesno(args.italicizesection),&lt;br /&gt;
		categorizeMissing = args.categorizemissing&lt;br /&gt;
	}&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p._formatLink(options)&lt;br /&gt;
	-- The formatLink export function, for use in modules.&lt;br /&gt;
	checkType(&amp;#039;_formatLink&amp;#039;, 1, options, &amp;#039;table&amp;#039;)&lt;br /&gt;
	local function check(key, expectedType) --for brevity&lt;br /&gt;
		checkTypeForNamedArg(&lt;br /&gt;
			&amp;#039;_formatLink&amp;#039;, key, options[key], expectedType or &amp;#039;string&amp;#039;, true&lt;br /&gt;
		)&lt;br /&gt;
	end&lt;br /&gt;
	check(&amp;#039;link&amp;#039;)&lt;br /&gt;
	check(&amp;#039;display&amp;#039;)&lt;br /&gt;
	check(&amp;#039;target&amp;#039;)&lt;br /&gt;
	check(&amp;#039;italicizePage&amp;#039;, &amp;#039;boolean&amp;#039;)&lt;br /&gt;
	check(&amp;#039;italicizeSection&amp;#039;, &amp;#039;boolean&amp;#039;)&lt;br /&gt;
	check(&amp;#039;categorizeMissing&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
	-- Normalize link and target and check that at least one is present&lt;br /&gt;
	if options.link == &amp;#039;&amp;#039; then options.link = nil end&lt;br /&gt;
	if options.target == &amp;#039;&amp;#039; then options.target = nil end&lt;br /&gt;
	if not (options.link or options.target) then&lt;br /&gt;
		return missingArgError(&amp;#039;Module:Format link&amp;#039;)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local parsed = parseLink(options.link)&lt;br /&gt;
	local display = options.display or parsed.display&lt;br /&gt;
	local catMissing = options.categorizeMissing&lt;br /&gt;
	local category = &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
	-- Find the display text&lt;br /&gt;
	if not display then display = formatDisplay(parsed, options) end&lt;br /&gt;
&lt;br /&gt;
	-- Handle the target option if present&lt;br /&gt;
	if options.target then&lt;br /&gt;
		local parsedTarget = parseLink(options.target)&lt;br /&gt;
		parsed.link = parsedTarget.link&lt;br /&gt;
		parsed.page = parsedTarget.page&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- Test if page exists if a diagnostic category is specified&lt;br /&gt;
	if catMissing and (mw.ustring.len(catMissing) &amp;gt; 0) then&lt;br /&gt;
		local title = nil&lt;br /&gt;
		if parsed.page then title = mw.title.new(parsed.page) end&lt;br /&gt;
		if title and (not title.isExternal) then&lt;br /&gt;
			local success, exists = pcall(function() return title.exists end)&lt;br /&gt;
			if success and not exists then&lt;br /&gt;
				category = mw.ustring.format(&amp;#039;[[Category:%s]]&amp;#039;, catMissing)&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- Format the result as a link&lt;br /&gt;
	if parsed.link == display then&lt;br /&gt;
		return mw.ustring.format(&amp;#039;[[:%s]]%s&amp;#039;, parsed.link, category)&lt;br /&gt;
	else&lt;br /&gt;
		return mw.ustring.format(&amp;#039;[[:%s|%s]]%s&amp;#039;, parsed.link, display, category)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--------------------------------------------------------------------------------&lt;br /&gt;
-- Derived convenience functions&lt;br /&gt;
--------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
function p.formatPages(options, pages)&lt;br /&gt;
	-- Formats an array of pages using formatLink and the given options table,&lt;br /&gt;
	-- and returns it as an array. Nil values are not allowed.&lt;br /&gt;
	local ret = {}&lt;br /&gt;
	for i, page in ipairs(pages) do&lt;br /&gt;
		ret[i] = p._formatLink{&lt;br /&gt;
			link = page,&lt;br /&gt;
			categorizeMissing = options.categorizeMissing,&lt;br /&gt;
			italicizePage = options.italicizePage,&lt;br /&gt;
			italicizeSection = options.italicizeSection&lt;br /&gt;
		}&lt;br /&gt;
	end&lt;br /&gt;
	return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>wikipedia&gt;Pppery</name></author>
	</entry>
</feed>