模組:DYKCount
外观
local p = {}
local lib_var = {}
local lib_arg = {}
local yesno = require('Module:Yesno')
local mError = require('Module:Error').error
local fullurl = require('Module:Fullurl')._fullurl
local function plainlink (url, text)
return mw.html.create():tag('span'):addClass('plainlinks'):wikitext('[' .. url .. ' ' .. text .. ']')
end
function p.find_dyk_edit_by_DYKEntry(frame)
local pattern = "{{%s*DYKEntry%s*|.-timestamp%s*="
if lib_var._arg_process == nil then lib_var = require('Module:Var') end
local args, working_frame = lib_var._arg_process(frame)
local title = args[1] or args['1']
local offset = tonumber(args[2] or args['2'] or '0') or 0
title = mw.title.new( args[1], '')
if title then
local wikitext = title:getContent()
wikitext = mw.text.decode(wikitext)
local _,count = mw.ustring.gsub(wikitext, pattern, '')
local h_id = count + 1 + offset
return title:fullUrl( {action='edit',section=h_id}, 'relative' )
end
return ''
end
function count_dyk_section(frame, title, offset)
local pattern = "\127'\"`UNIQ%-%-h%-(%d+)%-%-QINU`\"'\127" -- <h></h>或章節標題的strip marker是十進位 (截至2020年3月4日)
offset = tonumber(offset or 0) or 0
title = mw.title.new(title, '')
if title then
local wikitext = title:getContent()
wikitext = mw.text.decode(wikitext)
wikitext = mw.ustring.gsub(wikitext, '[%<%[%{%}%]%>%|%a%d]', '*') --有人奇特簽名會出錯,因此轉義掉;另{}不轉義掉會模板循環。
wikitext = mw.ustring.gsub(wikitext, '%*+', '%%') --清理重複轉義字元以減少解析器負擔
--mw.log(wikitext)
local avoid_r = frame:newChild{ title = title.fullText, args = {} }
local checker = avoid_r:preprocess( "== ~ ==\n\n" .. wikitext ) --前方字串是基底,移除會導致運算出問題
local max_id, min_id = 0, -1
--請勿改成計算評選數,因為不能排除評選中間出現子章節
mw.ustring.gsub(checker, pattern, function(h_id)
local h_idx = tonumber(h_id) -- <h></h>或章節標題的strip marker是十進位 (截至2020年3月4日,如未來有更動請修改或新增第二參數)
if h_idx then
if h_idx > max_id then max_id = h_idx end
if min_id < 0 or h_idx < max_id then min_id = h_idx end
end
end)
if (max_id + offset - min_id - 1) < 0 then
return max_id
end
return max_id + offset - min_id - 1
end
return ''
end
function p.find_dyk_edit_by_hN(frame)
local pattern = "\127'\"`UNIQ%-%-h%-(%d+)%-%-QINU`\"'\127" -- <h></h>或章節標題的strip marker是十進位 (截至2020年3月4日)
if lib_var._arg_process == nil then lib_var = require('Module:Var') end
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)
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 title = args[1] or args['1']
local offset = tonumber(args[2] or args['2'] or '0') or 0
title = mw.title.new( args[1], '')
if title then
local wikitext = title:getContent()
wikitext = mw.text.decode(wikitext)
wikitext = mw.ustring.gsub(wikitext, '[%<%[%{%}%]%>%|%a%d]', '*') --有人奇特簽名會出錯,因此轉義掉;另{}不轉義掉會模板循環。
wikitext = mw.ustring.gsub(wikitext, '%*+', '%%') --清理重複轉義字元以減少解析器負擔
--mw.log(wikitext)
local avoid_r = working_frame:newChild{ title = title.fullText, args = {} }
local checker = avoid_r:preprocess( "== ~ ==\n\n" .. wikitext ) --前方字串是基底,移除會導致運算出問題
local max_id, min_id = 0, -1
--請勿改成計算評選數,因為不能排除評選中間出現子章節
mw.ustring.gsub(checker, pattern, function(h_id)
local h_idx = tonumber(h_id) -- <h></h>或章節標題的strip marker是十進位 (截至2020年3月4日,如未來有更動請修改或新增第二參數)
if h_idx then
if h_idx > max_id then max_id = h_idx end
if min_id < 0 or h_idx < max_id then min_id = h_idx end
end
end)
return title:fullUrl( {action='edit',section=(max_id + offset - min_id - 1)}, 'relative' ) .. '&summary=提交新的新条目推荐评选提名'
end
return ''
end
function p.getDYKCount(frame)
if lib_var._arg_process == nil then lib_var = require('Module:Var') end
local args, working_frame = lib_var._arg_process(frame)
local input_n = mw.text.trim(args[1] or args['1'] or '')
local src = working_frame:preprocess{ text = "{{msgnw:" .. input_n .. "}}" }
local st,ed = mw.ustring.find(src, "{{%s-[Pp]roduceEncouragement%s-|%s-count%s-=%s-%d+%s-}}" );
if st then
local dyk_template = mw.ustring.sub(src, st,ed)
st,ed = mw.ustring.find(dyk_template, "count%s-=%s-%d+" )
dyk_template = mw.text.decode(mw.ustring.sub(dyk_template, st,ed))
st,ed = mw.ustring.find(dyk_template, "%d+" )
local dyk_count = mw.ustring.sub(dyk_template, st,ed)
return dyk_count;
else
return 0;
end
end
local function button(frame, args)
local text = args[1] or args['1'] or ''
local section = args[2] or args['2'] or 0
if text == '' then
text = '提交新评选提名'
end
local getlink = yesno(args['link'] or 0)
local onlyurl = yesno(args['only'] or 0)
local buttonclass = "mw-ui-button " .. (args['class'] or "mw-ui-progressive mw-ui-big")
local url_arg = {
title = 'Wikipedia:新条目推荐/候选',
action = 'edit',
section = count_dyk_section(frame, 'Wikipedia:新条目推荐/候选', section),
summary = '提交新的新条目推荐评选提名',
onlyurl = 1
}
local url = fullurl(url_arg)
local plainurl = plainlink( url , mw.text.tag('span',{ class = buttonclass } , text ) )
local wt
if getlink then
return plainlink( url , text )
end
if onlyurl then
wt = 'https:' .. url
return wt
end
wt = mw.text.tag('div',{ style = "text-align: center;"}, tostring(plainurl))
return wt
end
function p.button (frame)
if lib_var._arg_process == nil then lib_var = require('Module:Var') end
local args, working_frame = lib_var._arg_process(frame)
return button(frame, args)
end
return p