Module:Key and Module:Key/sandbox: Difference between pages

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
imported>The wub
convert to use TemplateStyles - Template:Key press/styles.css
 
imported>Juwan
adapted from wikt:Module:key press
 
Line 1: Line 1:
-- This module implements {{key press}}.
local p = {}


local kbdPrefix = '<kbd class="keyboard-key nowrap">'
local key_labels = {
 
-- Control keys
local kbdSuffix = '</kbd>'
['backspace'] = '← Backspace',
 
local keyText = {
['caps lock'] = '⇪ Caps Lock',
['caps lock'] = '⇪ Caps Lock',
['[[caps lock]]'] = '⇪ [[Caps Lock]]',
['shift'] = '⇧ Shift',
['[[shift key|shift]]'] = '⇧ [[Shift key|Shift]]',
['enter'] = '↵ Enter',
['[[enter key|enter]]'] = '↵ [[Enter key|Enter]]',
['cmd'] = '⌘ Cmd',
['cmd'] = '⌘ Cmd',
['[[command key|cmd]]'] = '⌘ [[Command key|Cmd]]',
['command'] = '⌘ Command',
['command'] = '⌘ Command',
['[[command key|command]]'] = '[[Command key|Command]]',
['enter'] = '↵ Enter',
['hyper'] = '✦ Hyper',
['menu'] = '≣ Menu',
['meta'] = '◆ Meta',
['opt'] = '⌥ Opt',
['opt'] = '⌥ Opt',
['[[option key|opt]]'] = '⌥ [[Option key|Opt]]',
['option'] = '⌥ Option',
['option'] = '⌥ Option',
['[[option key|option]]'] = '⌥ [[Option key|Option]]',
['shift'] = '⇧ Shift',
['tab'] = 'Tab ↹',
['tab'] = 'Tab ↹',
['[[tab key|tab]]'] = '[[Tab key|Tab]] ',
['super'] = '❖ Super',
['backspace'] = '← Backspace',
['win'] = '⊞ Win',
 
['[[alt]]'] = '[[Alt key|Alt]]',
['[[altgr]]'] = '[[AltGr key|AltGr]]',
['[[backspace]]'] = '← [[Backspace]]',
['[[backspace]]'] = '← [[Backspace]]',
['win'] = '⊞ Win',
['[[caps lock]]'] = '⇪ [[Caps Lock]]',
['[[windows key|win]]'] = '[[Windows key|Win]]',
['[[cmd]]'] = '⌘ [[Command key|Cmd]]',
['menu'] = '≣ Menu',
['[[command]]'] = '⌘ [[Command key|Command]]',
['[[menu key|menu]]'] = '≣ [[Menu key|Menu]]',
['[[delete]]'] = '[[Delete key|Delete]]',
['[[end]]'] = '[[End key|End]]',
['[[enter]]'] = '↵ [[Enter key|Enter]]',
['[[esc]]'] = '[[Esc key|Escape]]',
['[[escape]]'] = '[[Esc key|Esc]]',
['[[fn]]'] = '[[Fn key|Fn]]',
['[[home]]'] = '[[Home key|Home]]',
['[[hyper]]'] = '✦ [[Hyper key|Hyper]]',
['[[insert]]'] = '[[Insert key|Insert]]',
['[[menu]]'] = '≣ [[Menu key|Menu]]',
['[[meta]]'] = '◆ [[Meta key|Meta]]',
['[[opt]]'] = '⌥ [[Option key|Opt]]',
['[[option]]'] = '⌥ [[Option key|Option]]',
['[[page down]]'] = '[[Page Up and Page Down keys|Page Down]]',
['[[page up]]'] = '[[Page Up and Page Down keys|Page Up]]',
['[[pgdn]]'] = '[[Page Up and Page Down keys|PgDn]]',
['[[pgup]]'] = '[[Page Up and Page Down keys|PgUp]]',
['[[print screen]]'] = '[[Print Screen]]',
['[[prt sc]]'] = '[[Print Screen|Prt Sc]]',
['[[return]]'] = '↵ [[Enter key|Return]]',
['[[scrlk]]'] = '[[Scroll Lock|ScrLk]]',
['[[scroll lock]]'] = '[[Scroll Lock]]',
['[[shift]]'] = '⇧ [[Shift key|Shift]]',
['[[super]]'] = '❖ [[Super key (keyboard button)|Super]]',
['[[tab]]'] = '[[Tab key|Tab]] ↹',
['[[win]]'] = '⊞ [[Windows key|Win]]',
['[[windows]]'] = '⊞ [[Windows key|Windows]]',
-- Arrow keys
['up'] = '↑',
['up'] = '↑',
['[[arrow keys|up]]'] = '[[Arrow keys|↑]]',
['down'] = '↓',
['down'] = '↓',
['[[arrow keys|down]]'] = '[[Arrow keys|↓]]',
['left'] = '←',
['left'] = '←',
['[[arrow keys|left]]'] = '[[Arrow keys|←]]',
['right'] = '→',
['right'] = '→',
['[[arrow keys|right]]'] = '[[Arrow keys|→]]',
 
['[[up]]'] = '[[Arrow key|↑]]',
['[[down]]'] = '[[Arrow key|↓]]',
['[[left]]'] = '[[Arrow key|←]]',
['[[right]]'] = '[[Arrow key|→]]',
 
-- Characters
['asterisk'] = '&#42;',
['asterisk'] = '&#42;',
['hash'] = '&#35;',
['hash'] = '&#35;',
['[[#]]'] = '[[Number sign|#]]',
['colon'] = '&#58;',
['colon'] = '&#58;',
['[[:]]'] = '[[Colon (punctuation)|:]]',
['pipe'] = '&#124;',
['pipe'] = '&#124;',
['[[|]]'] = '[[Pipe symbol|&#124;]]',
['semicolon'] = '&#59;',
['semicolon'] = '&#59;',
['[[;]]'] = '[[Semi-colon|&#59;]]',
['equals'] = '&#61;',
['equals'] = '&#61;',


Line 66: Line 90:
['r sw'] = 'R↙',
['r sw'] = 'R↙',


-- PlayStation.
-- PlayStation keys
['ex'] = '×',
['ex'] = '×',
['circle'] = '○',
['circle'] = '○',
Line 72: Line 96:
['triangle'] = '△',
['triangle'] = '△',


-- Nintendo 64 and GameCube.
-- Nintendo 64 and GameCube keys
['c up'] = 'C↑',
['c up'] = 'C↑',
['c down'] = 'C↓',
['c down'] = 'C↓',
Line 83: Line 107:
}
}


