Module:Shortcut: Difference between revisions

    From Nonbinary Wiki
    (create replacement for Template:Shortcut)
     
    (use redirect=no)
    Line 29: Line 29:
    local anchorEncode = mw.uri.anchorEncode
    local anchorEncode = mw.uri.anchorEncode
    local format = string.format
    local format = string.format
    local fullUrl = mw.uri.fullUrl


    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    Line 81: Line 82:
    listArgs[#listArgs + 1] = link
    listArgs[#listArgs + 1] = link
    end
    end
    listArgs.class = 'plainlinks'
    return mList.makeList('bulleted', listArgs)
    return mList.makeList('bulleted', listArgs)
    end
    end


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


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

    Revision as of 06:25, 7 March 2014

    Documentation for this module may be created at Module:Shortcut/doc

    -- This module implements {{shortcut}}.
    
    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 = '[[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 = {}
    
    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 {{shortcut}} 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('<span id="%s"></span>', 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