跳转到内容

模組:Shortcut/sandbox

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

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

-- This module implements {{shortcut}}.

-- Set constants
local CONFIG_MODULE = 'Module:Shortcut/config'

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

local p = {}


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

function p.getshortlink(frame)
	return "[[Wikipedia:捷徑|" .. p._getshortname(frame) .. "]]"
end

function p.getshortlink_policy(frame)
	return "[[Wikipedia:捷徑|方針" .. p._getshortname(frame) .. "]]"
end

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

local function makeCategoryLink(cat)
	return string.format('[[Category:%s]]', cat)
end

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

function p._main(shortcuts, args, frame, cfg)
	checkType('_main', 1, shortcuts, 'table')
	checkType('_main', 2, args, 'table', true)
	frame = frame or mw.getCurrentFrame()
	args = args or frame.args
	cfg = cfg or mw.loadData(CONFIG_MODULE)

	-- Validate shortcuts
	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

	-- Make the list items. These are the shortcuts plus any extra lines such
	-- as args.msg.
	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)

	-- Return an error if we have nothing to display
	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

	-- Shortcut heading
	local shortcutHeading
	do
		local nShortcuts = #shortcuts
		if nShortcuts > 0 then
			shortcutHeading = message(p.getshortlink(frame), nShortcuts)
			shortcutHeading = frame:preprocess(shortcutHeading)
		end
	end
	if yesno(args.policy) then
	    shortcutHeading = p.getshortlink_policy(frame)
	end

	-- Shortcut box
	local shortcutList = ''
	if args.isombox == 1 then
		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 = 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, frame)
end

function p.ombox(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 = 1
	options.istemplate = 0
	
	shortcuts = compressArray(shortcuts)

	return p._main(shortcuts, options, frame)
end

function p.template(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 = 0
	options.istemplate = 1
	
	shortcuts = compressArray(shortcuts)

	return p._main(shortcuts, options, frame)
end

function p.ombox_template(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 = 0
	options.istemplate = 1

	return p._main(shortcuts, options, frame)
end

return p