跳转到内容

模組:Shortcut/sandbox

本页使用了标题或全文手工转换
维基百科,自由的百科全书

这是本页的一个历史版本,由SunAfterRain留言 | 贡献2020年2月21日 (五) 04:26编辑。这可能和当前版本存在着巨大的差异。

-- Load required modules
local checkType = require('libraryUtil').checkType
local yesno = require('Module:Yesno')

local p = {}

-- shortcut 的繁簡轉換
function p._getshortname(frame)
	return frame:expandTemplate{title = "Lan",args = {["zh-hans"]="快捷方式",["zh-hant"]="捷徑"}}
end

-- 訊息列表
local function localcfg (msg)
	local frame = mw.getCurrentFrame()
	local msglist = {
		["shortlink"] = "[[Wikipedia:捷徑|" .. p._getshortname(frame) .. "]]",
		["shortlink_policy"] = "[[Wikipedia:捷徑|方針" .. p._getshortname(frame) .. "]]",
		['invalid-shortcut-error'] = '捷徑「#$1」無效(捷徑必須為字串且長度至少為1個字元)[[Category:需要修正維基百科捷徑框參數的頁面|{{PAGENAME}}]]',
		['no-content-error'] = '錯誤:沒有指定捷徑名稱並且參數<code>msg</code>沒有定義。[[Category:需要修正維基百科捷徑框參數的頁面|{{PAGENAME}}]]'
	}
	return msglist[msg]
end

local function message(msg, ...)
	return mw.message.newRawMessage(msg, ...):plain()
end

-- 生成錯誤訊息
local function errormsg(mag)
	return require('Module:Error').error{[1] = '[[Module:Shortcut]]錯誤:' .. msg}
end

-- 主要段
function p._main(shortcuts, args, 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 args.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 args.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 args.isombox == 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
	
	options.isombox = frame.args['isombox'] and 1 or 0
	options.istemplate = frame.args['istemplate'] and 1 or 0
	
	shortcuts = compressArray(shortcuts)

	return p._main(shortcuts, options)
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)
end

return p