模块:Infobox settlement:修订间差异
来自Blockland Wiki
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第4行: | 第4行: | ||
local function hasValue(arg) | local function hasValue(arg) | ||
return arg and arg:match('%S') | return arg and arg:match('%S') | ||
end | end | ||
| 第23行: | 第12行: | ||
row:tag('th') | row:tag('th') | ||
:attr('colspan', '2') | :attr('colspan', '2') | ||
:css('background', '# | :css('background', '#E6E6FA') -- 淡紫色 | ||
:css('text-align', 'center') | :css('text-align', 'center') | ||
:wikitext(text) | :wikitext(text) | ||
return tostring(row) | |||
end | |||
return '' | |||
end | |||
-- 构建信息框的单行数据 | |||
local function buildRow(label, data) | |||
if hasValue(data) then | |||
local row = mw.html.create('tr') | |||
row:tag('th'):css('width', '30%'):css('text-align', 'left'):wikitext(label) | |||
row:tag('td'):wikitext(data) | |||
return tostring(row) | return tostring(row) | ||
end | end | ||
| 第39行: | 第39行: | ||
:css('font-size', '90%') | :css('font-size', '90%') | ||
-- 标题 | -- 标题 和 副标题 | ||
local title = args.name or mw.title.getCurrentTitle().text | local title = args.name or mw.title.getCurrentTitle().text | ||
local title_cell = root:tag('tr'):tag('th') | local title_cell = root:tag('tr'):tag('th') | ||
| 第45行: | 第45行: | ||
:css('text-align', 'center') | :css('text-align', 'center') | ||
:css('font-size', '125%') | :css('font-size', '125%') | ||
:css('padding', '0. | :css('padding', '0.2em') | ||
:wikitext(title) | :wikitext(title) | ||
if hasValue(args.native_name) then | if hasValue(args.native_name) then | ||
title_cell:wikitext('<br /><span style="font-size:85%; font-weight:normal;">' .. args.native_name .. '</span>') | |||
end | end | ||
| 第69行: | 第63行: | ||
end | end | ||
-- 开始构建数据部分 | -- 开始构建数据部分 | ||
local rows = {} | local rows = {} | ||
table.insert(rows, buildSubheader(' | table.insert(rows, buildSubheader('基本信息')) | ||
-- 特殊处理坐标行 | |||
if hasValue(args.lon) or hasValue(args.lat) then | |||
local coord_data = '经度 ' .. (args.lon or '未提供') .. '<br />纬度 ' .. (args.lat or '未提供') | |||
table.insert(rows, buildRow('坐标', coord_data)) | |||
end | |||
table.insert(rows, buildSubheader('地理位置')) | |||
table.insert(rows, buildRow('国家', args.country)) | table.insert(rows, buildRow('国家', args.country)) | ||
table.insert(rows, buildRow( | table.insert(rows, buildRow('省', args.province)) | ||
table.insert(rows, buildSubheader(' | table.insert(rows, buildSubheader('行政')) | ||
table.insert(rows, buildRow(' | table.insert(rows, buildRow('设立', args.established)) | ||
table.insert(rows, buildRow(' | table.insert(rows, buildRow('市长', args.mayor)) | ||
-- 人口信息(没有子标题) | |||
if hasValue(args.population) then | |||
local pop_data = args.population | |||
if hasValue(args.population_as_of) then | |||
pop_data = pop_data .. ' 人 (' .. args.population_as_of .. ')' | |||
end | |||
table.insert(rows, buildRow('人口', pop_data)) | |||
end | |||
-- 将所有行添加到表格中 | -- 将所有行添加到表格中 | ||
2025年10月31日 (五) 20:57的版本
此模块的文档可以在模块:Infobox settlement/doc创建
local p = {}
-- 辅助函数:检查参数是否存在且不为空
local function hasValue(arg)
return arg and arg:match('%S')
end
-- 构建分组子标题行
local function buildSubheader(text)
if hasValue(text) then
local row = mw.html.create('tr')
row:tag('th')
:attr('colspan', '2')
:css('background', '#E6E6FA') -- 淡紫色
:css('text-align', 'center')
:wikitext(text)
return tostring(row)
end
return ''
end
-- 构建信息框的单行数据
local function buildRow(label, data)
if hasValue(data) then
local row = mw.html.create('tr')
row:tag('th'):css('width', '30%'):css('text-align', 'left'):wikitext(label)
row:tag('td'):wikitext(data)
return tostring(row)
end
return ''
end
-- 主函数,生成整个信息框
function p.main(frame)
local args = frame:getParent().args
local root = mw.html.create('table')
root:addClass('infobox')
:css('width', '300px')
:css('font-size', '90%')
-- 标题 和 副标题
local title = args.name or mw.title.getCurrentTitle().text
local title_cell = root:tag('tr'):tag('th')
:attr('colspan', '2')
:css('text-align', 'center')
:css('font-size', '125%')
:css('padding', '0.2em')
:wikitext(title)
if hasValue(args.native_name) then
title_cell:wikitext('<br /><span style="font-size:85%; font-weight:normal;">' .. args.native_name .. '</span>')
end
-- 图片和图注
if hasValue(args.image) then
local image_cell = root:tag('tr'):tag('td')
:attr('colspan', '2')
:css('text-align', 'center')
local image_wikitext = '[[File:' .. args.image .. '|280px]]'
if hasValue(args.image_caption) then
image_wikitext = image_wikitext .. '<br /><span>' .. args.image_caption .. '</span>'
end
image_cell:wikitext(image_wikitext)
end
-- 开始构建数据部分
local rows = {}
table.insert(rows, buildSubheader('基本信息'))
-- 特殊处理坐标行
if hasValue(args.lon) or hasValue(args.lat) then
local coord_data = '经度 ' .. (args.lon or '未提供') .. '<br />纬度 ' .. (args.lat or '未提供')
table.insert(rows, buildRow('坐标', coord_data))
end
table.insert(rows, buildSubheader('地理位置'))
table.insert(rows, buildRow('国家', args.country))
table.insert(rows, buildRow('省', args.province))
table.insert(rows, buildSubheader('行政'))
table.insert(rows, buildRow('设立', args.established))
table.insert(rows, buildRow('市长', args.mayor))
-- 人口信息(没有子标题)
if hasValue(args.population) then
local pop_data = args.population
if hasValue(args.population_as_of) then
pop_data = pop_data .. ' 人 (' .. args.population_as_of .. ')'
end
table.insert(rows, buildRow('人口', pop_data))
end
-- 将所有行添加到表格中
root:wikitext(table.concat(rows))
return tostring(root)
end
return p