模組:Ilh
外观
![]() | 此模块被引用於約539,000個頁面。 為了避免造成大規模的影響,所有對此模块的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模块中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
本模块为{{Internal link helper}}的Lua实现。
本模块支持通过传入Lua调用参数代替模板调用参数。
local ilh = {}
local WikitextLC = require( 'Module:WikitextLC' )
local getArgs
local args
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:有蓝链却未移除内部链接助手模板的页面]]"
function ilh.main(frame)
if not getArgs then
getArgs = require('Module:Arguments').getArgs
end
args = getArgs(frame, {wrappers = 'Template:Internal link helper'})
local context={}
context["isMainPage"]=ilh.isMainPage()
context["localPage"]=args["1"]
context["foreignPage"]=args["2"]
context["displayName"]=ilh.displayName(args)
context["langCode"]=args["lang-code"]
context["lang"]=args["lang"]
context["nocat"]=(args["nocat"] and true)or false
context["isExist"]=ilh.isExist(context["localPage"])
local curPage_obj=mw.title.getTitle(0,mw.title.getCurrentTitle())
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"],'"','"')
context["_foreignPage"]=mw.ustring.gsub(context["foreignPage"],'"','"')
local need_cat= not(
context["nocat"]
or context["curPageNamespace"]==2 --User
or context["curPageNamespace"]==828 --Module
)
local exist_cat= needcat and context["isExist"]
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)
table.insert(output_context,MODEL.comment_head)
table.insert(output_context,MODEL.lang_head)
table.insert(output_context,context["lang"])
if exist_cat then
table.insert(output_context,TRA_CAT)
end
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)
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"]
return (_d and {_d} or {_1})[1]
end
--以下需要更高效的实现
function ilh.isMainPage()
local mainpage_title=mw.message.new('Mainpage')
local curpage_obj=mw.title.getCurrentTitle()
return mw.title.equals(curpage_obj.fullText,mainpage_title)
end
function ilh.isExist(pageName)
local localPage_obj=mw.title.makeTitle(0,context["localPage"])
return localPage_obj.exists
end
--end
return ilh