Module:Links:修订间差异

此后如竟没有炬火,我便是唯一的光。
跳转到导航 跳转到搜索
无编辑摘要
无编辑摘要
 
(未显示同一用户的6个中间版本)
第2行: 第2行:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs


function p.str2codelist(strin) --转换字符串到unicode码位构成的数组,仅支持第一平面字符
-- 将输入字符串转换为由 Unicode 码位构成的数组,仅支持第一平面字符
function p.str2codelist(strin)
local listout={}
local strk=1
local listout = {}
local listk=1
local strk = 1
local listk = 1

while strk <= #strin do
while strk <= #strin do
-- 处理三字节 UTF-8 字符
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)
if (math.modf(string.byte(strin, strk) / 16) == 14) then
listout[listk] = (string.byte(strin, strk) - 224) * 4096 +
listk=listk+1
strk=strk+3
(string.byte(strin, strk + 1) - 128) * 64 +
elseif(math.modf(string.byte(strin,strk)/32)==6)then
(string.byte(strin, strk + 2) - 128)
listout[listk]=(string.byte(strin,strk)-192)*64+(string.byte(strin,strk+1)-128)
listk = listk + 1
strk = strk + 3
listk=listk+1
strk=strk+2
-- 处理二字节 UTF-8 字符
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
-- 单字节字符(ASCII 范围)
else
else
listout[listk]=string.byte(strin,strk)
listout[listk] = string.byte(strin, strk)
listk=listk+1
listk = listk + 1
strk=strk+1
strk = strk + 1
end
end
end
end
第24行: 第32行:
end
end


-- 将 Unicode 码位数组转换回 UTF-8 字符串
function p.codelist2str(unicodeCodes)
function p.codelist2str(unicodeCodes)
local utf8Str = ""
local utf8Str = ""
if #unicodeCodes>0 then
if #unicodeCodes > 0 then
for _, code in ipairs(unicodeCodes) do
for _, code in ipairs(unicodeCodes) do
if code <= 0x7F then
-- 处理 ASCII 范围字符
utf8Str = utf8Str .. string.char(code)
if code <= 0x7F then
elseif code <= 0x7FF then
utf8Str = utf8Str .. string.char(code)
-- 处理二字节字符
utf8Str = utf8Str .. string.char(0xC0 + math.floor(code / 64), 0x80 + (code % 64))
else
elseif code <= 0x7FF then
utf8Str = utf8Str .. string.char(0xE0 + math.floor(code / 4096), 0x80 + math.floor((code % 4096) / 64), 0x80 + (code % 64))
utf8Str = utf8Str .. string.char(0xC0 + math.floor(code / 64), 0x80 + (code % 64))
end
-- 处理三字节字符
end
else
utf8Str = utf8Str .. string.char(0xE0 + math.floor(code / 4096),
0x80 + math.floor((code % 4096) / 64),
0x80 + (code % 64))
end
end
end
end
return utf8Str
return utf8Str
end
end


function p.links(frame) --实现复杂接表
-- 生成复杂的链接表
function p.links(frame)
local args = getArgs(frame)
local args = getArgs(frame)
-- 计算 args 的长度 (由于某些神秘的原因不能用 #args)
local count = 0
for _, v in ipairs(args) do
count = count + 1
end

local outstr = ""
local outstr = ""
-- 遍历每个参数
for k,v in ipairs(args)do
for k, v in ipairs(args) do
strli = p.str2codelist(v)
local flag=0
local strli = p.str2codelist(v)
local tag=""
local flag = 0
local itag=""
local tag = ""
for k2,v2 in ipairs(strli)do
local itag = ""
if v2==p.str2codelist("#")[1] then
-- 遍历字符转换后的 Unicode 码位
flag=1
for k2, v2 in ipairs(strli) do
else
if flag==0 then
if v2 == p.str2codelist("#")[1] then
flag = 1 -- 识别分隔符
tag=tag..p.codelist2str({v2})
else
else
-- 根据 flag 决定当前字符的标签存放位置
itag=itag..p.codelist2str({v2})
if flag == 0 then
end
tag = tag .. p.codelist2str({v2})
end
else
end
itag = itag .. p.codelist2str({v2})
if flag==0 then
end
outstr = outstr.."[["..tag.."]]".." • "
else
end
end
outstr = outstr.."[["..tag.."|"..itag.."]]".." • "
end
-- 组装输出字符串
if k < #args then
if flag == 0 then
outstr = outstr.." • "
outstr = outstr .. "[[" .. tag .. "]]"
end
else
outstr = outstr .. "[[" .. tag .. "|" .. itag .. "]]"
end
-- 添加分隔符
if k < count then
outstr = outstr .. " • "
end
end
end
return frame:preprocess(outstr) --预处理
return frame:preprocess(outstr) -- 预处理 wikitext 结果
end
end



2024年9月17日 (二) 01:01的最新版本

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

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

-- 将输入字符串转换为由 Unicode 码位构成的数组,仅支持第一平面字符
function p.str2codelist(strin)
    local listout = {}
    local strk = 1
    local listk = 1

    while strk <= #strin do
        -- 处理三字节 UTF-8 字符
        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
        -- 处理二字节 UTF-8 字符
        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
        -- 单字节字符(ASCII 范围)
        else
            listout[listk] = string.byte(strin, strk)
            listk = listk + 1
            strk = strk + 1
        end
    end
    return listout
end

-- 将 Unicode 码位数组转换回 UTF-8 字符串
function p.codelist2str(unicodeCodes)
    local utf8Str = ""
    if #unicodeCodes > 0 then
        for _, code in ipairs(unicodeCodes) do
            -- 处理 ASCII 范围字符
            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)
    
    -- 计算 args 的长度 (由于某些神秘的原因不能用 #args)
    local count = 0
    for _, v in ipairs(args) do
        count = count + 1
    end

    local outstr = ""
    -- 遍历每个参数
    for k, v in ipairs(args) do
        local strli = p.str2codelist(v)
        local flag = 0
        local tag = ""
        local itag = ""
        
        -- 遍历字符转换后的 Unicode 码位
        for k2, v2 in ipairs(strli) do
            if v2 == p.str2codelist("#")[1] then
                flag = 1  -- 识别分隔符
            else
                -- 根据 flag 决定当前字符的标签存放位置
                if flag == 0 then
                    tag = tag .. p.codelist2str({v2})
                else
                    itag = itag .. p.codelist2str({v2})    
                end
            end
        end
        
        -- 组装输出字符串
        if flag == 0 then
            outstr = outstr .. "[[" .. tag .. "]]"
        else
            outstr = outstr .. "[[" .. tag .. "|" .. itag .. "]]"
        end
        -- 添加分隔符
        if k < count then
            outstr = outstr .. " • "
        end
    end
    return frame:preprocess(outstr)  -- 预处理 wikitext 结果
end

return p