模块:Infobox/item/image:修订间差异

来自Cookie☆Wiki
无编辑摘要
(导入23个版本)
 
(未显示同一用户的18个中间版本)
第1行: 第1行:
-- <nowiki>
local p = {}
local p = {}


第5行: 第6行:
local parent = frame:getParent()
local parent = frame:getParent()
local arg = parent.args['image']
local arg = parent.args['image']

if string.byte(arg, 1) == 127 then --< 参数可能是 <gallery>…</gallery>
arg = string.sub(arg, 2, -1)
local gallery_mark_index = arg:find('gallery:')
if gallery_mark_index ~= 1 then --< 单张图片
pattern = '^%s*<gallery>.*</gallery>%s*$'
old_arg = arg
if string.byte(arg, 1) == 127 then
return '<span style="font-size: 24px; font-weight: bold; color: red">'
arg = string.match(arg, pattern)
.. '请更新画廊的语法: "<gallery>(换行)文件名|标签页名(换行)…</gallery>" '
if arg == nil then
.. '→ "gallery:(换行)文件名 :: 标签页名(换行)…" </span>'
error('The image argument of this template (module) must be a file name in plain text or a <gallery> element. Got:' .. old_arg)
.. '[[Category:含有在信息框中使用了不正确的画廊语法的页面]]'
end
images = {}
for image_line in arg:gmatch('[^\r\n]+') do
-- table.insert(lines, line)
local sep = image_line:find('|')
table.insert(images, {
file = string.sub(image_line, 1, sep-1),
title = string.sub(image_line, sep+1, -1)
})
end
end
return mw.dumpObject(images) --< debug
else --< 参数是图片名
return frame:expandTemplate{ title = 'Infobox/item/single image', args = {
return frame:expandTemplate{ title = 'Infobox/item/single image', args = {
image = arg, caption = parent.args['caption']
image = arg, caption = parent.args['caption']
} }
} }
end
end

-- 去掉 'gallery:'
arg = string.sub(arg, 9, -1)

images = {}
for image_line in arg:gmatch('[^\r\n]+') do

local sep = image_line:find('::')
-- 文件名
local file_name = string.sub(image_line, 1, sep-1)
local remain = string.sub(image_line, sep+2, -1)

if remain == nil then
return '<span style="font-size: 24px; font-weight: bold; color: red">'
.. '画廊语法不正确, 应为:'
.. '"gallery:<br/>'
.. '文件名 :: 标签页名<br/>'
.. '文件名 :: 标签页名 :: 可选的图注<br/>'
.. '…" (各部分前后的空格可有可无, 可多可少) </span>'
.. '[[Category:含有在信息框中使用了不正确的画廊语法的页面]]'
end

-- 标签名
local tab_name = ''
-- 图注
local caption = ''
sep = remain:find('::')
if sep == nil then
tab_name = remain
else
tab_name = string.sub(remain, 1, sep-1)
caption = string.sub(remain, sep+2, -1)
end

table.insert(images, {
file_name = mw.text.trim(file_name),
tab_name = mw.text.trim(tab_name),
caption = mw.text.trim(caption)
})
end

local tab_lis = ''
local image_divs = ''
for i, image in pairs(images) do

local current = ''
if i == 1 then current = 'current' end

tab_lis = tab_lis .. string.format('<li class="pi-tab-link '
.. 'pi-item-spacing %s" data-pi-tab="pi-tab-%d">',
current, (i-1)) .. image['tab_name'] .. '</li>'

local image = frame:expandTemplate{ title = 'Infobox/item/single image', args = {
image = image['file_name'],
caption = image['caption']
} }

image_divs = image_divs .. string.format(
'<div class="pi-image-collection-tab-content %s" id="pi-tab-%s">',
current, (i-1)) .. image .. '</div>'
end

local tabs_ul = '<ul class="pi-image-collection-tabs">'
.. tab_lis .. '</ul>'

return '[[Category:含有包含画廊的信息框的页面]]<div class="pi-image-collection">'
.. tabs_ul .. image_divs .. '</div>'
end
end


return p
return p

-- </nowiki>

2022年9月21日 (三) 23:44的最新版本

可在模块:Infobox/item/image/doc创建此模块的帮助文档

-- <nowiki>
local p = {}

function p.make()
    local frame = mw.getCurrentFrame()
    local parent = frame:getParent()
    local arg = parent.args['image']

    local gallery_mark_index = arg:find('gallery:')
    if gallery_mark_index ~= 1 then --< 单张图片
        if string.byte(arg, 1) == 127 then
            return '<span style="font-size: 24px; font-weight: bold; color: red">'
                .. '请更新画廊的语法: "<gallery>(换行)文件名|标签页名(换行)…</gallery>" '
                .. '→ "gallery:(换行)文件名 :: 标签页名(换行)…" </span>'
                .. '[[Category:含有在信息框中使用了不正确的画廊语法的页面]]'
        end
        return frame:expandTemplate{ title = 'Infobox/item/single image', args = {
            image = arg, caption = parent.args['caption']
        } }
    end

    -- 去掉 'gallery:'
    arg = string.sub(arg, 9, -1)

    images = {}
    for image_line in arg:gmatch('[^\r\n]+') do

        local sep = image_line:find('::')
        -- 文件名
        local file_name = string.sub(image_line, 1, sep-1)
        local remain = string.sub(image_line, sep+2, -1)

        if remain == nil then
            return '<span style="font-size: 24px; font-weight: bold; color: red">'
                .. '画廊语法不正确, 应为:'
                .. '"gallery:<br/>'
                .. '文件名 :: 标签页名<br/>'
                .. '文件名 :: 标签页名 :: 可选的图注<br/>'
                .. '…" (各部分前后的空格可有可无, 可多可少) </span>'
                .. '[[Category:含有在信息框中使用了不正确的画廊语法的页面]]'
        end

        -- 标签名
        local tab_name = ''
        -- 图注
        local caption = ''
        sep = remain:find('::')
        if sep == nil then
            tab_name = remain
        else
            tab_name = string.sub(remain, 1, sep-1)
            caption = string.sub(remain, sep+2, -1)
        end

        table.insert(images, {
            file_name = mw.text.trim(file_name),
            tab_name = mw.text.trim(tab_name),
            caption = mw.text.trim(caption)
        })
    end

    local tab_lis = ''
    local image_divs = ''
    for i, image in pairs(images) do

        local current = ''
        if i == 1 then current = 'current' end

        tab_lis = tab_lis .. string.format('<li class="pi-tab-link '
            .. 'pi-item-spacing %s" data-pi-tab="pi-tab-%d">',
            current, (i-1)) .. image['tab_name'] .. '</li>'

        local image = frame:expandTemplate{ title = 'Infobox/item/single image', args = {
            image = image['file_name'],
            caption = image['caption']
        } }

        image_divs = image_divs .. string.format(
            '<div class="pi-image-collection-tab-content %s" id="pi-tab-%s">',
            current, (i-1)) .. image .. '</div>'
    end

    local tabs_ul = '<ul class="pi-image-collection-tabs">'
        .. tab_lis .. '</ul>'

    return '[[Category:含有包含画廊的信息框的页面]]<div class="pi-image-collection">'
        .. tabs_ul .. image_divs .. '</div>'
end

return p

-- </nowiki>