Module:Shortcut: Difference between revisions

From Sarkarverse
Jump to navigation Jump to search
(Add module)
(test)
 
Line 1: Line 1:
-- This module implements {{shortcut}}.
-- This module implements {{shortcut}}.


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


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


local p = {}
-- 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'


function p._main(shortcuts, options, frame, cfg)
-- The header text for the list of shortcuts.
checkType('_main', 1, shortcuts, 'table')
-- cfg.shortcutHeaderSingular will be displayed if there is one shortcut, and
checkType('_main', 2, options, 'table', true)
-- cfg.shortcutHeaderPlural will be displayed if there are multiple shortcuts.
options = options or {}
cfg.shortcutHeaderSingular = '[[Wikipedia:Shortcut|Shortcut]]:'
frame = frame or mw.getCurrentFrame()
cfg.shortcutHeaderPlural = '[[Wikipedia:Shortcut|Shortcuts]]:'
cfg = cfg or mw.loadData(CONFIG_MODULE)


-- Validate shortcuts
--------------------------------------------------------------------------------
for i, shortcut in ipairs(shortcuts) do
-- Load external modules and define often-used functions
if type(shortcut) ~= 'string' or #shortcut < 1 then
--------------------------------------------------------------------------------
error(string.format(
'shortcut #%d was invalid (shortcuts must be strings of ' ..
'at least one character in length)'
), 2)
end
end


-- Make the list items. These are the shortcuts plus any extra lines such
-- Load external modules
-- as options.msg.
local mArguments = require('Module:Arguments')
local listItems = {}
local mTableTools = require('Module:TableTools')
for i, shortcut in ipairs(shortcuts) do
local mList = require('Module:List')
listItems[i] = string.format('[[%s]]', shortcut)
end
table.insert(listItems, options.msg)
local nListItems = #listItems


-- Exit if we have nothing to display
-- Define often-used functions
if nListItems < 1 then
local anchorEncode = mw.uri.anchorEncode
return nil
local format = string.format
end
local fullUrl = mw.uri.fullUrl


local root = mw.html.create()
--------------------------------------------------------------------------------
-- Main functions
--------------------------------------------------------------------------------


-- Anchors
local p = {}
local anchorDiv = root
:tag('div')
:css('position', 'relative')
:css('top', '-3em')
for i, shortcut in ipairs(shortcuts) do
local anchor = mw.uri.anchorEncode(shortcut)
anchorDiv:tag('span'):attr('id', anchor)
end


root:newline() -- To match the old [[Template:Shortcut]]
function p.main(frame)
local args = mArguments.getArgs(frame)
return p._main(args)
end


-- Shortcut heading
function p._main(args)
local shortcutHeading = mw.message.newRawMessage(
local shortcuts = p.getShortcuts(args)
cfg['shortcut-heading'],
local nShortcuts = #shortcuts
nListItems
if nShortcuts < 1 then
):plain()
-- Don't output anything if {{shortcut}} was called with no arguments.
shortcutHeading = frame:preprocess(shortcutHeading)
return ''
end
local anchors = p.makeAnchorList(shortcuts)
local shortcutList = p.makeShortcutList(shortcuts)
local errorCategories = p.getErrorCategories(shortcuts)
return p.export(anchors, nShortcuts, shortcutList, errorCategories)
end


-- Shortcut box
function p.getShortcuts(args)
local shortcutList = root
local shortcuts = mTableTools.compressSparseArray(args)
:tag('table')
return shortcuts
:addClass('shortcutbox noprint')
end
:css('float', 'right')
:css('border', '1px solid #aaa')
:css('background', '#fff')
:css('margin', '.3em .3em .3em 1em')
:css('padding', '3px')
:css('text-align', 'center')
:tag('tr')
:tag('th')
:addClass('plainlist')
:css('border', 'none')
:css('background', 'transparent')
:tag('small')
:wikitext(shortcutHeading)
:newline()
:tag('ul')
for i, item in ipairs(listItems) do
shortcutList:tag('li'):wikitext(item)
end


