“模块:Element/gallery”的版本间的差异
General Support(讨论 | 贡献) |
General Support(讨论 | 贡献) (修正单张图片标题位置不正确的问题) |
||
第50行: | 第50行: | ||
end | end | ||
− | local shouldUseGlobalCaption = captionCount == 1 and # | + | local shouldUseGlobalCaption = captionCount == 1 and #configs > 1 |
local figures = '' | local figures = '' |
2022年2月12日 (六) 23:40的最新版本
此模块的文档可以在模块:Element/gallery/doc创建
local p = {} function p.main(frame) -- {{Element/gallery| -- Snow.png|w300px|h200px|mainCaption -- Lake.png|w100%|h200px!|secondCaption -- }} -- {{Element/gallery| -- Snow.png -- Snow.png -- Lake.png|100%|h200px!|secondCaption -- }} -- contains "\n" ? split => (not first): file name -- (first): end with [.png, .gif, .jpg, .jpeg, .pdf, .bmp, .tif, .tiff, .svg] -- or start by [https://, http://, //] local configs = {} local tempConfig = {} local captionCount = 0 local globalCaption -- the only caption to be the globle one local args = frame:getParent().args local stream = splitArgs(args) for i = 1, #stream do local text = stream[i] local image = isImage(text) local style = parseSize(text) if (image == 1) then if (i > 1) then table.insert(configs, tempConfig) tempConfig = {} end tempConfig.image = "[[File:" .. text .. "|link=|alt=]]" elseif (image == 2) then tempConfig.image = '<image src="' .. text .. '">' elseif (style == 0) then tempConfig.caption = text captionCount = captionCount + 1 globalCaption = text else if (tempConfig.style == nil) then tempConfig.style = "" end tempConfig.style = tempConfig.style .. style end if (i == #stream) then table.insert(configs, tempConfig) end end local shouldUseGlobalCaption = captionCount == 1 and #configs > 1 local figures = '' for key, config in ipairs(configs) do local content = config.image if (captionCount > 0 and not shouldUseGlobalCaption) then content = content .. frame:extensionTag{name = 'figcaption', content = '<span>' .. (config.caption or '') .. '</span>', args = {class = "asWrapper"}} end local args = {} args.class = 'imageWrapper plain' if (config.style ~= nil) then args.style = config.style end if (not shouldUseGlobalCaption) then figures = figures .. frame:extensionTag{name = 'figure', content = content, args = args} else figures = figures .. frame:extensionTag{name = 'division', content = content, args = args} end end local classes = '' if (args.distinct == 'true') then classes = classes .. ' distinct' end if (args.align ~= nil and string.find(args.align, 'right') ~= nil) then classes = classes .. ' reverse' end if (args.align == nil or string.find(args.align, 'sideways') == nil) then classes = classes .. ' alignCenter' end if (args.margin ~= 'false') then classes = classes .. ' withMargin' end local style = '' local globalCaptionElement = '' if (shouldUseGlobalCaption) then style = ' style="margin-bottom:0"' globalCaptionElement = frame:extensionTag{name = 'figcaption', content = globalCaption} end local scrollView = '<div class="scrollContainer horizontal toEdge withPadding gallery' .. classes .. '"' .. style .. '"><div class="scrollClip"><div class="flexContainer spaced alignInline">' .. figures .. '</div></div></div>'; if (not shouldUseGlobalCaption) then return scrollView end return frame:extensionTag{name = 'figure', content = scrollView .. globalCaptionElement, args = {style = 'overflow:visible;'}} end function parseSize(text) local unit = "px" if (string.gsub(text, "[hw]?%d*px!?", "") == text) then if(string.gsub(text, "[hw]?%d*%%!?", "") == text) then return 0 else unit = "%" end end local dir = string.sub(text, 1, 1) local important = string.sub(text, string.len(text)) == "!" local number = string.gsub(text, "[^%d]", "") local style = "" -- it looks ugly without `?:` if (dir == "w") then style = "width: " .. number if (unit == "px") then style = style .. "px; " else style = style .. "%; width: calc(var(--content-width) * " .. (tonumber(number) / 100) .. "); " end if (important) then style = style .. "max-width: none; " end else style = "height: " .. number if (unit == "px") then style = style .. "px; " else style = style .. "%; " end if (important) then style = style .. "max-height: none; " end end return style end function splitArgs(args) local array = {} for index, arg in ipairs(args) do local items = mw.text.split(arg, "\n", true) for key, item in ipairs(items) do local trimmedItem = mw.text.trim(item) if (trimmedItem ~= "") then table.insert(array, mw.text.trim(item)) end end end return array end function isImage(text) if (isURL(text) == 1) then return 2 end local splitText = mw.text.split(text, ".", true) local extension = string.lower(splitText[#splitText]) if ( extension == "png" or extension == "jpg" or extension == "svg" or extension == "gif" or extension == "bmp" or extension == "tif" or extension == "jpeg" or extension == "tifd" ) then return 1 end return 0 end function isURL(url) if string.find(url, "/") ~= nil and ((string.find(url, "https?://") == 1) or (string.find(url, "//") == 1) or (string.find(url, "[%w%.]*%.([%w-]+%.%w+)") == 1)) then return 1 else return 0 end end return p