Module:ComicInfoboxWikitext:修订间差异

来自有兽档案馆
(// Edit via Wikiplus 测试)
标签已被回退
(// Edit via Wikiplus)
标签已被回退
第1行: 第1行:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs



function p.main(frame)
function p.main(frame)
第8行: 第7行:
local function getComicData(episode)
local function getComicData(episode)
-- 去ComicData.json获取漫画数据
-- 去ComicData.json获取漫画数据
local comicList = mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate { title = '漫画信息框/ComicData.json' })
local comicList = mw.text.jsonDecode(mw.title.new('Template:漫画信息框/ComicData.json'):getContent())


return comicList[episode]
return comicList[episode]
第15行: 第14行:
-- 用模板数据替换自动获取的数据
-- 用模板数据替换自动获取的数据
local data = {}
local data = {}
-- 当ComicData.json中不存在此话数据时 直接使用模板数据 防止报错
for k, v in pairs(data_from_json) do
data[k] = v
if data_from_json ~= nil then
for k, v in pairs(data_from_json) do
data[k] = v
end
end
end
for k, v in pairs(data_from_args) do
for k, v in pairs(data_from_args) do
if v ~= '' then
if v ~= '' then
-- 当模板数据为空值时 使用自动获取的数据
-- 当自动获取的数据为空值时 使用模板数据
data[k] = v
data[k] = v
end
end
第28行: 第30行:
end
end
local function fillInfobox(data, episode)
local function fillInfobox(data, episode)
-- 用漫画数据生成信息框
local function argsCheck(args)
-- 检查缺失数据 生成提示
local noticetext = ''
if args['color'] == nil then
noticetext = noticetext .. '<li>此信息框缺少背景色,请添加<code>color</code>参数。</li>'
end
if args['title_weibo'] == nil then
noticetext = noticetext .. '<li>此信息框缺少微博标题,请添加<code>title_weibo</code>参数。</li>'
end
if args['title_bili'] == nil then
noticetext = noticetext .. '<li>此信息框缺少B漫标题,请添加<code>title_bili</code>参数。</li>'
end
if args['episode'] == nil then
noticetext = noticetext .. '<li>此信息框缺少时间线,请添加<code>episode</code>参数。</li>'
end
if args['date_weibo'] == nil then
noticetext = noticetext .. '<li>此信息框缺少微博发布时间,请添加<code>date_weibo</code>参数。</li>'
end
if args['date_bili'] == nil then
noticetext = noticetext .. '<li>此信息框缺少B漫发布时间,请添加<code>date_bili</code>参数。</li>'
end


local args = {
if noticetext == '' then
number = data and data['number'] or ' ',
return noticetext
color = data and data['color'] or ' ',
end
--我知道这里写死很不礼貌 但是能用就懒得管了 少调一个模板榨点性能
title_weibo = data and data['title_weibo'] or ' ',
return '<table class="mw-collapsible mw-collapsed wikitable"><tr><th>信息框提示</th></tr><tr><td>' ..
title_bili = data and data['title_bili'] or ' ',
episode = data and data['episode'] or ' ',
noticetext .. '</td></tr></table>'
end
date_weibo = data and data['date_weibo'] or ' ',
date_bili = data and data['date_bili'] or ' ',
number = data and episode or ' '
}


local res = {}
local text = ''
local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
local res = ''


-- 补上前后话数据
res[#res + 1] = frame:expandTemplate { title = 'nowiki', args = { '{{漫画信息框' } }
for key, value in pairs(args) do
if data['next'] == nil then
res[#res + 1] = '<br>'
data['next'] = '[[第' .. episode + 1 .. '话]]'
text = '\n|' .. key .. '=' .. value
res[#res + 1] = frame:expandTemplate { title = 'nowiki', args = { text } }
end
end
if data['last'] == nil then
res[#res + 1] = frame:expandTemplate { title = 'nowiki', args = { '}}' } }
data['last'] = '[[第' .. episode - 1 .. '话]]'
return table.concat(res)
end
--return mw.getCurrentFrame():expandTemplate { title = 'Infobox', args = args }
if data['image'] == nil then
data['image'] = '[[File:第' .. episode .. '话标题卡.jpg]]'
end
if data['imagestyle'] == nil then
data['imagestyle'] = ''
end
if data['captionstyle'] == nil then
data['captionstyle'] = ''
end

data['labelstyle'] = data and data['labelstyle'] or ' '
data['headerstyle'] = data and data['headerstyle'] or ' '

--把color参数设置的样式放在前面 防止自定义的几个style被覆盖
if data['color'] == nil then
data['labelstyle'] = 'background:ced;' .. data['labelstyle']
data['headerstyle'] = 'background:ced;' .. data['headerstyle']
else
data['labelstyle'] = 'background:' .. data['color'] .. ';' .. data['labelstyle']
data['headerstyle'] = 'background:' .. data['color'] .. ';' .. data['headerstyle']
end

--检查缺失数据
local forcheck_args = {
color = data['color'],
title_weibo = data['title_weibo'],
title_bili = data['title_bili'],
episode = data['episode'],
date_weibo = data['date_weibo'],
date_bili = data['date_bili'],
number = episode
}
local noticetext = argsCheck(forcheck_args)

data['color'] = data and data['color'] or ' '
data['title_weibo'] = data and data['title_weibo'] or ' '
data['title_bili'] = data and data['title_bili'] or ' '
data['episode'] = data and data['episode'] or ' '
data['date_weibo'] = data and data['date_weibo'] or ' '
data['date_bili'] = data and data['date_bili'] or ' '

-- 用漫画数据生成信息框
local infobox_args = {
name = '漫画信息框',
bodystyle = 'border-radius: 10px',
title = '本话信息',
image = data['image'],
header1 = '基本信息',
label2 = '话数',
data2 = '[[ComicInfo:number::' .. episode .. ']]',
label3 = '微博标题',
data3 = '[[ComicInfo:titleWeibo::' .. data['title_weibo'] .. ']]',
label4 = 'B漫标题',
data4 = '[[ComicInfo:titleBili::' .. data['title_bili'] .. ']]',
label5 = '所属篇目',
data5 = '[[ComicInfo:episode::' .. data['episode'] .. ']]',
label6 = '微博发布日期',
data6 = '[[ComicInfo:dateWeibo::' .. data['date_weibo'] .. ']]',
label7 = 'B漫发布日期',
data7 = '[[ComicInfo:dateBili::' .. data['date_bili'] .. ']]',
header8 = '阅读向导',
label9 = '上一话',
data9 = data['last'],
label10 = '下一话',
data10 = data['next'],
imagestyle = data['imagestyle'],
labelstyle = data['labelstyle'],
headerstyle = data['headerstyle']
}
res = res .. frame:expandTemplate { title = 'Infobox', args = infobox_args }

-- 添加缺失数据提示
if noticetext ~= '' then
res = res .. noticetext
end

return res
end
end


第59行: 第152行:
local episode = 1
local episode = 1
-- 获取话数
-- 获取话数
if args[1] ~= nil then
if tonumber(args[1]) ~= nil then
episode = tonumber(args[1])
episode = tonumber(args[1])
end
end


if args['number'] ~= nil then
if tonumber(args['number']) ~= nil then
episode = tonumber(args['number'])
episode = tonumber(args['number'])
end
end
--获取自动补全用的数据
local episodeData_autoFill = getComicData(episode)
local episodeData_autoFill = getComicData(episode)
--合并数据
local episodeData = margeData(episodeData_autoFill, args)
local episodeData = margeData(episodeData_autoFill, args)
--生成信息框

return fillInfobox(episodeData, episode)
return fillInfobox(episodeData, episode)
end
end

2023年9月2日 (六) 19:52的版本

文档图示 模块文档[创建] [跳转到代码]

本模块还没有文档页面。

您可以创建文档以让编者更好地理解本模块的用途。
编者可以在本模块的沙盒创建 | 镜像和测试样例创建页面进行实验。
请将模块自身所属的分类添加在文档中。本模块的子页面
local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.main(frame)
    local args = getArgs(frame)

    local function getComicData(episode)
        -- 去ComicData.json获取漫画数据
        local comicList = mw.text.jsonDecode(mw.title.new('Template:漫画信息框/ComicData.json'):getContent())

        return comicList[episode]
    end
    local function margeData(data_from_json, data_from_args)
        -- 用模板数据替换自动获取的数据
        local data = {}
        -- 当ComicData.json中不存在此话数据时 直接使用模板数据 防止报错
        if data_from_json ~= nil then
            for k, v in pairs(data_from_json) do
                data[k] = v
            end
        end
        for k, v in pairs(data_from_args) do
            if v ~= '' then
                -- 当自动获取的数据为空值时 使用模板数据
                data[k] = v
            end
        end

        return data
    end
    local function fillInfobox(data, episode)
        local function argsCheck(args)
            -- 检查缺失数据 生成提示
            local noticetext = ''
            if args['color'] == nil then
                noticetext = noticetext .. '<li>此信息框缺少背景色,请添加<code>color</code>参数。</li>'
            end
            if args['title_weibo'] == nil then
                noticetext = noticetext .. '<li>此信息框缺少微博标题,请添加<code>title_weibo</code>参数。</li>'
            end
            if args['title_bili'] == nil then
                noticetext = noticetext .. '<li>此信息框缺少B漫标题,请添加<code>title_bili</code>参数。</li>'
            end
            if args['episode'] == nil then
                noticetext = noticetext .. '<li>此信息框缺少时间线,请添加<code>episode</code>参数。</li>'
            end
            if args['date_weibo'] == nil then
                noticetext = noticetext .. '<li>此信息框缺少微博发布时间,请添加<code>date_weibo</code>参数。</li>'
            end
            if args['date_bili'] == nil then
                noticetext = noticetext .. '<li>此信息框缺少B漫发布时间,请添加<code>date_bili</code>参数。</li>'
            end

            if noticetext == '' then
                return noticetext
            end
            --我知道这里写死很不礼貌 但是能用就懒得管了 少调一个模板榨点性能
            return '<table class="mw-collapsible mw-collapsed wikitable"><tr><th>信息框提示</th></tr><tr><td>' ..
                noticetext .. '</td></tr></table>'
        end

        local frame = mw.getCurrentFrame()
        local res = ''

        -- 补上前后话数据
        if data['next'] == nil then
            data['next'] = '[[第' .. episode + 1 .. '话]]'
        end
        if data['last'] == nil then
            data['last'] = '[[第' .. episode - 1 .. '话]]'
        end
        if data['image'] == nil then
            data['image'] = '[[File:第' .. episode .. '话标题卡.jpg]]'
        end
        if data['imagestyle'] == nil then
            data['imagestyle'] = ''
        end
        if data['captionstyle'] == nil then
            data['captionstyle'] = ''
        end

        data['labelstyle'] = data and data['labelstyle'] or ' '
        data['headerstyle'] = data and data['headerstyle'] or ' '

        --把color参数设置的样式放在前面 防止自定义的几个style被覆盖
        if data['color'] == nil then
            data['labelstyle'] = 'background:ced;' .. data['labelstyle']
            data['headerstyle'] = 'background:ced;' .. data['headerstyle']
        else
            data['labelstyle'] = 'background:' .. data['color'] .. ';' .. data['labelstyle']
            data['headerstyle'] = 'background:' .. data['color'] .. ';' .. data['headerstyle']
        end

        --检查缺失数据
        local forcheck_args = {
            color = data['color'],
            title_weibo = data['title_weibo'],
            title_bili = data['title_bili'],
            episode = data['episode'],
            date_weibo = data['date_weibo'],
            date_bili = data['date_bili'],
            number = episode
        }
        local noticetext = argsCheck(forcheck_args)

        data['color'] = data and data['color'] or ' '
        data['title_weibo'] = data and data['title_weibo'] or ' '
        data['title_bili'] = data and data['title_bili'] or ' '
        data['episode'] = data and data['episode'] or ' '
        data['date_weibo'] = data and data['date_weibo'] or ' '
        data['date_bili'] = data and data['date_bili'] or ' '

        -- 用漫画数据生成信息框
        local infobox_args = {
            name = '漫画信息框',
            bodystyle = 'border-radius: 10px',
            title = '本话信息',
            image = data['image'],
            header1 = '基本信息',
            label2 = '话数',
            data2 = '[[ComicInfo:number::' .. episode .. ']]',
            label3 = '微博标题',
            data3 = '[[ComicInfo:titleWeibo::' .. data['title_weibo'] .. ']]',
            label4 = 'B漫标题',
            data4 = '[[ComicInfo:titleBili::' .. data['title_bili'] .. ']]',
            label5 = '所属篇目',
            data5 = '[[ComicInfo:episode::' .. data['episode'] .. ']]',
            label6 = '微博发布日期',
            data6 = '[[ComicInfo:dateWeibo::' .. data['date_weibo'] .. ']]',
            label7 = 'B漫发布日期',
            data7 = '[[ComicInfo:dateBili::' .. data['date_bili'] .. ']]',
            header8 = '阅读向导',
            label9 = '上一话',
            data9 = data['last'],
            label10 = '下一话',
            data10 = data['next'],
            imagestyle = data['imagestyle'],
            labelstyle = data['labelstyle'],
            headerstyle = data['headerstyle']
        }
        res = res .. frame:expandTemplate { title = 'Infobox', args = infobox_args }

        -- 添加缺失数据提示
        if noticetext ~= '' then
            res = res .. noticetext
        end

        return res
    end

    -- 默认输出第一话的信息框
    local episode = 1
    -- 获取话数
    if tonumber(args[1]) ~= nil then
        episode = tonumber(args[1])
    end

    if tonumber(args['number']) ~= nil then
        episode = tonumber(args['number'])
    end
    --获取自动补全用的数据
    local episodeData_autoFill = getComicData(episode)
    --合并数据
    local episodeData = margeData(episodeData_autoFill, args)
    --生成信息框
    return fillInfobox(episodeData, episode)
end

return p