跳转到内容

模組:Ilh

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

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

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, {parentFirst=true})

    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.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"])
                   and 
                   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"]
    return (_d and {_d} or {_1})[1]
end

--以下需要更高效的实现
function ilh.isMainPage()
    local mainpage_obj=mw.message.new('Mainpage')
    --mainpage_obj=mainpage_obj:inLanguage('zh-CN')
    local curpage_obj=mw.title.getCurrentTitle()
    --mw.log(mainpage_obj:plain())
    --mw.log(curpage_obj.fullText)
    return curpage_obj.fullText==mainpage_obj:plain()
end

function ilh.isExist(pageName)
    local localPage_obj=mw.title.makeTitle(0,pageName)
    return localPage_obj.exists
end
--end

return ilh