local module = {} local getArgs = require('Module:Arguments').getArgs local buildLyrics = require('Module:Lyrics/Sandbox')._lyrics local initHandler = require('Module:HooksHandler').init local preParse = function (original, translated, customArgs) customArgs.mutiTags = {} customArgs.tags_map = {} customArgs.btn_args = {} local btn_count = 0 local idx, count, start, tag_name = 1, #original, nil, nil local frame = mw.getCurrentFrame() local count_base = frame:callParserFunction{ name = '#var', args = { 'Lyrics-Btn-Count', '0' } } local endup = function () if not customArgs.tags_map[tag_name] then customArgs.tags_map[tag_name] = 'Lyrics-Btn-' .. count_base + btn_count customArgs.btn_args['Lyrics-Btn-' .. count_base + btn_count] = tag_name btn_count = btn_count + 1 end table.insert(customArgs.mutiTags, {start, idx, name = tag_name}) table.remove(original, idx) count = count - 1 start = nil end while idx <= count do if start then if mw.ustring.match(original[idx], '^<%-%-Tag%-End%-%->$') then endup() else idx = idx + 1 end else tag_name = mw.ustring.match(original[idx], '^<%-%-Tag%-Start:(.-)%-%->$') if tag_name then table.remove(original, idx) count = count - 1 start = idx else idx = idx + 1 end end end if start then endup() end if customArgs.defaultVer and customArgs.tags_map[customArgs.defaultVer] then customArgs.btn_args['@default'] = customArgs.tags_map[customArgs.defaultVer] elseif btn_count == 1 and customArgs.defaultVer then customArgs.btn_args['Lyrics-Btn-' .. count_base + btn_count] = customArgs.defaultVer customArgs.btn_args['@default'] = 'Lyrics-Btn-' .. count_base + 1 btn_count = btn_count + 1 else customArgs.btn_args['@default'] = 'Lyrics-Btn-' .. count_base + 1 end frame:callParserFunction{ name = '#vardefine', args = { 'Lyrics-Btn-Count', count_base + btn_count } } end local postParse = function (lines, customArgs) for idx, tag in ipairs(customArgs.mutiTags) do table.insert(lines, tag[1]+(idx-1)*2, '<div class="textToggleDisplay hidden" data-id="' .. customArgs.tags_map[tag.name] .. '">') table.insert(lines, tag[2]+(idx-1)*2+1, '</div>') end end local preOutput = function (html, customArgs) local count = 1 customArgs.btn_args['@radio'] = true customArgs.btn_args['@forceNoCancel'] = true return mw.getCurrentFrame():expandTemplate{ title = '切换显示按钮', args = customArgs.btn_args } .. html end function module.initHooks(args, hooksHandler, customArgs) customArgs.defaultVer = args.default return hooksHandler({ preParse = preParse, postParse = postParse, preOutput = preOutput }) end function module.main(frame) local args = getArgs(frame, { wrappers = 'Template:LyricsKai/mutiver' }) local hooksHandler, customArgs = initHandler(), {} local hookTrigger = module.initHooks(args, hooksHandler, customArgs) return buildLyrics(args, hookTrigger, customArgs) end return module