پودمان: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 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 Types = require('Module:WorkInfoBox/Data/Types') | |||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
| خط ۱۴: | خط ۱۶: | ||
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 | 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 | |||
box:tag('tr') | |||
:tag('td') | |||
:attr('colspan', '2') | |||
:addClass('work-infobox-image') | |||
:wikitext( | |||
'[[File:' .. | |||
image .. | |||
'|' .. | |||
(getValue(args, 'image_size') or '250px') .. | |||
']]' | |||
) | |||
local caption = getValue(args, 'image_caption') | |||
if caption then | |||
box:tag('tr') | |||
:tag('td') | |||
:attr('colspan', '2') | |||
:addClass('work-infobox-caption') | |||
:wikitext(caption) | |||
end | |||
end | |||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
-- | -- Row | ||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
local function addRow( | local function addRow(box, label, value) | ||
if not value then | if not value then | ||
| خط ۶۰: | خط ۱۴۰: | ||
end | end | ||
box:tag('tr') | |||
:tag('th') | |||
:addClass('infobox-label') | :addClass('infobox-label') | ||
:wikitext(label) | :wikitext(label) | ||
:done() | |||
:tag('td') | |||
:addClass('infobox-data') | :addClass('infobox-data') | ||
:wikitext(value) | :wikitext(value) | ||
end | end | ||
---------------------------------------------------------- | |||
-- Render | |||
---------------------------------------------------------- | |||
local function render(frame) | local function render(frame) | ||
| خط ۷۶: | خط ۱۶۲: | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local | local kind = getValue(args, 'type') | ||
or 'default' | |||
-- تبدیل نوع فارسی به کلید داخلی | |||
kind = Types[kind] | |||
or kind | |||
local layout = Layouts[kind] | |||
or Layouts.default | |||
local box = html.create('table') | |||
:addClass('infobox') | :addClass('infobox') | ||
:addClass('work-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 | 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 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')
local Types = require('Module:WorkInfoBox/Data/Types')
----------------------------------------------------------
-- 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
box:tag('tr')
:tag('td')
:attr('colspan', '2')
:addClass('work-infobox-image')
:wikitext(
'[[File:' ..
image ..
'|' ..
(getValue(args, 'image_size') or '250px') ..
']]'
)
local caption = getValue(args, 'image_caption')
if caption then
box:tag('tr')
:tag('td')
:attr('colspan', '2')
:addClass('work-infobox-caption')
:wikitext(caption)
end
end
----------------------------------------------------------
-- Row
----------------------------------------------------------
local function addRow(box, label, value)
if not value then
return
end
box:tag('tr')
:tag('th')
:addClass('infobox-label')
:wikitext(label)
:done()
:tag('td')
:addClass('infobox-data')
:wikitext(value)
end
----------------------------------------------------------
-- Render
----------------------------------------------------------
local function render(frame)
local args = getArgs(frame)
local kind = getValue(args, 'type')
or 'default'
-- تبدیل نوع فارسی به کلید داخلی
kind = Types[kind]
or kind
local layout = Layouts[kind]
or Layouts.default
local box = html.create('table')
:addClass('infobox')
:addClass('work-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