http://proplan.55bet-pro.com/wiki/index.php?action=history&feed=atom&title=M%C3%B3dulo%3AFormat_linkMódulo:Format link - Histórico de revisão-55BET Pro2026-03-15T15:58:36ZHistórico de revisões para esta página neste wikiMediaWiki 1.42.7http://proplan.55bet-pro.com/wiki/index.php?title=M%C3%B3dulo:Format_link&diff=103&oldid=prevMódulo:Format link - Histórico de revisão-55BET Pro2024-06-27T11:11:55Z<p>uma edição</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<tr class="diff-title" lang="pt-BR">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Edição anterior</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Edição das 08h11min de 27 de junho de 2024</td>
</tr><tr><td colspan="2" class="diff-notice" lang="pt-BR"><div class="mw-diff-empty">(Sem diferença)</div>
</td></tr></table>Henryzordhttp://proplan.55bet-pro.com/wiki/index.php?title=M%C3%B3dulo:Format_link&diff=102&oldid=prevMódulo:Format link - Histórico de revisão-55BET Pro2022-10-04T13:37:11Z<p>Avoid Lua erroring when we run out of expensive parser function calls</p>
<p><b>Página nova</b></p><div>--------------------------------------------------------------------------------<br />
-- Format link<br />
--<br />
-- Makes a wikilink from the given link and display values. Links are escaped<br />
-- with colons if necessary, and links to sections are detected and displayed<br />
-- with " § " as a separator rather than the standard MediaWiki "#". Used in<br />
-- the {{format link}} template.<br />
--------------------------------------------------------------------------------<br />
local libraryUtil = require('libraryUtil')<br />
local checkType = libraryUtil.checkType<br />
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg<br />
local mArguments -- lazily initialise [[Module:Arguments]]<br />
local mError -- lazily initialise [[Module:Error]]<br />
local yesno -- lazily initialise [[Module:Yesno]]<br />
<br />
local p = {}<br />
<br />
--------------------------------------------------------------------------------<br />
-- Helper functions<br />
--------------------------------------------------------------------------------<br />
<br />
local function getArgs(frame)<br />
-- Fetches the arguments from the parent frame. Whitespace is trimmed and<br />
-- blanks are removed.<br />
mArguments = require('Module:Arguments')<br />
return mArguments.getArgs(frame, {parentOnly = true})<br />
end<br />
<br />
local function removeInitialColon(s)<br />
-- Removes the initial colon from a string, if present.<br />
return s:match('^:?(.*)')<br />
end<br />
<br />
local function maybeItalicize(s, shouldItalicize)<br />
-- Italicize s if s is a string and the shouldItalicize parameter is true.<br />
if s and shouldItalicize then<br />
return '<i>' .. s .. '</i>'<br />
else<br />
return s<br />
end<br />
end<br />
<br />
local function parseLink(link)<br />
-- Parse a link and return a table with the link's components.<br />
-- These components are:<br />
-- - link: the link, stripped of any initial colon (always present)<br />
-- - page: the page name (always present)<br />
-- - section: the page name (may be nil)<br />
-- - display: the display text, if manually entered after a pipe (may be nil)<br />
link = removeInitialColon(link)<br />
<br />
-- Find whether a faux display value has been added with the {{!}} magic<br />
-- word.<br />
local prePipe, display = link:match('^(.-)|(.*)$')<br />
link = prePipe or link<br />
<br />
-- Find the page, if it exists.<br />
-- For links like [[#Bar]], the page will be nil.<br />
local preHash, postHash = link:match('^(.-)#(.*)$')<br />
local page<br />
if not preHash then<br />
-- We have a link like [[Foo]].<br />
page = link<br />
elseif preHash ~= '' then<br />
-- We have a link like [[Foo#Bar]].<br />
page = preHash<br />
end<br />
<br />
-- Find the section, if it exists.<br />
local section<br />
if postHash and postHash ~= '' then<br />
section = postHash<br />
end<br />
<br />
return {<br />
link = link,<br />
page = page,<br />
section = section,<br />
display = display,<br />
}<br />
end<br />
<br />
local function formatDisplay(parsed, options)<br />
-- Formats a display string based on a parsed link table (matching the<br />
-- output of parseLink) and an options table (matching the input options for<br />
-- _formatLink).<br />
local page = maybeItalicize(parsed.page, options.italicizePage)<br />
local section = maybeItalicize(parsed.section, options.italicizeSection)<br />
if (not section) then<br />
return page<br />
elseif (not page) then<br />
return mw.ustring.format('§&nbsp;%s', section)<br />
else<br />
return mw.ustring.format('%s §&nbsp;%s', page, section)<br />
end<br />
end<br />
<br />
local function missingArgError(target)<br />
mError = require('Module:Error')<br />
return mError.error{message =<br />
'Error: no link or target specified! ([[' .. target .. '#Errors|help]])'<br />
}<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Main functions<br />
--------------------------------------------------------------------------------<br />
<br />
function p.formatLink(frame)<br />
-- The formatLink export function, for use in templates.<br />
yesno = require('Module:Yesno')<br />
local args = getArgs(frame)<br />
local link = args[1] or args.link<br />
local target = args[3] or args.target<br />
if not (link or target) then<br />
return missingArgError('Template:Format link')<br />
end<br />
<br />
return p._formatLink{<br />
link = link,<br />
display = args[2] or args.display,<br />
target = target,<br />
italicizePage = yesno(args.italicizepage),<br />
italicizeSection = yesno(args.italicizesection),<br />
categorizeMissing = args.categorizemissing<br />
}<br />
end<br />
<br />
function p._formatLink(options)<br />
-- The formatLink export function, for use in modules.<br />
checkType('_formatLink', 1, options, 'table')<br />
local function check(key, expectedType) --for brevity<br />
checkTypeForNamedArg(<br />
'_formatLink', key, options[key], expectedType or 'string', true<br />
)<br />
end<br />
check('link')<br />
check('display')<br />
check('target')<br />
check('italicizePage', 'boolean')<br />
check('italicizeSection', 'boolean')<br />
check('categorizeMissing')<br />
<br />
-- Normalize link and target and check that at least one is present<br />
if options.link == '' then options.link = nil end<br />
if options.target == '' then options.target = nil end<br />
if not (options.link or options.target) then<br />
return missingArgError('Module:Format link')<br />
end<br />
<br />
local parsed = parseLink(options.link)<br />
local display = options.display or parsed.display<br />
local catMissing = options.categorizeMissing<br />
local category = ''<br />
<br />
-- Find the display text<br />
if not display then display = formatDisplay(parsed, options) end<br />
<br />
-- Handle the target option if present<br />
if options.target then<br />
local parsedTarget = parseLink(options.target)<br />
parsed.link = parsedTarget.link<br />
parsed.page = parsedTarget.page<br />
end<br />
<br />
-- Test if page exists if a diagnostic category is specified<br />
if catMissing and (mw.ustring.len(catMissing) > 0) then<br />
local title = nil<br />
if parsed.page then title = mw.title.new(parsed.page) end<br />
if title and (not title.isExternal) then<br />
local success, exists = pcall(function() return title.exists end)<br />
if success and not exists then<br />
category = mw.ustring.format('[[Category:%s]]', catMissing)<br />
end<br />
end<br />
end<br />
<br />
-- Format the result as a link<br />
if parsed.link == display then<br />
return mw.ustring.format('[[:%s]]%s', parsed.link, category)<br />
else<br />
return mw.ustring.format('[[:%s|%s]]%s', parsed.link, display, category)<br />
end<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Derived convenience functions<br />
--------------------------------------------------------------------------------<br />
<br />
function p.formatPages(options, pages)<br />
-- Formats an array of pages using formatLink and the given options table,<br />
-- and returns it as an array. Nil values are not allowed.<br />
local ret = {}<br />
for i, page in ipairs(pages) do<br />
ret[i] = p._formatLink{<br />
link = page,<br />
categorizeMissing = options.categorizeMissing,<br />
italicizePage = options.italicizePage,<br />
italicizeSection = options.italicizeSection<br />
}<br />
end<br />
return ret<br />
end<br />
<br />
return p</div>infobox>Pppery