Module:Links:修订间差异

此后如竟没有炬火,我便是唯一的光。
跳转到导航 跳转到搜索
无编辑摘要
无编辑摘要
第53行: 第53行:
else
else
if flag==0 then
if flag==0 then
tag=tag..p.codelist2str(v2)
tag=tag..p.codelist2str({v2})
else
else
itag=tag..p.codelist2str(v2)
itag=tag..p.codelist2str({v2})
end
end
end
end

2024年9月16日 (一) 23:47的版本

可在Module:Links/doc创建此模块的帮助文档

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.str2codelist(strin)  --转换字符串到unicode码位构成的数组,仅支持第一平面字符
    local listout={}
    local strk=1
    local listk=1
    while strk <= #strin do
        if(math.modf(string.byte(strin,strk)/16)==14)then
            listout[listk]=(string.byte(strin,strk)-224)*4096+(string.byte(strin,strk+1)-128)*64+(string.byte(strin,strk+2)-128)
			listk=listk+1
            strk=strk+3
        elseif(math.modf(string.byte(strin,strk)/32)==6)then
            listout[listk]=(string.byte(strin,strk)-192)*64+(string.byte(strin,strk+1)-128)
			listk=listk+1
            strk=strk+2
        else
            listout[listk]=string.byte(strin,strk)
			listk=listk+1
            strk=strk+1
        end
    end
    return listout
end

function p.codelist2str(unicodeCodes)
    local utf8Str = ""
    if #unicodeCodes>0 then
	    for _, code in ipairs(unicodeCodes) do
	        if code <= 0x7F then
	            utf8Str = utf8Str .. string.char(code)
	        elseif code <= 0x7FF then
	            utf8Str = utf8Str .. string.char(0xC0 + math.floor(code / 64), 0x80 + (code % 64))
	        else
	            utf8Str = utf8Str .. string.char(0xE0 + math.floor(code / 4096), 0x80 + math.floor((code % 4096) / 64), 0x80 + (code % 64))
	        end
	    end
    end
    return utf8Str
end

function p.links(frame)  --实现复杂连接表
    local args = getArgs(frame)
    local outstr = ""
    for k,v in ipairs(args)do
    	strli = p.str2codelist(v)
    	local flag=0
    	local tag=""
    	local itag=""
	    for k2,v2 in ipairs(strli)do
	    	if v2==p.str2codelist("#")[1] then
	    		flag=1
	    	else
		    	if flag==0 then
		    		tag=tag..p.codelist2str({v2})
		    	else
		    		itag=tag..p.codelist2str({v2})	    	
		    	end
		    end
		end
    	local str = "[["+v+"]]"
    	if flag==0 then
    		outstr = outstr.."[["..tag.."]]"
    	else
    		outstr = outstr.."[["..tag.."|"..itag.."]]"
    	end
    	if k < #args then
    		outstr = outstr.." • "
    	end
    end
    return frame:preprocess(outstr)  --预处理
end

return p