Jump to content
Wikipedia The Free Encyclopedia

Module:Shortcut

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page template-protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
Warning This Lua module is used on approximately 27,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
This module depends on the following other modules:
Related pages

This module makes a box showing the shortcut links to a page.

Usage

From wikitext

From wikitext, this module should be called from a template, usually {{shortcut }}. Please see the template page for documentation. However, it can also be called using the syntax {{#invoke:shortcut|main|arguments}}.

From Lua

To use this module from Lua, first load it.

localmShortcut=require('Module:Shortcut')

Then you can create shortcut boxes with the following syntax:

mShortcut._main(shortcuts,options,frame,cfg)
  • shortcuts is an array of shortcut page names. (required)
  • options is a table of options. The following keys are supported:
    • msg - a message to leave after the list of shortcuts.
    • category - if set to false (or a value regarded as false by Module:Yesno, such as "no"), categories are suppressed.
  • frame is a frame object. This is optional, and only intended to be used internally.
  • cfg is a table of config values. This is optional, and is only intended for testing.

Technical details

This module has a configuration file at Module:Shortcut/config. It can be used to translate this module into different languages or to change details like category names.

The above documentation is transcluded from Module:Shortcut/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

 -- This module implements {{shortcut}}.

 -- Set constants
 localCONFIG_MODULE='Module:Shortcut/config'

 -- Load required modules
 localcheckType=require('libraryUtil').checkType
 localyesno=require('Module:Yesno')

 localp={}

 localfunctionmessage(msg,...)
 returnmw.message.newRawMessage(msg,...):plain()
 end

 localfunctionmakeCategoryLink(cat)
 returnstring.format('[[%s:%s]]',mw.site.namespaces[14].name,cat)
 end

 functionp._main(shortcuts,options,frame,cfg)
 checkType('_main',1,shortcuts,'table')
 checkType('_main',2,options,'table',true)
 options=optionsor{}
 frame=frameormw.getCurrentFrame()
 cfg=cfgormw.loadData(CONFIG_MODULE)
 localtemplateMode=options.templateandyesno(options.template)
 localredirectMode=options.redirectandyesno(options.redirect)
 localisCategorized=notoptions.categoryoryesno(options.category)~=false

 -- Validate shortcuts
 fori,shortcutinipairs(shortcuts)do
 iftype(shortcut)~='string'or#shortcut<1then
 error(message(cfg['invalid-shortcut-error'],i),2)
 end
 end

 -- Make the list items. These are the shortcuts plus any extra lines such
 -- as options.msg.
 locallistItems={}
 fori,shortcutinipairs(shortcuts)do
 localtemplatePath,prefix
 iftemplateModethen
 -- Namespace detection
 localtitleObj=mw.title.new(shortcut,10)
 iftitleObj.namespace==10then
 templatePath=titleObj.fullText
 else
 templatePath=shortcut
 end
 prefix=options['pre'..i]oroptions.preor''
 end
 ifoptions.targetandyesno(options.target)then
 listItems[i]=templateMode
 andstring.format("&#123;&#123;%s[[%s|%s]]&#125;&#125;",prefix,templatePath,shortcut)
 orstring.format("[[%s]]",shortcut)
 else
 listItems[i]=frame:expandTemplate{
 title='No redirect',
 args=templateModeand{templatePath,shortcut}or{shortcut,shortcut}
 }
 iftemplateModethen
 listItems[i]=string.format("&#123;&#123;%s%s&#125;&#125;",prefix,listItems[i])
 end
 end
 end
 table.insert(listItems,options.msg)

 -- Return an error if we have nothing to display
 if#listItems<1then
 localmsg=cfg['no-content-error']
 msg=string.format('<strong class="error">%s</strong>',msg)
 ifisCategorizedandcfg['no-content-error-category']then
 msg=msg..makeCategoryLink(cfg['no-content-error-category'])
 end
 returnmsg
 end

 localroot=mw.html.create()
 root:wikitext(frame:extensionTag{name='templatestyles',args={src='Module:Shortcut/styles.css'}})
 -- Anchors
 localanchorDiv=root
 :tag('div')
 :addClass('module-shortcutanchordiv')
 fori,shortcutinipairs(shortcuts)do
 localanchor=mw.uri.anchorEncode(shortcut)
 anchorDiv:tag('span'):attr('id',anchor)
 end

 -- Shortcut heading
 localshortcutHeading
 do
 localnShortcuts=#shortcuts
 ifnShortcuts>0then
 localheadingMsg=options['shortcut-heading']or
 redirectModeandcfg['redirect-heading']or
 cfg['shortcut-heading']
 shortcutHeading=message(headingMsg,nShortcuts)
 shortcutHeading=frame:preprocess(shortcutHeading)
 end
 end

 -- Shortcut box
 localshortcutList=root
 :tag('div')
 :addClass('module-shortcutboxplain noprint')
 :attr('role','note')
 ifoptions.floatandoptions.float:lower()=='left'then
 shortcutList:addClass('module-shortcutboxleft')
 end
 ifoptions.clearandoptions.clear~=''then
 shortcutList:css('clear',options.clear)
 end
 ifshortcutHeadingthen
 shortcutList
 :tag('div')
 :addClass('module-shortcutlist')
 :wikitext(shortcutHeading)
 end

 localubl=require('Module:List').unbulleted(listItems)
 shortcutList:wikitext(ubl)
 returntostring(root)
 end

 functionp.main(frame)
 localargs=require('Module:Arguments').getArgs(frame)

 -- Separate shortcuts from options
 localshortcuts,options={},{}
 fork,vinpairs(args)do
 iftype(k)=='number'then
 shortcuts[k]=v
 else
 options[k]=v
 end
 end

 -- Compress the shortcut array, which may contain nils.
 localfunctioncompressArray(t)
 localnums,ret={},{}
 forkinpairs(t)do
 nums[#nums+1]=k
 end
 table.sort(nums)
 fori,numinipairs(nums)do
 ret[i]=t[num]
 end
 returnret
 end
 shortcuts=compressArray(shortcuts)

 returnp._main(shortcuts,options,frame)
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /