模块:CharacterJsonLD
来自Blockland Wiki
更多操作
此模块的文档可以在模块:CharacterJsonLD/doc创建
local p = {}
local function clean(value)
if not value 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(
'^[Ff]ile:', ''
):gsub('^文件:', '')
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)
local JsonLD = mw.ext.JsonLD
if not JsonLD then
return ''
end
local parent = frame:getParent()
local args = parent and parent.args or frame.args
local title = mw.title.getCurrentTitle()
-- Do not publish entity data on template pages
if title.namespace ~= 0 then
return ''
end
local name = clean(args['名称']) or title.text
local pageUrl = title:fullUrl(''):gsub('%?$', '')
local entity = {
["@type"] = "Person",
["@id"] = pageUrl .. '#character',
["name"] = name,
["url"] = pageUrl
}
local description = clean(args['简介'])
if description then
entity.description = description
end
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
local images = {}
addImage(
images,
args['头像'],
name .. '的头像'
)
addImage(
images,
args['立绘'],
name .. '的角色立绘'
)
if #images > 0 then
entity.image = images
end
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
JsonLD.setMainEntity(entity)
return 'JSONLD_DEBUG_SUCCESS: ' .. name
end
return p