Module:Category see also
ښکارېدونکې بڼه
لاسوند لپاره ددې موډيول کېدای سی پهModule:Category see also/لاسوندکي وي
-- This module implements {{Category see also}}
localmHatnote=require('Module:Hatnote')
localp={}
localfunctionmakeWikitextError(msg)
returnstring.format(
'<strong class= "error" >Error: %s ([[Template:Category see also]])</strong>',
msg
)
end
-- Gets the length of the sequence seq. Usually this should be done with the #
-- operator, but we need to work with tables that get their values through an
-- __index metamethod.
localfunctiongetSequenceLength(seq)
locallength=0
foriinipairs(seq)do
length=i
end
returnlength
end
-- Given a table of options, returns a function that formats categories for
-- those options.
--
-- Options:
-- project - a project code such as "fr" (for the French Wikipedia)
-- showPrefix - a boolean value for whether to show the "Category:" prefix
-- (and the project prefix if specified)
--
-- This is implemented as a function generator rather than a simple function
-- so that we can just process the options once, instead of every time we
-- generate a category.
localfunctionnewCategoryLinker(options)
localformatString
ifoptions.projectthen
ifoptions.showPrefixthen
formatString='[[:'..options.project..':Category:%s]]'
else
formatString='[[:'..options.project..':Category:%s|%s]]'
end
else
ifoptions.showPrefixthen
formatString='[[:Category:%s]]'
else
formatString='[[:Category:%s|%s]]'
end
end
returnfunction(category)
localtitle=mw.title.new(category)
localpageName,display
ifnottitlethen
-- category is not a valid title, usually because of invalid
-- characters like < or [. Raise an error and suppress the stack
-- level information so that we can catch it and format the error
-- message as wikitext.
error(string.format(
"'%s' is not a valid category name",
category
),0)
elseiftitle.namespace==14then-- Category namespace
pageName=title.text
display=title.text
else
pageName=title.prefixedText
display=category
end
-- We can get away with using two arguments even when
-- options.showDisplay is false, as string.format ignores extra
-- arguments as long as there is an argument for each flag in the
-- format string.
returnformatString:format(pageName,display)
end
end
functionp._main(args)
localnLinks=getSequenceLength(args)
ifnLinks<1then
returnmakeWikitextError('at least one parameter required')
end
localmakeCategoryLink=newCategoryLinker{
project=args.project,
showPrefix=nLinks==1,
}
locallinks={}
fori,catinipairs(args)do
localsuccess,categoryLink=pcall(makeCategoryLink,cat)
ifsuccessthen
links[i]=categoryLink
else
-- If there was an error, then categoryLink is the error message.
returnmakeWikitextError(categoryLink)
end
end
localformatString
ifnLinks==1then
formatString='%s: %s'
else
formatString='%s the categories %s'
end
-- Don't output a comma before the "and" if we have only two links.
localconjunction
ifnLinks==2then
conjunction=' and '
else
conjunction=', and '
end
localhatnoteText=formatString:format(
args.LABELor'See also',
mw.text.listToText(links,', ',conjunction)
)
returnmHatnote._hatnote(hatnoteText,{selfref=true})
end
functionp.main(frame)
localargs=require('Module:Arguments').getArgs(frame,{
wrappers='Template:Category see also',
})
returnp._main(args)
end
returnp