Jump to content
Wikipedia The Free Encyclopedia

Module:Class/sandbox

From Wikipedia, the free encyclopedia
This is the module sandbox page for Module:Class (diff).
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 65,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 can only be edited by administrators because it is transcluded onto one or more cascade-protected pages.
This module depends on the following other modules:

Usage

[edit ]

This module implements Template:Class, Template:Class/icon and Template:Class/colour.

Documentation for Module:Class/definition.json

[edit ]

Following are notes documenting the model for the class definition JSON.

  • The top-level JSON object contains a number of objects. Each is either a class definition, or an alias. The keys are "class codes" that can be used as input. The keys containing class definitions are the canonical class codes. Class codes should be lowercase (except the special code DEFAULT) and canonical class codes are, so far, strictly alphabetic (bplus, not b+)
  • An alias should be an object with exactly one property alias that contains the canonical class code string.
  • A class definition can have a number of properties:
    categoryRoot
    (string) The root of a category name for articles of the quality class. For example, the top-level category for featured articles is Category:FA-Class articles, and subject-level categories might exist at "Category:FA-Class subject articles", so its root is FA-Class.
    colour
(object) An object containing colour-related properties; each value within should be a string that can be used as a CSS colour value, e.g. #6699ff
  • base
    (string) a colour string reflecting a base background colour[model 1]
    textOnBlack
    (string) a colour string reflecting a suitable colour to use for text on a black background
    textOnWhite
    (string) a colour string reflecting a suitable colour to use for text on a white background
    icon
    (object) An object containing icon-related properties:
    file
    (string) A filename, without File: prefix, preferably formatted with underscores,[model 2] e.g. Featured_article_star.svg
    default
    (boolean) Whether the icon should be displayed by default—usually only high quality ranks display icons by default
    requiresAttribution
    (boolean) true if the file property refers to a file that is not public domain or CC0-licensed. When a file is PD/CC0 and thus this property false, we can disable image linking and alt text, which is good for accessibility for purely-decorative images.
    labels
    (object) An object containing label strings. All the label properties are particularly tentative.
    adjective
    (string) A label string describing an adjective that could be used to describe a page with this class. Prefer lowercase or title case.[model 3] This property is highly tentative.
    full
    (string) A label string describing a noun phrase that could be used to describe a page with this class. Prefer lowercase or title case.[model 3] This property is highly tentative.
    short
    (string) A label string describing a short name for the class that can be used in context. This may frequently be the same as the class code, but should usually be capitalized (e.g. Redirect or GA). One particular precedent imported to this property is that class code unassessed uses ??? as its short label. This property is highly tentative.
    tooltip
    (string) A label string with a tooltip that should be used to describe the class if more details are needed. This property is optional and probably should only be specified if needed.
    page
    (string) The full page name of a wiki page describing the quality class, preferably formatted with spaces,[model 4] e.g. Wikipedia:Featured articles
  • At the moment, there's no definition of which properties might be mandatory or optional for class definitions; for now, it's probably safest to assume that "label" properties are mandatory.
  • Tentatively a special DEFAULT object, using capitalization to separate it from other keys, defines defaults that can be used. This should either be a complete object (all properties mandatory) or an alias to a complete object. In English Wikipedia's case this is currently an alias to the unassessed class definition.

Object model notes

[edit ]
  1. ^ This is assumed to be a light-mode colour; it might be desirable to split this into light- and dark-mode properties.
  2. ^ Underscores are probably better for file names because they're more easily used in URLs, and don't hurt anything being used in wikitext.
  3. ^ a b Lowercase is better for full or generic words, e.g. "featured", but title case is better for constructed values, e.g. "Start-Class".
  4. ^ Spaces are probably better for referencing wiki pages because they're easily used as strings or in wikitext; use in URLs would have to do other transformations anyway.
