Module:Hatnote list/sandbox
See also the companion subpage for test cases (run).
Changes to it can cause immediate changes to the Wikipedia user interface.
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them.
Usage from wikitext
[edit ]This module is not designed to be used directly from wikitext even though forSee
does take an initial frame
argument and could potentially be used from wikitext, e.g.:
{{hatnote|PREFIX {{#invoke:Hatnote list|forSee|{{tl|For}}|Module:For|{{tl|About}}|Module:About}} POSTFIX}}
→
Usage from Lua
[edit ]localmHatList=require('Module:Hatnote list')
mHatList
variable (or whatever was used).
andList
[edit ]andList
takes a list in table form, and returns a string with the list separated with "and" and commas as appropriate.
orList
[edit ]orList
takes a list in table form, and returns a string with the list separated with "or" and commas as appropriate.
forSee
[edit ]_forSee
takes three arguments: a table of trimmed arguments with blanks removed, a "from" number with the index to start at, and an options table, and returns a string with a number of "For X, see [[Y]]
" sentences. The links are formatted using the methods from Module:Hatnote.
As a convenience, the forSee
method (without the leading underscore) takes the same arguments except with a frame instead of an args table, using getArgs()
from Module:Arguments to preprocess the arguments.
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Add categories to the /doc subpage. Subpages of this module.
-------------------------------------------------------------------------------- -- Module:Hatnote list -- -- -- -- This module produces and formats lists for use in hatnotes. In particular, -- -- it implements the for-see list, i.e. lists of "For X, see Y" statements, -- -- as used in {{about}}, {{redirect}}, and their variants. Also introduced -- -- are andList & orList helpers for formatting lists with those conjunctions. -- -------------------------------------------------------------------------------- localmArguments--initialize lazily localmFormatLink=require('Module:Format link') localmHatnote=require('Module:Hatnote') locallibraryUtil=require('libraryUtil') localcheckType=libraryUtil.checkType localp={} -------------------------------------------------------------------------------- -- List stringification helper functions -- -- These functions are used for stringifying lists, usually page lists inside -- the "Y" portion of "For X, see Y" for-see items. -------------------------------------------------------------------------------- --default options table used across the list stringification functions localstringifyListDefaultOptions={ conjunction="and", separator=",", altSeparator=";", space=" ", formatted=false } --Searches display text only localfunctionsearchDisp(haystack,needle) returnstring.find( string.sub(haystack,(string.find(haystack,'|')or0)+1),needle ) end -- Stringifies a list generically; probably shouldn't be used directly localfunctionstringifyList(list,options) -- Type-checks, defaults, and a shortcut checkType("stringifyList",1,list,"table") if#list==0thenreturnnilend checkType("stringifyList",2,options,"table",true) options=optionsor{} fork,vinpairs(stringifyListDefaultOptions)do ifoptions[k]==nilthenoptions[k]=vend end locals=options.space -- Format the list if requested ifoptions.formattedthen list=mFormatLink.formatPages( {categorizeMissing=mHatnote.missingTargetCat},list ) end -- Set the separator; if any item contains it, use the alternate separator localseparator=options.separator fork,vinpairs(list)do ifsearchDisp(v,separator)then separator=options.altSeparator break end end -- Set the conjunction, apply Oxford comma, and force a comma if #1 has "§" localconjunction=s..options.conjunction..s if#list==2andsearchDisp(list[1],"§")or#list>2then conjunction=separator..conjunction end -- Return the formatted string returnmw.text.listToText(list,separator..s,conjunction) end --DRY function functionp.conjList(conj,list,fmt) returnstringifyList(list,{conjunction=conj,formatted=fmt}) end -- Stringifies lists with "and" or "or" functionp.andList(...)returnp.conjList("and",...)end functionp.orList(...)returnp.conjList("or",...)end -------------------------------------------------------------------------------- -- For see -- -- Makes a "For X, see [[Y]]." list from raw parameters. Intended for the -- {{about}} and {{redirect}} templates and their variants. -------------------------------------------------------------------------------- --default options table used across the forSee family of functions localforSeeDefaultOptions={ andKeyword='and', title=mw.title.getCurrentTitle().text, otherText='other uses', forSeeForm='For %s, see %s.', } --Collapses duplicate punctuation at end of string, ignoring italics and links localfunctionpunctuationCollapse(text) returntext:match("[.?!]('?)%1(%]?)%2%.$")andtext:sub(1,-2)ortext end -- Structures arguments into a table for stringification, & options functionp.forSeeArgsToTable(args,from,options) -- Type-checks and defaults checkType("forSeeArgsToTable",1,args,'table') checkType("forSeeArgsToTable",2,from,'number',true) from=fromor1 checkType("forSeeArgsToTable",3,options,'table',true) options=optionsor{} fork,vinpairs(forSeeDefaultOptions)do ifoptions[k]==nilthenoptions[k]=vend end -- maxArg's gotten manually because getArgs() and table.maxn aren't friends localmaxArg=0 fork,vinpairs(args)do iftype(k)=='number'andk>maxArgthenmaxArg=kend end -- Structure the data out from the parameter list: -- * forTable is the wrapper table, with forRow rows -- * Rows are tables of a "use" string & a "pages" table of pagename strings -- * Blanks are left empty for defaulting elsewhere, but can terminate list localforTable={} locali=from localterminated=false -- If there is extra text, and no arguments are given, give nil value -- to not produce default of "For other uses, see foo (disambiguation)" ifoptions.extratextandi>maxArgthenreturnnilend -- Loop to generate rows repeat -- New empty row localforRow={} -- On blank use, assume list's ended & break at end of this loop forRow.use=args[i] ifnotargs[i]thenterminated=trueend -- New empty list of pages forRow.pages={} -- Insert first pages item if present table.insert(forRow.pages,args[i+1]) -- If the param after next is "and", do inner loop to collect params -- until the "and"'s stop. Blanks are ignored: "1|and||and|3" → {1, 3} whileargs[i+2]==options.andKeyworddo ifargs[i+3]then table.insert(forRow.pages,args[i+3]) end -- Increment to next "and" i=i+2 end -- Increment to next use i=i+2 -- Append the row table.insert(forTable,forRow) untilterminatedori>maxArg returnforTable end -- Stringifies a table as formatted by forSeeArgsToTable functionp.forSeeTableToString(forSeeTable,options) -- Type-checks and defaults checkType("forSeeTableToString",1,forSeeTable,"table",true) checkType("forSeeTableToString",2,options,"table",true) options=optionsor{} fork,vinpairs(forSeeDefaultOptions)do ifoptions[k]==nilthenoptions[k]=vend end -- Stringify each for-see item into a list localstrList={} ifforSeeTablethen fork,vinpairs(forSeeTable)do localuseStr=v.useoroptions.otherText localpagesStr= p.andList(v.pages,true)or mFormatLink._formatLink{ categorizeMissing=mHatnote.missingTargetCat, link=mHatnote.disambiguate(options.title) } localforSeeStr=string.format(options.forSeeForm,useStr,pagesStr) forSeeStr=punctuationCollapse(forSeeStr) table.insert(strList,forSeeStr) end end ifoptions.extratextthentable.insert(strList,punctuationCollapse(options.extratext..'.'))end -- Return the concatenated list returntable.concat(strList,' ') end -- Produces a "For X, see [[Y]]" string from arguments. Expects index gaps -- but not blank/whitespace values. Ignores named args and args < "from". functionp._forSee(args,from,options) localforSeeTable=p.forSeeArgsToTable(args,from,options) returnp.forSeeTableToString(forSeeTable,options) end -- As _forSee, but uses the frame. functionp.forSee(frame,from,options) mArguments=require('Module:Arguments') returnp._forSee(mArguments.getArgs(frame),from,options) end returnp