模块:Shortcut/sandbox
跳转到导航
跳转到搜索
此模块的文档可以在Module:Shortcut/sandbox/doc创建
-- Load required modules
local checkType = require('libraryUtil').checkType
local yesno = require('Module:Yesno')
local parse = {}
parse.__index = parse
function parse.new(config)
local obj = {}
obj.data = mw.loadData(config.data or 'Module:Shortcut/config')
obj.styles = config.styles or 'Template:Shortcut/styles.css'
obj.frame = mw.getCurrentFrame()
obj.template = config.istemplate or false
setmetatable(obj, parse)
return obj
end
-- shortcut 的繁簡轉換
function parse.getshortname(self)
return self.frame:expandTemplate{
title = "Lan",
args = {
["zh-hans"] = "快捷方式",
["zh-hant"] = "捷徑"
}
}
end
function parse.message(self, msg, ...)
return mw.message.newRawMessage(msg, ...):plain()
end
-- 生成錯誤訊息
function parse.errormsg(self, msg)
return require('Module:Error').error{[1] = '[[Module:Shortcut]]錯誤:' .. msg}
end
-- 主要段
function parse.box(self, shortcuts, args, ismobox, istemplate, extcfg)
checkType('_main', 1, shortcuts, 'table')
checkType('_main', 2, args, 'table', true)
frame = mw.getCurrentFrame()
if args then else
return errormsg('args丟失')
end
local function cfg (msg)
local cfg = {}
-- 外部值
for k,v in ipairs(extcfg) do
cfg[k] = v
end
-- 外部值缺失時由本地值補(無外部值時也是調用本地值)
if cfg[msg] then else
for k,v in ipairs(localcfg) do
cfg[k] = v
end
end
return cfg[msg]
end
-- 測試捷徑是否有效
for i, shortcut in ipairs(shortcuts) do
if type(shortcut) ~= 'string' or #shortcut < 1 then
error(message(cfg['invalid-shortcut-error'], i), 2)
end
end
-- 生成捷徑列表
local listItems = {}
for i, shortcut in ipairs(shortcuts) do
if istemplate then
-- 模板專用
local subst = ''
if args.subst then
subst = '[[Help:替换引用|subst]]:'
end
listItems[i] = '{{'.. subst .. frame:expandTemplate{
title = 'Template_shortcut/link',
args = {shortcut}
}..'}}'
else
-- 一般
listItems[i] = frame:expandTemplate{
title = 'No redirect',
args = {shortcut}
}
end
end
table.insert(listItems, (args.msg or ''))
-- 測試捷徑、參數msg是否存在
if #listItems < 1 then
local msg = errormsg(cfg['no-content-error'])
if yesno(args.category,1) then
msg = msg .. makeCategoryLink(cfg['no-content-error-category'])
end
return msg
end
local root = mw.html.create()
root:wikitext(frame:extensionTag{ name = 'templatestyles', args = { src = 'Shortcut/styles.css'} })
-- Anchors
local anchorDiv = root:tag('div'):addClass('shortcut-anchordiv')
for i, shortcut in ipairs(shortcuts) do
local anchor = mw.uri.anchorEncode(shortcut)
if istemplate then
anchorDiv:tag('span'):attr('id', 'Template:' .. anchor)
else
anchorDiv:tag('span'):attr('id', anchor)
end
end
-- 捷徑頂部
local shortcutHeading
do
local nShortcuts = #shortcuts
if nShortcuts > 0 then
shortcutHeading = message(cfg['shortlink'], nShortcuts)
shortcutHeading = frame:preprocess(shortcutHeading)
end
end
--檢測是否是方針(參數policy)
if yesno(args.policy) then
shortcutHeading = cfg['shortlink_policy']
end
-- 生成邊框
local shortcutList = ''
if ismobox == 1 then
-- ombox
shortcutList = root
:tag('div')
:addClass('shortcutbox-ombox plainlist noprint')
:attr('role', 'note')
else
-- 一般
shortcutList = root
:tag('div')
:addClass('shortcutbox plainlist noprint')
:attr('role', 'note')
if args.float == 'left' then
-- 靠左
shortcutList:addClass('shortcutbox-left')
end
end
if shortcutHeading then
shortcutList
:tag('div')
:addClass('shortcutlist')
:wikitext(shortcutHeading)
end
local list = shortcutList:tag('ul')
for i, item in ipairs(listItems) do
list:tag('li'):wikitext(item)
end
return tostring(root)
end
local function compressArray(t)
local nums, ret = {}, {}
for k in pairs(t) do
nums[#nums + 1] = k
end
table.sort(nums)
for i, num in ipairs(nums) do
ret[i] = t[num]
end
return ret
end
function p.main(frame)
local args = frame:getParent().args
-- Separate shortcuts from options
local shortcuts, options = {}, {}
for k, v in pairs(args) do
if type(k) == 'number' then
shortcuts[k] = v
else
options[k] = v
end
end
ismobox = frame.args['isombox'] and 1 or 0
istemplate = frame.args['istemplate'] and 1 or 0
shortcuts = compressArray(shortcuts)
return p._main(shortcuts, options, ismobox, istemplate)
end
function p.shortcut(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Shortcut'
})
-- Separate shortcuts from options
local shortcuts, options = {}, {}
for k, v in pairs(args) do
if type(k) == 'number' then
shortcuts[k] = v
else
options[k] = v
end
end
options.isombox = 0
options.istemplate = 0
shortcuts = compressArray(shortcuts)
return p._main(shortcuts, options, 0, 0)
end
return p