跳转到内容

模块:Message box:修订间差异

来自TPC
Harry00025留言 | 贡献
创建页面,内容为“local p = {} local function commentSprite( pos, link, size ) if not pos then return '' end pos = mw.text.trim( pos ) if pos == '' then return '' end local icon = '%s|class=comment-icon|link=%s|alt=' local iconMap = { 'question', 'warning', 'done', 'undone', 'support', 'oppose', 'doing', 'information', 'disallow', 'on hold', 'strong support', 'weak support', 'neutral', 'weak oppose', 'strong oppose', 'transferred', 'withdr…”
 
Harry00025留言 | 贡献
Harry00025移动页面模块:Message Box模块:Message box
 
(没有差异)

2026年5月28日 (四) 06:04的最新版本

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

local p = {}

local function commentSprite( pos, link, size )
	if not pos then
		return ''
	end
	pos = mw.text.trim( pos )
	if pos == '' then
		return ''
	end
	local icon = '[[File:Comment %s.svg|%s|class=comment-icon|link=%s|alt=]]'
	local iconMap = {
		'question', 'warning', 'done', 'undone', 'support', 'oppose', 'doing', 'information', 'disallow',
		'on hold', 'strong support', 'weak support', 'neutral', 'weak oppose', 'strong oppose', 'transferred', 'withdrawn'
	}
	return icon:format( tonumber( pos ) and iconMap[ tonumber( pos ) ] or pos:lower(), size or '16px', link or '' )
end

local function argAlter( args )
	if type( args ) ~= 'table' then
		return nil
	end
	local defaultAltMethod = function( base, alt ) return alt or base end
	local altMethods = {
		class = function( base, alt )
			local result = mw.text.trim( table.concat( { base or '', alt or '' }, ' ' ) )
			return ( result == '' ) and nil or result
		end
	}
	for k, _ in pairs( args ) do
		if k:sub( 1, 1 ) == '_' then
			k = k:sub( 2 )
		end
		local v = ( altMethods[ k ] or defaultAltMethod )( args[ k ], args[ '_' .. k ] )
		if v then
			args[ k ] = v
		end
	end
	return args
end

function p.getStyles( f )
	local args = f or {}
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	end

	local loadStyle = require( 'Module:TSLoader' ).call

	local styles = loadStyle( args[ 1 ] or args.basic or 'Template:Message box/styles.css' )
	local currentNamespace = mw.title.getCurrentTitle().namespace

	return styles .. ( ( ( not args.nocustom ) and ( currentNamespace == 2 or currentNamespace == 3 ) ) and loadStyle( args[ 2 ] or args.custom or 'Template:Message box/custom.css' ) or '' )
end

function p.main( f )
	local rawTitle = nil
	local rawText = nil

	local args = f
	local frame = mw.getCurrentFrame()
	if f == frame then
		args = argAlter( require( 'Module:ProcessArgs' ).merge( true ) )
	else
		-- Will not use type() to detect whether f is a table or not, which is an expensive function.
		-- So, ideally, calling from templates will not cause an error ( f is a frame ), but will occurs when calling from modules if f is not a table.
		rawTitle = f.title
		rawText = f.text
	end

	-- Equivalent to args.title and args.text, but without trimming, to avoid formatting issues.
	-- Variables args.title or args.text will not use to form a real title or text, just indicates whether respective arguments is set or not.
	-- Arguments from the parent frame take priority of the current frame, per Module:ProcessArgs.
	rawTitle = frame:preprocess( rawTitle or ( frame:getParent().args._title or frame.args._title ) or ( frame:getParent().args.title or frame.args.title ) )
	rawText = frame:preprocess( rawText or ( frame:getParent().args._text or frame.args._text ) or ( frame:getParent().args.text or frame.args.text ) )

	-- Create main frame
	local result = mw.html.create( 'div' ):addClass( 'msgbox' )
	if args.mini then
		result:addClass( 'msgbox-mini' )
		if not args.width then
			result:addClass( 'msgbox-compat' )
		end
	elseif args.compat then
		if not args.width then
			result:addClass( 'msgbox-compat' )
		end
	elseif not args.wide then
		result:addClass( 'msgbox-normal' )
	end
	result:addClass( 'searchaux' ):addClass( args.bgclass or 'msgbox-' .. ( args.type or 'notice' ) ):addClass( args.class ):cssText( args.css ):css{
		[ 'background-color' ] = args.bgcol,
		[ 'border-left-color' ] = args.linecol,
		[ 'font-size' ] = args[ 'font-size' ],
		[ 'width' ] = args.width,
	}

	-- Create optional icon / image frame
	if args.icon or args.image then
		result:addClass( 'has-image' )
		local resultOpt = mw.html.create( 'div' ):addClass( 'msgbox-icon-image' ):cssText( args.imagecss )
		local imagefield = ''
		if args.icon then
			imagefield = imagefield .. commentSprite( args.icon, args.iconlink, args.iconsize )
		end
		if args.image then
			imagefield = imagefield .. mw.ustring.format(
				'[[File:%s|%s%s%s|text-top]]',
				args.image,
				args.imagesize or ( args.mini and '16px' or '32px' ),
				args.imagelink and ( '|link=' .. args.imagelink ) or '',
				args.imageclass and ( '|class=' .. args.imageclass ) or ''
			)
		end
		resultOpt:wikitext( frame:preprocess( args.imagetextbefore or '' ) .. imagefield .. frame:preprocess( args.imagetextafter or '' ) )
		result:node( resultOpt )
	end

	-- Create displaytext frame
	local resultNext = mw.html.create( 'div' ):addClass( 'msgbox-body' ):addClass( 'align-' .. ( args[ 'text-align' ] or 'left' ) ):cssText( args.textcss )
	-- Create optional title frame inside displaytext frame
	if args.title then
		local resultNextTitle = mw.html.create( 'div' ):addClass( 'msgbox-title' )
		local resultNextTitleText = "<b>" .. rawTitle .. "</b>"
		if args.customaction then
			resultNextTitleText = resultNextTitleText .. '<sup>' .. frame:preprocess( args.customaction ) .. '</sup>'
		end
		if args.discuss or args.discussPage or args.discussAnchor then
			local directLinkTarget = args.discussPage or ( mw.title.getCurrentTitle().talkPageTitle and mw.title.getCurrentTitle().talkPageTitle.fullText )
			if args.discussAnchor then
				directLinkTarget = directLinkTarget .. '#' .. args.discussAnchor
			end
			local discussText = '&nbsp;<sup>' .. require( 'Module:Direct link' ).main{ directLinkTarget, '讨论' } .. '</sup>'
			if args.linkshere then
				discussText = discussText .. '&nbsp;<sup>[[Special:WhatLinksHere/' .. mw.title.getCurrentTitle().fullText .. '|' .. require( 'Module:STConversion' ).call{ [ 'zh-cn' ] = '链入', [ 'zh-tw' ] = '連入' } .. ']]</sup>'
				if mw.title.getCurrentTitle().namespace == 6 then
					discussText = discussText .. '&nbsp;<sup>[[Special:GlobalUsage/' .. mw.title.getCurrentTitle().text .. '|全域使用]]</sup>'
				end
			end
			resultNextTitleText = resultNextTitleText .. discussText
		end
		resultNextTitle:wikitext( resultNextTitleText )
		resultNext:node( resultNextTitle )
	end
	-- Create optional text frame inside displaytext frame, after optional title frame
	if args.text then
		resultNext:tag( 'div' ):addClass( 'msgbox-text' ):wikitext( '\n' .. rawText .. '\n' )
		-- Lint: p tag would be auto generated by wikitext parser with the new lines here
	end
	-- Add displaytext frame to main frame
	result:node( resultNext )

	-- Add templatestyles to final result
	result = ( args.noautoloadstyles and '' or p.getStyles() ) .. tostring( result )

	-- Append tracking categories
	if args.bgcol then
		result = result .. '[[Category:使用Message box的bgcol参数的页面]]'
	end
	if args.linecol then
		result = result .. '[[Category:使用Message box的linecol参数的页面]]'
	end

	return result
end

return p