可在模块:Navplate SMW row/doc创建此模块的帮助文档
-- Module:NavplateSMWRow
local p = {}
local mw = require('mw')
function p.navplateSmwRow(frame)
local args = frame:getParent().args
local category = args[1]
local property = args[2]
local text = args['text'] or args[3]
local icon = args['icon']
-- 使用缓存结果,减少查询
local cacheKey = category .. (property or '') .. (text or '')
if p.cache[cacheKey] then
return p.cache[cacheKey]
end
-- 构建查询
local query = '[[Category:' .. category .. ']]'
if property and property:find("::") then
local propertyParts = mw.text.split(property, "::")
query = query .. '[[Category:' .. propertyParts[1] .. ']]'
query = query .. '[[' .. propertyParts[2] .. '::SubCategory]]'
end
-- 执行查询
local results = frame:extensionTag('ask', query .. '|format=list')
-- 缓存查询结果
p.cache[cacheKey] = results
-- 格式化输出
return mw.html.create('div')
:addClass('template-navplate-item')
:node(mw.html.create('div')
:addClass('template-navplate-item__label')
:wikitext(icon and '[[File:' .. icon .. '|20px|link=]]' or '')
:wikitext(text)
)
:node(mw.html.create('div')
:addClass('template-navplate-item__list')
:wikitext(results)
)
:allDone()
end
p.cache = {}
return p