모듈:Shortcut

-- This module implements {{shortcut}}.-- Set constantslocal CONFIG_MODULE = 'Module:Shortcut/config'-- Load required moduleslocal checkType = require('libraryUtil').checkTypelocal yesno = require('Module:Yesno')local p = {}local function message(msg, ...)return mw.message.newRawMessage(msg, ...):plain()endlocal function makeCategoryLink(cat)return string.format('[[%s:%s]]', mw.site.namespaces[14].name, cat)endfunction p._main(shortcuts, options, frame, cfg)checkType('_main', 1, shortcuts, 'table')checkType('_main', 2, options, 'table', true)options = options or {}frame = frame or mw.getCurrentFrame()cfg = cfg or mw.loadData(CONFIG_MODULE)local templateMode = options.template and yesno(options.template)local redirectMode = (options.redirect or options['넘겨주기']) and yesno(options.redirect or options['넘겨주기'])local isCategorized = not options.category or yesno(options.category) ~= false-- Validate shortcutsfor i, shortcut in ipairs(shortcuts) doif type(shortcut) ~= 'string' or #shortcut < 1 thenerror(message(cfg['invalid-shortcut-error'], i), 2)endend-- Make the list items. These are the shortcuts plus any extra lines such-- as options.msg.local listItems = {}for i, shortcut in ipairs(shortcuts) dolocal templatePath, prefixif templateMode then-- Namespace detectionlocal titleObj = mw.title.new(shortcut, 10)if titleObj.namespace == 10 thentemplatePath = titleObj.fullTextelsetemplatePath = shortcutendprefix = options['pre' .. i] or options.pre or ''endif options.target and yesno(options.target) thenlistItems[i] = templateModeand string.format("&#123;&#123;%s[[%s|%s]]&#125;&#125;", prefix, templatePath, shortcut)or string.format("[[%s]]", shortcut)elselistItems[i] = frame:expandTemplate{title = '안넘겨주기',args = templateMode and {templatePath, shortcut} or {shortcut, shortcut}}if templateMode thenlistItems[i] = string.format("&#123;&#123;%s%s&#125;&#125;", prefix, listItems[i])endendendtable.insert(listItems, options.msg)-- Return an error if we have nothing to displayif #listItems < 1 thenlocal msg = cfg['no-content-error']msg = string.format('<strong class="error">%s</strong>', msg)if isCategorized and cfg['no-content-error-category'] thenmsg = msg .. makeCategoryLink(cfg['no-content-error-category'])endreturn msgendlocal root = mw.html.create()root:wikitext(frame:extensionTag{ name = 'templatestyles', args = { src = '단축/styles.css'} })-- Anchorslocal anchorDiv = root:tag('div'):addClass('module-shortcutanchordiv')for i, shortcut in ipairs(shortcuts) dolocal anchor = mw.uri.anchorEncode(shortcut)anchorDiv:tag('span'):attr('id', anchor)end-- Shortcut headinglocal shortcutHeadingdolocal nShortcuts = #shortcutsif nShortcuts > 0 thenlocal headingMsg = options['shortcut-heading'] orredirectMode and cfg['redirect-heading'] orcfg['shortcut-heading']shortcutHeading = message(headingMsg, nShortcuts)shortcutHeading = frame:preprocess(shortcutHeading)endend-- Shortcut boxlocal shortcutList = root:tag('div'):addClass('module-shortcutboxplain plainlist noprint'):attr('role', 'note')if options.float and options.float:lower() == 'left' thenshortcutList:addClass('module-shortcutboxleft')endif options.clear and options.clear ~= '' thenshortcutList:css('clear', options.clear)endif shortcutHeading thenshortcutList:tag('div'):addClass('module-shortcutlist'):wikitext(shortcutHeading)endlocal list = shortcutList:tag('ul')for i, item in ipairs(listItems) dolist:tag('li'):wikitext(item)endreturn tostring(root)endfunction p.main(frame)local args = require('Module:Arguments').getArgs(frame)-- Separate shortcuts from optionslocal shortcuts, options = {}, {}for k, v in pairs(args) doif type(k) == 'number' thenshortcuts[k] = velseoptions[k] = vendend-- Compress the shortcut array, which may contain nils.local function compressArray(t)local nums, ret = {}, {}for k in pairs(t) donums[#nums + 1] = kendtable.sort(nums)for i, num in ipairs(nums) doret[i] = t[num]endreturn retendshortcuts = compressArray(shortcuts)return p._main(shortcuts, options, frame)endreturn p