Module:Choose
跳到导航
跳到搜索
- input
{{#invoke: Choose | main
| tag = ul
| inner-tag = li
| class = my-custom-class
| inner-class = heimu
| style = color: blue; text-shadow: 0 0 4px red
| count = 2
| 1 = foo
| 2 = bar
| 3 = baz
| 4 = lol
| weight-2 = 2
| w3 = 3
| 权重4 = 4
}}
- output
<ul class="mgp-choose my-custom-class" data-count="2" style="color: blue; text-shadow: 0 0 4px red"><li class="mgp-choose-option heimu">foo</li><li data-weight="2" class="mgp-choose-option heimu">bar</li><li data-weight="3" class="mgp-choose-option heimu">baz</li><li data-weight="4" class="mgp-choose-option heimu">lol</li></ul>
- display
- foo
- bar
- baz
- lol
--[[
@name MGP_Choose 萌娘百科の选择
@desc 对 {{#choose}} 解析器函数的纯 lua 实现,但有一些额外用法
@TODO 我们规定作为 <option> 使用的内容一定是匿名传参,即键值为数字
任意键值非数字的传参,我们都认为是有特殊作用的
--]]
local p = {}
local getArgs = require('Module:Arguments').getArgs
function p.main(frame)
local args = getArgs(frame)
-- config
local outerClass = args['class'] or ''
local outerStyle = args['style'] or ''
local outerTag = args['wrapper-tag'] or args['tag'] or 'div'
local innerTag = args['options-tag'] or args['inner-tag'] or args['tag'] or 'div'
local innerClass = args['options-class'] or args['inner-class'] or ''
-- Create container
local html =
mw.html.create(outerTag):attr(
{
['class'] = 'mgp-choose ' .. outerClass,
['style'] = outerStyle,
['data-count'] = args['count']
}
)
for k, v in ipairs(args) do
html:tag(innerTag):attr(
{
['class'] = 'mgp-choose-option ' .. innerClass,
-- @TODO `weight-x` `wx` `权重x` 等传参被认为是选项 x 的权重
['data-weight'] = args['weight-' .. k] or args['权重' .. k] or args['w' .. k]
}
):wikitext(v):done()
end
return html:done()
end
return p
--[[
-- @test 以下测试用例使用了全部选项
mw.log(
p.main({
['tag'] = 'ul',
['inner-tag'] = 'li',
['class'] = 'custom-class',
['inner-class'] = 'heimu',
['style'] = 'color: blue',
['count'] = '123',
'foo', 'bar', 'baz', 'lol',
['weight-4'] = 4
})
)
-- ]]