Module:If preview/sandbox

This is an old revision of this page, as edited by Izno (talk | contribs) at 14:52, 29 April 2021 (see if I broke something). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

--[[
main

This function returns the either the first argument or second argument passed to this module, depending on whether it is being previewed.

Usage:
{{#invoke:If preview|main|value_if_preview|value_if_not_preview}}

]]
function p.main(frame)
	if p.boolean(frame) then
		return frame.args[1] or ''
	else
		return frame.args[2] or ''
	end
end

--[[
pmain

This function returns either the first argument or second argument passed to
this module's parent (i.e. template using this module), depending on whether it
is being previewed.

Usage:
{{#invoke:If preview|pmain}}

]]

function p.pmain(frame)
	return p.main(frame:getParent())
end

--[[
boolean

This function returns true when it is a preview and false otherwise

Usage:
{{#invoke:If preview|boolean}}

]]

function p.boolean(frame)
	local preview_mode = frame:preprocess('{{REVISIONID}}')
	return not preview_mode == nil and not preview_mode == ''
end
 
return p