全站通知:

模块:GalleryWrapper

来自高达WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:GalleryWrapper/doc创建

local p = {}

-- 主函数,生成n个<div style="width:150px;text-align:center;">[[File:图片1.jpg|150px]]说明1<div>
function p.createPhotos(frame)
	-- 拆掉gallery标签
	local lines = p.stripGallery(frame)
	-- 把所有"\"换成"|",无论是gallery的内容,或者模板的分行或者[[File:]]分割
	lines = p.escapePipes(lines)
	-- 把输入的lines按照分行符拆开,如果一行内有[[File:]]也分割变成string的array
	local fileLines = p.cleanImages(lines)
	local divs = ''
	if fileLines ~= nil and fileLines ~= '' and #fileLines > 0 then
		for _, fline in ipairs(fileLines) do
		    line = mw.text.trim(fline)
		    local file, desc = mw.ustring.match(line, "^(.-)|(.*)$")
		    if file then
		        file = mw.text.trim(file)
		        desc = desc and mw.text.trim(desc) or ''
		    else
		        -- 没有 | 的情况
		        file = mw.text.trim(line)
		        desc = '&nbsp;'
		    end
			divs = divs .. "<div>[[File:" .. file .. "|150px]]" .. desc .. "</div>"
		end
	end
	
	return divs
end

-- 辅助函数:把字符串中的所有 | 替换成 {{!}}
function p.escapePipes(str)
    if not str or str == '' then
        return ''
    end
    -- 使用 mw.ustring.gsub(推荐,比 string.gsub 更好,支持 unicode)
    return mw.ustring.gsub(str, '\\', '|')
end

-- 辅助函数:把前后的gallery标签删掉
function p.stripGallery(frame)
    -- 支持两种调用方式
    local content = frame.args.content or frame.args[1] or ''
    local paramName = frame.args.param or frame.args[2] or ''
    
    if paramName == '' then
    	mw.log("无模板参数,返回空值")
    	return ''
    end
    
    mw.log("=== stripGallery Debug ===")
    mw.log("参数名: " .. paramName)
    mw.log("传入内容长度: " .. mw.ustring.len(content))
    
    -- 如果不是 strip marker,直接清理返回
    if not mw.ustring.find(content, "\127") then
        local result = mw.ustring.gsub(content, "<gallery[^>]*>", "")
        result = mw.ustring.gsub(result, "</gallery>", "")
        mw.log("没有strip marker,清理可能的gallery然后返回")
        return mw.text.trim(result)
    end
    
    -- === 读取当前页面原始 wikitext ===
    local title = mw.title.getCurrentTitle()
    local rawPage = title:getContent() or ''
    
    -- 构造正则:匹配 |参数名= <gallery>...</gallery>
    -- 支持换行、空格、属性等
    local pattern = "|%s*" .. mw.ustring.gsub(paramName, "([%-%.$])", "%%%1") ..
                    "%s*=%s*(<gallery.-</gallery>)"
    
    local galleryMatch = mw.ustring.match(rawPage, pattern)
    
    -- 备用匹配(更宽松)
    if not galleryMatch then
        galleryMatch = mw.ustring.match(rawPage, "<gallery.-</gallery>")
    end
    
    if galleryMatch then
        local result = mw.ustring.gsub(galleryMatch, "<gallery[^>]*>", "")
        result = mw.ustring.gsub(result, "</gallery>", "")
        result = mw.text.trim(result)
        
        mw.log("成功提取 gallery 内容,长度: " .. mw.ustring.len(result))
        return result
    else
        mw.log("有strip marker但是未能匹配到 gallery,返回空")
        return ''   -- 或返回 "未找到 gallery" 等提示
    end
end

-- 清理 [[File: ]]符号 以及除了Caption以外的参数,变成n行 图片1.jpg|说明1
function p.cleanImages(text)
    mw.log("=== cleanImages Debug ===")
    mw.log("传入内容长度: " .. mw.ustring.len(text or ""))
    if text == '' or text == nil then 
        mw.log("返回空结果")
        return {} 
    end

    local results = {}

    -- 先按行拆分
    local lines = mw.text.split(text, '[\r\n]+')

    for _, line in ipairs(lines) do
        line = mw.text.trim(line)
        if line == '' then 
            -- 空行直接跳过
        else
            -- 在每一行里匹配所有 [[File:...]]
            local pattern = "%[%[%s*[Ff][Ii][Ll][Ee]%s*:%s*([^|%]]+)(.-)%]%]"
            local foundTag = false
            for filename, params in mw.ustring.gmatch(line, pattern) do
                filename = mw.text.trim(filename)
                
                -- 处理说明文字
                local caption = ''
                if params and params ~= '' then
                    local parts = mw.text.split(params, '|')
                    for i = #parts, 1, -1 do
                        local p = mw.text.trim(parts[i])
                        if p ~= '' then
                            if not (p:match("^%d+px$") or 
                                    p:match("^[Uu]pright") or 
                                    p:match("^(thumb|thumbnail|left|right|center|none)$") or
                                    p:match("^%d+$")) then
                                caption = p
                                break
                            end
                        end
                    end
                end

                if filename ~= '' then
                    local output = filename
                    if caption ~= '' then
                        output = output .. '|' .. caption
                    end
                    table.insert(results, output)
                    foundTag = true
                    mw.log("提取到: " .. output)
                end
            end
            if foundTag == false then
            	local lineStrip = mw.ustring.match(line, "[Ff][Ii][Ll][Ee]:%s*(.-)%s*$")
    			lineStrip = lineStrip and mw.text.trim(lineStrip) or mw.text.trim(line)
    			mw.log("提取到: " .. lineStrip)
            	table.insert(results, lineStrip)
            end
        end
    end

    mw.log("共提取 " .. #results .. " 个图片")
    return results
end

return p