Module:Tlx

MyWikiBiz, Author Your Legacy — Friday November 29, 2024
Jump to navigationJump to search

Template:Module rating

Usage

{{#invoke:Tlx|function_name|SISTER=sister project name}}

Implements a version of {{tlx}}, {{tlxs}}, {{tlxb}}, {{tlxi}}, {{tlxc}}, and {{temt}}.

{{tlx}} and {{temt}} use the function name tlx, {{tlxs}} uses tlxs, {{tlxb}} uses tlxb, {{tlxi}} uses tlxi, and {{tlxs}} uses tlx with Template:Para.

Generally, a call to this module can be placed at the front of a template call to display formatted code instead of the template output. For example, if this module is implemented at {{tlx/sandbox}}, a function call such as {{convert|1|m|disp=flip}} can be displayed as formatted text using the code {{tlx/sandbox|convert|1|m|disp=flip}}, which outputs Template:Tlx/sandbox.

This has the following caveats:

  • Template:Para, Template:Para, and Template:Para are reserved for compatibility with the legacy {{tlx}} template. In order to display {{x0|LANG=de|SISTER=wikt|subst=yes}} you would need to use workarounds from the old template such as {{tlx/sandbox|x0|<nowiki>LANG=en</nowiki>|SISTER{{=}}wikt|4=subst=yes}} to produce Template:Tlx/sandbox.
  • Parameters are presented in alphabetical order, starting with numbered parameters, regardless of input order. {{tlx/sandbox|x0|z=y|x=w|a|b}} displays Template:Tlx/sandbox
  • Since the Template:Para parameter is used for the template name, numbered parameters where the number is explicitly specified must be offset by one (as was true with the legacy {{tlx}} template): {{tlx/sandbox|x0|2=first|3=second|4=third=***}} produces Template:Tlx/sandbox.
  • NOTE: you must be careful not to double-define a parameter. {{tlx/sandbox|x0|first|2=second=**}} will output Template:Tlx/sandbox (and generate a warning) since the 2 parameter was defined twice. Use {{tlx/sandbox|x0|first|3=second=**}} to display Template:Tlx/sandbox.
  • To display the number of a numbered parameter explicitly, the same workarounds from the legacy template still work: {{tlx/sandbox|x0|<nowiki>1=first</nowiki>|2{{=}}second|4=3=third}} produces Template:Tlx/sandbox



local p = {}

bold, ital, subst = "", false, false

function p.tlxs(frame)
	subst=true
	return p.tlx(frame)
end

function p.tlxb(frame)
	bold="'''"
	return p.tlx(frame)
end

function p.tlxi(frame)
	ital = true
	return p.tlx(frame)
end

function p.tlx(frame)
	local outStr = frame:extensionTag{name='nowiki',content='{{'}
	local args = frame:getParent().args
	
	if (subst or args['subst'] or '') ~= '' then outStr = outStr..'[[Help:Substitution|subst]]:' end
	
	local tempTitle = (args[1] or ' ')
	
	if mw.title.new(tempTitle) ~= nil then
		if (mw.title.new(tempTitle).nsText or '') == '' and (mw.title.new(tempTitle).interwiki or '') == '' then 
			tempLink=frame:callParserFunction{name='ns',args='Template'}..':'..tempTitle
		else
			tempLink = tempTitle
		end 
	else
		tempLink = tempTitle
	end

	outStr = outStr..'[[:'..(args['LANG'] or frame.args['LANG'] or '')..(args['SISTER'] or frame.args['SISTER'] or '')..tempLink..'|'..bold..tempTitle..bold..']]'
	
	local k, v
	
	for k, v in pairs(args) do
		if type(k) == 'number' and k ~= 1 then 
			if ital then outStr = outStr..'|'..frame:extensionTag{name='var',content=v} else outStr = outStr..'|'..v end
		elseif k ~= 1 and k ~= 'subst' and k ~= 'LANG' and k ~= 'SISTER' then
			if ital then outStr = outStr..'|'..frame:extensionTag{name='var',content=k..'='..v} else outStr = outStr..'|'..k..'='..v end
		end
	end
			
	outStr = outStr..frame:extensionTag{name='nowiki',content='}}'}
	
	return frame:extensionTag{name='code',content=outStr}
end

return p