Module:Shortcut: Difference between revisions

rewrite this without so many dependencies and with it hopefully easier to read
(use redirect=no)
(rewrite this without so many dependencies and with it hopefully easier to read)
Line 1: Line 1:
-- This module implements {{shortcut}}.
-- This module implements {{shortcut}}.


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


--------------------------------------------------------------------------------
-- Load required modules
-- Configuration
local checkType = require('libraryUtil').checkType
--------------------------------------------------------------------------------
 
-- Name for the error category produced if the first shortcut is not an existing
-- page on the wiki.
cfg.errorCategory = 'Wikipedia shortcut box first parameter needs fixing'
 
-- The header text for the list of shortcuts.
-- cfg.shortcutHeaderSingular will be displayed if there is one shortcut, and
-- cfg.shortcutHeaderPlural will be displayed if there are multiple shortcuts.
cfg.shortcutHeaderSingular = '[[Wikipedia:Shortcut|Shortcut]]:'
cfg.shortcutHeaderPlural = '[[Wikipedia:Shortcut|Shortcuts]]:'
 
--------------------------------------------------------------------------------
-- Load external modules and define often-used functions
--------------------------------------------------------------------------------
 
-- Load external modules
local mArguments = require('Module:Arguments')
local mTableTools = require('Module:TableTools')
local mList = require('Module:List')
 
-- Define often-used functions
local anchorEncode = mw.uri.anchorEncode
local format = string.format
local fullUrl = mw.uri.fullUrl
 
--------------------------------------------------------------------------------
-- Main functions
--------------------------------------------------------------------------------


local p = {}
local p = {}


function p.main(frame)
function p._main(shortcuts, options, frame, cfg)
local args = mArguments.getArgs(frame)
checkType('_main', 1, shortcuts, 'table')
return p._main(args)
checkType('_main', 2, options, 'table', true)
end
options = options or {}
frame = frame or mw.getCurrentFrame()
cfg = cfg or mw.loadData(CONFIG_MODULE)
local nShortcuts = #shortcuts


function p._main(args)
-- Validate shortcuts
local shortcuts = p.getShortcuts(args)
local nShortcuts = #shortcuts
if nShortcuts < 1 then
if nShortcuts < 1 then
-- Don't output anything if {{shortcut}} was called with no arguments.
return nil
return ''
else
for i, shortcut in ipairs(shortcuts) do
if type(shortcut) ~= 'string' or #shortcut < 1 then
error(string.format(
'shortcut #%d was invalid (shortcuts must be strings of ' ..
'least one character in length)'
), 2)
end
end
end
end
local anchors = p.makeAnchorList(shortcuts)
local shortcutList = p.makeShortcutList(shortcuts)
local errorCategories = p.getErrorCategories(shortcuts)
return p.export(anchors, nShortcuts, shortcutList, errorCategories)
end


function p.getShortcuts(args)
local root = mw.html.create()
local shortcuts = mTableTools.compressSparseArray(args)
return shortcuts
end


function p.makeAnchorList(shortcuts)
-- Anchors
local makeAnchor = p.makeAnchor
local anchorDiv =