模块:CharNews
来自星砂岛百科
更多操作
概述
CharNews 提供角色广场动态与社交身份的独立展示能力。
用法
{{#invoke:CharNews|newsTable|枫汐}}
local common = require('Module:Common')
local character_common = require('Module:CharacterCommon')
local p = {}
local data_cache
local mapping_cache
local function load_data()
if data_cache then
return
end
data_cache, mapping_cache = character_common.loadDomainData({
'数据:Character/character_news_index.json',
'数据:Character/character news index.json',
})
end
local function find_record(key)
load_data()
return character_common.findRecord(data_cache, mapping_cache, key)
end
local function render_desc_list(values)
local ordered = character_common.orderedNumericValues(values)
if #ordered == 0 then
return '<div class="char-empty char-news-empty">暂无动态样例</div>'
end
local root = mw.html.create('ul'):addClass('char-news-list')
for _, value in ipairs(ordered) do
local text = common.trim(common.toText(value))
if text ~= '' then
root:tag('li'):wikitext('“' .. mw.text.encode(text) .. '”')
end
end
local output = tostring(root)
if output == '' or output == '<ul class="char-news-list"></ul>' then
return '<div class="char-empty char-news-empty">暂无动态样例</div>'
end
return output
end
local function add_metric(container, label, value)
local item = container:tag('div'):addClass('char-news-metric')
item:tag('div'):addClass('char-news-metric-label'):wikitext(label)
item:tag('div'):addClass('char-news-metric-value'):wikitext(value)
end
function p.newsTable(frame)
local record = find_record(common.getArg(frame, 1, ''))
if not record then
return '<div class="char-empty char-news-empty">未找到角色广场动态数据</div>'
end
local fake_name = common.trim(common.toText(record.fake_name or ''))
local description = common.trim(common.toText(record.default_description or ''))
local root = mw.html.create('div'):addClass('char-news-panel')
local identity = root:tag('div'):addClass('char-news-identity')
local headline = identity:tag('div'):addClass('char-news-headline')
headline:tag('div'):addClass('char-news-label'):wikitext('广场昵称')
headline:tag('div'):addClass('char-news-name'):wikitext(fake_name ~= '' and mw.text.encode(fake_name) or '—')
if description ~= '' then
identity:tag('div'):addClass('char-news-label'):wikitext('个性签名')
identity:tag('div'):addClass('char-news-signature'):wikitext(mw.text.encode(description))
end
local metrics = root:tag('div'):addClass('char-news-metrics')
add_metric(metrics, '自动动态', tostring(record.auto_square_news_count or 0))
add_metric(metrics, '粉丝配置', tostring(record.fans_configuration_count or 0))
add_metric(metrics, '星级配置', tostring(record.star_configuration_count or 0))
add_metric(metrics, '每日最大发帖', tostring(record.max_share_count_per_day or 0))
local samples = root:tag('div'):addClass('char-news-samples')
samples:tag('div'):addClass('char-card-title'):wikitext('动态样例')
samples:wikitext(render_desc_list(record.desc_list))
return tostring(root)
end
function p.hasNews(frame)
local record = find_record(common.getArg(frame, 1, ''))
if not record then
return ''
end
local has_content = common.trim(common.toText(record.fake_name or '')) ~= ''
or common.trim(common.toText(record.default_description or '')) ~= ''
or (type(record.desc_list) == 'table' and next(record.desc_list) ~= nil)
return has_content and '1' or ''
end
function p.newsBlock(frame)
if p.hasNews(frame) == '' then
return ''
end
return p.newsTable(frame)
end
return p