http://proplan.55bet-pro.com/wiki/index.php?action=history&feed=atom&title=M%C3%B3dulo%3AFormat_link Módulo:Format link - Histórico de revisão-55BET Pro 2026-03-15T15:58:36Z Histórico de revisões para esta página neste wiki MediaWiki 1.42.7 http://proplan.55bet-pro.com/wiki/index.php?title=M%C3%B3dulo:Format_link&diff=103&oldid=prev Módulo:Format link - Histórico de revisão-55BET Pro 2024-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> Henryzord http://proplan.55bet-pro.com/wiki/index.php?title=M%C3%B3dulo:Format_link&diff=102&oldid=prev Módulo:Format link - Histórico de revisão-55BET Pro 2022-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 &quot; § &quot; as a separator rather than the standard MediaWiki &quot;#&quot;. Used in<br /> -- the {{format link}} template.<br /> --------------------------------------------------------------------------------<br /> local libraryUtil = require(&#039;libraryUtil&#039;)<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(&#039;Module:Arguments&#039;)<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(&#039;^:?(.*)&#039;)<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 &#039;&lt;i&gt;&#039; .. s .. &#039;&lt;/i&gt;&#039;<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&#039;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(&#039;^(.-)|(.*)$&#039;)<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(&#039;^(.-)#(.*)$&#039;)<br /> local page<br /> if not preHash then<br /> -- We have a link like [[Foo]].<br /> page = link<br /> elseif preHash ~= &#039;&#039; 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 ~= &#039;&#039; 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(&#039;§&amp;nbsp;%s&#039;, section)<br /> else<br /> return mw.ustring.format(&#039;%s §&amp;nbsp;%s&#039;, page, section)<br /> end<br /> end<br /> <br /> local function missingArgError(target)<br /> mError = require(&#039;Module:Error&#039;)<br /> return mError.error{message =<br /> &#039;Error: no link or target specified! ([[&#039; .. target .. &#039;#Errors|help]])&#039;<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(&#039;Module:Yesno&#039;)<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(&#039;Template:Format link&#039;)<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(&#039;_formatLink&#039;, 1, options, &#039;table&#039;)<br /> local function check(key, expectedType) --for brevity<br /> checkTypeForNamedArg(<br /> &#039;_formatLink&#039;, key, options[key], expectedType or &#039;string&#039;, true<br /> )<br /> end<br /> check(&#039;link&#039;)<br /> check(&#039;display&#039;)<br /> check(&#039;target&#039;)<br /> check(&#039;italicizePage&#039;, &#039;boolean&#039;)<br /> check(&#039;italicizeSection&#039;, &#039;boolean&#039;)<br /> check(&#039;categorizeMissing&#039;)<br /> <br /> -- Normalize link and target and check that at least one is present<br /> if options.link == &#039;&#039; then options.link = nil end<br /> if options.target == &#039;&#039; then options.target = nil end<br /> if not (options.link or options.target) then<br /> return missingArgError(&#039;Module:Format link&#039;)<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 = &#039;&#039;<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) &gt; 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(&#039;[[Category:%s]]&#039;, 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(&#039;[[:%s]]%s&#039;, parsed.link, category)<br /> else<br /> return mw.ustring.format(&#039;[[:%s|%s]]%s&#039;, 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