پودمان:Userbox: تفاوت میان نسخهها
Hasaninasab (بحث | مشارکتها) صفحهای تازه حاوی «-- This module implements {{userbox}}. local getArgs = require('Module:Arguments').getArgs local categoryHandler = require('Module:Categor...» ایجاد کرد |
جز ۱ نسخه واردشده |
||
| (یک نسخهٔ میانی ویرایش شده توسط یک کاربر دیگر نشان داده نشد) | |||
| خط ۱: | خط ۱: | ||
-- This module implements {{userbox}}. | -- This module implements {{userbox}}. | ||
-- این پودمان برای سازگاری با ویکیپدیای فارسی تغییر کرده است | |||
-- هنگام بهروزرسانی از ویکیپدیای انگلیسی به بومیسازیها نیز دقت کنید | |||
local categoryHandler = require('Module:Category handler').main | local categoryHandler = require('Module:Category handler').main | ||
local numConv = require('Module:Numeral converter').convert | |||
local leftRight = require('Module:Leftright').leftRight | |||
local setLocalArgs = require('Module:Set local argument names') | |||
local yesno = require('Module:Yesno') | |||
local p = {} | local p = {} | ||
| خط ۱۳: | خط ۱۸: | ||
-- Checks whether a value is a number greater than or equal to zero. If so, | -- Checks whether a value is a number greater than or equal to zero. If so, | ||
-- returns it as a number. If not, returns a default value. | -- returns it as a number. If not, returns a default value. | ||
val = tonumber(val) | val = tonumber(numConv('en', val)) | ||
if val and val >= 0 then | if val and val >= 0 then | ||
return val | return val | ||
| خط ۲۴: | خط ۲۹: | ||
-- Turns a number into a string and adds a suffix. | -- Turns a number into a string and adds a suffix. | ||
if num then | if num then | ||
return tostring(num) .. suffix | return tostring(numConv('en', num)) .. suffix | ||
else | else | ||
return nil | return nil | ||
| خط ۳۹: | خط ۴۴: | ||
-- Makes a category link. | -- Makes a category link. | ||
if sort then | if sort then | ||
return mw.ustring.format('', cat, sort) | return mw.ustring.format('[[رده:%s|%s]]', cat, sort) | ||
else | else | ||
return mw.ustring.format('', cat) | return mw.ustring.format('[[رده:%s]]', cat) | ||
end | end | ||
end | end | ||
| خط ۵۱: | خط ۵۶: | ||
local function makeInvokeFunc(funcName) | local function makeInvokeFunc(funcName) | ||
return function (frame) | return function (frame) | ||
local | local origArgs = require('Module:Arguments').getArgs(frame) | ||
local args = {} | |||
for k, v in pairs(origArgs) do | |||
args[k] = v | |||
end | |||
return p.main(funcName, args) | return p.main(funcName, args) | ||
end | end | ||
| خط ۶۵: | خط ۷۴: | ||
function p.main(funcName, args) | function p.main(funcName, args) | ||
-- Local args | |||
setLocalArgs( | |||
args, | |||
{ | |||
['nocat'] = 'بیرده', ['notcatsubpages'] = 'زیرصفحههای-بیرده', | |||
['bodyclass'] = 'سبک بدنه', ['float'] = 'شناور', | |||
['border-s'] = 'حاشیه-ا', ['border-c'] = 'حاشیه-ر', ['border-color'] = 'حاشیه-رنگ', | |||
['border-width'] = 'حاشیه-عرض', | |||
['id'] = 'شناسه', ['id-s'] = 'شناسه-ا', ['id-h'] = 'شناسه-ار', ['id-lh'] = 'شناسه-ارس', | |||
['id-op'] = 'شناسه-پد', ['id-a'] = 'شناسه-ت', ['id-c'] = 'شناسه-ر', ['id-fc'] = 'شناسه-رق', | |||
['id-w'] = 'شناسه-ع', ['id-class'] = 'شناسه-کلاس', ['id-p'] = 'شناسه-لگ', | |||
['id1'] = 'شناسه۱', ['id1-s'] = 'شناسه۱-ا', ['id1-lh'] = 'شناسه۱-اس', ['id1-op'] = 'شناسه۱-پد', | |||
['id1-c'] = 'شناسه۱-ر', ['id1-fc'] = 'شناسه۱-رق', ['id1-w'] = 'شناسه۱-ع', ['id1-p'] = 'شناسه۱-لگ', | |||
['id2'] = 'شناسه۲', ['id2-s'] = 'شناسه۲-ا', ['id2-lh'] = 'شناسه۲-اس', ['id2-op'] = 'شناسه۲-پد', | |||
['id2-fc'] = 'شناسه۲-رق', ['id2-w'] = 'شناسه۲-ع', ['id2-p'] = 'شناسه۲-لگ', | |||
['info'] = 'اطلاعات', ['info-a'] = 'اطلاعات-ا', ['info-line-height'] = 'اطلاعات-ارتفاع-سطر', | |||
['info-lh'] = 'اطلاعات-اس', ['info-size'] = 'اطلاعات-اندازه', ['info-other-param'] = 'اطلاعات-پارامتر-دیگر', | |||
['info-background'] = 'اطلاعات-پسزمینه', ['info-align'] = 'اطلاعات-تراز', ['info-c'] = 'اطلاعات-ر', | |||
['info-fc'] = 'اطلاعات-رق', ['info-color'] = 'اطلاعات-رنگ', ['info-class'] = 'اطلاعات-کلاس', | |||
['info-padding'] = 'اطلاعات-لایهگذاری', ['info-p'] = 'اطلاعات-لگ', | |||
['logo'] = 'لوگو', ['logo-height'] = 'لوگو-ارتفاع', ['logo-line-height'] = 'لوگو-ارتفاع-سطر', | |||
['logo-size'] = 'لوگو-اندازه', ['logo-other-param'] = 'لوگو-پارامتر-دیگر', ['logo-background'] = 'لوگو-پسزمینه', | |||
['logo-color'] = 'لوگو-رنگ', ['logo-width'] = 'لوگو-عرض', ['logo-padding'] = 'لوگو-لایهگذاری', | |||
['usercategory'] = 'ردهکاربری', ['usercategory2'] = 'ردهکاربری۲', | |||
['usercategory3'] = 'ردهکاربری۳', ['usercategory4'] = 'ردهکاربری۴', ['usercategory5'] = 'ردهکاربری۵', | |||
} | |||
) | |||
local userboxData = p[funcName](args) | local userboxData = p[funcName](args) | ||
local userbox = p.render(userboxData) | local userbox = p.render(userboxData) | ||
| خط ۷۶: | خط ۱۲۱: | ||
-- Get div tag values. | -- Get div tag values. | ||
data.float = args.float | data.float = leftRight(args.float, 'right') | ||
local borderWidthNum = checkNum(args['border-width'] or args['border-s'], 1) -- Used to calculate width. | local borderWidthNum = checkNum(args['border-width'] or args['border-s'], 1) -- Used to calculate width. | ||
data.borderWidth = addSuffix(borderWidthNum, 'px') | data.borderWidth = addSuffix(borderWidthNum, 'px') | ||
data.borderColor = args['border-color' | data.borderColor = args['border-color'] or args['border-c'] or args[1] or args['id-c'] or '#999' | ||
data.width = addSuffix(240 - 2 * borderWidthNum, 'px') -- Also used in the table tag. | data.width = addSuffix(240 - 2 * borderWidthNum, 'px') -- Also used in the table tag. | ||
data.bodyClass = args.bodyclass | data.bodyClass = args.bodyclass | ||
| خط ۸۵: | خط ۱۳۰: | ||
-- Get table tag values. | -- Get table tag values. | ||
data.backgroundColor = args['info-background'] or args[2] or args['info-c'] or '#eee' | data.backgroundColor = args['info-background'] or args[2] or args['info-c'] or '#eee' | ||
-- Get info values. | |||
data.info = args.info or args[4] or "<code>{{{اطلاعات}}}</code>" | |||
data.infoTextAlign = leftRight(args['info-a'], 'right') | |||
data.infoFontSize = checkNumAndAddSuffix(args['info-size'] or args['info-s'], 8, 'pt') | |||
data.infoHeight = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 45, 'px') | |||
data.infoPadding = args['info-padding'] or args['info-p'] or '0 4px 0 4px' | |||
data.infoLineHeight = args['info-line-height'] or args['info-lh'] or '1.25em' | |||
data.infoColor = args['info-color'] or args['info-fc'] or 'black' | |||
data.infoOtherParams = args['info-other-param'] or args['info-op'] | |||
data.infoClass = args['info-class'] | |||
-- Get id values. | -- Get id values. | ||
| خط ۹۳: | خط ۱۴۹: | ||
data.idHeight = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 45, 'px') | data.idHeight = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 45, 'px') | ||
data.idBackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd' | data.idBackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd' | ||
data.idTextAlign = args['id-a'] | data.idTextAlign = leftRight(args['id-a'], 'center') | ||
data.idFontSize = | data.idFontSize = checkNum(args['logo-size'] or args[5] or args['id-s'], 14) | ||
data.idColor = args['logo-color'] or args['id-fc'] or | data.idColor = args['logo-color'] or args['id-fc'] or data.infoColor | ||
data.idPadding = args['logo-padding'] or args['id-p'] or '0 | data.idPadding = args['logo-padding'] or args['id-p'] or '0 0 0 1px' | ||
data.idLineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em' | data.idLineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em' | ||
data.idOtherParams = args['logo-other-param'] or args['id-op'] | data.idOtherParams = args['logo-other-param'] or args['id-op'] | ||
data.idClass = args['id-class'] | data.idClass = args['id-class'] | ||
return data | return data | ||
| خط ۱۱۸: | خط ۱۶۳: | ||
-- Does argument processing for {{userbox-2}}. | -- Does argument processing for {{userbox-2}}. | ||
local data = {} | local data = {} | ||
-- Get div tag values. | -- Get div tag values. | ||
data.float = args.float or 'right' | data.float = args.float or 'right' | ||
local borderWidthNum = checkNum(args['border-s'] or args[9], 1) -- Used to calculate width. | |||
data.borderColor = | data.borderWidth = addSuffix(borderWidthNum, 'px') | ||
data.width = ' | data.borderColor = args['border-c'] or args[6] or args['id1-c'] or args[1] or '#999999' | ||
data.width = addSuffix(240 - 2 * borderWidthNum, 'px') -- Also used in the table tag. | |||
data.bodyClass = args.bodyclass | data.bodyClass = args.bodyclass | ||
-- Get table tag values. | -- Get table tag values. | ||
data.backgroundColor = args[2] or args['info- | data.backgroundColor = args['info-c'] or args[2] or '#eeeeee' | ||
-- Get info values. | |||
data.info = args.info or args[4] or "<code>{{{اطلاعات}}}</code>" | |||
data.infoTextAlign = laftRight(args['info-a'], 'right') | |||
data.infoFontSize = checkNumAndAddSuffix(args['info-s'], 8, 'pt') | |||
data.infoColor = args['info-fc'] or args[8] or 'black' | |||
data.infoPadding = args['info-p'] or '0 4px 0 4px' | |||
data.infoLineHeight = args['info-lh'] or '1.25em' | |||
data.infoOtherParams = args['info-op'] | |||
-- Get id values. | -- Get id values. | ||
data.showId = true | data.showId = true | ||
data.id = args.logo or args[3] or args.id1 or ' | data.id = args.logo or args[3] or args.id1 or 'شناسه۱' | ||
data.idWidth = checkNumAndAddSuffix(args['id1-w'], 45, 'px') | data.idWidth = checkNumAndAddSuffix(args['id1-w'], 45, 'px') | ||
data.idHeight = checkNumAndAddSuffix(args['id-h'], 45, 'px') | data.idHeight = checkNumAndAddSuffix(args['id-h'], 45, 'px') | ||
data.idBackgroundColor = | data.idBackgroundColor = args['id1-c'] or args[1] or '#dddddd' | ||
data.idTextAlign = 'center' | data.idTextAlign = 'center' | ||
data.idFontSize = | data.idFontSize = checkNum(args['id1-s'], 14) | ||
data.idLineHeight = args['id1-lh'] or '1.25em' | data.idLineHeight = args['id1-lh'] or '1.25em' | ||
data.idColor = args['id1-fc'] or | data.idColor = args['id1-fc'] or data.infoColor | ||
data.idPadding = args['id1-p'] or '0 0 0 1px' | data.idPadding = args['id1-p'] or '0 0 0 1px' | ||
data.idOtherParams = args['id1-op'] | data.idOtherParams = args['id1-op'] | ||
-- Get id2 values. | -- Get id2 values. | ||
data.showId2 = true | data.showId2 = true | ||
data.id2 = args.logo or args[5] or args.id2 or ' | data.id2 = args.logo or args[5] or args.id2 or 'شناسه۲' | ||
data.id2Width = checkNumAndAddSuffix(args['id2-w'], 45, 'px') | data.id2Width = checkNumAndAddSuffix(args['id2-w'], 45, 'px') | ||
data.id2Height = data.idHeight | data.id2Height = data.idHeight | ||
data.id2BackgroundColor = | data.id2BackgroundColor = args['id2-c'] or args[7] or args[1] or '#dddddd' | ||
data.id2TextAlign = 'center' | data.id2TextAlign = 'center' | ||
data.id2FontSize = | data.id2FontSize = checkNum(args['id2-s'], 14) | ||
data.id2LineHeight = args['id2-lh'] or '1.25em' | data.id2LineHeight = args['id2-lh'] or '1.25em' | ||
data.id2Color = args['id2-fc'] or | data.id2Color = args['id2-fc'] or data.infoColor | ||
data.id2Padding = args['id2-p'] or '0 1px 0 0' | data.id2Padding = args['id2-p'] or '0 1px 0 0' | ||
data.id2OtherParams = args['id2-op'] | data.id2OtherParams = args['id2-op'] | ||
| خط ۱۷۲: | خط ۲۱۸: | ||
-- Get div tag values. | -- Get div tag values. | ||
data.float = args.float | data.float = leftRight(args.float, 'right') | ||
local borderWidthNum = checkNum(args['border-width'] or args['border-s'], 1) -- Used to calculate width. | |||
data.borderColor = args['border-color' | data.borderWidth = addSuffix(borderWidthNum, 'px') | ||
data.width = ' | data.borderColor = args['border-color'] or args['border-c'] or args[1] or args['id-c'] or '#999' | ||
data.width = addSuffix(240 - 2 * borderWidthNum, 'px') -- Also used in the table tag. | |||
data.bodyClass = args.bodyclass | data.bodyClass = args.bodyclass | ||
| خط ۱۸۵: | خط ۲۳۲: | ||
-- Get info values. | -- Get info values. | ||
data.info = args.info or args[4] or " | data.info = args.info or args[4] or "<code>{{{اطلاعات}}}</code>" | ||
data.infoTextAlign = args['info-align'] or args['info-a'] | data.infoTextAlign = leftRight(args['info-align'] or args['info-a'], 'right') | ||
data.infoFontSize = checkNumAndAddSuffix(args['info-size'] or args['info-s'], 8, 'pt') | data.infoFontSize = checkNumAndAddSuffix(args['info-size'] or args['info-s'], 8, 'pt') | ||
data.infoPadding = args['info-padding'] or args['info-p'] or '0 4px 0 4px' | data.infoPadding = args['info-padding'] or args['info-p'] or '0 4px 0 4px' | ||
| خط ۱۹۵: | خط ۲۴۲: | ||
-- Get id2 values. | -- Get id2 values. | ||
data.showId2 = true | data.showId2 = true | ||
data.id2 = args.logo or args[3] or args.id or ' | data.id2 = args.logo or args[3] or args.id or 'شناسه' | ||
data.id2Width = checkNumAndAddSuffix(args['logo-width'] or args['id-w'], 45, 'px') | data.id2Width = checkNumAndAddSuffix(args['logo-width'] or args['id-w'], 45, 'px') | ||
data.id2Height = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 45, 'px') | data.id2Height = checkNumAndAddSuffix(args['logo-height'] or args['id-h'], 45, 'px') | ||
data.id2BackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd' | data.id2BackgroundColor = args['logo-background'] or args[1] or args['id-c'] or '#ddd' | ||
data.id2TextAlign = args['id-a'] | data.id2TextAlign = leftRight(args['id-a'], 'center') | ||
data.id2FontSize = | data.id2FontSize = checkNum(args['logo-size'] or args[5] or args['id-s'], 14) | ||
data.id2Color = args['logo-color'] or args['id-fc'] or | data.id2Color = args['logo-color'] or args['id-fc'] or data.infoColor | ||
data.id2Padding = args['logo-padding'] or args['id-p'] or '0 1px 0 0' | data.id2Padding = args['logo-padding'] or args['id-p'] or '0 1px 0 0' | ||
data.id2LineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em' | data.id2LineHeight = args['logo-line-height'] or args['id-lh'] or '1.25em' | ||
| خط ۲۱۸: | خط ۲۶۵: | ||
:css('margin', '1px') | :css('margin', '1px') | ||
:css('width', data.width) | :css('width', data.width) | ||
:addClass(' | :addClass('wikipediauserbox') | ||
:addClass(data.bodyClass) | :addClass(data.bodyClass) | ||
| خط ۲۲۴: | خط ۲۷۱: | ||
local tableroot = root:tag('table') | local tableroot = root:tag('table') | ||
tableroot | tableroot | ||
:attr('role', 'presentation') | |||
:css('border-collapse', 'collapse') | :css('border-collapse', 'collapse') | ||
:css('width', data.width) | :css('width', data.width) | ||
:css('margin-bottom', '0') | :css('margin-bottom', '0') | ||
:css('margin-top', '0') | |||
:css('background', data.backgroundColor) | :css('background', data.backgroundColor) | ||
:css('color', 'inherit') | |||
-- Render the id html. | -- Render the id html. | ||
local tablerow = tableroot:tag('tr') | local tablerow = tableroot:tag('tr') | ||
if data.showId then | if data.showId then | ||
tablerow:tag(' | tablerow:tag('td') | ||
:css('border', '0') | :css('border', '0') | ||
:css('width', data.idWidth) | :css('width', data.idWidth) | ||
| خط ۲۳۸: | خط ۲۸۸: | ||
:css('background', data.idBackgroundColor) | :css('background', data.idBackgroundColor) | ||
:css('text-align', data.idTextAlign) | :css('text-align', data.idTextAlign) | ||
:css('font-size', data.idFontSize) | :css('font-size', data.idFontSize .. 'pt') | ||
:css('font-weight', 'bold') | |||
:css('color', data.idColor) | :css('color', data.idColor) | ||
:css('padding', data.idPadding) | :css('padding', data.idPadding) | ||
| خط ۲۶۴: | خط ۳۱۵: | ||
-- Render the second id html. | -- Render the second id html. | ||
if data.showId2 then | if data.showId2 then | ||
tablerow:tag(' | tablerow:tag('td') | ||
:css('border', '0') | :css('border', '0') | ||
:css('width', data.id2Width) | :css('width', data.id2Width) | ||
| خط ۲۷۰: | خط ۳۲۱: | ||
:css('background', data.id2BackgroundColor) | :css('background', data.id2BackgroundColor) | ||
:css('text-align', data.id2TextAlign) | :css('text-align', data.id2TextAlign) | ||
:css('font-size', data.id2FontSize) | :css('font-size', data.id2FontSize .. 'pt') | ||
:css('font-weight', 'bold') | |||
:css('color', data.id2Color) | :css('color', data.id2Color) | ||
:css('padding', data.id2Padding) | :css('padding', data.id2Padding) | ||
| خط ۲۷۷: | خط ۳۲۹: | ||
:cssText(data.id2OtherParams) | :cssText(data.id2OtherParams) | ||
:wikitext(data.id2) | :wikitext(data.id2) | ||
end | |||
local title = mw.title.getCurrentTitle() | |||
if (title.namespace == 2) and not mw.ustring.match(title.text, "/") then | |||
return tostring(root) -- regular user page | |||
elseif title.namespace == 14 then | |||
return tostring(root) -- category | |||
elseif title.isTalkPage then | |||
return tostring(root) -- talk page | |||
end | |||
local legible = true | |||
local contrast = require('Module:Color contrast')._ratio | |||
local function has_text(wikitext) | |||
wikitext = mw.ustring.gsub(wikitext, "%]%]", "|%]%]") | |||
wikitext = mw.ustring.gsub(wikitext, "%[%[%s*[Mm][Ee][Dd][Ii][Aa]%s*:[^|]-(|.-)%]%]", "") | |||
wikitext = mw.ustring.gsub(wikitext, "%[%[%s*[Ii][Mm][Aa][Gg][Ee]%s*:[^|]-(|.-)%]%]", "") | |||
wikitext = mw.ustring.gsub(wikitext, "%[%[%s*[Ff][Ii][Ll][Ee]%s*:[^|]-(|.-)%]%]", "") | |||
wikitext = mw.ustring.gsub(wikitext, "%[%[%s*پرونده%s*:[^|]-(|.-)%]%]", "") | |||
return mw.text.trim(wikitext) ~= "" | |||
end | |||
if contrast { data.infoColor, data.backgroundColor, error = 0 } < 4.5 then | |||
legible = false | |||
end | |||
-- For bold text >= 14pt, requirement is only 3. | |||
local idContrastThreshold = 4.5 | |||
local id2ContrastThreshold = 4.5 | |||
if (data.idFontSize or 0) >= 14 then | |||
idContrastThreshold = 3 | |||
end | |||
if (data.id2FontSize or 0) >= 14 then | |||
id2ContrastThreshold = 3 | |||
end | |||
if data.showId and contrast { data.idColor, data.idBackgroundColor, error = 0 } < idContrastThreshold then | |||
if has_text(data.id or "") then | |||
legible = false | |||
end | |||
end | |||
if data.showId2 and contrast { data.id2Color, data.id2BackgroundColor, error = 0 } < id2ContrastThreshold then | |||
if has_text(data.id2 or "") then | |||
legible = false | |||
end | |||
end | |||
if not legible then | |||
root:wikitext('[[رده:جعبههای کاربری دارای کنتراست رنگ ناکافی]]') | |||
end | end | ||
| خط ۲۹۰: | خط ۳۹۳: | ||
cats[#cats + 1] = args.usercategory2 | cats[#cats + 1] = args.usercategory2 | ||
cats[#cats + 1] = args.usercategory3 | cats[#cats + 1] = args.usercategory3 | ||
cats[#cats + 1] = args.usercategory4 | |||
cats[#cats + 1] = args.usercategory5 | |||
-- Get the title object | |||
local title | |||
if page then | |||
title = mw.title.new(page) | |||
else | else | ||
title = mw.title.getCurrentTitle() | |||
end | |||
-- Build category handler arguments. | |||
local chargs = {} | |||
chargs.page = page | |||
chargs.nocat = yesno(args.nocat) | |||
chargs.main = '[[رده:صفحات با الگوها در فضای نام اشتباه]]' | |||
if args.notcatsubpages then | |||
chargs.subpage = 'no' | |||
end | |||
-- User namespace. | |||
local user = '' | |||
for i, cat in ipairs(cats) do | |||
user = user .. makeCat(cat) | |||
end | |||
chargs.user = user | |||
-- Template namespace. | |||
local basepage = title.baseText | |||
local template = '' | |||
for i, cat in ipairs(cats) do | |||
template = template .. makeCat(cat, ' ' .. basepage) | |||
end | end | ||
chargs.template = template | |||
return categoryHandler(chargs) | |||
end | end | ||
return p | return p | ||