模組:Shortcut/sandbox

本页使用了标题或全文手工转换
维基百科,自由的百科全书
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

本模組可以產出一個文字方塊,顯示可連結至某個頁面的捷徑。

用法

用於Wiki標記語法

如果要從Wiki標記語法引入,本模板應當從某個模板進行呼叫,通常使用的模板為{{shortcut}}。請參考該模板的文件說明。然而,您也可以用Wiki標記語法進行呼叫:{{#invoke:shortcut|main|捷徑}}

用於Lua

如果要從Lua使用本模組,首先請讀取本模板。

local mShortcut = require('Module:Shortcut')

然後您可以依據下列語法建立捷徑方塊:

mShortcut._main(shortcuts, options, frame, cfg)
  • shortcuts為捷徑頁的名稱陣列(必填)。
  • options為表格偏好設定,支援下列參數:
    • msg - 顯示捷徑列表之後的訊息。
    • category - 如果設為no,則隱藏分類,不建議使用。(調用Module:Yesno
  • frame a frame object. This is optional, and only intended to be used internally.
  • cfg - 表格格式測試,正常情況下請不要使用,該參數應僅供測試

技術細節

本模組有一份組態檔案Module:Shortcut/config。該檔案可將本模板翻譯成不同的語言,或是改變分類名稱的細節。

-- 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] = '&#123;&#123;'.. subst .. frame:expandTemplate{
				title = 'Template_shortcut/link',
				args = {shortcut}
			}..'&#125;&#125;'
		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