模块:InfoBox:修订间差异
来自「荏苒之境」
无编辑摘要 |
无编辑摘要 |
||
(未显示同一用户的1个中间版本) | |||
第76行: | 第76行: | ||
for k, v in ipairs(args) do | for k, v in ipairs(args) do | ||
local subt = string.gsub(v, "\n", " | local subt = string.gsub(utils_str.trim(v), "\n", "\\n") | ||
local parts = csv.parse_row(subt) | local parts = csv.parse_row(subt) | ||
for i = 1, #parts do | for i = 1, #parts do | ||
parts[i] = utils_str.trim(parts[i]) | parts[i] = utils_str.trim(string.gsub(parts[i], "\\n", "\n")) | ||
end | end | ||
append_row(parent_frame, table_elem, unpack(parts)) | append_row(parent_frame, table_elem, unpack(parts)) |
2025年8月25日 (一) 13:16的最新版本
此模块的文档可以在模块:InfoBox/doc创建
local utils_str = require("Module:Utils/String")
local csv = require("Module:Csv")
local html = mw.html
local infobox = {}
local function append_full_row(table_elem)
return table_elem:tag("tr"):tag("th")
:attr("colspan", "2")
end
local function append_header(frame, parent, text)
local title = append_full_row(parent):wikitext(text)
local args = frame.args
local title_color = args.title_color
local title_background = args.title_background
if title_color then
title:css("color", title_color)
end
if title_background then
title:css("background", title_background)
end
return title
end
local row_appenders = {
title = function(...)
append_header(...):addClass("infobox-title")
end,
section = function(...)
append_header(...):addClass("infobox-header")
end,
image = function(frame, parent, file)
local actual_file = frame:preprocess(file)
if actual_file ~= "" then
append_full_row(parent)
:addClass("infobox-image")
:wikitext("[[File:"..actual_file.."|300px]]")
end
end,
caption = function(frame, parent, text)
local actual_text = frame:preprocess(text)
if actual_text ~= "" then
append_full_row(parent)
:addClass("infobox-caption")
:wikitext(actual_text)
end
end,
text = function(frame, parent, header, text)
local actual_text = frame:preprocess(text)
if actual_text ~= "" then
parent:tag("tr")
:tag("th"):wikitext(header):done()
:tag("td"):wikitext(actual_text):done()
end
end
}
local function append_row(frame, parent, row_type, ...)
local appender = row_appenders[row_type]
if appender == nil then
return
end
return appender(frame, parent, ...)
end
infobox.show = function(frame)
local parent_frame = frame:getParent()
local args = parent_frame.args
local view = html.create("div"):addClass("infobox")
local table_elem = view:tag("table")
for k, v in ipairs(args) do
local subt = string.gsub(utils_str.trim(v), "\n", "\\n")
local parts = csv.parse_row(subt)
for i = 1, #parts do
parts[i] = utils_str.trim(string.gsub(parts[i], "\\n", "\n"))
end
append_row(parent_frame, table_elem, unpack(parts))
end
table_elem:done()
return view
end
return infobox