Module:If preview/sandbox: Difference between revisions

Content deleted Content added
change that to if_preview
Undid revision 1246785899 by Awesome Aasim (talk)
 
(60 intermediate revisions by 3 users not shown)
Line 1:
local p = {}
local getArgs = require("Module:Arguments").getArgs
local yn = require("Module:Yesno")
local cfg = mw.loadData('Module:If preview/configuration')
p.pvwc = p.pvwc or package.loaded["Module:If preview/preview warning counter"]
or require("Module:If preview/preview warning counter")
 
--[[
main
is_preview
 
This function returns aeither booleanthe indicatingfirst whetherargument theor pagesecond isargument apassed preview.to
this module, depending on whether the page is being previewed.
 
]]
local function is_previewp.main(frame)
local args = getArgs(frame)
local revision_id = mw.getCurrentFrame():preprocess('{{REVISIONID}}')
if is_preview()cfg.preview then
-- {{REVISIONID}} is usually the empty string when previewed.
return frame.args[1] or ''
return not revision_id == nil and not revision_id == ''
else
return frame.args[2] or ''
end
end
 
--[[
pmain
main
 
This function returns either the first argument or second argument passed to
this module,'s dependingparent on(i.e. whethertemplate theusing pagethis ismodule), beingdepending previewed.on whether it
is being previewed.
 
Usage:
{{#invoke:If preview|main|value_if_preview|value_if_not_preview}}
 
]]
function p.if_previewpmain(frame)
return p.main(frame:getParent())
if is_preview() then
end
return frame.args[1] or ''
 
else
local function warning_text(warning)
return frame.args[2] or ''
return mw.ustring.format(
cfg.warning_infrastructure,
cfg.templatestyles,
warning
)
end
 
function p._warning(args)
local warning = frame.args[1] and args[1]:match('^%s*(.-)%s*$') or ''
if warning == '' then
return warning_text(cfg.missing_warning)
end
if not is_preview()cfg.preview then return '' end
if yn(args['consolewarning']) then mw.addWarning('[' .. p.pvwc.incr() .. '] ' .. (args[1] or cfg.missing_warning)) end
return warning_text(warning)
end
 
--[[
warning
preview_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.
 
]]
Usage:
-- function p.warning(frame)
{{#invoke:If preview|warning|preview_warning_text}}
-- mw.addWarning(frame.args[1] or cfg.missing_warning)
-- return p._warning(frame.args)
-- end
 
--[[
warning, but for pass-through templates like {{preview warning}}
]]
function p.warningpwarning(frame)
local args = getArgs(frame)
if not is_preview() then return '' end
return p._warning(args)
end
local warning = frame.args[1]:match('^%s*(.-)%s*$') or ''
 
if warning == '' then
--[[
warning = 'Something is wrong with this template'
Does both mw.addWarning and preview warning
end
]]
 
return mw.ustring.format(
function p.warn(text)
'%s<div class="preview-warning"><strong>Preview warning:</strong> %s</div>',
if text == nil or text == "" then return "" end
frame:extensionTag{
mw.addWarning('[' .. p.pvwc.incr() .. '] ' .. text)
name = 'templatestyles', args = { src = 'Module:Preview warning/styles.css' }
return p._warning({text})
},
end
warning
 
)
--[[
Console warning
]]
function p.consoleWarning(frame)
local args = getArgs(frame)
mw.addWarning('[' .. p.pvwc.incr() .. '] ' .. (args[1] or cfg.missing_warning))
return ''
end