Module:Keys: Difference between revisions

From Miranda NG
Jump to navigation Jump to search
(to produce keys sequences)
 
(new version: language support)
 
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
local p = {}
local p = {}
p.keys = function(f)
local args = f
function p.main(frame)
if f == mw.getCurrentFrame() then
local args = getArgs(frame)
args = f:getParent().args
return p._main(args)
end
end
function p._main(args)
local langCode = args.lang:lower()
local keys = {}
local keys = {}
for _, key in ipairs(args) do
for _, key in ipairs(args) do
key = mw.text.trim(key)
table.insert(keys, p.key(key, langCode))
table.insert(keys, p.key(key))
end
end
return table.concat(keys, '+')
return table.concat(keys, '+')
end
end
p.key = function(key)
 
function p.key(key, langCode)
if not key then
if not key then
return ''
return ''
Line 20: Line 24:
local symbols = mw.loadData('Module:Keys/Symbols')
local symbols = mw.loadData('Module:Keys/Symbols')
local symbol = (symbols[key:lower()] or key)
if type(symbol) == 'table' then
if symbol[langCode] then
symbol = symbol[langCode]
else
symbol = symbol['default']
end
end
return '<kbd class="key nowrap" style="' .. table.concat({
return '<kbd class="key nowrap" style="' .. table.concat({
'border: 1px solid #AAA',
'border: 1px solid #AAA',
Line 29: Line 44:
'font-family: inherit',
'font-family: inherit',
'font-size: 85%'
'font-size: 85%'
}, ';') .. '">' .. (symbols[key:lower()] or key) .. '</kbd>'
}, ';') .. '">' .. symbol .. '</kbd>'
end
end
return p
return p

Latest revision as of 01:12, 2 December 2015

Module documentation[view] [edit] [history] [purge]
Todo: Add ability to wrap line

This module implements {{Hotkey}}. Please see the template page for documentation.

local getArgs = require('Module:Arguments').getArgs
local p = {}
 
function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end
 
function p._main(args)
	local langCode = args.lang:lower()
	local keys = {}
	
	for _, key in ipairs(args) do
		table.insert(keys, p.key(key, langCode))
	end
	
	return table.concat(keys, '+')
end

function p.key(key, langCode)
	if not key then
		return ''
	end
	
	local symbols = mw.loadData('Module:Keys/Symbols')
	local symbol = (symbols[key:lower()] or key)
		
	if type(symbol) == 'table' then
		if symbol[langCode] then
			symbol = symbol[langCode]
		else
			symbol = symbol['default']
		end
	end
	
		
	return '<kbd class="key nowrap" style="' .. table.concat({
		'border: 1px solid #AAA',
		'background-color: #F9F9F9',
		'background-image: -webkit-linear-gradient(#EEE, #F9F9F9, #EEE)',
		'background-image: -o-linear-gradient(#EEE, #F9F9F9, #EEE)',
		'background-image: linear-gradient(#EEE, #F9F9F9, #EEE)',
		'padding: 0.1em 0.3em',
		'font-family: inherit',
		'font-size: 85%'
	}, ';') .. '">' .. symbol .. '</kbd>'
end
 
return p