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 17:23, 4 May 2021 (fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local pv = mw.loadData('Module:Preview warning/is preview')
--[[
is_preview

This function returns a boolean indicating whether the page is a preview.

]]
local function is_preview(frame)
	local revision_id = frame:preprocess('{{REVISIONID}}')
	-- {{REVISIONID}} is usually the empty string when previewed.
	-- I don't know why we're checking for nil but hey, maybe someday things
	-- would have broken
	return revision_id == nil or revision_id == ''
end

--[[
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 pv.preview then
		return frame.args[1] or ''
	else
		return frame.args[2] or ''
	end
end

--[[
pmain

This function returns the 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

--[[
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)
	if not pv.preview then return '' end
	
	local warning = frame.args[1]:match('^%s*(.-)%s*$') or ''
	if warning == '' then
		warning = 'The template has no warning text. Please add a warning.'
	end
	
	return mw.ustring.format(
		'%s<div class="preview-warning"><strong>Preview warning:</strong> %s</div>',
		frame:extensionTag{
			name = 'templatestyles', args = { src = 'Module:Preview warning/styles.css' }
		},
		warning
	)
end

return p