Module:Format link: Difference between revisions

From NvWiki
Jump to navigation Jump to search
Reverted 1 edit by 41.121.61.119 (talk): Irrelevant. WP:NOTFORUM
m 1 revision imported: Import modules used with Template:Infobox software
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==+{{para|target}}==
--------------------------------------------------------------------------------
{{edit template-protected|answered=yes}}
-- Format link
--
-- Makes a wikilink from the given link and display values. Links are escaped
-- with colons if necessary, and links to sections are detected and displayed
-- with " § " as a separator rather than the standard MediaWiki "#". Used in
-- the {{format link}} template.
--------------------------------------------------------------------------------
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local mArguments -- lazily initialise [[Module:Arguments]]
local mError -- lazily initialise [[Module:Error]]
local yesno -- lazily initialise [[Module:Yesno]]


I request the module be edited to reflect the changes in [[Special:PermaLink/1062625990]], which add a {{para|target}} parameter. The intention of this parameter is mostly for humor (to look like a fancy link to one thing but really be a link to another thing) in talk and project space. It also has some use in {{tl|format linkr}} as it can allow users to link to pages which would be URL-encoding converted right now. Tests were added in [[Special:PermaLink/1061891226]].
local p = {}


{{small|1=FYI, {{u|Nihiltres}}, sorry I keep pinging you, I just have found a lot of issues with your recent update slowly at different times is all, otherwise I would've combined them, but you [https://en.wikipedia.org/w/index.php?title=Template:Format_link/sandbox&type=revision&diff=1062620489&oldid=1061136558&diffmode=source forgot to update the sandbox of <nowiki>{{format link}}</nowiki> to point to the sandbox(!)] when you updated the module's name, boy was that an annoying bug to track down while writing this diff, easily accounts for 75% of time spent. I'm not that bright, though, a sharper student would've gotten it quickly.}} {{=3|8}} [[User:Psiĥedelisto|Psiĥedelisto]] ([[User talk:Psiĥedelisto|talk]] • [[Special:Contributions/Psiĥedelisto|contribs]]) <sup>please ''always'' [[Help:Notifications|ping]]!</sup> 16:04, 29 December 2021 (UTC)
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------


