跳转到内容

模块:LinkTools

被永久保护的模块
维基百科,自由的百科全书
local p={}
--{{#invoke:LinkTools|navRedirect}}
function p.navRedirect(frame)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        args = frame.args
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local title = mw.text.trim(args[1] or args['1'] or '')
    local display = mw.text.trim(args[2] or args['2'] or '')
    local title_obj = mw.title.new(title)
    local title_target = title_obj
    local section = ''
    local title_sp = mw.text.trim(mw.ustring.sub(title, (({mw.ustring.find(title, '#')})[1]or 0)+1, -1) or '')
    if title_obj then
	    if  title_obj.isRedirect then
	    	title_target = title_obj.redirectTarget
	    end
	    if mw.text.trim(display) == '' then display = ((mw.text.trim(title_sp) == '') and title or title_sp) end
	    section = mw.text.trim(title_target.fragment or '')
	    local cur_title = mw.title.getCurrentTitle()
	    if (cur_title.namespace == title_target.namespace) and (cur_title.text == title_target.text)then
	    	local section_text = ''
	    	local section_name = mw.text.trim(title_target.fragment or '')
	    	if section_name ~= '' then
	    		section_text = "([[#" .. section_name .. "|章節]])"
	    	end
	    	return "'''"..display.."'''"..section_text
	    end
	    return "[["..title.."|"..display.."]]"
	else return ''
	end
end

function p.loadpage(page)
	local page_name = page
	if type(page) == type({}) then page_name = (page.args or page)[1]
	elseif type(page) == type(nil) then return ''
	elseif type(page) ~= type("string") then page_name = tostring(page) end
	page_name = tostring(page_name or '')
	if mw.text.trim(page_name) ~= '' then
		local title_obj = mw.title.new(page_name)
		pcall(title_obj.getContent, title_obj)
	end
	return ''
end

function p.linkExist(_title)
	local title = _title
	if type(_title) == type(0) then
		title = tostring(_title)
	elseif type(_title.args) == type({"table"}) then
		title = _title.args[1] or _title.args['1'] or ''
	elseif type(_title) == type({"table"}) then
		title = _title[1] or _title['1'] or ''
	end
	local number = tonumber(title)
	if number then
		if number >= -1 and number <= 101 then
			return ("[[%d]]"):format(number)
		end
	end
	local titleObj = mw.title.new(title)
	if titleObj then
		if titleObj:getContent() then
			local target = titleObj.redirectTarget or titleObj
			return ("[[%s]]"):format(title)
		end
	end
	return title
end

function p.linkExistNotDisambig(_title)
	local lib_pjc = require("Module:PJBSClass/main")
	local title = _title
	if type(_title) == type(0) then
		title = tostring(_title)
	elseif type(_title.args) == type({"table"}) then
		title = _title.args[1] or _title.args['1'] or ''
	elseif type(_title) == type({"table"}) then
		title = _title[1] or _title['1'] or ''
	end
	local number = tonumber(title)
	if number then
		if number >= -1 and number <= 101 then
			return ("[[%d]]"):format(number)
		end
	end
	local titleObj = mw.title.new(title)
	if titleObj then
		if titleObj:getContent() then
			local target = titleObj.redirectTarget or titleObj
			if lib_pjc.is_disambiguation(target.fullText) then
				return title
			else
				return ("[[%s]]"):format(title)
			end
		end
	end
	return title
end
	
return p