模块:CharacterJsonLD:修订间差异
来自Blockland Wiki
更多操作
无编辑摘要 |
无编辑摘要 |
||
| (未显示同一用户的1个中间版本) | |||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
-- ========================================== | |||
-- Blockland Wiki - Character JSON-LD | |||
-- Universal character structured data module | |||
-- ========================================== | |||
local function clean(value) | local function clean(value) | ||
| 第15行: | 第21行: | ||
end | end | ||
-- Build a direct MediaWiki file URL | |||
local function fileUrl(filename) | local function fileUrl(filename) | ||
filename = clean(filename) | filename = clean(filename) | ||
| 第30行: | 第37行: | ||
end | end | ||
local function | -- Get DynamicJsonLD Lua library | ||
local function getJsonLD() | |||
if mw.ext and mw.ext.JsonLD then | |||
return mw.ext.JsonLD | |||
end | |||
local ok, lib = pcall( | |||
require, | |||
'mw.ext.JsonLD' | |||
) | |||
if ok and type(lib) == 'table' then | |||
return lib | |||
end | end | ||
return nil | |||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
-- | -- Parameters from template or #invoke | ||
local args = frame.args | local args = frame.args | ||
local parent = frame:getParent() | local parent = frame:getParent() | ||
| 第61行: | 第75行: | ||
local name = clean(args['名称']) or title.text | local name = clean(args['名称']) or title.text | ||
local pageUrl = tostring(title:fullUrl()) | local pageUrl = tostring(title:fullUrl()) | ||
-- ========================================== | |||
-- Main character entity | |||
-- ========================================== | |||
local entity = { | local entity = { | ||
| 第69行: | 第87行: | ||
} | } | ||
-- Description | |||
local description = clean(args['简介']) | local description = clean(args['简介']) | ||
| 第75行: | 第94行: | ||
end | end | ||
-- ========================================== | |||
-- Alternate names | -- Alternate names | ||
-- Avoid Lua arrays for DynamicJsonLD | |||
-- ========================================== | |||
local aliases = {} | local aliases = {} | ||
local seen = {} | |||
for _, key in ipairs({ | for _, key in ipairs({ | ||
| 第86行: | 第110行: | ||
local value = clean(args[key]) | local value = clean(args[key]) | ||
if value and value ~= name then | if value and value ~= name and not seen[value] then | ||
table.insert(aliases, value) | table.insert(aliases, value) | ||
seen[value] = true | |||
end | end | ||
end | end | ||
if #aliases > 0 then | if #aliases > 0 then | ||
entity.alternateName = aliases | entity.alternateName = table.concat( | ||
aliases, | |||
'、' | |||
) | |||
end | |||
-- ========================================== | |||
-- Primary avatar | |||
-- ========================================== | |||
local avatar = fileUrl(args['头像']) | |||
if avatar then | |||
entity.image = { | |||
["@type"] = "ImageObject", | |||
["name"] = name .. '的头像', | |||
["url"] = avatar, | |||
["contentUrl"] = avatar | |||
} | |||
end | end | ||
-- | -- ========================================== | ||
-- Full-body illustration | |||
-- ========================================== | |||
local artwork = fileUrl(args['立绘']) | |||
if # | if artwork then | ||
entity.image = | local illustration = { | ||
["@type"] = "ImageObject", | |||
["name"] = name .. '的角色立绘', | |||
["url"] = artwork, | |||
["contentUrl"] = artwork, | |||
["about"] = { | |||
["@id"] = pageUrl .. '#character' | |||
} | |||
} | |||
-- Keep main image as avatar | |||
-- Associate illustration with character | |||
if avatar then | |||
entity.subjectOf = illustration | |||
else | |||
entity.image = illustration | |||
end | |||
end | end | ||
-- ========================================== | |||
-- Optional music group | -- Optional music group | ||
-- ========================================== | |||
local group = clean(args['所属组合']) | local group = clean(args['所属组合']) | ||
if group then | if group then | ||
local musicGroup = { | |||
["@type"] = "MusicGroup", | ["@type"] = "MusicGroup", | ||
["name"] = group | ["name"] = group | ||
| 第117行: | 第179行: | ||
if groupUrl then | if groupUrl then | ||
musicGroup.url = groupUrl | |||
end | end | ||
entity.memberOf = musicGroup | |||
end | end | ||
-- ========================================== | |||
-- DynamicJsonLD integration | |||
-- ========================================== | |||
local JsonLD = getJsonLD() | |||
local JsonLD = | |||
if not JsonLD then | |||
if not JsonLD then | |||
return '<strong class="error">' .. | return '<strong class="error">' .. | ||
'CharacterJsonLD: Lua | 'CharacterJsonLD: DynamicJsonLD Lua API unavailable' .. | ||
'</strong>' | '</strong>' | ||
end | end | ||
if type(JsonLD.setMainEntity) ~= 'function' then | if type(JsonLD.setMainEntity) ~= 'function' then | ||
| 第160行: | 第214行: | ||
end | end | ||
-- | -- Optional debugging | ||
if clean(args['调试']) == '1' then | if clean(args['调试']) == '1' then | ||
return 'JSONLD_SUCCESS: ' .. name | return 'JSONLD_SUCCESS: ' .. name | ||
2026年10月19日 (一) 17:02的最新版本
此模块的文档可以在模块:CharacterJsonLD/doc创建
local p = {}
-- ==========================================
-- Blockland Wiki - Character JSON-LD
-- Universal character structured data module
-- ==========================================
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
-- Build a direct MediaWiki file URL
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
-- Get DynamicJsonLD Lua library
local function getJsonLD()
if mw.ext and mw.ext.JsonLD then
return mw.ext.JsonLD
end
local ok, lib = pcall(
require,
'mw.ext.JsonLD'
)
if ok and type(lib) == 'table' then
return lib
end
return nil
end
function p.main(frame)
-- Parameters from template or #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())
-- ==========================================
-- Main character entity
-- ==========================================
local entity = {
["@type"] = "Person",
["@id"] = pageUrl .. '#character',
["name"] = name,
["url"] = pageUrl
}
-- Description
local description = clean(args['简介'])
if description then
entity.description = description
end
-- ==========================================
-- Alternate names
-- Avoid Lua arrays for DynamicJsonLD
-- ==========================================
local aliases = {}
local seen = {}
for _, key in ipairs({
'本名',
'英文名',
'别名',
'其他名称'
}) do
local value = clean(args[key])
if value and value ~= name and not seen[value] then
table.insert(aliases, value)
seen[value] = true
end
end
if #aliases > 0 then
entity.alternateName = table.concat(
aliases,
'、'
)
end
-- ==========================================
-- Primary avatar
-- ==========================================
local avatar = fileUrl(args['头像'])
if avatar then
entity.image = {
["@type"] = "ImageObject",
["name"] = name .. '的头像',
["url"] = avatar,
["contentUrl"] = avatar
}
end
-- ==========================================
-- Full-body illustration
-- ==========================================
local artwork = fileUrl(args['立绘'])
if artwork then
local illustration = {
["@type"] = "ImageObject",
["name"] = name .. '的角色立绘',
["url"] = artwork,
["contentUrl"] = artwork,
["about"] = {
["@id"] = pageUrl .. '#character'
}
}
-- Keep main image as avatar
-- Associate illustration with character
if avatar then
entity.subjectOf = illustration
else
entity.image = illustration
end
end
-- ==========================================
-- Optional music group
-- ==========================================
local group = clean(args['所属组合'])
if group then
local musicGroup = {
["@type"] = "MusicGroup",
["name"] = group
}
local groupUrl = clean(args['组合链接'])
if groupUrl then
musicGroup.url = groupUrl
end
entity.memberOf = musicGroup
end
-- ==========================================
-- DynamicJsonLD integration
-- ==========================================
local JsonLD = getJsonLD()
if not JsonLD then
return '<strong class="error">' ..
'CharacterJsonLD: DynamicJsonLD Lua API unavailable' ..
'</strong>'
end
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
-- Optional debugging
if clean(args['调试']) == '1' then
return 'JSONLD_SUCCESS: ' .. name
end
return ''
end
return p