Module:Ilh

Documentation for this module may be created at Module:Ilh/doc

Documentation for this module may be created at Module:Ilh/doc

local ilh = {}
local getArgs
local yesno = require('Module:Yesno')

local args
--local frameArgs
local COMMON_TAIL='</span>'
local MODEL={
	 frame_head='<span class="ilh-all %s" data-orig-title="%s" data-lang-code="%s" data-lang-name="%s" data-foreign-title="%s">'
	,frame_tail=COMMON_TAIL
	,page_head='<span class="ilh-page">'
	,page_tail=COMMON_TAIL
	,comment_head='<span class="noprint ilh-comment">('
	,comment_tail=')'..COMMON_TAIL
	,lang_head='<span class="ilh-lang">'
	,lang_tail=COMMON_TAIL
	,colon='<span class="ilh-colon">:</span>'
	,link_head='<span class="ilh-link">'
	,link_body='-{[[:%s:%s|<span lang="%s" dir="auto">%s</span>]]}-'
	,link_tail=COMMON_TAIL
}
local clazz_pageExist_framehead='ilh-blue'
local TRA_CAT='[[Category:Pages with a blue link but without removing the internal link helper template]]'

function ilh.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	args = getArgs(frame, {parentFirst=true})
	
	return ilh._ilh(arg)
end

function ilh._ilh(arg)
	--frameArgs = getArgs(frame, {frameOnly=true})

	local context={}
	context["isMainPage"]=ilh.isMainPage()
	context["localPage"]=args[1]
	context["foreignPage"]=args[2] or args[1] --If {{{2}}} does not exist
	context["displayName"]=ilh.displayName(args)
	context["langCode"]=args["lang-code"]
	context["lang"]=args["lang"]
	context["nocat"]=yesno( args["nocat"] , false )

	context["isExist"]= (args["$exist$"] and args["$exist$"]==1) or ilh.isExist(context["localPage"])
	
	local curPage_obj=mw.title.getCurrentTitle()
	context["isNoCatWithNamespace"]=curPage_obj:inNamespaces(2,828) --User,Module
	--context["curPageNamespace"]=curPage_obj.namespace

	return (context["isMainPage"] and ilh.onlyLink(context)) or ilh.functionLink(context)
end

function ilh.onlyLink(context)
	return ( context["isExist"] and mw.ustring.format( '[[%s|%s]]', context["localPage"], context["displayName"] ) ) or context["displayName"]
end

function ilh.functionLink(context)
	context["_localPage"]=mw.ustring.gsub(context["localPage"],'"','&quot;')
	context["_foreignPage"]=mw.ustring.gsub(context["foreignPage"],'"','&quot;')
	local need_cat=
				   (not context["nocat"])
				   and
				   (not context["isNoCatWithNamespace"])
				   --[[not (
					   context["curPageNamespace"]==2 --User
					or context["curPageNamespace"]==828 --Module
				   )]]
	--mw.log(context["nocat"])
	--mw.log(context["curPageNamespace"])
	--mw.log(need_cat)

	local output_context={}
	table.insert(output_context,
					mw.ustring.format(MODEL.frame_head ,
						 (context["isExist"] and clazz_pageExist_framehead) or ''
						,context["_localPage"]
						,context["langCode"]
						,context["lang"]
						,context["_foreignPage"]
					)
				)
		table.insert(output_context,MODEL.page_head)
			table.insert(output_context,
							mw.ustring.format('[[:%s|%s]]' ,
								context["localPage"],context["displayName"]
							)
						)
		table.insert(output_context,MODEL.page_tail)
		if context["isExist"] then
			if need_cat then
				table.insert(output_context,TRA_CAT)
			end
		else
			table.insert(output_context,MODEL.comment_head)
				table.insert(output_context,MODEL.lang_head)
					table.insert(output_context,context["lang"])
				table.insert(output_context,MODEL.lang_tail)

				table.insert(output_context,MODEL.colon)

				table.insert(output_context,MODEL.link_head)
					table.insert(output_context,
									mw.ustring.format(MODEL.link_body,
										 context["langCode"]
										,(context["foreignPage"] or context["localPage"])
										,context["langCode"]
										,(context["foreignPage"] or context["localPage"])
									)
								)
				table.insert(output_context,MODEL.link_tail)
			table.insert(output_context,MODEL.comment_tail)
		end
	table.insert(output_context,MODEL.frame_tail)

	return table.concat(output_context,"")
end

function ilh.displayName(args)
	local _d=args["d"]
	local _1=args["1"]
	local _3=args["3"]
	local dpN1=_3 or _d
	return (dpN1 and {dpN1} or {_1})[1]
end

--The following needs a more efficient implementation

--Confirm homepage
--Use mw information to get the homepage name
--Use the language library to obtain the default language code (zh) of this site to determine the corresponding language of the information and obtain the full homepage name
---Because other zh sub-languages have only names and no namespace, they can be recognized, but it is troublesome
--Then determine whether the current page and the home page are consistent
---Plan to do redirect judgment, but no need
function ilh.isMainPage()
	local mainpage_msgobj=mw.message.new('Mainpage')
	mainpage_msgobj=mainpage_msgobj:inLanguage(mw.getContentLanguage():getCode())
	local mainPage_obj=mw.title.makeTitle(0,mainpage_msgobj:plain())
	local curpage_obj=mw.title.getCurrentTitle()
	--local curpage_redirectFrom_obj=curpage_obj.redirectTarget
	--[[if curpage_redirectFrom_obj ~=false then
		curpage_obj=curpage_redirectFrom_obj
	end]]
	return mw.title.equals(mainPage_obj,curpage_obj) --and curpage_obj.namespace==4
end

--Make sure the page exists
---exists is a high-cost method that requires a more efficient implementation
--Packing method with protection
--Because exists and parser function ifexist are both high-cost methods
--And if ifexist reaches the limit, the operation that returns the result as false by default
--And exists will directly throw an error and interrupt execution
--Therefore, the package will be called accordingly to suppress the throwing errors of exists. According to the concept of ifexist, it returns false
--Under normal circumstances, everything is normal
function ilh.isExist(pageName)
	local execStatus,result=pcall(ilh._isExist,pageName)
	
	if execStatus then
		return result
	else
		return false
	end
end
--real method
function ilh._isExist(pageName)
	local localPage_obj=mw.title.makeTitle(0,pageName)
	return localPage_obj.exists
end
--end

return ilh


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.