Module:Aksabun
跳到导航
跳到搜索
-- This is a module re-implementing T:Aksabun. Extended functions are added -- To reduce costly parser function use in certian cases and to cope with -- The messier-by-day Ak source naming. -- Crappy code originally by One-Six(U:公的驱逐舰), released under CC BY 4.0. -- Gotta Praise the Crocc. local getArgs = require('Module:Arguments').getArgs local p = {} local wrapperArray = { 'Template:Aksabun', 'Template:沙盒' } local function isEmpty( s ) return (s == nil or s == '') end local function getArgFromAlias ( args, argsAliasArray ) for i = 1, #argsAliasArray do if ( args[argsAliasArray[i]] ~= nil ) then return args[argsAliasArray[i]] end end return nil end local function arrayFromCSV ( s ) if s == nil then return {} end local array = {} for w in (s:gsub("%,?%s*$",",")):gmatch("([^%,]*)%,%s*") do table.insert(array, w) end return array end local function compactArray ( a ) local j=0 for i=1,#a do if a[i]~=nil then j=j+1 a[j]=a[i] end end for i=j+1,#a do a[i]=nil end return a end local function tableInvert(t) local s= {} for k,v in ipairs(t) do s[v]=k end return s end -- generate sabun array function p.getSabunArray ( frame ) local args = getArgs ( frame, { wrappers = wrapperArray } ) return p._getSabunArray ( frame, args ) end function p._getSabunArray ( frame, args ) local verboseArg = getArgFromAlias ( args, { "verbose", "manual", "手动" } ) -- if verbose argument defined, use directly and return if verboseArg ~= nil then return arrayFromCSV ( verboseArg ) end local suffixArray = arrayFromCSV ( getArgFromAlias ( args, { "suffix-list", "后缀列表" } ) ) local sabunType = string.lower ( getArgFromAlias ( args, { "sabun-type", "差分类型", "类型", "1" } ) ) if sabunType == 'npc' then sabunType = 'avg_npc' end local sabunCode = getArgFromAlias ( args, { "sabun-code", "差分代号", "代号", "2" } ) local separator = getArgFromAlias ( args, { "separator", "分隔符" } ) or '_' -- check if suffix array is given if suffixArray[1] == nil then -- no suffix array, gotta generate it suffixArray = {''} -- take care of the first term. local firstSuffix = getArgFromAlias ( args, { "first", "首项" } ) if not isEmpty ( firstSuffix ) then -- first term already given, use directly suffixArray[1] = firstSuffix else -- first term not given, test four possible cases local PFSA = { separator..'1', '', separator..'2', separator..'0' } local i = 1 -- HACK TO AVOID COSTLY PARSER FUNCTION USE local sabunExist = frame:callParserFunction('filepath', "Ak_"..sabunType.."_"..sabunCode..PFSA[i]..".png") while ( sabunExist == '' and i < #PFSA ) do i = i + 1 sabunExist = frame:callParserFunction( 'filepath', "Ak_"..sabunType.."_"..sabunCode..PFSA[i]..".png") end suffixArray[1] = PFSA[i] end -- generate the rest of the suffixes local lastSuffix = tonumber ( getArgFromAlias ( args, { "last", "末项" } ) ) --if not number, will get nil local omitArray = arrayFromCSV ( getArgFromAlias ( args, { "omit", "忽略" } ) ) -- check if last term is given if lastSuffix ~= nil then --good, given! much less costly parser function use for i = 2, lastSuffix do table.insert( suffixArray, separator..i ) end if omitArray[1] ~= nil then -- remove omitted suffixes from suffix array local arraySuffix = tableInvert ( suffixArray ) for i = 1, #omitArray do local key = arraySuffix[ separator..omitArray[i] ] suffixArray[key] = nil end suffixArray = compactArray ( suffixArray ) end else -- shit, not defined! gotta check EVERYTHING with a messy omitArray to deal with local nextSuffix -- heck next suffix (starts on this number+1, because the first suffix is already defined) if suffixArray[1] == "" then nextSuffix = 1 else nextSuffix = suffixArray[1]:gsub( "^"..separator, "" ) nextSuffix = tonumber ( nextSuffix ) end -- get ready for while loop local notDone = true local i = 1 while notDone do if i ~= 1 then table.insert( suffixArray, separator..nextSuffix ) end -- check next suffix nextSuffix = nextSuffix + 1 local omit = false local j = 1 while omitArray[j] ~= nil and not omit do omit = ( nextSuffix == omitArray[j] ) j = j + 1 end if not omit then i = i + 1 -- HACK TO AVOID COSTLY PARSER FUNCTION USE notDone = ( frame:callParserFunction('filepath', "Ak_"..sabunType.."_"..sabunCode..separator..nextSuffix..".png") ~= '' ) else omitArray[j-1] = nil omitArray = compactArray( omitArray ) end end end -- else: suffix array given, go straight to generating sabun array from suffix array end -- generate the sabun array from suffix array local outputArray = {} for i = 1, #suffixArray do outputArray[i] = "Ak_" .. sabunType .. "_" .. sabunCode .. suffixArray[i] .. ".png" end return outputArray end function p.main ( frame ) local args = getArgs ( frame, { wrappers = wrapperArray } ) return p._main ( frame, args ) end function p._main ( frame, args ) local sabunArray = p._getSabunArray ( frame, args ) local size = tonumber ( getArgFromAlias ( args, { "size" } ) ) or 400 local outputString = '<div class="Tabs black" data-default-tab="1" data-auto-width="yes" style="max-width:calc('..size..'px + 2em);">' for i = 1, #sabunArray do outputString = outputString .. '<div class="Tab"><div class="TabLabelText">差分'..i..'</div><div class="TabContentText">[[File:'..sabunArray[i]..'|'..size..'px]]</div></div>' end return outputString .. '</div>' end return p