モジュール:仮リンク/link

    提供:Wikiminati

    このモジュールについての説明文ページを モジュール:仮リンク/link/doc に作成できます

    local p = {}
    
    function p.otherLangLinkList(args)
    
        local label = ""
    	if args['label'] ~= "" and args['label'] ~= nil then
    		label = "|" .. args['label']
    	end
    	
    	if args['view'] ~= "" and args['view'] ~= nil then
    		local result = {}
    		
    		-- ツールチップ指定
    		result[1] = string.format(
    			'<span title="%s">[[%s%s]]',
    			args['title'],
    			args[1],
    			label
    			)
    			
    		if args[2] ~= "" and args[3] ~= "" then
    			-- フォント指定
    			if args['viewInterwikiPageName'] ~= '1' then
    				result[2] = '<span style="font-size: 0.77em; font-weight: normal;" class="noprint">'
    			else
    				result[2] = ''
    			end
    
    			-- 改行指定対応
    			if args['br'] == "" or args['br'] == nil then
    				result[3] = ""
    			else
    				result[3] = '<br/>'
    			end
    			
    			result[4] = '('
    			if args['viewInterwikiPageName'] == '1' then
    				result[5] = p.otherLangLinkDispName(args[2], args[3], args['interwikilabel'], args['short'], args['links'])
    			else
    				local tmp = {}
    				tmp[1] = p.otherLangLink(args[2], args[3])
    				tmp[2] = p.otherLangLink(args[4], args[5])
    				tmp[3] = p.otherLangLink(args[6], args[7])
    				tmp[4] = p.otherLangLink(args[8], args[9])
    				result[5] = table.concat(p._tableShorten(tmp), '、')
    			end
    			result[6] = ')'
    			
    			if args['viewInterwikiPageName'] ~= '1' then
    				result[7] = '</span>'
    			else
    				result[7] = ''
    			end
    		end
    		
    		result[8] = '</span>'
    		
    		return table.concat(p._tableShorten(result))
    	else
    		return string.format(
    			'[[%s%s]]',
    			args[1],
    			label
    			)
    	end
    end
    
    function p.otherLangLink(langCode, pageName)
    	-- 引数が指定されていない場合は空文字を返す
    	if langCode == "" or langCode == nil or pageName == "" or pageName == nil then
    		return ""
    	end
    	
    	local langDisp
    	
    	-- wikidataの場合は「wikidata」、それ以外の場合は言語名+「版」とする
    	if langCode == 'wikidata' then
    		langDisp = 'wikidata'
    	else
    		local mWikipediaLangName = require('モジュール:Wikipedia言語名')
    	
    		local newFrameArg = {}
    		newFrameArg[1] = langCode
    		local newFrame = { args = newFrameArg }
    		
    		langDisp = mWikipediaLangName.GetWikipediaPageName(newFrame)
    	end
    	
    	if langDisp == nil then
    		error('言語コードの指定に誤りがあります')
    	elseif langCode ~= 'wikidata' then
    		langDisp = langDisp .. '版'
    	end
    	
    	return string.format(
    		'[[:%s:%s|%s]]',
    		langCode,
    		pageName,
    		langDisp
    		)
    end
    
    function p.otherLangLinkDispName(langCode, pageName, interwikilabel, short, links)
    	-- 必須引数が指定されていない場合は空文字を返す
    	if langCode == "" or langCode == nil or pageName == "" or pageName == nil then
    		return ""
    	end
    	
    	-- 他言語版リンクラベル名が指定されていない場合はページ名を指定
    	if interwikilabel == "" or interwikilabel == nil then
    		interwikilabel = pageName
    	end
    	
    	-- ページリンク部分取得
    	local pageLink = string.format(
    		'<span lang="%s" xml:lang="%s">[[:%s:%s|%s]]</span>',
    		langCode,
    		langCode,
    		langCode,
    		pageName,
    		interwikilabel
    		)
    	
    	-- 言語ページ取得
    	local mWikipediaLangName = require('モジュール:Wikipedia言語名')
    	local newFrameArg = {}
    	newFrameArg[1] = langCode
    	local newFrame = { args = newFrameArg }
    	local langPage = mWikipediaLangName.GetWikipediaPageName(newFrame) 
    	
    	if langPage == nil then
    		error('言語コードの指定に誤りがあります')
    	end
    	
    	-- 言語表示名取得
    	local langDisp
    	local localShort = short
    	if short == 'on' then
    		newFrameArg = {}
    		newFrameArg[1] = langCode .. '-short'
    		newFrame = { args = newFrameArg }
    		langDisp = mWikipediaLangName.GetWikipediaPageName(newFrame) 
    		if langPage == langDisp then
    			localShort = 'off'
    		end
    	end
    	
    	-- 言語表示部分取得
    	local langLink
    	if links ~= 'no' and localShort ~= 'on' then
    		-- デフォルト(リンクあり、言語名省略なし)
    		langLink = string.format(
    			'[[%s]]',
    			langPage
    			)
    	elseif links ~= 'no' and localShort == 'on' then
    		-- リンクあり、言語名省略
    		langLink = string.format(
    			'[[%s|%s]]',
    			langPage,
    			langDisp
    			)
    	elseif links == 'no' and localShort ~= 'on' then
    		-- リンクなし、言語名省略なし
    		langLink = langPage
    	else
    		-- リンクなし、言語名省略
    		langLink = langDisp
    	end
    	
    	return langLink .. ': ' .. pageLink
    
    end
    
    function p._tableShorten(t)
    	local newTable = {}
    	local i = 0
    	for j,m in ipairs(t) do
    		if m ~= "" and m ~= nil then
    			i = i + 1
    			newTable[i] = m
    		end
    	end
    	
    	return newTable
    end
    
    return p