“模块:FileInfo”的版本间的差异

这里有,偶像们的一切!
跳到导航 跳到搜索
(建立内容为“local module = {} local getArgs = require('Module:Arguments').getArgs local nowiki_blocks = { 'do', 'condition' } function pack_inner_loop(loop) -- 给内…”的新页面)
 
(页面内容被替换为“local module = {} module.main = function(frame) local fileName = frame.args[1] local name = frame.args[2] local attrNames = { 'width', 'height', 'size', 'mimeTyp…”)
标签替换
 
第1行: 第1行:
 
local module = {}
 
local module = {}
  
local getArgs = require('Module:Arguments').getArgs
+
module.main = function(frame)
 
+
local fileName = frame.args[1]
local nowiki_blocks = { 'do', 'condition' }
+
local name = frame.args[2]
 
+
local attrNames = { 'width', 'height', 'size', 'mimeType' }
function pack_inner_loop(loop)
+
local title = mw.title.new('File:'..fileName)
   -- 给内层循环的do参数包裹<nowiki>标签
+
if title ~= nil then
   loop = mw.ustring.gsub(loop, '{{%s-[Ll]oop%s-|', '{{#invoke:loop|loop|')
+
for i, v in ipairs(attrNames) do
   loop = mw.ustring.gsub(loop, '{{%s-[Ww]hile%s-|', '{{#invoke:loop|while|')
+
if v == name then
   loop = mw.ustring.gsub(loop, '{{%s-[Dd]o[Ww]hile%s-|', '{{#invoke:loop|dowhile|')
+
return title.file[name]
   return mw.ustring.gsub(loop, '({{#invoke:loop.*)', function(a)
+
end
     local i = 1
 
     local stack = {}
 
     local block_begin = -1
 
     local block_end = -1
 
     while (i < mw.ustring.len(a)) do
 
     for _, block in ipairs(nowiki_blocks) do
 
       if block_begin == -1 and mw.ustring.find(a, '^|%s-'..block..'%s-=', i) then
 
         _, block_begin = mw.ustring.find(a, '^|%s-'..block..'%s-=', i)
 
         block_begin = block_begin + 1
 
         i = block_begin
 
        
 
         break
 
       end
 
     end
 
       if block_begin ~= -1 then
 
         if mw.ustring.sub(a, i, i + 2) == '{{{' then
 
           i = i + 3
 
           table.insert(stack, 3)
 
         elseif mw.ustring.sub(a, i, i + 1) == '{{' then
 
           i = i + 2
 
           table.insert(stack, 2)
 
         elseif mw.ustring.sub(a, i, i + 2) == '}}}' then
 
           if #stack ~= 0 and stack[#stack] == 3 then
 
             i = i + 3
 
             table.remove(stack)
 
           elseif #stack ~= 0 and stack[#stack] == 2 then
 
             i = i + 2
 
             table.remove(stack)
 
           else
 
             block_end = i - 1
 
             break
 
           end
 
         elseif mw.ustring.sub(a, i, i + 1) == '}}' then
 
           if #stack ~= 0 then
 
             i = i + 2
 
             table.remove(stack)
 
           else
 
             block_end = i - 1
 
             break
 
           end
 
         elseif mw.ustring.sub(a, i, i) == '|' and #stack == 0 then
 
           block_end = i - 1
 
           break
 
         else
 
           i = i + 1
 
         end
 
       else
 
         i = i + 1
 
       end
 
     end
 
     if block_begin ~= -1 and block_end == -1 then block_end = i - 1 end
 
     if block_begin ~= -1 and block_end ~= -1 then
 
     -- mw.log('发现内层循环 <nowiki>'..mw.ustring.sub(a, block_begin, block_end)..'</nowiki>')
 
     return mw.ustring.sub(a, 1, block_begin - 1)..'<nowiki>'..
 
        mw.text.trim(mw.ustring.sub(a, block_begin, block_end))..'</nowiki>'..
 
        pack_inner_loop(mw.ustring.sub(a, block_end + 1, -1))
 
     else
 
       return a
 
     end
 
   end )
 
end
 
 
 
function _loop(args, frame)
 
   local name = ''
 
   local _min = 1
 
   local _max = tonumber(args['max'] or args[1] or 10)
 
   local addend = tonumber(args[2] or 1)
 
   local loop = args['do'] or ''
 
   loop = mw.text.unstripNoWiki(loop)
 
   loop = string.gsub(loop, '<!%-%-.-%-%->', '')
 
 
 
   loop = pack_inner_loop(loop)
 
   local trim = mw.text.split(loop, '\n')
 
   loop = ''
 
   for i, v in ipairs(trim) do
 
     loop = loop..mw.text.trim(v)
 
   end
 
 
 
   for k, v in pairs(args) do
 
     if(k ~= 'do' and k ~= 'max' and k ~= 1 and k ~= 2) then
 
       name = string.gsub(k, '([%%%(%)%.%+%-%*%?%[%]%^%$])', '%%%1')
 
       _min = tonumber(v)
 
       break
 
     end
 
   end
 
   if name == '' then error('必须设置自增/自减的变量名!') end
 
 
 
   loop = mw.text.decode(loop)
 
   -- mw.log('循环体= '..loop)
 
 
 
   local result = ''
 
   if type(args[2]) == 'nil' and _min > _max and addend > 0 then addend = -1 end
 
   for i=_min, _max, addend do
 
     frame:callParserFunction('#vardefine', name, i)
 
     -- mw.log('执行循环体('..name..'='..tostring(i)..') '..loop)
 
     result = result..frame:preprocess(loop)
 
   end
 
   -- mw.log('循环结果 '..result)
 
   return result
 
end
 
 
 
function module.loop(frame)
 
   if (frame:getParent() or frame):getTitle() == 'Template:Loop' and
 
     frame.args['do'] == nil then
 
     args = getArgs(frame)
 
   else
 
     args = frame.args
 
   end
 
   return _loop(args, frame:getParent() or frame)
 
end
 
 
 
-- 向后兼容
 
function module.main(frame)
 
   return module.loop(frame)
 
end
 
 
 
-- 别名
 
module["for"] = function(frame)
 
   return module.loop(frame)
 
end
 
 
 
local _while = function(args, frame, dowhile)
 
   --初步处理条件语句
 
   local condition = args['condition'] or ''
 
   condition = mw.text.unstripNoWiki(condition)
 
   condition = string.gsub(condition, '<!%-%-.-%-%->', '')
 
  
 
   --修整条件语句的格式
 
   local trim = mw.text.split(condition, '\n')
 
   condition = ''
 
   for i, v in ipairs(trim) do
 
     condition = condition..mw.text.trim(v)
 
   end
 
  
 
   condition = mw.text.decode(condition)
 
   -- mw.log('循环体= '..condition)
 
  
 
   --初步处理循环体
 
   local loop = args['do'] or ''
 
   loop = mw.text.unstripNoWiki(loop)
 
   loop = string.gsub(loop, '<!%-%-.-%-%->', '')
 
  
 
   --处理循环体中的次级循环语句
 
   loop = pack_inner_loop(loop)
 
   --修整循环体中语句的格式
 
   local trim = mw.text.split(loop, '\n')
 
   loop = ''
 
   for i, v in ipairs(trim) do
 
     loop = loop..mw.text.trim(v)
 
   end
 
 
 
   loop = mw.text.decode(loop)
 
   -- mw.log('循环体= '..loop)
 
 
 
   local result = ''
 
   if dowhile then
 
     -- mw.log('执行循环体('..name..'='..tostring(i)..') '..loop)
 
     repeat result = result..frame:preprocess(loop) until frame:preprocess(condition) == ''
 
   else
 
     -- mw.log('执行循环体('..name..'='..tostring(i)..') '..loop)
 
     while frame:preprocess(condition) ~= '' do result = result..frame:preprocess(loop) end
 
   end
 
   -- mw.log('循环结果 '..result)
 
   return result
 
end
 
 
 
module["while"] = function(frame)
 
   if (frame:getParent() or frame):getTitle() == 'Template:While' and
 
     frame.args['do'] == nil then
 
     args = getArgs(frame)
 
   else
 
     args = frame.args
 
   end
 
   return _while(args, frame:getParent() or frame, false)
 
end
 
 
 
module.dowhile = function(frame)
 
   if (frame:getParent() or frame):getTitle() == 'Template:While' and
 
     frame.args['do'] == nil then
 
     args = getArgs(frame)
 
   else
 
     args = frame.args
 
   end
 
   return _while(args, frame:getParent() or frame, true)
 
end
 
 
 
function module.fornumargs(frame)
 
local args = frame.args
 
local parent = frame:getParent()
 
local prefix = mw.text.trim(args[1] or '')
 
local numname = mw.text.trim(args[2] or '')
 
local valname = mw.text.trim(args[3] or '')
 
local cond = mw.text.decode(mw.text.unstripNoWiki(mw.text.trim(args.cond or '')))
 
local text = mw.text.decode(mw.text.unstripNoWiki(mw.text.trim(args[4] or '')))
 
if not parent or numname == '' or valname == '' or text == '' then return '' end
 
 
 
local expr = '^' .. mw.ustring.gsub(prefix, '([-+*?%%])', '%%%1') .. '(%d+)$'
 
local keys = {}
 
local output = {}
 
for key, _ in pairs(parent.args) do
 
local num = mw.ustring.match(key, expr)
 
if num then
 
table.insert(keys, tonumber(num))
 
 
end
 
end
end
+
if (frame.args['w'] or '') ~= 'false' then
table.sort(keys)
+
error('没有这个文件属性,可用的文件属性包含:width、height、size、mimeType')
for _, idx in ipairs(keys) do
+
end
parent:callParserFunction('#vardefine', numname, idx)
+
else
parent:callParserFunction('#vardefine', valname, parent.args[prefix .. idx])
+
if (frame.args['w'] or '') ~= 'false' then
if cond == '' or parent:preprocess(cond) ~= '' then
+
error('没有这个文件,请检查文件名是否正确!')
table.insert(output, parent:preprocess(text))
 
 
end
 
end
 
end
 
end
return table.concat(output)
 
 
end
 
end
  
 
return module
 
return module

2024年12月1日 (日) 10:41的最新版本

此模块的文档可以在模块:FileInfo/doc创建

local module = {}

module.main = function(frame)
	local fileName = frame.args[1]
	local name = frame.args[2]
	local attrNames = { 'width', 'height', 'size', 'mimeType' }
	local title = mw.title.new('File:'..fileName)
	if title ~= nil then
		for i, v in ipairs(attrNames) do
			if v == name then
				return title.file[name]
			end
		end
		if (frame.args['w'] or '') ~= 'false' then
			error('没有这个文件属性,可用的文件属性包含:width、height、size、mimeType')
		end
	else
		if (frame.args['w'] or '') ~= 'false' then
			error('没有这个文件,请检查文件名是否正确!')	
		end
	end
end

return module