Zeroclanzhang(讨论 | 贡献) (创建页面,内容为“-- Module:NavplateSMWRow local p = {} local mw = require('mw') local frame = mw.getCurrentFrame() -- 缓存查询结果 local cache = {} function p.navplateSmwRow(frame) local category = frame.args[1] local property = frame.args[2] local text = frame.args['text'] or category local icon = frame.args['icon'] -- 使用缓存结果,减少查询 local cacheKey = category .. (property or '') .. (text or '') if cache[cacheKey] then…”) |
Zeroclanzhang(讨论 | 贡献) 无编辑摘要 |
||
(未显示同一用户的55个中间版本) | |||
第1行: | 第1行: | ||
-- | -- 自定义的trim函数,增加对换行符的处理 | ||
local function trim(s) | |||
return (s:gsub("^%s*(.-)%s*$", "%1"):gsub("[\n\r]+", "")) | |||
end | |||
local p = {} | local p = {} | ||
local mw = require('mw') | local mw = require('mw') | ||
p.cache = {} -- 初始化缓存 | |||
function p.navplateSmwRow(frame) | function p.navplateSmwRow(frame) | ||
local | local args = frame:getParent().args | ||
local property = | local categoryName = args[1] | ||
local text = | local property = args[2] | ||
local icon = | local text = args[3] | ||
local icon = args[4] | |||
local cacheKey = categoryName .. (property or '') .. (text or '') | |||
local cacheKey = | if p.cache[cacheKey] then | ||
if cache[cacheKey] then | return p.cache[cacheKey] | ||
return cache[cacheKey] | |||
end | end | ||
local query = '[[Category:' .. mw.text.encode(categoryName) .. ']]' | |||
local query = '[[Category:' .. | if property and property ~= '' then | ||
if property and property | query = query .. '[[' .. mw.text.encode(property) .. ']]' | ||
query = query .. '[[ | |||
end | end | ||
-- | local ask = '{{#ask:' .. query .. | ||
local | '|format=list' .. | ||
'|link=all' .. | |||
'|headers=hide' .. | |||
'|searchlabel=' .. | |||
'|class=smwlist' .. | |||
'|sep=' .. | |||
'}}' | |||
local results = frame:preprocess(ask) | |||
p.cache[cacheKey] = results | |||
local itemDiv = mw.html.create('div'):addClass('template-navplate-item') | |||
local metadataAndTextDiv = mw.html.create('div'):addClass('template-navplate-item__label') | |||
local metadata = mw.html.create('span'):addClass('metadata'):wikitext('[[File:' .. mw.text.encode(icon) .. '|20px|link=]] ' .. mw.text.encode(text) .. '') | |||
metadataAndTextDiv:node(metadata) | |||
local listDiv = mw.html.create('div'):addClass('template-navplate-item__list'):wikitext(results) | |||
itemDiv | |||
:node(metadataAndTextDiv) | |||
:node(listDiv) | |||
local finalHtml = tostring(itemDiv:allDone()) | |||
finalHtml = trim(finalHtml) | |||
return finalHtml | |||
return | |||
end | end | ||
return p | return p |
2024年1月24日 (三) 19:55的最新版本
可在模块:Navplate SMW row/doc创建此模块的帮助文档
-- 自定义的trim函数,增加对换行符的处理
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"):gsub("[\n\r]+", ""))
end
local p = {}
local mw = require('mw')
p.cache = {} -- 初始化缓存
function p.navplateSmwRow(frame)
local args = frame:getParent().args
local categoryName = args[1]
local property = args[2]
local text = args[3]
local icon = args[4]
local cacheKey = categoryName .. (property or '') .. (text or '')
if p.cache[cacheKey] then
return p.cache[cacheKey]
end
local query = '[[Category:' .. mw.text.encode(categoryName) .. ']]'
if property and property ~= '' then
query = query .. '[[' .. mw.text.encode(property) .. ']]'
end
local ask = '{{#ask:' .. query ..
'|format=list' ..
'|link=all' ..
'|headers=hide' ..
'|searchlabel=' ..
'|class=smwlist' ..
'|sep=' ..
'}}'
local results = frame:preprocess(ask)
p.cache[cacheKey] = results
local itemDiv = mw.html.create('div'):addClass('template-navplate-item')
local metadataAndTextDiv = mw.html.create('div'):addClass('template-navplate-item__label')
local metadata = mw.html.create('span'):addClass('metadata'):wikitext('[[File:' .. mw.text.encode(icon) .. '|20px|link=]] ' .. mw.text.encode(text) .. '')
metadataAndTextDiv:node(metadata)
local listDiv = mw.html.create('div'):addClass('template-navplate-item__list'):wikitext(results)
itemDiv
:node(metadataAndTextDiv)
:node(listDiv)
local finalHtml = tostring(itemDiv:allDone())
finalHtml = trim(finalHtml)
return finalHtml
end
return p