コンテンツにスキップ

モジュール:NamespaceConversion

半永久的に拡張半保護されているモジュール
モジュールの解説[作成]
local p = {}

---@param str string
---@return string, number
local function clean(str)
    -- Remove any of following directional markers and trim leading/trailing spaces
	-- LRM : LEFT-TO-RIGHT MARK (U+200E)         : hex e2 80 8e = 226 128 142
	-- LRE : LEFT-TO-RIGHT EMBEDDING (U+202A)    : hex e2 80 aa = 226 128 170
	-- PDF : POP DIRECTIONAL FORMATTING (U+202C) : hex e2 80 ac = 226 128 172
	-- This is required for MediaWiki:Blockedtext message.
	return str:gsub('\226\128[\142\170\172]', ''):gsub("^%s*%[*%s*(.-)%s*%]*%s*$", "%1")
end

---@param pagetitle string Page title with a namespace prefix
---@param toTalk boolean? Whether to convert the page title to the talk page title (if false, converted to the main page title)
---@return string
function p.convertNamespace(pagetitle, toTalk)
    pagetitle = clean(pagetitle)
    toTalk = not not toTalk
    local success, title = pcall(mw.title.new, pagetitle)
    if not title then
        error(string.format('「%s」は不正なページ名です', pagetitle))
    elseif not title.canTalk then
        return title.prefixedText
    elseif toTalk then
        return title.talkPageTitle.prefixedText
    else
        return title.subjectPageTitle.prefixedText
    end
end

function p.main(frame)
    local args = frame.args
    if not args.pagetitle or args.pagetitle == '' then
        error('ページ名が指定されていません')
    else
        return p.convertNamespace(args.pagetitle, require('Module:Yesno')(args.toTalk))
    end
end

return p