:I don't see the benefit of this change given that the first positional parameter ''already'' specifies the target, and the second already specifies the display. It seems like it complicates the code needlessly, especially as implemented (I'd override the ''output'' of <code>parseLink</code> rather than integrate a second parameter to it). That said, perhaps I'm not interpreting the use-case quite right. What does this add that the template/module can't already do? <span style="white-space:nowrap;">{&#123;[[User:Nihiltres|<span style="color:#233D7A;">Nihiltres</span>]]&#8202;&#124;[[User talk:Nihiltres|talk]]&#8202;&#124;[[Special:Contributions/Nihiltres|edits]]}&#125;</span> 18:21, 29 December 2021 (UTC)
local function getArgs(frame)
::{{re|Nihiltres}} I don't mind reimplementing, but we should probably agree it's useful before I bother. So, consider e.g. {{code|<nowiki>{{format linkr|:ja:%E5%A1%A9#原料||:ja:%E3%82%B3%E3%82%B7%E3%83%A7%E3%82%A6}}</nowiki>}} which would create {{format linkr/sandbox|:ja:%E5%A1%A9#原料||:ja:%E3%82%B3%E3%82%B7%E3%83%A7%E3%82%A6}}. It is intended mostly for use with {{tl|format linkr}}, mostly where you rely heavily on that template's auto-formatting to remove underscores, URL encoding, etc., and is useless when <code>display</code> is given. All three should never be given. [[User:Psiĥedelisto|Psiĥedelisto]] ([[User talk:Psiĥedelisto|talk]] • [[Special:Contributions/Psiĥedelisto|contribs]]) <sup>please ''always'' [[Help:Notifications|ping]]!</sup> 19:55, 30 December 2021 (UTC)
-- Fetches the arguments from the parent frame. Whitespace is trimmed and
:::{{re|Psiĥedelisto}} I think I might still not understand—in particular, the double pipe in your example really complicates it (is it an error?)—but translating to a functional goal, my guess is that you essentially want the option of applying the link formatting to the ''display'', so that e.g. <code><nowiki>{{format link|Display#Section|target=Target}}</nowiki></code> would resolve to <code><nowiki>[[:Target|Display §&amp;nbsp;Section]]</nowiki></code>. Is that a reasonable interpretation? I'm concerned that it's over-specialized, but if it isn't awful to implement and doesn't introduce edge-cases to extant uses, I don't see why not. <span style="white-space:nowrap;">{&#123;[[User:Nihiltres|<span style="color:#233D7A;">Nihiltres</span>]]&#8202;&#124;[[User talk:Nihiltres|talk]]&#8202;&#124;[[Special:Contributions/Nihiltres|edits]]}&#125;</span> 21:27, 30 December 2021 (UTC)
-- blanks are removed.
::::{{re|Nihiltres}} Not an error. It skips {{para|display}}, which is also possible. Your example, byte for byte, already works with my sandbox version: {{format link/sandbox|Display#Section|target=Target}}. Now, whether or not this is marginal use is somewhat debatable…I think more people would use {{tl|format linkr}} if they knew about it, because it really speeds up section linking, ''especially'' on foreign-language wikis (I bet this is why it was so quickly copied to Vietnamese Wikipedia). URL encoding and underlines really slow me down. {{=3|8}} [[User:Psiĥedelisto|Psiĥedelisto]] ([[User talk:Psiĥedelisto|talk]] • [[Special:Contributions/Psiĥedelisto|contribs]]) <sup>please ''always'' [[Help:Notifications|ping]]!</sup> 22:24, 30 December 2021 (UTC)
mArguments = require('Module:Arguments')
::::{{re|Nihiltres}} I thought of another use for the feature, which I just used on [[Talk:Furry fandom]]—often I want to link to a section ''in the article'' but write it ''on talk''. So I subst'd the /sandbox form of {{tl|format linkr}} with args {{code|<nowiki>{{format linkr/sandbox|#BDSM||Furry fandom#BDSM}}</nowiki>}} to get [[:Furry fandom#BDSM|§&nbsp;BDSM]]. That's actually very useful in practice, so I hope you'll reconsider this feature's utility. [[User:Psiĥedelisto|Psiĥedelisto]] ([[User talk:Psiĥedelisto|talk]] • [[Special:Contributions/Psiĥedelisto|contribs]]) <sup>please ''always'' [[Help:Notifications|ping]]!</sup> 02:20, 31 December 2021 (UTC)
return mArguments.getArgs(frame, {parentOnly = true})
end


{{undent}} {{re|Psiĥedelisto}} I think you've made the case for including the feature, but I'd like to see a sandbox version that doesn't pollute <var>parseLink</var>, so as to uphold the [[single-responsibility principle]]. I'll poke at making a version like that; the main annoyance is that I'll probably need to extract the functionality that does the <code><nowiki>formatting §nbsp;for display</nowiki></code> to separate it from the part that makes a <code><nowiki>[[:link]]</nowiki></code>. I'll probably work on that, oh, next year (remember the current date). ;) <span style="white-space:nowrap;">{&#123;[[User:Nihiltres|<span style="color:#233D7A;">Nihiltres</span>]]&#8202;&#124;[[User talk:Nihiltres|talk]]&#8202;&#124;[[Special:Contributions/Nihiltres|edits]]}&#125;</span> 20:31, 31 December 2021 (UTC)
local function removeInitialColon(s)
: Deactivating edit request since I see no need for an uninvolved template editor to do anything here (Nihiltres is an admin and can make then change themselves once they are satisfied) [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 05:16, 1 January 2022 (UTC)
-- Removes the initial colon from a string, if present.
::{{small|Thanks. [[User:Psiĥedelisto|Psiĥedelisto]] ([[User talk:Psiĥedelisto|talk]] • [[Special:Contributions/Psiĥedelisto|contribs]]) <sup>please ''always'' [[Help:Notifications|ping]]!</sup> 10:11, 4 January 2022 (UTC)}}
return s:match('^:?(.*)')
:{{re|Psiĥedelisto}} I've implemented a version that's a bit cleaner. Would you please review it? It passes extant tests, but I think that we could probably add a few more to make certain edge-case behaviours more explicit. <span style="white-space:nowrap;">{&#123;[[User:Nihiltres|<span style="color:#233D7A;">Nihiltres</span>]]&#8202;&#124;[[User talk:Nihiltres|talk]]&#8202;&#124;[[Special:Contributions/Nihiltres|edits]]}&#125;</span> 22:53, 1 January 2022 (UTC)
end
::{{re|Nihiltres}} Only had one suggestion, to use [[Module:Error]]. [https://en.wikipedia.org/w/index.php?title=Module%3AFormat_link%2Fsandbox&type=revision&diff=1063683884&oldid=1063220129]. Looks good!~ [[User:Psiĥedelisto|Psiĥedelisto]] ([[User talk:Psiĥedelisto|talk]] • [[Special:Contributions/Psiĥedelisto|contribs]]) <sup>please ''always'' [[Help:Notifications|ping]]!</sup> 10:11, 4 January 2022 (UTC)
 
:::{{re|Psiĥedelisto}} Great, I'll make the update as soon as I've got the time to also update the documentation with all the new stuff. :) <span style="white-space:nowrap;">{&#123;[[User:Nihiltres|<span style="color:#233D7A;">Nihiltres</span>]]&#8202;&#124;[[User talk:Nihiltres|talk]]&#8202;&#124;[[Special:Contributions/Nihiltres|edits]]}&#125;</span> 18:07, 4 January 2022 (UTC)
local function maybeItalicize(s, shouldItalicize)
-- Italicize s if s is a string and the shouldItalicize parameter is true.
if s and shouldItalicize then
return '<i>' .. s .. '</i>'
else
return s
end
end
 
local function parseLink(link)
-- Parse a link and return a table with the link's components.
-- These components are:
-- - link: the link, stripped of any initial colon (always present)
-- - page: the page name (always present)
-- - section: the page name (may be nil)
-- - display: the display text, if manually entered after a pipe (may be nil)
link = removeInitialColon(link)
 
-- Find whether a faux display value has been added with the {{!}} magic
-- word.
local prePipe, display = link:match('^(.-)|(.*)$')
link = prePipe or link
 
-- Find the page, if it exists.
-- For links like [[#Bar]], the page will be nil.
local preHash, postHash = link:match('^(.-)#(.*)$')
local page
if not preHash then
-- We have a link like [[Foo]].
page = link
elseif preHash ~= '' then
-- We have a link like [[Foo#Bar]].
page = preHash
end
 
-- Find the section, if it exists.
local section
if postHash and postHash ~= '' then
section = postHash
end
return {
link = link,
page = page,
section = section,
display = display,
}
end
 
local function formatDisplay(parsed, options)
-- Formats a display string based on a parsed link table (matching the
-- output of parseLink) and an options table (matching the input options for
-- _formatLink).
local page = maybeItalicize(parsed.page, options.italicizePage)
local section = maybeItalicize(parsed.section, options.italicizeSection)
if (not section) then
return page
elseif (not page) then
return mw.ustring.format('§&nbsp;%s', section)
else
return mw.ustring.format('%s §&nbsp;%s', page, section)
end
end
 
local function missingArgError(target)
mError = require('Module:Error')
return mError.error{message =
'Error: no link or target specified! ([[' .. target .. '#Errors|help]])'
}
end
 
--------------------------------------------------------------------------------
-- Main functions
--------------------------------------------------------------------------------
 
function p.formatLink(frame)
-- The formatLink export function, for use in templates.
yesno = require('Module:Yesno')
local args = getArgs(frame)
local link = args[1] or args.link
local target = args[3] or args.target
if not (link or target) then
return missingArgError('Template:Format link')
end
 
return p._formatLink{
link = link,
display = args[2] or args.display,
target = target,
italicizePage = yesno(args.italicizepage),
italicizeSection = yesno(args.italicizesection),
categorizeMissing = args.categorizemissing
}
end
 
function p._formatLink(options)
-- The formatLink export function, for use in modules.
checkType('_formatLink', 1, options, 'table')
local function check(key, expectedType) --for brevity
checkTypeForNamedArg(
'_formatLink', key, options[key], expectedType or 'string', true
)
end
check('link')
check('display')
check('target')
check('italicizePage', 'boolean')
check('italicizeSection', 'boolean')
check('categorizeMissing')
 
-- Normalize link and target and check that at least one is present
if options.link == '' then options.link = nil end
if options.target == '' then options.target = nil end
if not (options.link or options.target) then
return missingArgError('Module:Format link')
end
 
local parsed = parseLink(options.link)
local display = options.display or parsed.display
local catMissing = options.categorizeMissing
local category = ''
 
-- Find the display text
if not display then display = formatDisplay(parsed, options) end
 
-- Handle the target option if present
if options.target then
local parsedTarget = parseLink(options.target)
parsed.link = parsedTarget.link
parsed.page = parsedTarget.page
end
 
-- Test if page exists if a diagnostic category is specified
if catMissing and (mw.ustring.len(catMissing) > 0) then
local title = nil
if parsed.page then title = mw.title.new(parsed.page) end
if title and (not title.isExternal) then
local success, exists = pcall(function() return title.exists end)
if success and not exists then
category = mw.ustring.format('[[Category:%s]]', catMissing)
end
end
end
-- Format the result as a link
if parsed.link == display then
return mw.ustring.format('[[:%s]]%s', parsed.link, category)
else
return mw.ustring.format('[[:%s|%s]]%s', parsed.link, display, category)
end
end
 
--------------------------------------------------------------------------------
-- Derived convenience functions
--------------------------------------------------------------------------------
 
function p.formatPages(options, pages)
-- Formats an array of pages using formatLink and the given options table,
-- and returns it as an array. Nil values are not allowed.
local ret = {}
for i, page in ipairs(pages) do
ret[i] = p._formatLink{
link = page,
categorizeMissing = options.categorizeMissing,
italicizePage = options.italicizePage,
italicizeSection = options.italicizeSection
}
end
return ret
end
 
return p

Latest revision as of 16:57, 26 December 2025

Template:Used in system Template:Module rating

This module, migrated from origins in Module:Hatnote, provides functionality for formatting links for display, including that powering the {{format link}} template.

It can pretty-format section links with the section symbol ("§") and appropriate whitespace, it automatically escapes category and file names with the colon trick, and includes functionality for italicizing the page or section name, and for detecting and categorizing results that produce red links.

Use from wikitext

The functions in this module cannot be used directly from #invoke, and must be used through templates instead. Please see Template:Format link for documentation on usage of that template.

Use from other Lua modules

To load this module from another Lua module, use the following code:

local mFormatLink = require('Module:Format link')

You can then use the functions as documented below.

_formatLink

mFormatLink._formatLink{
    link = 'Link',
    display = 'Display',
    target = 'Target',
    italicizePage = true,
    italicizeSection = true,
    categorizeMissing = 'Pages using formatted red links'
}

Formats link as a wikilink. Categories and files are automatically escaped with the colon trick, and links to sections are automatically formatted as page § section, rather than the MediaWiki default of page#section.

Several options modify the output:

  • If the display value is present, then it will be used as a display value. Any manual piping (using the {{!}} magic word or similar) present in link will be overridden by the display value if present.
  • If the target value is present, then it will override link as a target, but the result will still be displayed using either the value from display, or the result of formatting link.
  • If italicizePage is true, then the page portion of the link is italicized if present.
  • If italicizeSection is true, then the section portion of the link is italicized if present.
  • If categorizeMissing is a non-empty string, then that value is used as a category name, and that category will be applied if the resulting target of the link (no matter whether through link or through target) doesn't exist.
Examples
mFormatLink._formatLink{link = 'Foo#Bar'} → [[:Foo#Bar|Foo §&nbsp;Bar]] → Template:Format link
mFormatLink._formatLink{link = 'Baz', display = 'Qux'} → [[:Baz|Qux]] → Template:Format link
mFormatLink._formatLink{link = 'Foo|Bar', display = 'Baz'} → [[:Foo|Baz]] → Template:Format link
mFormatLink._formatLink{link = '#Foo', target = 'Example#Foo'} → [[:Example#Foo|§&nbsp;Foo]] → Template:Format link
mFormatLink._formatLink{link = 'The Lord of the Rings#Plot', italicizePage = true} → [[:The Lord of the Rings#Plot|''The Lord of the Rings'' §&nbsp;Plot]] → Template:Format link
mFormatLink._formatLink{link = 'Cybercrime Prevention Act of 2012#Disini v. Secretary of Justice', italicizeSection = true} → [[:Cybercrime Prevention Act of 2012#Disini v. Secretary of Justice|Cybercrime Prevention Act of 2012 §&nbsp;''Disini v. Secretary of Justice'']] → Template:Format link
mFormatLink._formatLink{link = 'Nonexistent page', categorizeMissing = 'Example'} → [[:Nonexistent page]][[Category:Example]] → Template:Format link
mFormatLink._formatLink{link = 'Existing', categorizeMissing = 'Example'} → [[:Existing]] → Template:Format link

formatPages

mFormatLink.formatPages(options, pages)

This derived function is useful for lists that format many links. It formats an array of pages using the _formatLink function, and returns the result as an array. Options in the options table are applied, and use the same names as the options for _formatLink.

Example
Template:CodeTemplate:Code

Errors

If _formatLink is used and neither a link nor a target argument is provided, then the module will produce an error message instead of its usual output, as it cannot then produce valid output.

You can solve this error by providing appropriate parameters to _formatLink, or you may want to ensure that a more descriptive error is provided by a downstream template or module when it would otherwise call _formatLink with inadequate arguments.


--------------------------------------------------------------------------------
-- Format link
--
-- Makes a wikilink from the given link and display values. Links are escaped
-- with colons if necessary, and links to sections are detected and displayed
-- with " § " as a separator rather than the standard MediaWiki "#". Used in
-- the {{format link}} template.
--------------------------------------------------------------------------------
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local mArguments -- lazily initialise [[Module:Arguments]]
local mError -- lazily initialise [[Module:Error]]
local yesno -- lazily initialise [[Module:Yesno]]

local p = {}

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

local function getArgs(frame)
	-- Fetches the arguments from the parent frame. Whitespace is trimmed and
	-- blanks are removed.
	mArguments = require('Module:Arguments')
	return mArguments.getArgs(frame, {parentOnly = true})
end

local function removeInitialColon(s)
	-- Removes the initial colon from a string, if present.
	return s:match('^:?(.*)')
end

local function maybeItalicize(s, shouldItalicize)
	-- Italicize s if s is a string and the shouldItalicize parameter is true.
	if s and shouldItalicize then
		return '<i>' .. s .. '</i>'
	else
		return s
	end
end

local function parseLink(link)
	-- Parse a link and return a table with the link's components.
	-- These components are:
	-- - link: the link, stripped of any initial colon (always present)
	-- - page: the page name (always present)
	-- - section: the page name (may be nil)
	-- - display: the display text, if manually entered after a pipe (may be nil)
	link = removeInitialColon(link)

	-- Find whether a faux display value has been added with the {{!}} magic
	-- word.
	local prePipe, display = link:match('^(.-)|(.*)$')
	link = prePipe or link

	-- Find the page, if it exists.
	-- For links like [[#Bar]], the page will be nil.
	local preHash, postHash = link:match('^(.-)#(.*)$')
	local page
	if not preHash then
		-- We have a link like [[Foo]].
		page = link
	elseif preHash ~= '' then
		-- We have a link like [[Foo#Bar]].
		page = preHash
	end

	-- Find the section, if it exists.
	local section
	if postHash and postHash ~= '' then
		section = postHash
	end
	
	return {
		link = link,
		page = page,
		section = section,
		display = display,
	}
end

local function formatDisplay(parsed, options)
	-- Formats a display string based on a parsed link table (matching the
	-- output of parseLink) and an options table (matching the input options for
	-- _formatLink).
	local page = maybeItalicize(parsed.page, options.italicizePage)
	local section = maybeItalicize(parsed.section, options.italicizeSection)
	if (not section) then
		return page
	elseif (not page) then
		return mw.ustring.format('§&nbsp;%s', section)
	else
		return mw.ustring.format('%s §&nbsp;%s', page, section)
	end
end

local function missingArgError(target)
	mError = require('Module:Error')
	return mError.error{message =
		'Error: no link or target specified! ([[' .. target .. '#Errors|help]])'
	}
end

--------------------------------------------------------------------------------
-- Main functions
--------------------------------------------------------------------------------

function p.formatLink(frame)
	-- The formatLink export function, for use in templates.
	yesno = require('Module:Yesno')
	local args = getArgs(frame)
	local link = args[1] or args.link
	local target = args[3] or args.target
	if not (link or target) then
		return missingArgError('Template:Format link')
	end

	return p._formatLink{
		link = link,
		display = args[2] or args.display,
		target = target,
		italicizePage = yesno(args.italicizepage),
		italicizeSection = yesno(args.italicizesection),
		categorizeMissing = args.categorizemissing
	}
end

function p._formatLink(options)
	-- The formatLink export function, for use in modules.
	checkType('_formatLink', 1, options, 'table')
	local function check(key, expectedType) --for brevity
		checkTypeForNamedArg(
			'_formatLink', key, options[key], expectedType or 'string', true
		)
	end
	check('link')
	check('display')
	check('target')
	check('italicizePage', 'boolean')
	check('italicizeSection', 'boolean')
	check('categorizeMissing')

	-- Normalize link and target and check that at least one is present
	if options.link == '' then options.link = nil end
	if options.target == '' then options.target = nil end
	if not (options.link or options.target) then
		return missingArgError('Module:Format link')
	end

	local parsed = parseLink(options.link)
	local display = options.display or parsed.display
	local catMissing = options.categorizeMissing
	local category = ''

	-- Find the display text
	if not display then display = formatDisplay(parsed, options) end

	-- Handle the target option if present
	if options.target then
		local parsedTarget = parseLink(options.target)
		parsed.link = parsedTarget.link
		parsed.page = parsedTarget.page
	end

	-- Test if page exists if a diagnostic category is specified
	if catMissing and (mw.ustring.len(catMissing) > 0) then
		local title = nil
		if parsed.page then title = mw.title.new(parsed.page) end
		if title and (not title.isExternal) then
			local success, exists = pcall(function() return title.exists end)
			if success and not exists then
				category = mw.ustring.format('[[Category:%s]]', catMissing)
			end
		end
	end
	
	-- Format the result as a link
	if parsed.link == display then
		return mw.ustring.format('[[:%s]]%s', parsed.link, category)
	else
		return mw.ustring.format('[[:%s|%s]]%s', parsed.link, display, category)
	end
end

--------------------------------------------------------------------------------
-- Derived convenience functions
--------------------------------------------------------------------------------

function p.formatPages(options, pages)
	-- Formats an array of pages using formatLink and the given options table,
	-- and returns it as an array. Nil values are not allowed.
	local ret = {}
	for i, page in ipairs(pages) do
		ret[i] = p._formatLink{
			link = page,
			categorizeMissing = options.categorizeMissing,
			italicizePage = options.italicizePage,
			italicizeSection = options.italicizeSection
		}
	end
	return ret
end

return p