پودمان:WorkInfoBox: تفاوت میان نسخهها
ظاهر
صفحهای تازه حاوی «local p = {} local getArgs = require('Module:Arguments').getArgs local html = mw.html local Fields = require('Module:WorkInfoBox/Data/Fields') local Layouts = require('Module:WorkInfoBox/Data/Layouts') local Labels = require('Module:WorkInfoBox/Data/Labels') local Aliases = require('Module:WorkInfoBox/Data/Aliases') ---------------------------------------------------------- -- Helpers ------------------------------------------------...» ایجاد کرد |
جزبدون خلاصۀ ویرایش |
||
| خط ۳: | خط ۳: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local html = mw.html | local html = mw.html | ||
local InfoboxImage = require('Module:InfoboxImage') | |||
local Fields = require('Module:WorkInfoBox/Data/Fields') | local Fields = require('Module:WorkInfoBox/Data/Fields') | ||
| خط ۸: | خط ۱۰: | ||
local Labels = require('Module:WorkInfoBox/Data/Labels') | local Labels = require('Module:WorkInfoBox/Data/Labels') | ||
local Aliases = require('Module:WorkInfoBox/Data/Aliases') | local Aliases = require('Module:WorkInfoBox/Data/Aliases') | ||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
| خط ۱۴: | خط ۱۷: | ||
local function norm(value) | local function norm(value) | ||
if not value then | if not value then | ||
return nil | return nil | ||
end | end | ||
value = mw.text.trim(tostring(value)) | |||
value = mw.text.trim(value) | |||
if value == '' then | if value == '' then | ||
| خط ۲۶: | خط ۲۹: | ||
return value | return value | ||
end | end | ||
local function | |||
local function getValue(args, field) | |||
local value = norm(args[field]) | local value = norm(args[field]) | ||
| خط ۳۵: | خط ۴۱: | ||
return value | return value | ||
end | end | ||
local alias = Aliases[field] | local alias = Aliases[field] | ||
if alias then | if alias then | ||
for _, name in ipairs(alias) do | for _, name in ipairs(alias) do | ||
value = norm(args[name]) | value = norm(args[name]) | ||
if value then | if value then | ||
return value | return value | ||
end | end | ||
end | end | ||
end | end | ||
return nil | return nil | ||
end | |||
local function getLabel(kind, field) | |||
if Labels[kind] | |||
and Labels[kind][field] then | |||
return Labels[kind][field] | |||
end | |||
if Fields[field] then | |||
return Fields[field].label | |||
end | |||
return field | |||
end | |||
---------------------------------------------------------- | |||
-- Image | |||
---------------------------------------------------------- | |||
local function addImage(box, args) | |||
local image = getValue(args, 'image') | |||
if not image then | |||
return | |||
end | |||
local row = box:tag('tr') | |||
local cell = row:tag('td') | |||
:attr('colspan', '2') | |||
:addClass('infobox-image') | |||
cell:wikitext( | |||
InfoboxImage.infoboxImage({ | |||
image = image, | |||
size = getValue(args, 'image_size') or '250px', | |||
alt = getValue(args, 'image_alt') | |||
}) | |||
) | |||
local caption = getValue(args, 'image_caption') | |||
if caption then | |||
cell:tag('div') | |||
:addClass('infobox-caption') | |||
:wikitext(caption) | |||
end | |||
end | end | ||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
-- | -- Row | ||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
local function addRow( | local function addRow(box, label, value) | ||
if not value then | if not value then | ||
| خط ۶۰: | خط ۱۴۱: | ||
end | end | ||
local row = | |||
local row = box:tag('tr') | |||
row:tag('th') | row:tag('th') | ||
:addClass('infobox-label') | :addClass('infobox-label') | ||
:wikitext(label) | :wikitext(label) | ||
row:tag('td') | row:tag('td') | ||
| خط ۷۱: | خط ۱۵۵: | ||
end | end | ||
---------------------------------------------------------- | |||
-- Render | |||
---------------------------------------------------------- | |||
local function render(frame) | local function render(frame) | ||
| خط ۷۶: | خط ۱۶۶: | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local layout = Layouts[kind] or Layouts.default | local kind = getValue(args, 'type') | ||
or 'default' | |||
local layout = Layouts[kind] | |||
or Layouts.default | |||
local | local box = html.create('table') | ||
:addClass('infobox') | :addClass('infobox') | ||
------------------------------------------------------ | |||
-- Title | |||
------------------------------------------------------ | |||
local title = getValue(args, 'title') | |||
if title then | |||
box:tag('caption') | |||
:addClass('infobox-title') | |||
:wikitext(title) | |||
end | |||
------------------------------------------------------ | ------------------------------------------------------ | ||
-- | -- Image | ||
-- ( | ------------------------------------------------------ | ||
addImage(box, args) | |||
------------------------------------------------------ | |||
-- Sections | |||
------------------------------------------------------ | ------------------------------------------------------ | ||
for _, section in ipairs(layout) do | for _, section in ipairs(layout) do | ||
if section.title then | |||
box:tag('tr') | |||
:tag('th') | |||
:attr('colspan', '2') | |||
:addClass('infobox-header') | |||
:wikitext(section.title) | |||
end | |||
for _, field in ipairs(section.fields) do | for _, field in ipairs(section.fields) do | ||
local value = | |||
local value = getValue(args, field) | |||
if value then | if value then | ||
addRow( | |||
box, | |||
getLabel(kind, field), | |||
value | |||
) | |||
end | |||
end | end | ||
end | end | ||
return tostring( | |||
return tostring(box) | |||
end | end | ||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
function p.main(frame) | function p.main(frame) | ||
return render(frame) | return render(frame) | ||
end | end | ||
return p | return p | ||
نسخهٔ ۱۳ ژوئیهٔ ۲۰۲۶، ساعت ۲۱:۲۸
توضیحات این پودمان میتواند در پودمان:WorkInfoBox/توضیحات قرار گیرد.
local p = {}
local getArgs = require('Module:Arguments').getArgs
local html = mw.html
local InfoboxImage = require('Module:InfoboxImage')
local Fields = require('Module:WorkInfoBox/Data/Fields')
local Layouts = require('Module:WorkInfoBox/Data/Layouts')
local Labels = require('Module:WorkInfoBox/Data/Labels')
local Aliases = require('Module:WorkInfoBox/Data/Aliases')
----------------------------------------------------------
-- Helpers
----------------------------------------------------------
local function norm(value)
if not value then
return nil
end
value = mw.text.trim(tostring(value))
if value == '' then
return nil
end
return value
end
local function getValue(args, field)
local value = norm(args[field])
if value then
return value
end
local alias = Aliases[field]
if alias then
for _, name in ipairs(alias) do
value = norm(args[name])
if value then
return value
end
end
end
return nil
end
local function getLabel(kind, field)
if Labels[kind]
and Labels[kind][field] then
return Labels[kind][field]
end
if Fields[field] then
return Fields[field].label
end
return field
end
----------------------------------------------------------
-- Image
----------------------------------------------------------
local function addImage(box, args)
local image = getValue(args, 'image')
if not image then
return
end
local row = box:tag('tr')
local cell = row:tag('td')
:attr('colspan', '2')
:addClass('infobox-image')
cell:wikitext(
InfoboxImage.infoboxImage({
image = image,
size = getValue(args, 'image_size') or '250px',
alt = getValue(args, 'image_alt')
})
)
local caption = getValue(args, 'image_caption')
if caption then
cell:tag('div')
:addClass('infobox-caption')
:wikitext(caption)
end
end
----------------------------------------------------------
-- Row
----------------------------------------------------------
local function addRow(box, label, value)
if not value then
return
end
local row = box:tag('tr')
row:tag('th')
:addClass('infobox-label')
:wikitext(label)
row:tag('td')
:addClass('infobox-data')
:wikitext(value)
end
----------------------------------------------------------
-- Render
----------------------------------------------------------
local function render(frame)
local args = getArgs(frame)
local kind = getValue(args, 'type')
or 'default'
local layout = Layouts[kind]
or Layouts.default
local box = html.create('table')
:addClass('infobox')
------------------------------------------------------
-- Title
------------------------------------------------------
local title = getValue(args, 'title')
if title then
box:tag('caption')
:addClass('infobox-title')
:wikitext(title)
end
------------------------------------------------------
-- Image
------------------------------------------------------
addImage(box, args)
------------------------------------------------------
-- Sections
------------------------------------------------------
for _, section in ipairs(layout) do
if section.title then
box:tag('tr')
:tag('th')
:attr('colspan', '2')
:addClass('infobox-header')
:wikitext(section.title)
end
for _, field in ipairs(section.fields) do
local value = getValue(args, field)
if value then
addRow(
box,
getLabel(kind, field),
value
)
end
end
end
return tostring(box)
end
----------------------------------------------------------
function p.main(frame)
return render(frame)
end
return p