模組:JavaScript banner
外观
local yesno = require('Module:Yesno')
local mList = require('Module:List')
local mTableTools = require('Module:TableTools')
local mMessageBox = require('Module:Message box')
local TrackingCategory = require('Module:TrackingCategory')
local p = {}
function p.main(frame)
local origArgs = frame:getParent().args
local args = {}
for k, v in pairs(origArgs) do
v = v:match('^%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
return p._main(frame, args)
end
function p._main(frame, args)
local _ = frame
local javascripts = mTableTools.compressSparseArray(args)
local box = p.renderBox(javascripts)
p.renderTrackingCategories(args, javascripts)
return box
end
-- if action=edit
function p.main2(frame)
local origArgs = frame:getParent().args
local args = {}
for k, v in pairs(origArgs) do
v = v:match('^%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
local javascripts = mTableTools.compressSparseArray(args)
return p.renderBox(javascripts)
end
--end
function p.clearExternal(pageName)
local unpack_pattern, clear_pattern, empty, default = '^[%:%s_]*[^%:]+%:*%:', '^[%:%s_]*', '', '0'
local clearedPageName = mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.format('%s', pageName), clear_pattern, empty), '[%s_]*%:[%s_]*', ':'), '[%:]+', ':')
local title = mw.title.new(clearedPageName)
local newName = (not (title or {}).isExternal) and (title or {fullText=clearedPageName}).fullText or (mw.ustring.gsub((title or {fullText=clearedPageName}).fullText, unpack_pattern, empty))
local result = newName
if (mw.title.new(newName) or mw.title.new(default)).isExternal then
repeat newName = mw.ustring.gsub(newName, clear_pattern, empty)
title = mw.title.new(newName)
newName = (not title.isExternal) and title.fullText or (mw.ustring.gsub(title.fullText, unpack_pattern, empty))
if mw.text.trim(mw.ustring.format('%s', newName)) == '' then break end
result = newName
until not (mw.title.new(newName) or mw.title.new(default)).isExternal
end
title = mw.title.new(mw.ustring.format('%s', result))
local ns = mw.site.namespaces[title.namespace].canonicalName
result = mw.site.namespaces[title.namespace].canonicalName .. ((ns=='')and''or':') .. title.text
return mw.ustring.gsub(result, '^[%s%:_]*','')
end
local function _get_js_title_obj(pagename)
local javascript = pagename
local count
javascript, count = mw.ustring.gsub(javascript, '^%s*%#+', '')
local is_need = count
local tail_text = ''
if (is_need or 0) > 0 then
local _is_need
tail_text, _is_need = mw.ustring.gsub(javascript, '^[^%#]+%#', '')
if (_is_need or 0) <= 0 then
tail_text = ''
else
javascript = mw.ustring.gsub(javascript, '%#[^%#]+$', '')
end
end
local js_title = mw.title.new(mw.ustring.format('%s', javascript))
local js_name = (not js_title.isExternal) and js_title.fullText or p.clearExternal(js_title.fullText)
local javascriptLink = '[[:' .. mw.ustring.gsub(js_title.fullText, '^[%s%:_]*','') .. '|' .. js_name .. ']]'
local maybeSandbox = mw.title.new(mw.ustring.gsub(js_title.fullText, '^[%s%:_]*','') .. '/sandbox')
local anthor_link = ''
if maybeSandbox.exists then
anthor_link = anthor_link .. (mw.text.trim(anthor_link) == '' and '' or '、') .. mw.ustring.format('[[:%s|沙盒]]', maybeSandbox.fullText)
end
if mw.text.trim(tail_text) ~= '' then
anthor_link = anthor_link .. (mw.text.trim(anthor_link) == '' and '' or '、') .. mw.ustring.format('[[:%s|說明]]', mw.ustring.gsub(tail_text, '^[%s%:_]*',''))
end
if mw.text.trim(anthor_link) ~= '' then
javascriptLink = javascriptLink .. mw.ustring.format('(%s)', anthor_link)
end
return {
javascriptLink = javascriptLink,
is_need = is_need,
js_title = js_title,
js_name = js_name
}
end
function p.renderBox(javascripts)
local boxArgs = {}
if #javascripts < 1 then
boxArgs.text = '<strong class="error">错误:未指定小工具</strong>'
else
local javascriptLinks = {}
local needJavascriptLinks = {}
local index
for i, javascript in ipairs(javascripts) do
local js_title_obj = _get_js_title_obj(javascript)
if (js_title_obj.is_need or 0) > 0 then index = #needJavascriptLinks + 1
else index = #javascriptLinks + 1 end
if (js_title_obj.is_need or 0) > 0 then needJavascriptLinks[index] = js_title_obj.javascriptLink
else javascriptLinks[index] = js_title_obj.javascriptLink end
end
local javascriptList = mList.makeList('bulleted', javascriptLinks)
local needJavascriptList = mList.makeList('bulleted', needJavascriptLinks)
boxArgs.text = ''
if #javascriptLinks > 0 then
boxArgs.text = boxArgs.text .. (mw.text.trim(boxArgs.text) == '' and '' or '\n\n') .. '本' ..
(mw.title.getCurrentTitle():inNamespaces(828,829) and '-{zh-hans:模块; zh-hant:模組;}-' or '模板') ..
'使用以下[[JavaScript]][[Wikipedia:小工具|小工具]]:\n' .. javascriptList
end
if #needJavascriptLinks > 0 then
boxArgs.text = boxArgs.text .. (mw.text.trim(boxArgs.text) == '' and '' or '\n\n') .. '使用本' ..
(mw.title.getCurrentTitle():inNamespaces(828,829) and '-{zh-hans:模块; zh-hant:模組;}-' or '模板') ..
'需安裝以下[[JavaScript]][[Wikipedia:小工具|小工具]]:\n' .. needJavascriptList
end
end
boxArgs.type = 'notice'
boxArgs.small = true
boxArgs.image = '[[File:Font Awesome 5 brands js-square.svg|class=skin-invert|32px|alt=JavaScript|link=]]'
return mMessageBox.main('mbox', boxArgs)
end
function p.renderTrackingCategories(args, javascripts, titleObj)
if yesno(args.nocat) then
return ''
end
local cats = {}
titleObj = titleObj or mw.title.getCurrentTitle()
-- 错误分类
if #javascripts < 1 then
cats[#cats + 1] = '使用JavaScript的出錯' .. (titleObj:inNamespaces(828,829) and '模組' or '模板')
end
-- javascripts category
local subpageBlacklist = {
doc = true,
sandbox = true,
sandbox2 = true,
testcases = true
}
if (titleObj.namespace == 10 or titleObj.namespace == 828)
and not subpageBlacklist[titleObj.subpageText]
then
cats[#cats + 1] = '使用JavaScript的' .. (titleObj:inNamespaces(828,829) and '模組' or '模板')
local category = args.category
if not category then
local categories = {
['Module:String'] = '使用Module:String的' .. (titleObj:inNamespaces(828,829) and '模組' or '模板'),
}
categories['Module:Citation/CS1'] = categories['Module:Citation']
category = javascripts[1] and categories[javascripts[1]]
end
if category then
cats[#cats + 1] = category
end
local protLevels = {
autoconfirmed = 1,
extendedconfirmed = 2,
templateeditor = 3,
sysop = 4
}
local currentProt = titleObj.protectionLevels["edit"][1]
if currentProt == nil then currentProt = 0 else currentProt = protLevels[currentProt] end
for i, javascript in ipairs(javascripts) do
local js_title_obj = _get_js_title_obj(javascript)
if mw.title.new('MediaWiki:Common.js').namespace ~= js_title_obj.js_title.namespace then
cats[#cats + 1] = '使用外部JavaScript的' .. (titleObj:inNamespaces(828,829) and '模組' or '模板')
end
if (js_title_obj.js_title or {}).protectionLevels then
if js_title_obj.js_title.protectionLevels["edit"] then
local javascriptProt = js_title_obj.js_title.protectionLevels["edit"][1]
if javascriptProt == nil then javascriptProt = 0 else javascriptProt = protLevels[javascriptProt] end
if javascriptProt < currentProt then
if mw.title.new('MediaWiki:Common.js').namespace ~= js_title_obj.js_title.namespace then
cats[#cats + 1] = "使用JavaScript的受保護" .. (titleObj:inNamespaces(828,829) and "模組" or "模板")
end
break
end
end
end
end
end
local frame = mw.getCurrentFrame()
for i, cat in ipairs(cats) do
TrackingCategory.append(frame, cat)
end
end
return p