跳转到内容

模組:Ilh

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由Cwek留言 | 贡献2017年1月3日 (二) 03:47编辑。这可能和当前版本存在着巨大的差异。

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"],'"','&quot;')
    context["_foreignPage"]=mw.ustring.gsub(context["foreignPage"],'"','&quot;')
    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