Module:Links:修订间差异
外观
无编辑摘要 |
小 我草写错了 |
||
| (未显示2个用户的8个中间版本) | |||
| 第2行: | 第2行: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
function p.str2codelist(strin) | -- 将输入字符串转换为由 Unicode 码位构成的数组 | ||
local listout={} | function p.str2codelist(strin) | ||
local listout = {} | |||
for codepoint in mw.ustring.gcodepoint(strin) do | |||
table.insert(listout, codepoint) | |||
end | end | ||
return listout | return listout | ||
end | end | ||
-- 将 Unicode 码位数组转换回 UTF-8 字符串 | |||
function p.codelist2str(unicodeCodes) | function p.codelist2str(unicodeCodes) | ||
local utf8Str = "" | local utf8Str = "" | ||
for _, code in ipairs(unicodeCodes) do | |||
utf8Str = utf8Str .. mw.ustring.char(code) | |||
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 | |||
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 | end | ||
return frame:preprocess(outstr) --预处理 | return frame:preprocess(outstr) -- 预处理 wikitext 结果 | ||
end | end | ||
return p | return p | ||
2025年10月5日 (日) 12:39的最新版本
此模块的文档可以在Module:Links/doc创建
local p = {}
local getArgs = require('Module:Arguments').getArgs
-- 将输入字符串转换为由 Unicode 码位构成的数组
function p.str2codelist(strin)
local listout = {}
for codepoint in mw.ustring.gcodepoint(strin) do
table.insert(listout, codepoint)
end
return listout
end
-- 将 Unicode 码位数组转换回 UTF-8 字符串
function p.codelist2str(unicodeCodes)
local utf8Str = ""
for _, code in ipairs(unicodeCodes) do
utf8Str = utf8Str .. mw.ustring.char(code)
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