-- Error category
function p.makeAnchorList(shortcuts)
if shortcuts[1] and not yesno(options.nocat) then
local makeAnchor = p.makeAnchor
local title = mw.title.new(shortcuts[1])
local anchors = {}
if not title or not title.exists then
for i, shortcut in ipairs(shortcuts) do
root:wikitext(string.format(
anchors[#anchors + 1] = makeAnchor(shortcut)
'[[%s:%s]]',
mw.site.namespaces[14].name,
cfg['first-parameter-error-category']
))
end
end
end
return table.concat(anchors)
end


return tostring(root)
function p.makeAnchor(s)
s = anchorEncode(s)
local anchor = format('<span id="%s"></span>', s)
return anchor
end
end


function p.main(frame)
function p.makeShortcutList(shortcuts)
local args = require('Module:Arguments').getArgs(frame, {
local makeShortcutLink = p.makeShortcutLink
wrappers = 'Template:Shortcut'
local listArgs = {}
})
for i, shortcut in ipairs(shortcuts) do
local link = makeShortcutLink(shortcut)
listArgs[#listArgs + 1] = link
end
listArgs.class = 'plainlinks'
return mList.makeList('bulleted', listArgs)
end


-- Separate shortcuts from options
function p.makeShortcutLink(s)
local shortcuts, options = {}, {}
local uriObj = fullUrl(s, {redirect = 'no'})
for k, v in pairs(args) do
local url = tostring(uriObj)
if type(k) == 'number' then
return format('[%s %s]', url, s)
shortcuts[k] = v
end
else
options[k] = v
end
end


-- Compress the shortcut array, which may contain nils.
function p.getErrorCategories(shortcuts)
local function compressArray(t)
local shortcut1 = shortcuts[1]
local nums, ret = {}, {}
local title = mw.title.new(shortcut1)
for k in pairs(t) do
if not title or not title.exists then
nums[#nums + 1] = k
local categoryNsName = mw.site.namespaces[14].name
end
return format('[[%s:%s]]', categoryNsName, cfg.errorCategory)
table.sort(nums)
else
for i, num in ipairs(nums) do
return nil
ret[i] = t[num]
end
return ret
end
end
shortcuts = compressArray(shortcuts)
end


return p._main(shortcuts, options, frame)
function p.export(anchors, nShortcuts, shortcutList, errorCategories)
local root = mw.html.create('')
root
:tag('div')
:css{position = 'relative', top = '-3em'}
:wikitext(anchors)
root
:tag('table')
:addClass('shortcutbox noprint')
:css{
float = 'right',
border = '1px solid #aaa',
background = '#fff',
margin = '.3em .3em .3em 1em',
padding = '3px',
['text-align'] = 'center'
}
:tag('tr')
:tag('th')
:addClass('plainlist')
:css{border = 'none', background = 'transparent'}
:tag('small')
:wikitext(
nShortcuts <= 1
and cfg.shortcutHeaderSingular
or cfg.shortcutHeaderPlural
)
:newline()
:wikitext(shortcutList)
root:wikitext(errorCategories)
return tostring(root)
end
end


return p
return p

Latest revision as of 04:03, 4 February 2015

-- This module implements .

local cfg = {}


-- Configuration


-- 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 = 'Shortcut:' cfg.shortcutHeaderPlural = '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 = {}

function p.main(frame) local args = mArguments.getArgs(frame) return p._main(args) end

function p._main(args) local shortcuts = p.getShortcuts(args) local nShortcuts = #shortcuts if nShortcuts < 1 then -- Don't output anything if was called with no arguments. return 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 shortcuts = mTableTools.compressSparseArray(args) return shortcuts end

function p.makeAnchorList(shortcuts) local makeAnchor = p.makeAnchor local anchors = {} for i, shortcut in ipairs(shortcuts) do anchors[#anchors + 1] = makeAnchor(shortcut) end return table.concat(anchors) end

function p.makeAnchor(s) s = anchorEncode(s) local anchor = format('', s) return anchor end

function p.makeShortcutList(shortcuts) local makeShortcutLink = p.makeShortcutLink local listArgs = {} for i, shortcut in ipairs(shortcuts) do local link = makeShortcutLink(shortcut) listArgs[#listArgs + 1] = link end listArgs.class = 'plainlinks' return mList.makeList('bulleted', listArgs) end

function p.makeShortcutLink(s) local uriObj = fullUrl(s, {redirect = 'no'}) local url = tostring(uriObj) return format('[%s %s]', url, s) end

function p.getErrorCategories(shortcuts) local shortcut1 = shortcuts[1] local title = mw.title.new(shortcut1) if not title or not title.exists then local categoryNsName = mw.site.namespaces[14].name return format('%s:%s', categoryNsName, cfg.errorCategory) else return nil end end

function p.export(anchors, nShortcuts, shortcutList, errorCategories) local root = mw.html.create() root :tag('div') :css{position = 'relative', top = '-3em'} :wikitext(anchors) root :tag('table') :addClass('shortcutbox noprint') :css{ float = 'right', border = '1px solid #aaa', background = '#fff', margin = '.3em .3em .3em 1em', padding = '3px', ['text-align'] = 'center' } :tag('tr') :tag('th') :addClass('plainlist') :css{border = 'none', background = 'transparent'} :tag('small') :wikitext( nShortcuts <= 1 and cfg.shortcutHeaderSingular or cfg.shortcutHeaderPlural ) :newline() :wikitext(shortcutList) root:wikitext(errorCategories) return tostring(root) end

return p