Zeroclanzhang(讨论 | 贡献) 无编辑摘要 |
Zeroclanzhang(讨论 | 贡献) 无编辑摘要 |
||
| 第2行: | 第2行: | ||
local p = {} | local p = {} | ||
local mw = require('mw') | local mw = require('mw') | ||
p.cache = {} -- 初始化缓存 | p.cache = {} -- 初始化缓存 | ||
| 第18行: | 第19行: | ||
-- 构建查询 | -- 构建查询 | ||
local query = '[[Category:' .. category .. ']]' | local query = '[[Category:' .. mw.text.encode(category) .. ']]' | ||
if property and property:find("::") then | if property and property:find("::") then | ||
local propertyParts = mw.text.split(property, "::") | local propertyParts = mw.text.split(property, "::", true) | ||
if #propertyParts == 2 and text and text ~= '' then -- 确保text非空 | if #propertyParts == 2 and text and text ~= '' then -- 确保text非空 | ||
query = query .. '[[ | query = query .. '[[' .. propertyParts[1] .. '::' .. propertyParts[2] .. ']]' | ||
end | end | ||
end | end | ||
-- | local askParameters = { | ||
local results = frame: | query = query, | ||
format = 'template', | |||
template = 'NavplateSMWRowItem' | |||
} | |||
-- 执行查询 | |||
local results = frame:expandTemplate{ title = 'ask', args = askParameters } | |||
-- 缓存查询结果 | -- 缓存查询结果 | ||
| 第38行: | 第44行: | ||
local labelDiv = mw.html.create('div'):addClass('template-navplate-item__label') | local labelDiv = mw.html.create('div'):addClass('template-navplate-item__label') | ||
if icon then | if icon then | ||
labelDiv:wikitext('[[File:' .. icon .. '|20px|link=]]') | labelDiv:wikitext('[[File:' .. mw.text.encode(icon) .. '|20px|link=]]') | ||
end | end | ||
labelDiv:wikitext(text) | labelDiv:wikitext(text) | ||
| 第47行: | 第53行: | ||
div:node(listDiv) | div:node(listDiv) | ||
return div:allDone() | return tostring(div:allDone()) | ||
end | end | ||
return p | return p | ||
2024年1月24日 (三) 16:34的版本
可在模块: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:find("::") then
local propertyParts = mw.text.split(property, "::", true)
if #propertyParts == 2 and text and text ~= '' then -- 确保text非空
query = query .. '[[' .. propertyParts[1] .. '::' .. propertyParts[2] .. ']]'
end
end
local askParameters = {
query = query,
format = 'template',
template = 'NavplateSMWRowItem'
}
-- 执行查询
local results = frame:expandTemplate{ title = 'ask', args = askParameters }
-- 缓存查询结果
p.cache[cacheKey] = results
-- 格式化输出
local div = mw.html.create('div'):addClass('template-navplate-item')
local labelDiv = mw.html.create('div'):addClass('template-navplate-item__label')
if icon then
labelDiv:wikitext('[[File:' .. mw.text.encode(icon) .. '|20px|link=]]')
end
labelDiv:wikitext(text)
div:node(labelDiv)
local listDiv = mw.html.create('div'):addClass('template-navplate-item__list')
listDiv:wikitext(results)
div:node(listDiv)
return tostring(div:allDone())
end
return p