「モジュール:Infobox/former」の版間の差分
ナビゲーションに移動
検索に移動
template>Waiesu 細編集の要約なし |
Triple-Zeta (トーク | 投稿記録) 細 (10版 をインポートしました) |
||
(2人の利用者による、間の7版が非表示) | |||
1行目: | 1行目: | ||
local p = {} | local p = {} | ||
function p.main(frame) | function p.main(frame) | ||
local args = require('Module:Arguments').getArgs(frame, {parentOnly = true}) | local args = require('Module:Arguments').getArgs(frame, {parentOnly = true}) --引数取得 | ||
local child = (args.child == 'yes') | local child = (args.child == 'yes') | ||
local h = {subheader = {}, image = {{}}} | local subbox = (args.subbox == 'yes') | ||
local body = {} | local h = {subheader = {}, image = {{}}} --ヘッダー部(subheader, image)テーブル | ||
local link = args.tnavbar or args.name | local body, sbody = {}, {} --本体部テーブル, ソート済み本体部テーブル | ||
local result = '' | local link = args.tnavbar or args.name --(フッター部)テンプレート名 | ||
local result = '' --結果格納用 | |||
--[[ | |||
subheader, image用引数振り分け | |||
]] | |||
local function args2tbl(str, k, v) | local function args2tbl(str, k, v) | ||
local num = string.match(k, '%d*$') | local num = string.match(k, '%d*$') | ||
39行目: | 38行目: | ||
end | end | ||
--[[ | |||
引数振り分け | |||
]] | |||
for k, v in pairs(args) do | for k, v in pairs(args) do | ||
--subheader | --subheader | ||
54行目: | 56行目: | ||
h['image'][num]['caption'] = '<div style="' .. (args.captionstyle or '') .. '">' .. v .. '</div>' | h['image'][num]['caption'] = '<div style="' .. (args.captionstyle or '') .. '">' .. v .. '</div>' | ||
end | end | ||
-- | --その他(本体部) | ||
elseif string.match(k, '^%D+%d+$') then | elseif string.match(k, '^%D+%d+$') then | ||
local str, num = string.match(k, '^(%D+)(%d+)$') | local str, num = string.match(k, '^(%D+)(%d+)$') | ||
num = tonumber(num) | num = tonumber(num) | ||
body[num] = body[num] or | if not body[num] then | ||
body[num][str] = v | local OddOrEven = (num % 2 ~= 0) and 'odd' or 'even' | ||
body[num] = { | |||
num, | |||
headerstyle = (args.headerstyle or '') .. (args[OddOrEven .. 'headerstyle'] or ''), | |||
labelstyle = (args.labelstyle or '') .. (args[OddOrEven .. 'labelstyle'] or ''), | |||
datastyle = (args.datastyle or '') .. (args[OddOrEven .. 'datastyle'] or '') | |||
} | |||
end | |||
body[num][str] = (body[num][str] or '') .. v | |||
end | end | ||
end | end | ||
local function row(header, headerstyle, label, labelstyle, data, datastyle, class, rowclass, id, itemprop, rowitemprop, itemtype, rowitemtype, itemref, rowitemref) | --[[ | ||
Template:Infobox/row | |||
]] | |||
local function row(header, headerstyle, label, labelstyle, data, datastyle, rowstyle, class, rowclass, id, itemprop, rowitemprop, itemtype, rowitemtype, itemref, rowitemref) | |||
local result ='' | local result ='' | ||
if header then | if header then | ||
result = '<tr | result = '<tr style="' .. (rowstyle or '') ..'"' .. (rowitemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (rowitemref or '') .. '"><th scope="col" colspan="2" class="' .. (class or '') .. '" style="text-align:center; ' .. (headerstyle or '') .. '">' .. header .. '</th></tr>' | ||
elseif data then | elseif data then | ||
result = '<tr class="' .. (rowclass or '') .. '" itemprop="' .. (rowitemprop or '') .. '"' .. (rowitemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (rowitemref or '') .. '">' | result = '<tr class="' .. (rowclass or '') .. '" style="' .. (rowstyle or '') .. '" itemprop="' .. (rowitemprop or '') .. '"' .. (rowitemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (rowitemref or '') .. '">' | ||
if label then | if label then | ||
result = result .. '<th scope="row" style="text-align:left; white-space:nowrap; ' .. (labelstyle or '') .. '">' .. label .. '</th><td class="' .. (class or '') .. '" style="' .. (datastyle or '') .. '" itemprop="' .. (itemprop or '') .. '"' .. (itemtype and (' itemscope itemtype="' .. itemtype .. '"') or '') .. ' itemref="' .. (itemref or '') .. '">' | result = result .. '<th scope="row" style="text-align:left; white-space:nowrap; ' .. (labelstyle or '') .. '">' .. label .. '</th><td class="' .. (class or '') .. '" style="' .. (datastyle or '') .. '" itemprop="' .. (itemprop or '') .. '"' .. (itemtype and (' itemscope itemtype="' .. itemtype .. '"') or '') .. ' itemref="' .. (itemref or '') .. '">' | ||
79行目: | 92行目: | ||
end | end | ||
--[[ | |||
Template:Infobox | |||
]] | |||
--ヘッダー部 | |||
if not child then | if not child then | ||
result = '<table class="infobox ' .. (args.bodyclass or '') .. '" style="width:22em; ' .. (args.bodystyle or '') .. '"' .. (args.bodyitemtype and (' itemscope itemtype="' .. args.bodyitemtype .. '"') or '') .. ' itemref="' .. (args.bodyitemref or '') .. '">' | --tableタグ | ||
result = '<table class="' .. (subbox and '' or 'infobox ') .. (args.bodyclass or '') .. '" style="' .. (subbox and 'min-width:100%; width:calc(100% + 6px); margin:-3px; ' or 'width:22em; ') .. (args.bodystyle or '') .. '"' .. (args.bodyitemtype and (' itemscope itemtype="' .. args.bodyitemtype .. '"') or '') .. ' itemref="' .. (args.bodyitemref or '') .. '">' | |||
if args.title then | if args.title then | ||
result = result .. '<caption itemprop="name" class="' .. (args.titleclass or '') .. ' style="' .. (args.titlestyle or '') .. '">' .. args.title .. '</caption>' | --captionタグ | ||
result = result .. '<caption itemprop="name" class="' .. (args.titleclass or '') .. '" style="' .. (args.titlestyle or '') .. '">' .. args.title .. '</caption>' | |||
end | end | ||
if args.above then | if args.above then | ||
93行目: | 112行目: | ||
end | end | ||
for k, v in pairs(h.subheader) do | |||
result = result .. row(nil, nil, nil, nil, v[1], v.style or h.subheader.style, v.rowstyle, h.subheader.class, v.rowclass, nil, nil, nil, nil, nil, nil, nil) | |||
end | end | ||
for k, v in pairs(h.image) do | |||
result = result .. row(nil, nil, nil, nil, v[1] and (v[1] .. (v.caption or '')), v.style or h.image.style, v.rowstyle, h.image.class, v.rowclass, nil, nil, nil, nil, nil, nil, nil) | |||
end | end | ||
--本体部ソート | |||
for k, v in pairs(body) do | for k, v in pairs(body) do | ||
result = result .. row(v.header, | sbody[#sbody + 1] = v | ||
end | |||
table.sort(sbody, | |||
function (a, b) return a[1] < b[1] end | |||
) | |||
--本体部 | |||
for k, v in ipairs(sbody) do | |||
result = result .. row(v.header, v.headerstyle, v.label, v.labelstyle, v.data, v.datastyle, v.rowstyle, v.class, v.rowclass, v.id, v.itemprop, v.rowitemprop, v.itemtype, v.rowitemtype, v.itemref, v.rowitemref) | |||
end | end | ||
--フッター部 | |||
if args.below then | if args.below then | ||
result = result .. '<tr><td colspan="2" class="' .. (args.belowclass or '') .. '" style="text-align:center; ' .. (args.belowstyle or '') .. '">' .. args.below .. '</td></tr>' | result = result .. '<tr><td colspan="2" class="' .. (args.belowclass or '') .. '" style="text-align:center; ' .. (args.belowstyle or '') .. '">' .. args.below .. '</td></tr>' | ||
113行目: | 137行目: | ||
if link then | if link then | ||
--Template:Transclude | |||
link = string.gsub(link, ':?[Tt]emplate:', '') | link = string.gsub(link, ':?[Tt]emplate:', '') | ||
if not string.find(link, ':') then | if not string.find(link, ':') then | ||
120行目: | 145行目: | ||
end | end | ||
--tableタグ閉じ | |||
if not child then | if not child then | ||
result = result .. '</table>' | result = result .. '</table>' | ||
end | end | ||
--出力 | |||
return result | return result | ||
end | end | ||
return p | return p |
2021年3月3日 (水) 14:47時点における最新版
このモジュールについての説明文ページを モジュール:Infobox/former/doc に作成できます
local p = {}
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {parentOnly = true}) --引数取得
local child = (args.child == 'yes')
local subbox = (args.subbox == 'yes')
local h = {subheader = {}, image = {{}}} --ヘッダー部(subheader, image)テーブル
local body, sbody = {}, {} --本体部テーブル, ソート済み本体部テーブル
local link = args.tnavbar or args.name --(フッター部)テンプレート名
local result = '' --結果格納用
--[[
subheader, image用引数振り分け
]]
local function args2tbl(str, k, v)
local num = string.match(k, '%d*$')
num = (num == '') and 1 or tonumber(num)
h[str][num] = h[str][num] or {}
if k == str then
h[str][1][1] = v
elseif string.match(k, str .. '%d+') then
h[str][num][1] = v
elseif string.find(k, 'style') then
if string.match(k, 'style$') then
h[str]['style'] = v
else
h[str][num]['style'] = v
end
elseif string.find(k, 'rowclass') then
if string.match(k, 'rowclass$') then
h[str]['rowclass'] = v
else
h[str][num]['rowclass'] = v
end
elseif string.match(k, 'class$') then
h[str]['class'] = v
end
end
--[[
引数振り分け
]]
for k, v in pairs(args) do
--subheader
if string.find(k, 'subheader') then
args2tbl('subheader', k, v)
--image
elseif string.find(k, 'image') then
args2tbl('image', k, v)
elseif string.find(k, 'caption') then
if string.match(k, 'caption$') then
h['image'][1]['caption'] = '<div style="' .. (args.captionstyle or '') .. '">' .. v .. '</div>'
elseif string.match(k, 'caption%d+') then
local num = tonumber(string.match(k, '%d*$'))
h['image'][num] = h['image'][num] or {}
h['image'][num]['caption'] = '<div style="' .. (args.captionstyle or '') .. '">' .. v .. '</div>'
end
--その他(本体部)
elseif string.match(k, '^%D+%d+$') then
local str, num = string.match(k, '^(%D+)(%d+)$')
num = tonumber(num)
if not body[num] then
local OddOrEven = (num % 2 ~= 0) and 'odd' or 'even'
body[num] = {
num,
headerstyle = (args.headerstyle or '') .. (args[OddOrEven .. 'headerstyle'] or ''),
labelstyle = (args.labelstyle or '') .. (args[OddOrEven .. 'labelstyle'] or ''),
datastyle = (args.datastyle or '') .. (args[OddOrEven .. 'datastyle'] or '')
}
end
body[num][str] = (body[num][str] or '') .. v
end
end
--[[
Template:Infobox/row
]]
local function row(header, headerstyle, label, labelstyle, data, datastyle, rowstyle, class, rowclass, id, itemprop, rowitemprop, itemtype, rowitemtype, itemref, rowitemref)
local result =''
if header then
result = '<tr style="' .. (rowstyle or '') ..'"' .. (rowitemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (rowitemref or '') .. '"><th scope="col" colspan="2" class="' .. (class or '') .. '" style="text-align:center; ' .. (headerstyle or '') .. '">' .. header .. '</th></tr>'
elseif data then
result = '<tr class="' .. (rowclass or '') .. '" style="' .. (rowstyle or '') .. '" itemprop="' .. (rowitemprop or '') .. '"' .. (rowitemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (rowitemref or '') .. '">'
if label then
result = result .. '<th scope="row" style="text-align:left; white-space:nowrap; ' .. (labelstyle or '') .. '">' .. label .. '</th><td class="' .. (class or '') .. '" style="' .. (datastyle or '') .. '" itemprop="' .. (itemprop or '') .. '"' .. (itemtype and (' itemscope itemtype="' .. itemtype .. '"') or '') .. ' itemref="' .. (itemref or '') .. '">'
else
result = result .. '<td colspan="2" class="' .. (class or '') .. '" style="text-align:center; ' .. (datastyle or '') .. '" itemprop="' .. (itemprop or '') .. '"' .. (itemtype and (' itemscope itemtype="' .. rowitemtype .. '"') or '') .. ' itemref="' .. (itemref or '') .. '">'
end
result = result .. '\n' .. data .. '</td></tr>'
end
return result
end
--[[
Template:Infobox
]]
--ヘッダー部
if not child then
--tableタグ
result = '<table class="' .. (subbox and '' or 'infobox ') .. (args.bodyclass or '') .. '" style="' .. (subbox and 'min-width:100%; width:calc(100% + 6px); margin:-3px; ' or 'width:22em; ') .. (args.bodystyle or '') .. '"' .. (args.bodyitemtype and (' itemscope itemtype="' .. args.bodyitemtype .. '"') or '') .. ' itemref="' .. (args.bodyitemref or '') .. '">'
if args.title then
--captionタグ
result = result .. '<caption itemprop="name" class="' .. (args.titleclass or '') .. '" style="' .. (args.titlestyle or '') .. '">' .. args.title .. '</caption>'
end
if args.above then
result = result .. '<tr><th colspan="2" class="' .. (args.aboveclass or '') .. '" style="text-align:center; font-size:125%; font-weight:bold; ' .. (args.abovestyle or '') .. '" itemprop="' .. (args.aboveitemprop or '') .. '"' .. (args.aboveitemtype and (' itemscope itemtype="' .. args.aboveitemtype .. '"') or '') .. ' itemref="' .. (args.aboveitemref or '') .. '">' .. args.above ..'</th></tr>'
end
else
if args.title then
result = '<b itemprop="name' .. '"' .. (args.bodyitemtype and (' itemscope itemtype="' .. args.bodyitemtype .. '"') or '') .. ' itemref="' .. (args.bodyitemref or '') .. '">' .. args.title .. '</b>'
end
end
for k, v in pairs(h.subheader) do
result = result .. row(nil, nil, nil, nil, v[1], v.style or h.subheader.style, v.rowstyle, h.subheader.class, v.rowclass, nil, nil, nil, nil, nil, nil, nil)
end
for k, v in pairs(h.image) do
result = result .. row(nil, nil, nil, nil, v[1] and (v[1] .. (v.caption or '')), v.style or h.image.style, v.rowstyle, h.image.class, v.rowclass, nil, nil, nil, nil, nil, nil, nil)
end
--本体部ソート
for k, v in pairs(body) do
sbody[#sbody + 1] = v
end
table.sort(sbody,
function (a, b) return a[1] < b[1] end
)
--本体部
for k, v in ipairs(sbody) do
result = result .. row(v.header, v.headerstyle, v.label, v.labelstyle, v.data, v.datastyle, v.rowstyle, v.class, v.rowclass, v.id, v.itemprop, v.rowitemprop, v.itemtype, v.rowitemtype, v.itemref, v.rowitemref)
end
--フッター部
if args.below then
result = result .. '<tr><td colspan="2" class="' .. (args.belowclass or '') .. '" style="text-align:center; ' .. (args.belowstyle or '') .. '">' .. args.below .. '</td></tr>'
end
if link then
--Template:Transclude
link = string.gsub(link, ':?[Tt]emplate:', '')
if not string.find(link, ':') then
link = 'Template:' .. link
end
result = result .. '<tr class="noprint"><td colspan=2 style="text-align:right; font-size:85%;">[[' .. link .. '|テンプレートを表示]]</td></tr>'
end
--tableタグ閉じ
if not child then
result = result .. '</table>'
end
--出力
return result
end
return p