Module:Format link: Difference between revisions

From NeuroWiki
Jump to navigation Jump to search
创建页面,内容为“local p = {} function p.call( inputArgs ) if not inputArgs or type( inputArgs ) ~= 'table' then return nil end local rawPage = inputArgs[ 1 ] or '' local rawSection = inputArgs[ 2 ] or '' local finalPage, finalSection if rawPage:find( '[', 1, true ) then return rawPage else if rawPage:find( '|', 1, true ) then return '' .. rawPage .. '' else if rawPage:find( '#', 1, true ) then local splitParts = mw.text.split( rawPage:gsub( '#+…”
 
m 1 revision imported
(No difference)

Revision as of 12:29, 1 November 2024

此模块用于实现{{Format link}}。




local p = {}

function p.call( inputArgs )
	if not inputArgs or type( inputArgs ) ~= 'table' then
		return nil
	end

	local rawPage = inputArgs[ 1 ] or ''
	local rawSection = inputArgs[ 2 ] or ''

	local finalPage, finalSection
	if rawPage:find( '[', 1, true ) then
		return rawPage
	else
		if rawPage:find( '|', 1, true ) then
			return '[[:' .. rawPage .. ']]'
		else
			if rawPage:find( '#', 1, true ) then
				local splitParts = mw.text.split( rawPage:gsub( '#+', '#' ), '#', true )
				finalPage = splitParts[ 1 ]
				finalSection = splitParts[ 2 ]
			else
				finalPage = rawPage
				finalSection = rawSection
			end

			finalPage = finalPage:gsub( '^:', '' )

			if finalSection == '' then
				return '[[:' .. finalPage .. ']]'
			elseif finalPage == '' then
				return '[[#' .. finalSection .. '|§ ' .. finalSection .. ']]'
			else
				return '[[:' .. finalPage .. '#' .. finalSection .. '|' .. finalPage .. ' § ' .. finalSection .. ']]'
			end
		end
	end
end

function p.main( f )
	local args = f
	local frame = mw.getCurrentFrame()
	if f == frame then
		args = require( 'Module:ProcessArgs' ).merge( true )
	end

	return p.call( {
		args[ 1 ] or '',
		args[ 2 ] or ''
	} )
end

return p