Module:Link if exists

Implements {{Link if exists}} and {{Auto link}}.


Implements {{Link if exists}} and {{Auto link}}.

Usage

{{#invoke:Link if exists|main}}

Arguments

1
title of page to link to
2
text to display
prefix
prepend this string to title
nsp
namespace for title, does not display
color
Color of text if no link is found
hide_display
If page does not exist, do not display anything



require('strict')

local p = {}

local function titleExistsUnprotected(titleObject)
    return titleObject.exists
end

-- test for title existing
-- if we get an error accessing titleObject.exists, assume it doesn't exist
local function titleExists(titleObject)
    local success, exists = pcall(titleExistsUnprotected, titleObject)
    return success and exists
end

function p._main(args)
    local title = args[1]
    if not title then
        return title
    end
    local display = args[2] or title
    title = args.prefix and args.prefix..':'..title or title
    local titleObject = mw.title.new(title, args.nsp)
    local result = ''
    if titleObject and titleExists(titleObject) then
       -- use prefix only if args[2] is empty/false
        display = args[2] or title
        result = result..'[['
        result = result..(titleObject.namespace ~= 0 and ':' or '')
        result = result..(titleObject.fullText ~= display and titleObject.fullText..'|' or '')
        result = result..display..']]'
    elseif not args.hide_display then
        result = result..(args.color and '<span style="color:'..args.color..'">' or '')
        result = result..display
        result = result..(args.color and '</span>' or '')
    end
    return result
end

function p.main(frame)
    local getArgs = require('Module:Arguments').getArgs
    local args = getArgs(frame)
    return p._main(args) or ""
end

-- implement entry points that accept limited arguments
local function filteredMain(args, acceptList)
   local filtered = {}
   filtered[1] = args[1]
   for _, key in ipairs(acceptList) do
       filtered[key] = args[key]
   end
   return p._main(filtered) or ""
end

-- implement [[Template:Link if exists]]
function p.lie(frame)
   local getArgs = require('Module:Arguments').getArgs
   local args = getArgs(frame)
   return filteredMain(args, {"nsp","prefix"})
end

-- implement [[Template:Auto link]]
function p.auto(frame)
   local getArgs = require('Module:Arguments').getArgs
   local args = getArgs(frame)
   return filteredMain(args, {2})
end

--implement [[Template:Category link if exists]]
function p.clie(frame)
  local getArgs = require('Module:Arguments').getArgs
  local args = getArgs(frame)
  args.nsp = 'Category'
  args.color = '#888'
  return filteredMain(args, {2, "nsp", "color", "hide_display"})
end

return p


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.