وحدة:Gallery
المظهر
| هذه الوحدة تعتمد على الوحدات الأخرى التالية: |
| صفحة وحدة لوا متضمنة في عدة صفحات ويمكن ملاحظتها على نطاق واسع. أختبر تغييرات الوحدة في /الملعب أو /المختبر أو صفحات فرعية، أو عبر ملعب الوحدة. فكر في مناقشة التغييرات في صفحة نقاش قبل تنفيذها. |
| هذه الوحدة تستخدم أنماط القوالب: |
تنفذ {{معرض الصور}}
تصانيف التتبع
[عدل]
-- This module implements {{gallery}} by wrapping the <gallery> core extension tag.
local p = {}
function p.getArgs(frame, options)
options = options or {}
local ok, argaliases = pcall(require, 'Module:Gallery/argaliases')
if not ok or type(argaliases) ~= 'table' then
-- fallback to Module:Arguments with aliases if argaliases missing
local ok2, argsmod = pcall(require, 'Module:Arguments with aliases')
if ok2 and type(argsmod.getArgs) == 'function' then
return argsmod.getArgs(frame, options)
end
-- ultimate fallback: read direct args
local origArgs = (type(frame.getParent) == 'function') and frame:getParent().args or frame
local args = {}
for k, v in pairs(origArgs) do
if v ~= '' then args[k] = v end
end
return args
end
-- build alias map: alias_lower -> canonical_name
local alias_map = {}
for canon, aliases in pairs(argaliases.aliases or {}) do
-- ensure canonical itself maps to itself
alias_map[mw.ustring.lower(canon)] = canon
if type(aliases) == 'table' then
for _, a in ipairs(aliases) do
if type(a) == 'string' then
alias_map[mw.ustring.lower(a)] = canon
end
end
end
end
-- prepare numbered alias patterns
local numbered_patterns = {}
for canon_with_hash, aliases in pairs(argaliases.numbered_aliases or {}) do
-- canonical base (e.g., "class#") -> base "class"
local canon_base = tostring(canon_with_hash):gsub('#', '')
if type(aliases) == 'table' then
for _, a in ipairs(aliases) do
if type(a) == 'string' then
-- build lua pattern for matching: replace '#' with '(%d+)' and anchor
local pat = '^' .. mw.ustring.gsub(a, '#', '(%d+)') .. '$'
-- but also prepare lowercase variant for matching
table.insert(numbered_patterns, { canon_base = canon_base, alias_pattern = pat, raw_alias = a })
end
end
end
end
-- read original args
local origArgs = (type(frame.getParent) == 'function') and frame:getParent().args or frame
local args = {}
-- first preserve numeric sequential fields (1..n) and any numeric-indexed args
for i = 1, 1000 do
-- stop when no more numeric indices (this is safe: it will break quickly)
local v = origArgs[i]
if v == nil then break end
if v ~= '' then args[#args + 1] = v end
end
-- now handle named args and aliases
for k, v in pairs(origArgs) do
-- skip numeric indices already handled
if type(k) ~= 'number' then
if v == '' or v == nil then
-- skip empty as per old behavior
else
local klow = mw.ustring.lower(tostring(k))
local mapped = nil
-- exact alias or canonical
if alias_map[klow] then
mapped = alias_map[klow]
args[mapped] = v
else
-- try numbered alias patterns
for _, patinfo in ipairs(numbered_patterns) do
-- use mw.ustring.match to support unicode
local num = mw.ustring.match(klow, patinfo.alias_pattern)
if num then
-- create canonical key like "class3"
local canon_key = patinfo.canon_base .. tostring(num)
args[canon_key] = v
mapped = canon_key
break
end
end
end
-- if still not mapped, see if the key itself looks like canonical numbered (e.g., class3) or alt3
if not mapped then
-- match something that ends with digits, capture base and number
local base, num = mw.ustring.match(klow, '^([%a_%-]+)(%d+)$')
if base and num then
-- accept it as-is (preserve original case by using lowercased key)
args[base .. num] = v
else
-- no alias: keep original key (preserve case)
args[k] = v
end
end
end
end
end
-- final: return args table
return args
end
-- ===== end getArgs =====
-- Dependencies
local templatestyles = 'Module:Gallery/styles.css'
local yesno = require('Module:Yesno')
local plaintextModule = require('Module:Plain text')
local function plaintext(text)
-- stips out external links without labels,
-- and then passes to the Plain_text module to clean the rest
return plaintextModule.main({ args = {
text:gsub("([^%[])%[([^%[%]%s]+)%]", '%1')
, encode = "no" } })
end
local function trim(s)
return mw.ustring.gsub(mw.ustring.gsub(s or '', '%s', ' '), '^%s*(.-)%s*$', '%1')
end
local tracking, preview
local function isImage(file)
local file = trim(file):lower() -- Case insensitive check
-- Check if it starts with "File:", "Image:", or "Media:"
local prefix = file:match("^(%a+):")
if prefix and (prefix == "file" or prefix == "image" or prefix == "media") then
return true
end
local valid_extensions = {
"apng", "djvu", "flac", "gif", "jfi", "jfif", "jif", "jpe", "jpeg", "jpg",
"m1a", "m1v", "m2a", "m2v", "mid", "mp1", "mp2", "mp3", "mpa", "mpe", "mpeg", "mpg",
"mpv", "oga", "ogg", "ogv", "opus", "pdf", "png", "stl", "svg", "svgz", "tif", "tiff",
"wav", "wave", "webm", "webp", "xcf"
}
-- Extract file extension, of 3 or 4 characters only
local ext = file:match("%.(%w%w%w%w?)$")
-- Check if the extension is in the valid list
if ext then
for _, valid_ext in ipairs(valid_extensions) do
if ext == valid_ext then
table.insert(tracking, '[[تصنيف:صفحات تستخدم معرض الصور بدون بادئة وسائط]]')
return true
end
end
end
return false
end
local function checkarg(k,v)
if k and type(k) == 'string' then
if k == 'align' or k == 'state' or k == 'style' or k == 'title' or
k == 'width' or k == 'طول' or k == 'الطول' or k == 'height' or k == 'whitebg' or
k == 'mode' or k == 'footer' or k == 'perrow' or k == 'noborder' or
k:match('^alt%d+$') or k:match('^class%d+$') or k:match('^%d+$') then
-- valid
elseif k == 'captionstyle' then
if not v:match('^text%-align%s*:%s*center[;%s]*$') then
table.insert(tracking, '[[تصنيف:صفحات تستخدم قالب مع وسيط نمط تعليق]]')
end
else
-- invalid
local vlen = mw.ustring.len(k)
k = mw.ustring.sub(k, 1, (vlen < 25) and vlen or 25)
k = mw.ustring.gsub(k, '[^%w%-_ ]', '?')
table.insert(tracking, '[[تصنيف:صفحات تستخدم معرض الصور بمعلمات غير معروفة|' .. k .. ']]')
table.insert(preview, '"' .. k .. '"')
end
end
end
function p.gallery(frame)
local options = {}
local args = p.getArgs(frame, options)
tracking, preview = {}, {}
for k, v in pairs(args) do
checkarg(k, v)
end
if (args.mode or '') == 'packed' and (args.align or '') == '' then
args.align = 'center'
end
if (args.align or '') == 'centre' then
args.align = 'center'
end
local tbl = mw.html.create('div')
tbl:addClass('mod-gallery')
if args.state then
tbl
:addClass('mod-gallery-collapsible')
:addClass('collapsible')
:addClass(args.state)
end
if args.style then
tbl:cssText(args.style)
else
tbl:addClass('mod-gallery-default')
end
if args.align then
tbl:addClass('mod-gallery-' .. args.align:lower())
end
if args.title then
tbl:tag('div')
:addClass('title')
:tag('div')
:wikitext(args.title)
end
local gargs = {}
gargs['class'] = 'nochecker' .. (args.noborder and '' or ' bordered-images')
gargs['widths'] = tonumber(args.width) or 180
gargs['heights'] = tonumber(args.height) or 180
gargs['style'] = args.captionstyle
gargs['perrow'] = args.perrow
gargs['mode'] = args.mode
if yesno(args.whitebg or 'yes') then
gargs['class'] = gargs['class'] .. ' whitebg'
end
local virtualgallery = {}
local gallery = {}
local imageCount = 0
local zwsp = string.char(0xE2, 0x80, 0x8B) -- U+200B Zero Width Space
local zwnj = string.char(0xE2, 0x80, 0x8C) -- U+200C Zero Width Non-Joiner
local skininvert = zwsp .. zwsp .. zwsp;
local bgtransparent = zwsp .. zwsp .. zwnj;
for i = 1, #args do
local currentfield = trim(args[i]) or ''
if currentfield == '' then
-- Skip empty fields
elseif isImage(currentfield) then
imageCount = imageCount + 1
virtualgallery[imageCount] = { currentfield }
elseif imageCount > 0 and virtualgallery[imageCount][2] == nil then
virtualgallery[imageCount][2] = currentfield
end
end
local altCount = 0;
for n = 1, #virtualgallery do
local img = virtualgallery[n][1]
local caption = virtualgallery[n][2] or ''
local alt = trim(args['alt' .. n] or '')
local class = trim(args['class' .. n] or '')
if alt ~= '' then
altCount = altCount + 1
else
alt = (caption ~= '') and plaintext(caption) or ''
end
if mw.ustring.find(class, 'skin%-invert') then
alt = alt .. skininvert
end
if mw.ustring.find(class, 'bg%-transparent') then
alt = alt .. bgtransparent
end
table.insert(gallery, img ..
(alt ~= '' and (' |alt=' .. alt) or '') ..
(caption ~= '' and (' |' .. caption) or ''))
end
if math.ceil(#args / 2) > imageCount then
if altCount > 0 then
table.insert(tracking, '[[تصنيف:صفحات تستخدم قالب معرض مع احتمال عدم تطابق النص البديل]]')
end
end
tbl:tag('div')
:addClass('main')
:tag('div')
:wikitext(
frame:extensionTag{ name = 'gallery', content = '\n' .. table.concat(gallery,'\n'), args = gargs}
)
if args.footer then
tbl:tag('div')
:addClass('footer')
:tag('div')
:wikitext(args.footer)
end
local trackstr = (#tracking > 0) and table.concat(tracking, '') or ''
if #preview > 0 then
trackstr = require('Module:If preview')._warning({
'وسائط غير معروفة ' .. table.concat(preview, '; ') .. '.'
}) .. trackstr
end
return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(tbl) .. trackstr
end
return p