Idi na sadržaj

Modul:Hatnote list

S Wikipedije, slobodne enciklopedije

Dokumentaciju za ovaj modul možete napraviti na straniciModul:Hatnote list/dok

--------------------------------------------------------------------------------
-- 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
localmHatnote=require('Modul: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="i",
separator=",",
altSeparator=";",
space="",
formatted=false
}

-- Stringifies a list generically; probably shouldn't be used directly
functionstringifyList(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.formattedthenlist=mHatnote.formatPages(unpack(list))end
-- Set the separator; if any item contains it, use the alternate separator
localseparator=options.separator
--searches display text only
functionsearchDisp(t,f)
returnstring.find(string.sub(t,(string.find(t,'|')or0)+1),f)
end
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],"§")then
conjunction=separator..conjunction
end
-- Return the formatted string
returnmw.text.listToText(list,separator..s,conjunction)
end

--DRY function
functionconjList(conj,list,fmt)
returnstringifyList(list,{conjunction=conj,formatted=fmt})
end

-- Stringifies lists with "and" or "or"
functionp.andList(...)returnconjList("i",...)end
functionp.orList(...)returnconjList("ili",...)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='i',
title=mw.title.getCurrentTitle().text,
otherText='druga značenja',
forSeeForm='Za %s, pogledajte %s.'
}

--Collapses duplicate punctuation
functionpunctuationCollapse(text)
localreplacements={
["%.%.$"]=".",
["%?%.$"]="?",
["%!%.$"]="!",
["%.%]%]%.$"]=".]]",
["%?%]%]%.$"]="?]]",
["%!%]%]%.$"]="!]]"
}
fork,vinpairs(replacements)dotext=string.gsub(text,k,v)end
returntext
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
-- 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")
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={}
fork,vinpairs(forSeeTable)do
localuseStr=v.useoroptions.otherText
localpagesStr=p.andList(v.pages,true)or
mHatnote._formatLink(mHatnote.disambiguate(options.title))
localforSeeStr=string.format(options.forSeeForm,useStr,pagesStr)
forSeeStr=punctuationCollapse(forSeeStr)
table.insert(strList,forSeeStr)
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('Modul:Arguments')
returnp._forSee(mArguments.getArgs(frame),from,options)
end

returnp