模块:Character:修订间差异
来自星砂岛百科
更多操作
无编辑摘要 |
同步 Character 域阅读体验与 Citizen 样式优化 |
||
| 第1行: | 第1行: | ||
local common = require('Module:Common') | local common = require('Module:Common') | ||
local character_common = require('Module:CharacterCommon') | |||
local p = {} | local p = {} | ||
local data_cache | local data_cache | ||
local mapping | local mapping | ||
| 第18行: | 第18行: | ||
['4'] = 'Winter', | ['4'] = 'Winter', | ||
} | } | ||
local function pick_first(...) | local function pick_first(...) | ||
| 第52行: | 第48行: | ||
local function localize_gender(value) | local function localize_gender(value) | ||
local normalized = | local normalized = character_common.normalizeKey(value) | ||
return gender_map[normalized] or common.trim(value) | return gender_map[normalized] or common.trim(value) | ||
end | end | ||
| 第74行: | 第70行: | ||
end | end | ||
data_cache = | data_cache, mapping = character_common.loadDomainData('数据:Character/character_index.json') | ||
end | end | ||
local function find_record(key) | local function find_record(key) | ||
load_data() | load_data() | ||
return character_common.findRecord(data_cache, mapping, key) | |||
end | end | ||
2026年3月28日 (六) 15:57的最新版本
概述
Character 提供角色域字段读取与展示辅助字段,供 {{Infobox character}} 等角色页模板调用。
用法
{{#invoke:Character|getField|晨星|occupation}}
{{#invoke:Character|getBirthday|晨星}}
{{#invoke:Character|getGender|晨星}}
示例
{{#invoke:Character|getField|晨星|occupation}}
函数
getField:读取角色字段,也支持birthday、gender_display、image等展示字段。getId:返回角色 ID。getImage:返回角色图片文件名。getBirthday:返回生日展示结果。getGender:返回本地化后的性别文本。
数据来源
local common = require('Module:Common')
local character_common = require('Module:CharacterCommon')
local p = {}
local data_cache
local mapping
local gender_map = {
male = '男',
female = '女',
}
local season_map = {
['1'] = 'Spring',
['2'] = 'Summer',
['3'] = 'Autumn',
['4'] = 'Winter',
}
local function pick_first(...)
local values = { ... }
for _, value in ipairs(values) do
value = common.trim(value)
if value ~= '' then
return value
end
end
return ''
end
local function format_birthday(record)
if type(record) ~= 'table' then
return ''
end
local month = common.trim(record.birthday_month)
local day = common.trim(record.birthday_day)
local season = season_map[month]
if season ~= nil and day ~= '' then
return mw.getCurrentFrame():expandTemplate{
title = 'Season',
args = { season, day },
}
end
return ''
end
local function localize_gender(value)
local normalized = character_common.normalizeKey(value)
return gender_map[normalized] or common.trim(value)
end
local function display_image(record)
if type(record) ~= 'table' then
return ''
end
local image_key = common.trim(record.image_key)
if image_key ~= '' then
return 'Head ' .. image_key .. '.png'
end
return pick_first(record.default_image, record.upgrade_icon, record.map_image)
end
local function load_data()
if data_cache then
return
end
data_cache, mapping = character_common.loadDomainData('数据:Character/character_index.json')
end
local function find_record(key)
load_data()
return character_common.findRecord(data_cache, mapping, key)
end
local function get_display_field(record, field)
if type(record) ~= 'table' then
return ''
end
if field == 'birthday_display' or field == 'birthday' then
return format_birthday(record)
end
if field == 'gender_display' or field == 'gender_zh' then
return localize_gender(record.gender)
end
if field == 'image' or field == 'image_fallback' then
return display_image(record)
end
if field == 'display_name' then
return pick_first(record.name, record.title, record.id)
end
if field == 'display_title' then
return pick_first(record.title, record.name)
end
return record[field]
end
function p.getField(frame)
local key = common.getArg(frame, 1, '')
local field = common.getArg(frame, 2, '')
if field == '' then
return ''
end
local record = find_record(key)
if not record then
return ''
end
return common.toText(get_display_field(record, field))
end
function p.getId(frame)
local key = common.getArg(frame, 1, '')
local record = find_record(key)
if not record then
return ''
end
return common.toText(record.id)
end
function p.getImage(frame)
local key = common.getArg(frame, 1, '')
local record = find_record(key)
if not record then
return ''
end
return common.toText(display_image(record))
end
function p.getBirthday(frame)
local key = common.getArg(frame, 1, '')
local record = find_record(key)
if not record then
return ''
end
return format_birthday(record)
end
function p.getGender(frame)
local key = common.getArg(frame, 1, '')
local record = find_record(key)
if not record then
return ''
end
return localize_gender(record.gender)
end
return p