Jump to content

Module:If preview/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 21:01, 4 May 2021 (and that also does not exist). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local cfg = mw.loadData('Module:If preview/configuration')

--[[
main

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

]]
function p.main(frame)
	if cfg.preview 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.

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


local function warning_text(warning)
	return mw.ustring.format(
		cfg.warning_infrastructure,
		cfg.templatestyles,
		warning
	)
end

--[[
warning

This function returns a "preview warning", which is the first argument marked
up with HTML and some supporting text, depending on whether the page is being previewed.

]]
function p.warning(frame)
	
	local warning = frame.args[1] and frame.args[1]:match('^%s*(.-)%s*$') or ''
	if warning == '' then
		return warning_text(frame, cfg.missing_warning)
	end
	
	if not cfg.preview then return '' end
	
	return warning_text(warning)
end

return p