置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Choose

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]

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
    })
  )
-- ]]