跳转到内容
打开/关闭菜单
  • 5 条目
  • 6 文件
  • 13 用户
  • 665 编辑
久远澪Wiki
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

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

-- Module:TextEffect
-- 解析文字背景效果串(如「毛玻璃+遮罩」「阴影 + 遮罩」「遮罩+毛玻璃」),
-- 顺序无关、容忍空格,输出 CSS 类名(.text-bg-*)。
--
-- 函数:
--   p.class(frame)  输出类名:{{#invoke:TextEffect|class|毛玻璃+遮罩+阴影}}
--   p.text(frame)   渲染多段文字背景(由 Template:文字背景 调用):
--                   位置1=默认效果,位置2..N=各段内容;
--                   第 N 段(位置 N+1)可用 效果N/颜色N/浅色N/深色N/透明度N 覆盖。
--                   兼容单段命名用法:效果=...|内容=...
local p = {}

-- 效果名 → 类名(支持常见别名)
local classMap = {
	['毛玻璃'] = 'text-bg-glass',
	['玻璃'] = 'text-bg-glass',
	['遮罩'] = 'text-bg-mask',
	['阴影'] = 'text-bg-shadow',
}

-- 效果串 → 类名串(含 .text-bg 前缀;去重、排序稳定)
local function buildClasses(raw)
	if not raw then raw = '' end
	local seen = {}
	for part in raw:gmatch('[^%+]+') do
		local name = part:gsub('^%s+', ''):gsub('%s+$', '')
		local cls = classMap[name]
		if cls then
			seen[cls] = true
		end
	end
	local out = { 'text-bg' }
	for cls in pairs(seen) do
		table.insert(out, cls)
	end
	table.sort(out)
	return table.concat(out, ' ')
end

-- 输出类名
function p.class(frame)
	local raw = frame.args[1] or frame.args['效果'] or frame.args['effect'] or ''
	return buildClasses(raw)
end

-- 渲染多段文字背景(供模板使用,取父帧全部参数)
function p.text(frame)
	local args = (frame:getParent() and frame:getParent().args) or frame.args
	local defaultEffect = args['效果'] or args[1] or ''

	-- 收集段:位置 2..N 为各段内容;若无位置段但给了「内容」则作为单段
	local segs = {}
	local i = 2
	while args[i] and args[i] ~= '' do
		table.insert(segs, { content = args[i], idx = i })
		i = i + 1
	end
	if #segs == 0 and args['内容'] and args['内容'] ~= '' then
		table.insert(segs, { content = args['内容'], idx = 0 })
	end
	if #segs == 0 then
		return ''
	end

	local out = {}
	for _, seg in ipairs(segs) do
		-- 段号:位置 N+1 → 第 N 段(覆盖参数为 效果N 等);命名「内容」段无编号
		local n = seg.idx >= 2 and (seg.idx - 1) or 0
		local getN = function(prefix)
			if n > 0 then
				local v = args[prefix .. n]
				if v and v ~= '' then return v end
			end
			return args[prefix]
		end

		local effect = getN('效果')
		if not effect or effect == '' then
			effect = defaultEffect
		end
		local cls = buildClasses(effect)
		local style = {}
		local function add(var, val)
			if val and val ~= '' then
				table.insert(style, var .. ':' .. val .. ';')
			end
		end
		add('--text-bg-color', getN('颜色'))
		add('--text-bg-color-light', getN('浅色'))
		add('--text-bg-color-night', getN('深色'))
		add('--text-bg-opacity', getN('透明度'))

		local span = '<span class="' .. cls .. '"'
		if #style > 0 then
			span = span .. ' style="' .. table.concat(style, '') .. '"'
		end
		span = span .. '>' .. seg.content .. '</span>'
		table.insert(out, span)
	end
	return table.concat(out, '<br/>')
end

return p