モジュール:Noredirect

半永久的に拡張半保護されているモジュール
モジュールの解説[表示] [編集] [履歴] [キャッシュを破棄]

非転送リンクを生成するモジュール。

使い方

モジュールによる呼び出し

local noredirect = require('Module:Noredirect').noredirect
local link = noredirect('ページ名', '表示文字列') -- 第2引数省略可

テンプレートによる呼び出し

{{#invoke:Noredirect|Main|1={{{1|}}}|2={{{2|}}}}}

関連項目

local p = {}

-- Return a blue noredirect wikilink for an existing page, a red wikilink for a non-existing page
function p.noredirect(pagetitle, label)

    -- Evaluate pagetitle
    if pagetitle == nil or pagetitle == '' then
        return error('ページ名が指定されていません')
    end
    pagetitle = pagetitle:gsub('^%[*', ''):gsub('%]*$', '') -- Remove leading/trailing brackets to prevent errors
    pagetitle = pagetitle:gsub('_', ' ') -- Replace underscores with spaces because this variable will be used for the link's label
    pagetitle = pagetitle:gsub('^%s*', ''):gsub('%s*$', '') -- trim
    if pagetitle == '' then
        return error('ページ名が不正です')
    elseif pagetitle:find('[<>[%]{}|]') then
        return error('ページ名に使用できない記号が含まれています( < > [ ] { } | )')
    end

    -- Create link
    local title = mw.title.new(pagetitle)
    if label == nil or label == '' then
        label = pagetitle -- Don't use title.prefixedText here because the namespace alias (if the title contains any) is replaced by its canonical one
    end
    if title.exists then
        local link = '[' .. tostring(mw.uri.fullUrl(title.prefixedText, {redirect = 'no'})) .. ' ' .. label .. ']'
        link = '<span class="plainlinks">' .. link .. '</span>'
        return link
    else
        return '[[:' .. title.prefixedText .. '|' .. label .. ']]'
    end

end

-- Main package function
function p.Main(frame)
    return p.noredirect(frame.args['1'], frame.args['2'])
end

return p