“模块:Element/gallery”的版本间的差异

 
(未显示同一用户的1个中间版本)
第75行: 第75行:
 
    local reversed = ''
 
    local reversed = ''
 
    local centered = ''
 
    local centered = ''
 +
   local margin = ''
 
    if (args.distinct == 'true') then
 
    if (args.distinct == 'true') then
 
      distinct = ' distinct'
 
      distinct = ' distinct'
第83行: 第84行:
 
    if (args.align == nil or string.find(args.align, 'sideways') == nil) then
 
    if (args.align == nil or string.find(args.align, 'sideways') == nil) then
 
   centered = ' alignCenter'
 
   centered = ' alignCenter'
 +
   end
 +
   if (args.margin ~= 'false') then
 +
   centered = ' withMargin'
 
    end
 
    end
  
    return '<div class="scrollContainer horizontal toEdge withPadding withMargin gallery' .. distinct .. reversed .. centered .. '"><div class="scrollClip"><div class="flexContainer spaced alignInline">' .. figures .. '</div></div></div>'
+
    return '<div class="scrollContainer horizontal toEdge withPadding gallery' .. margin .. distinct .. reversed .. centered .. '"><div class="scrollClip"><div class="flexContainer spaced alignInline">' .. figures .. '</div></div></div>'
 
end
 
end
  

2020年8月2日 (日) 23:39的最新版本

此模块的文档可以在模块: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 figures = ''
    for key, config in ipairs(configs) do
        local content = config.image
        if (captionCount == 1) then
            if (key == 1) then
                content = content .. frame:extensionTag{name = 'figcaption', content = '<span>' .. globalCaption .. '</span>', args = {class = "asWrapper"}}
            else
                content = content .. frame:extensionTag{name = 'figcaption', content = ""}
            end
        elseif (captionCount ~= 0) then
            content = content .. frame:extensionTag{name = 'figcaption', content = '<span>' .. config.caption .. '</span>', args = {class = "asWrapper"}}
        end

        local args = {}
        args.class = 'imageWrapper plain'
        if (config.style ~= nil) then
            args.style = config.style
        end

        figures = figures .. frame:extensionTag{name = 'figure', content = content, args = args}        
    end

    local distinct = ''
    local reversed = ''
    local centered = ''
    local margin = ''
    if (args.distinct == 'true') then
        distinct = ' distinct'
    end
    if (args.align ~= nil and string.find(args.align, 'right') ~= nil) then
	    reversed = ' reverse'
    end
    if (args.align == nil or string.find(args.align, 'sideways') == nil) then
	    centered = ' alignCenter'
    end
    if (args.margin ~= 'false') then
	    centered = ' withMargin'
    end

    return '<div class="scrollContainer horizontal toEdge withPadding gallery' .. margin .. distinct .. reversed .. centered .. '"><div class="scrollClip"><div class="flexContainer spaced alignInline">' .. figures .. '</div></div></div>'
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, "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