The above documentation is transcluded from Module:Class/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
 -- This module implements [[Template:Class]], [[Template:Class/icon]] and
 -- [[Template:Class/colour]].

 localmArguments-- lazily loaded
 localdefinitions=mw.loadJsonData('Module:Class/sandbox/definition.json')

 localp={}

 --------------------------------------------------------------------------------
 -- Local configuration and messages
 --------------------------------------------------------------------------------

 localcfg={
 defaultCode='DEFAULT',
 classPrefix='assess-',
 globalClass='assess',
 defaultClassSuffix='default',
 unboldClassSuffix='unbold',
 catRootFormat='%s %s',
 catTopicFormat='%s %s %s',
 catBasicFormat='%s %s',
 categoryFormat='[[:Category:%s|%s]]',
 templateLocation='Template:Class',
 iconTemplateLocation='Template:Class/icon',
 colourTemplateLocation='Template:Class/colour',
 stylesLocation='Module:Class/styles.css',
 baseColourPath={'colour','base'},
 iconPath={"icon","file"},
 iconDefaultPath={"icon","default"},
 iconAttribPath={"icon","requiresAttribution"},
 fullLabelPath={"labels","full"},
 shortLabelPath={"labels","short"},
 categoryRootPath={"categoryRoot"},
 tooltipPath={"labels","tooltip"},
 articlePath={"article"},
 yes="yes",
 no="no",
 articles="articles",
 pages="pages",
 argumentNames={
 class="class",
 style="style"
 },
 getOptions={
 --First item is localized argument name, second is case-sensitivity
 bold={"bold",false},
 header={"header",false},
 image={"image",false},
 rowspan={"rowspan",false},
 fullcategory={"fullcategory",true},
 category={"category",true},
 topic={"topic",true}
 }
 }

 --------------------------------------------------------------------------------
 -- Argument helper functions
 --------------------------------------------------------------------------------

 localfunctiongetRawArgs(frame,wrapper)
 --Retrieves the arguments from the frame
 mArguments=mArgumentsorrequire('Module:Arguments')
 returnmArguments.getArgs(frame,{
 wrappers=wrapper,
 trim=false,
 removeBlanks=false
 })
 end

 localfunctionmakeInvokeFunction(func,wrapper)
 --Wraps a general function into an invokable version
 returnfunction(frame)
 localargs=getRawArgs(frame,wrapper)
 returnfunc(args)
 end
 end

 --------------------------------------------------------------------------------
 -- String helper functions
 --------------------------------------------------------------------------------

 localfunctiontrim(str)
 --Trims strings, passes through non-strings without modification
 return(type(str)=='string')andmw.text.trim(str)orstr
 end

 localfunctionnormalizeValue(val)
 --Normalizes strings, particularly class codes
 iftype(val)=='string'thenval=trim(val):lower()end
 ifval==''thenval=nilend
 returnval
 end

 localfunctionucfirst(str)
 --Capitalizes the first character of a string
 returnmw.ustring.upper(mw.ustring.sub(str,1,1))..mw.ustring.sub(str,2)
 end

 --------------------------------------------------------------------------------
 -- Definition helper functions
 --------------------------------------------------------------------------------

 localfunctiongetDefinition(code)
 --Retrieves the definition and canonical class code for a given code.
 --Returns two values: the definition object and the canonical class code
 --string.
 localcanonicalCode=normalizeValue(code)
 ifcode==cfg.defaultCodethencanonicalCode=codeend
 localclass=definitions[canonicalCode]
 whileclassandclass.aliasdo
 canonicalCode=class.alias
 class=definitions[class.alias]
 end
 ifnotclassthen
 returnnil,nil
 end
 returnclass,canonicalCode
 end

 localfunctiongetDefault()
 --Shortcut function for retrieving the default definition
 returngetDefinition(cfg.defaultCode)end

 localfunctiongetProperty(class,default,map)
 --Retrieves a given property from a string given a class definition, a
 --default class definition, and a map for the path to traverse through the
 --class object. The map should be a sequential table of string property
 --names, e.g. {"colour", "base"} would retrieve someClass.colour.base
 localprop,dProp=class,default
 fork,vinipairs(map)do
 prop=((type(prop)=='table')ornil)andprop[v]
 dProp=((type(dProp)=='table')ornil)anddProp[v]
 end
 ifprop==nilthenprop=dPropend
 returnprop
 end

 --------------------------------------------------------------------------------
 -- Color functions
 --------------------------------------------------------------------------------

 functionp._colour(code)
 --Retrieves the base colour for a given code
 returngetProperty(getDefinition(code),getDefault(),cfg.baseColourPath)
 end

 functionp.colour(frame)
 --Retrieves the base colour for a given code; is invokable
 localargs=getRawArgs(frame,cfg.colourTemplateLocation)
 -- Nowiki tags prevent output beginning with "#" from triggering bug 14974.
 returnframe:extensionTag('nowiki',p._colour(args[1]))
 end

 --------------------------------------------------------------------------------
 -- Icon functions
 --------------------------------------------------------------------------------

 functionp._icon(args)
 --Retrieves an icon image and formats it as wikitext
 localclass=getDefinition(args[cfg.argumentNames.class]orargs[1])
 localdefault=getDefault()
 localfile=getProperty(class,default,cfg.iconPath)
 locallabel=
 getProperty(class,default,cfg.tooltipPath)or
 ucfirst(getProperty(class,default,cfg.fullLabelPath))
 localattrib=getProperty(class,default,cfg.iconAttribPath)
 localsize=args.sizeor'16px'
 localspan=mw.html.create('span')

 span
 :cssText(args[cfg.argumentNames.style])
 :attr('title',label)
 :wikitext(
 string.format(
 '[[File:%s|%s|'..size..'%s|class=noviewer|alt=]]',
 file,
 label,
 attriband''or'|link='
 )
 )
 returntostring(span)
 end

 p.icon=makeInvokeFunction(p._icon,cfg.iconTemplateLocation)
 --Invokable version of p._icon

 --------------------------------------------------------------------------------
 -- Class functions
 --------------------------------------------------------------------------------

 functionp._class(args)
 --Parses its arguments into a table cell with an optional icon, a name
 --linked to an appropriate category, and appropriate colour styling
 localclassDef,classCode=getDefinition(args[cfg.argumentNames.class]orargs[1])
 localdefault=getDefault()
 localiconDefault=getProperty(classDef,default,cfg.iconDefaultPath)
 localshortLabel=getProperty(classDef,default,cfg.shortLabelPath)
 localcategoryRoot=getProperty(classDef,default,cfg.categoryRootPath)
 localarticle=getProperty(classDef,false,cfg.articlePath)
 localpagetype=articleandcfg.articlesorcfg.pages
 --o is short for "options", go for "get options". Bool true → case-sensitive
 localo,go={},cfg.getOptions
 fork,vinpairs(go)do
 o[k]=v[2]andtrim(args[v[1]])ornormalizeValue(args[v[1]])
 end

 localcell=mw.html.create(o.headerand'th'or'td')
 --image=yes forces icon, image=no disables it, otherwise checks default
 localicon=iconDefaultand(o.image~=cfg.no)or(o.image==cfg.yes)
 icon=iconandp.icon(args)..' 'or''

 localcategory
 ifo.fullcategorythen
 category=o.fullcategory
 elseifo.categorythen
 category=cfg.catRootFormat:format(categoryRoot,o.category)
 elseifo.topicthen
 category=cfg.catTopicFormat:format(categoryRoot,o.topic,pagetype)
 else
 category=cfg.catBasicFormat:format(categoryRoot,pagetype)
 end
 localtext=string.format(cfg.categoryFormat,category,shortLabel)
 cell
 :addClass(cfg.globalClass)
 :addClass(
 o.bold==cfg.noandcfg.classPrefix..cfg.unboldClassSuffixornil
 )
 :addClass(cfg.classPrefix..(classCodeorcfg.defaultClassSuffix))
 :attr('rowspan',tonumber(o.rowspan))
 :wikitext(mw.getCurrentFrame():extensionTag{name='templatestyles',args={src=cfg.stylesLocation}},icon,text)

 returntostring(cell)
 end

 p.class=makeInvokeFunction(p._class,cfg.templateLocation)
 --Invokable version of p._class

 returnp

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