לדלג לתוכן

יחידה:קישור

מתוך ויקיפדיה, האנציקלופדיה החופשית

יחידה זו מיועדת לפונקציות שימושיות לקישורים.


פונקציות

linkIfExists

פונקציה שיוצרת קישור, רק אם דף קיים. הפונקציה יכולה ליצור את הקישור רק אם הדף אינו דף פירושונים.

הפונקציה לא תיצור קישור לדף באורך גדול מ70 תווים מסיבות של ביצועים.

isDismabig

פונקציה לבדיקה האם הטקסט הוא קישור.

בדיקות


local linkUtilities = {}

-- target page should be of type title
function linkUtilities.checkIsDismabig( targetPage )
	if targetPage == nil then
		return false
	end
	if targetPage.isDisambiguationPage == nil then
		targetPage = mw.title.new(targetPage)
	end
	return targetPage.isDisambiguationPage
end

function linkUtilities._linkIfExists(targetPage, linkText, noDisambig, currentPage) 
	-- if it already contains link - skip
	if mw.ustring.match(targetPage, '%[%[') then
		return targetPage
	end
	if linkText == '' or linkText == nil then
		linkText = targetPage
    	-- remove parentheses 
    	linkText= mw.ustring.gsub(linkText, " *[(].*[)]","")
	end
	-- skip too long string - unlikely to be link. using cutoff of 70
	if mw.ustring.len( targetPage ) >= 70 then
		return linkText
	end
	
	if targetPage == currentPage then return linkText	end
	local targetTitle = mw.title.new( targetPage )
	
	-- target title may be nil if the targetPage is invalid title
	if targetTitle ==nil or not targetTitle.exists then
		return linkText
	end
	
	if noDisambig and linkUtilities.checkIsDismabig(targetTitle) then
		return linkText
	end	
	return mw.ustring.format('[[%s|%s]]', targetPage, linkText)
end

-- check if target is disambig page
function linkUtilities.isDismabig( frame )
	local targetPage = frame.args['1']
	if targetPage == '' or targetPage == nil then
		targetPage = mw.title.getCurrentTitle()
	else
		targetPage = mw.title.new(targetPage)
	end
	return linkUtilities.checkIsDismabig(targetPage)
end

function linkUtilities.linkIfExists( frame )
	local targetPage = frame.args['1']
	local linkText = frame.args['2']
	local noDisambig = frame.args['noDisambig']~=nil and frame.args['noDisambig']~=''
	local currentPage = mw.title.getCurrentTitle().text
	return linkUtilities._linkIfExists(targetPage, linkText, noDisambig ,currentPage)
end

return linkUtilities