Zeroclanzhang(讨论 | 贡献) 无编辑摘要 |
Zeroclanzhang(讨论 | 贡献) 无编辑摘要 |
||
| 第20行: | 第20行: | ||
-- 构建查询 | -- 构建查询 | ||
local query = '[[Category:' .. mw.text.encode(category) .. ']]' | local query = '[[Category:' .. mw.text.encode(category) .. ']]' | ||
if property and property | if property and property ~= '' then | ||
query = query .. '[[' .. mw.text.encode(property) .. ']]' | |||
end | end | ||
-- 转换查询为# | -- 转换查询为#ask解析器函数的形式,取消template格式,用'|'进行间隔 | ||
local ask = '{{#ask:' .. query .. | local ask = '{{#ask:' .. query .. | ||
'|format= | '|format=broadtable' .. | ||
'| | '|link=all' .. | ||
'|headers=hide' .. | |||
'|searchlabel=' .. | |||
'|class=sortable wikitable smwtable' .. | |||
'|sep= | ' .. | |||
'}}' | '}}' | ||
| 第40行: | 第41行: | ||
-- 格式化输出 | -- 格式化输出 | ||
local div = mw.html.create('div'):addClass(' | local div = mw.html.create('div'):addClass('navplate-smw-row') | ||
if icon then | if icon then | ||
div:wikitext('[[File:' .. mw.text.encode(icon) .. '|20px|link=]] ') | |||
end | end | ||
div:wikitext(text .. ' | ' .. results) | |||
return tostring(div:allDone()) | return tostring(div:allDone()) | ||
2024年1月24日 (三) 17:07的版本
可在模块:Navplate SMW row/doc创建此模块的帮助文档
-- Module:NavplateSMWRow
local p = {}
local mw = require('mw')
p.cache = {} -- 初始化缓存
function p.navplateSmwRow(frame)
local args = frame:getParent().args
local category = args[1]
local property = args[2]
local text = args[3]
local icon = args[4]
-- 使用缓存结果,减少查询
local cacheKey = category .. (property or '') .. (text or '')
if p.cache[cacheKey] then
return p.cache[cacheKey]
end
-- 构建查询
local query = '[[Category:' .. mw.text.encode(category) .. ']]'
if property and property ~= '' then
query = query .. '[[' .. mw.text.encode(property) .. ']]'
end
-- 转换查询为#ask解析器函数的形式,取消template格式,用'|'进行间隔
local ask = '{{#ask:' .. query ..
'|format=broadtable' ..
'|link=all' ..
'|headers=hide' ..
'|searchlabel=' ..
'|class=sortable wikitable smwtable' ..
'|sep= | ' ..
'}}'
-- 执行查询
local results = frame:preprocess(ask)
-- 缓存查询结果
p.cache[cacheKey] = results
-- 格式化输出
local div = mw.html.create('div'):addClass('navplate-smw-row')
if icon then
div:wikitext('[[File:' .. mw.text.encode(icon) .. '|20px|link=]] ')
end
div:wikitext(text .. ' | ' .. results)
return tostring(div:allDone())
end
return p