模組:EditState
外观
![]() | 此模块被引用於約705,000個頁面。 為了避免造成大規模的影響,所有對此模块的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模块中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
本模組提供一些能偵測頁面被編輯之狀況的函數。
函數說明
isPreview
用來偵測頁面是否處於預覽狀態。
previewNotice
提供在頁面預覽狀態添加警告以提醒編者的功能。
isCreating
用來偵測頁面是否處於「建立中」的狀態。
hasParent
檢查模板呼叫是否存在外層。
getTitle
獲得當下模板呼叫的標題。
nextSubst
讓替換引用延遲運行的函數。
local p={}
local lib_arg={};
local yesno = {}
function p.isPreview()
local frame = mw.getCurrentFrame()
local check_str = '{{REVISIONID}}'
if mw.isSubsting() then check_str = "{{safesubst:REVISIONID}}" end
local Preview_mode = frame:preprocess(check_str); -- use magic word to get revision id
return (Preview_mode == nil or mw.text.trim(Preview_mode or '') == '') -- if there is a value then this is not a preiview
end
function p.previewNotice(frame)
if mw.isSubsting() then return '' end
local text = ''
if type(frame) == type('string') then --type('string') avoid version change
text = mw.text.trim(frame)
else
if type((frame or {}).args) ~= type({}) then frame.args = {} end
text = mw.text.trim(frame.args['1'] or frame.args[1] or frame.args.text or
frame['1'] or frame[1] or frame.text or '')
end
text = mw.text.trim(text)
if text ~= '' then
mw.addWarning(text)
end
return ''
end
function p.isCreating()
local frame = mw.getCurrentFrame()
local check_str = '{{PAGEID}}'
if mw.isSubsting() then check_str = "{{safesubst:PAGEID}}" end
local Preview_mode = frame:preprocess(check_str);
return (Preview_mode == nil or mw.text.trim(Preview_mode or '') == '')
end
function p.hasParent(layer)
local frame = mw.getCurrentFrame()
local times = tonumber(layer or '') or 0
local Parent = frame:getParent();
for i=2,tonumber(times) do
Parent = Parent:getParent()
if Parent == nil then break end
end
return not (Parent == nil or Parent == '')
end
function p.getTitle(layer)
local frame = mw.getCurrentFrame()
local times = tonumber(layer or '') or 0
if times <= 0 then return frame:getTitle()end
local Parent = frame:getParent();
for i=2,tonumber(times) do
Parent = Parent:getParent()
if Parent == nil then return '' end
end
return Parent:getTitle();
end
function p.nextSubst(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {
parentFirst=true,
trim = false,
removeBlanks = false
})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local input_data = args[1] or args['1']
if mw.text.trim(input_data or '') == '' then return '' end
local num_args = {}
for key,value in pairs(args) do
local num = tonumber(key) or 0
local stored = false
if num then
if num > 1 then
num_args[key - 1] = value
stored = true
end
if num == 1 then stored = true end
end
if stored ~= true then num_args[key] = value end
end
local str_args = mw.ustring.sub(require('Module:Template invocation').invocation(input_data, num_args),3,-3)
if(mw.isSubsting())then
return "{{subst:" .. str_args .. "}}" ;
end
return working_frame:preprocess("{{safesubst:" .. str_args .. "}}")
end
function p.passNArgs(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {parentFirst=true})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local template_name = args.template_name or 'void'
local skip_args = tonumber(args.skip) or 1
local numberic_args = {}
local text_args = {}
local min_args, max_args = tonumber("inf"), tonumber("-inf")
for key, value in pairs(args) do
local i = tonumber(key)
local check = mw.text.trim(key):lower()
local arg_value = mw.ustring.gsub(value, "[=%|%{%}]", function(esc_str)
local mapping = {} mapping['|']='!' mapping['=']='=' mapping['{']='(' mapping['}']=')'
if mapping[esc_str] then
return "{{" .. mapping[esc_str] .. "}}"
end
return esc_str
end)
if i ~= nil and check~='inf' and check~='-inf' and check~='nan' and check~='-nan' then
numberic_args[i] = arg_value
if i > max_args then max_args = i end
if i < min_args then min_args = i end
else
text_args[key] = arg_value
end
end
local body = "{{" .. template_name
local continuous = true
for i = min_args, max_args do
if numberic_args[i] ~= nil then
if i <= 0 or i > skip_args then
local value = numberic_args[i]
body = body .. '|'
if i <= 0 or not continuous then
body = body .. (i - ((i > 0) and skip_args or 0)) .. '='
end
body = body .. value
end
else
if i > skip_args then continuous = false end
end
end
for key, value in pairs(text_args) do
body = body .. '|' .. key .. '=' .. value
end
body = body .. '}}'
return working_frame:preprocess(body)
end
return p