local keyAlias = {
local label_aliases = {
-- ['alternate name for key (alias)'] = 'name for key used in key table'
['[[cmd key|cmd]]'] = '[[command key|cmd]]',
['[[cmd key|command]]'] = '[[command key|command]]',
['[[opt key|opt]]'] = '[[option key|opt]]',
['[[option key]]'] = '[[option key|option]]',
['[[opt key|option]]'] = '[[option key|option]]',
['[[win key|win]]'] = '[[windows key|win]]',
['*'] = 'asterisk',
['*'] = 'asterisk',
['#'] = 'hash',
['#'] = 'hash',
[':'] = 'colon',
[':'] = 'colon',
[';'] = 'semicolon',
[';'] = 'semicolon',
['l-up'] = 'l up',
['l-up'] = 'l up',
['l-down'] = 'l down',
['l-down'] = 'l down',
Line 111: Line 129:
['r-nw'] = 'r nw',
['r-nw'] = 'r nw',
['r-sw'] = 'r sw',
['r-sw'] = 'r sw',
['ps x'] = 'ex',
['ps x'] = 'ex',
['ps c'] = 'circle',
['ps c'] = 'circle',
['ps s'] = 'square',
['ps s'] = 'square',
['ps t'] = 'triangle',
['ps t'] = 'triangle',
['c-up'] = 'c up',
['c-up'] = 'c up',
['c-down'] = 'c down',
['c-down'] = 'c down',
Line 125: Line 145:
}
}


local Collection = {}
local function format_key_press(args)
Collection.__index = Collection
local result = {}
do
local chain = args.chain or ' +&nbsp;'
function Collection:add(item)
if item ~= nil then
self.n = self.n + 1
self[self.n] = item
end
end
function Collection:join(sep)
return table.concat(self, sep)
end
function Collection:sort(comp)
table.sort(self, comp)
end
function Collection.new()
return setmetatable({n = 0}, Collection)
end
end


local function keyPress(args)
local chainNames = {
'chain first',
'chain second',
'chain third',
'chain fourth',
'chain fifth',
'chain sixth',
'chain seventh',
'chain eighth',
'chain ninth',
}
local result = Collection.new()
local chainDefault = args.chain or '+'
for i, id in ipairs(args) do
for i, id in ipairs(args) do
if i > 1 then
result:add(args[chainNames[i - 1]] or chainDefault)
end
local lc = id:lower()
local lc = id:lower()
local text = keyText[lc] or keyText[keyAlias[lc]] or id
local text = key_labels[lc] or key_labels[label_aliases[lc]] or id
result:add(kbdPrefix .. text .. kbdSuffix)
 
local label = mw.html.create("kbd")
:addClass("keyboard-key nowrap")
:wikitext(text)
table.insert(result, tostring(label))
end
end
return mw.getCurrentFrame():extensionTag{
return mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Template:Key press/styles.css'}  
name = 'templatestyles',
} .. result:join()
args = { src = 'Template:Key press/styles.css'}  
end
} .. table.concat(result, chain)
 
local function keypress(frame)
-- Called by "{{key press|...}}".
-- Using the template doubles the post‐expand include size.
return keyPress(frame:getParent().args)
end
end


local function press(frame)
function p.key_press(frame)
-- Called by "{{#invoke:key|press|...}}".
return format_key_press(frame:getParent().args)
return keyPress(frame.args)
end
end


return {
return p
keypress = keypress,
press = press,
}