跳转到内容
打开/关闭菜单
  • 451 条目
  • 798 文件
  • 22 用户
  • 8955 编辑
Blockland Wiki
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:CharacterJsonLD:修订间差异

来自Blockland Wiki
无编辑摘要
无编辑摘要
第3行: 第3行:


local function clean(value)
local function clean(value)
     if not value then
     if type(value) ~= 'string' then
         return nil
         return nil
     end
     end
第23行: 第23行:
     end
     end


     filename = filename:gsub(
     filename = filename:gsub('^文件:', '')
        '^[Ff]ile:', ''
     filename = filename:gsub('^[Ff]ile:', '')
     ):gsub('^文件:', '')
 
     filename = mw.text.trim(filename)
     filename = mw.text.trim(filename)


第46行: 第44行:


function p.main(frame)
function p.main(frame)
    local JsonLD = mw.ext.JsonLD


     if not JsonLD then
    -- Support both template and direct #invoke
         return ''
    local args = frame.args
    local parent = frame:getParent()
 
     if not clean(args['名称']) and parent then
         args = parent.args
     end
     end
    local parent = frame:getParent()
    local args = parent and parent.args or frame.args


     local title = mw.title.getCurrentTitle()
     local title = mw.title.getCurrentTitle()


     -- Do not publish entity data on template pages
     -- Only main namespace articles
     if title.namespace ~= 0 then
     if title.namespace ~= 0 then
         return ''
         return ''
第63行: 第61行:


     local name = clean(args['名称']) or title.text
     local name = clean(args['名称']) or title.text
 
     local pageUrl = tostring(title:fullUrl())
     local pageUrl = title:fullUrl(''):gsub('%?$', '')


     local entity = {
     local entity = {
第79行: 第76行:
     end
     end


    -- Alternate names
     local aliases = {}
     local aliases = {}


第98行: 第96行:
     end
     end


    -- Images
     local images = {}
     local images = {}


     addImage(
     addImage(images, args['头像'], name .. '的头像')
        images,
     addImage(images, args['立绘'], name .. '的角色立绘')
        args['头像'],
        name .. '的头像'
    )
 
     addImage(
        images,
        args['立绘'],
        name .. '的角色立绘'
    )


     if #images > 0 then
     if #images > 0 then
第116行: 第106行:
     end
     end


    -- Optional music group
     local group = clean(args['所属组合'])
     local group = clean(args['所属组合'])


第131行: 第122行:
     end
     end


     JsonLD.setMainEntity(entity)
     -- DynamicJsonLD integration
    if not mw.ext or not mw.ext.JsonLD then
        return '<strong class="error">' ..
            'CharacterJsonLD: DynamicJsonLD Lua API unavailable' ..
            '</strong>'
    end
 
    local JsonLD = mw.ext.JsonLD
 
    if type(JsonLD.setMainEntity) ~= 'function' then
        return '<strong class="error">' ..
            'CharacterJsonLD: setMainEntity unavailable' ..
            '</strong>'
    end
 
    local ok, err = pcall(function()
        JsonLD.setMainEntity(entity)
    end)
 
    if not ok then
        return '<strong class="error">' ..
            'CharacterJsonLD: ' ..
            mw.text.encode(tostring(err)) ..
            '</strong>'
    end
 
    -- Temporary debug mode: |调试=1
    if clean(args['调试']) == '1' then
        return 'JSONLD_SUCCESS: ' .. name
    end


     return 'JSONLD_DEBUG_SUCCESS: ' .. name
     return ''
end
end


return p
return p

2026年10月19日 (一) 16:56的版本

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


local p = {}

local function clean(value)
    if type(value) ~= 'string' then
        return nil
    end

    value = mw.text.trim(value)

    if value == '' then
        return nil
    end

    return value
end

local function fileUrl(filename)
    filename = clean(filename)

    if not filename then
        return nil
    end

    filename = filename:gsub('^文件:', '')
    filename = filename:gsub('^[Ff]ile:', '')
    filename = mw.text.trim(filename)

    return 'https://blockland.wiki/wiki/Special:FilePath/'
        .. mw.uri.encode(filename, 'PATH')
end

local function addImage(images, filename, label)
    local url = fileUrl(filename)

    if url then
        table.insert(images, {
            ["@type"] = "ImageObject",
            ["url"] = url,
            ["name"] = label
        })
    end
end

function p.main(frame)

    -- Support both template and direct #invoke
    local args = frame.args
    local parent = frame:getParent()

    if not clean(args['名称']) and parent then
        args = parent.args
    end

    local title = mw.title.getCurrentTitle()

    -- Only main namespace articles
    if title.namespace ~= 0 then
        return ''
    end

    local name = clean(args['名称']) or title.text
    local pageUrl = tostring(title:fullUrl())

    local entity = {
        ["@type"] = "Person",
        ["@id"] = pageUrl .. '#character',
        ["name"] = name,
        ["url"] = pageUrl
    }

    local description = clean(args['简介'])

    if description then
        entity.description = description
    end

    -- Alternate names
    local aliases = {}

    for _, key in ipairs({
        '本名',
        '英文名',
        '别名',
        '其他名称'
    }) do
        local value = clean(args[key])

        if value and value ~= name then
            table.insert(aliases, value)
        end
    end

    if #aliases > 0 then
        entity.alternateName = aliases
    end

    -- Images
    local images = {}

    addImage(images, args['头像'], name .. '的头像')
    addImage(images, args['立绘'], name .. '的角色立绘')

    if #images > 0 then
        entity.image = images
    end

    -- Optional music group
    local group = clean(args['所属组合'])

    if group then
        entity.memberOf = {
            ["@type"] = "MusicGroup",
            ["name"] = group
        }

        local groupUrl = clean(args['组合链接'])

        if groupUrl then
            entity.memberOf.url = groupUrl
        end
    end

    -- DynamicJsonLD integration
    if not mw.ext or not mw.ext.JsonLD then
        return '<strong class="error">' ..
            'CharacterJsonLD: DynamicJsonLD Lua API unavailable' ..
            '</strong>'
    end

    local JsonLD = mw.ext.JsonLD

    if type(JsonLD.setMainEntity) ~= 'function' then
        return '<strong class="error">' ..
            'CharacterJsonLD: setMainEntity unavailable' ..
            '</strong>'
    end

    local ok, err = pcall(function()
        JsonLD.setMainEntity(entity)
    end)

    if not ok then
        return '<strong class="error">' ..
            'CharacterJsonLD: ' ..
            mw.text.encode(tostring(err)) ..
            '</strong>'
    end

    -- Temporary debug mode: |调试=1
    if clean(args['调试']) == '1' then
        return 'JSONLD_SUCCESS: ' .. name
    end

    return ''
end

return p