Jump to content
Wikipedia The Free Encyclopedia

Module:Item

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
[画像:Edit-copy green with red question mark] This module's documentation is missing, inadequate, or does not accurately describe its functionality or the parameters in its code. Please help to expand and improve it. (February 2024)

This module implements the templates {{Item }}, {{Component }} and {{Format item }}.

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

 localp={}

 localfunctionescape(str)
 returnstr:gsub("[|\\]",function(c)returnstring.format("\\%03d",c:byte())end)
 end

 localfunctionunescape(str)
 returnstr:gsub("\\(%d%d%d)",function(d)returnstring.char(d)end)
 end

 -- Implements [[Template:Item]]
 functionp.pack(frame)
 localparent=frame:getParent()
 localresult=''
 forkey,valueinpairs(parent.args)do
 result=result.."|"..escape(tostring(key)).."|"..escape(value)
 end
 returnresult.."|";
 end

 localfunctionunpack(str)
 localresult={}
 forkey,valueinstr:gfind("|([^|]*)|([^|]*)")do
 result[unescape(key)]=unescape(value)
 end
 returnresult
 end

 -- Implements [[Template:Component]]
 functionp.component(frame)
 returnunpack(frame.args[1])[frame.args[2]]
 end

 localfunctiongetItems(frame)
 returnframe:getParent().args
 end

 localfunctioninvert(tbl)
 localresult={}
 forkey,valueinpairs(tbl)do
 result[value]=key
 end
 returnresult
 end

 -- Add args into item as appropriate (see [[Template:Format item]])
 localfunctionaddArgs(
 item,-- unpacked item to modify
 args,-- arguments for adding into item
 ignore,-- pass in invert{keys to ignore}
 shift-- for numbered arguments, args[key+shift] is assigned to item[key]
 -- returns: item
 )
 forkey,valueinpairs(args)do
 ifnotignore[key]then
 local_,_,paramKey=string.find(key,"^param (.*)")
 local_,_,importantKey=string.find(key,"^important (.*)")
 paramKey=paramKeyorimportantKeyorkey
 ifshiftandtype(paramKey)=="number"then
 paramKey=paramKey-shift
 ifparamKey<1thenparamKey=nilend
 end
 ifparamKeyand(importantKeyoritem[paramKey]==nil)then
 item[paramKey]=value
 end
 end
 end

 returnitem
 end

 -- Implements [[Template:Format item]]
 functionp.format(frame)
 localargs=frame:getParent().args
 localignore=invert{"template","item"}
 localtemplateArgs=addArgs(unpack(args.item),args,ignore)

 returnframe:expandTemplate{title=args.template,args=templateArgs}
 end

 -- See [[Template:Item#Format each item using a template]]
 functionp.each(frame)
 localargs=frame.args
 localitems=getItems(frame)
 localseparator=args[1]or""
 localprepend=args[2]or""
 localappend=args[3]or""
 localignore=invert{"template"}
 localshift=3

 localresult=""
 fori,iteminipairs(items)do
 localtemplateArgs=addArgs(unpack(item),args,ignore,shift)
 result=result..prepend..frame:expandTemplate{title=args.template,args=templateArgs}..append
 ifitems[i+1]then
 result=result..separator
 end
 end
 returnresult
 end

 -- See [[Template:Item#Gather given parameter from all items]]
 functionp.gather(frame)
 localargs=frame.args
 localitems=getItems(frame)
 localparameter=args.parameteror"1"

 localtemplateArgs={}
 fori,iteminipairs(items)do
 templateArgs[i]=unpack(item)[parameter]
 end

 returnframe:expandTemplate{title=args.template,args=templateArgs}
 end

 returnp

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