模块:L

来自东方活动维基
稀神灵梦留言 | 贡献2024年5月26日 (日) 23:42的版本 (创建页面,内容为“local L = {} local getArgs = require('Module:Arguments').getArgs local isInArray = require('Module:Utils').isInArray local isInDict = require('Module:Utils').isInDict local yesno = require('Module:Yesno') local linksTable = { ['bv'] = 'https://www.bilibili.com/video/BV', ['av'] = 'https://www.bilibili.com/video/av', ['sm'] = 'https://nicovideo.jp/watch/sm', ['nm'] = 'https://nicovideo.jp/watch/nm', ['ac'] = 'https:///www.acfun.cn/v/ac',…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

可在模块:L/doc创建此模块的帮助文档

local L = {}

local getArgs = require('Module:Arguments').getArgs
local isInArray = require('Module:Utils').isInArray
local isInDict = require('Module:Utils').isInDict
local yesno = require('Module:Yesno')

local linksTable = {
    ['bv'] = 'https://www.bilibili.com/video/BV',
    ['av'] = 'https://www.bilibili.com/video/av',
    ['sm'] = 'https://nicovideo.jp/watch/sm',
    ['nm'] = 'https://nicovideo.jp/watch/nm',
    ['ac'] = 'https:///www.acfun.cn/v/ac',
    ['au'] = 'https://www.bilibili.com/audio/au',
    ['cv'] = 'https://www.bilibili.com/read/cv',
    ['rl'] = 'https://www.bilibili.com/read/readlist/rl',
    ['ml'] = 'https://www.bilibili.com/medialist/play/ml',
    ['ep'] = 'https://www.bilibili.com/bangumi/play/ep',
    ['ss'] = 'https://www.bilibili.com/bangumi/play/ss',
    ['im'] = 'https://seiga.nicovideo.jp/seiga/im',
    ['pid'] = 'https://www.pixiv.net/artworks/',
    ['uid'] = 'https://space.bilibili.com/'
}

local function parseParam(paramTable)
    local res = {}
	local spliter = #res == 0 and '?' or '&'
    for k, v in pairs(paramTable) do
    	-- mw.log(k, v)
        if #v > 0 then
            table.insert(res, spliter .. mw.text.nowiki(k .. '=' .. v))
        end
    end
    return table.concat(res, '')
end

function L.genLink(id, params)
    local prefix =
        linksTable[id:sub(1, 3):lower()] and id:sub(1, 3):lower() or
            id:sub(1, 2):lower()
	
    local digit = id:sub(#prefix + 1)
    if not linksTable[prefix] then
        return id
    end
    local link = linksTable[prefix] .. digit
    return link
end

function L.generate(frame)
    --[=[
      生成特定号码对应链接的模块
      @param 第一个参数,默认的视频号,会被archive参数覆盖
      @param 第二个参数,默认的显示文本,为空时被第一个参数或archive参数覆盖
      @param _status 状态
      @param p 分p号
      @param t 时间号
      @param archive 搬运地址或者视频号
      @param origin 原地址或者视频号,没什么用,准备deprecate
      @param option 样式的设置
      @return html
    --]=]
    local args = getArgs(frame)

    local id = args[1]
    local archive = args["archive"] or args["转载"] or args["补档"]
    local text = args[2] or id
    local _status = args["status"]
    local option = args["option"]
	-- TODO 拆分错误处理函数或者模块
	
    local part = args["p"] or "" -- 为 1 的时候忽略该值
    local _time = args["t"] or ""
    local paramTable = {["p"] = part, ["t"] = _time}
    local params = parseParam(paramTable)
	local link = L.genLink(id, params)

    local category = option ~= "nocategory" and
                         "[[分类:有失效链接的页面]]" or ""
    local partText = (#part > 0 and "<sup>第" .. part .. "P</sup>") or ''
    local titleText = ""

    if archive then
        titleText = "原视频号为 " .. args[1]
    elseif args['origin'] then
        titleText = "此视频搬运自 " .. args['origin']
    else
        titleText = _status and "此作品目前处于" .. _status ..
                        "状态。" or id
    end

    -- TODO 使用mediawiki自带模块处理html
    local res
    local _output = (yesno(args['pl']) and '' or '%s[') .. link ..
                       (yesno(args['pl']) and '' or ' %s]%s')
    local statusTable = {
        normal = _output:format('',
                               "<span title='" .. titleText .. "'>" .. text ..
                                   partText .. "</span>", '')
    }

	-- TODO 完全使用模板字符串定义statusTable
    if _status then
        statusTable = {
            normal = statusTable.normal,
            invalidLink = _output:format(category .. "<span class='plainlinks'>",
                                        "<span title='" .. titleText ..
                                            "' style='color:grey'><s>" .. text ..
                                            partText .. "</s></span>", "</span>"),
            invalidPart = _output:format(category,
                                        "<span title='" .. titleText .. "'>" ..
                                            text .. "<span style='color:grey'>" ..
                                            partText .. "</span>", ''),
            reproduceProhibited = statusTable.normal ..
                "<span style='color:red'><small>(禁止转载)</small></span>"
        }
        if _status:isInArray({"失效", "删除", "削除", "非公开"}) then
            res = statusTable.invalidLink
        elseif #part > 0 and
            _status:isInArray({"分p失效", "分p删除", "分p削除"}) then
            res = statusTable.invalidPart
        elseif _status == "禁止转载" then
            res = statusTable.reproduceProhibited
        else
            res = statusTable.normal
        end
    else
        res = statusTable.normal
    end
    
    -- TODO 使用栈处理各个标签组件
    if archive then
    	res = res .. '<div class="repost-circle">['..L.genLink(archive)..' 转]</div>'
	end
    
    return res
end

return L