Module:Buffer/doc

This was originally developed to optimize string concatenation as a helper method within Module:Asbox, but has been generalized for all modules.

The interface for Module:Buffer objects is similar to that of objects in that you may build complex strings with independent child nodes. In most cases, you may use Buffer objects like a normal string, including using .. operator. See also:

Additionally, there are several specialized forms and extended objects, described further in their respective sections:


 * 
 * _G (callable)
 * _G (callable)
 * _G (callable)
 * _G (callable)
 * _G (callable)
 * _G (callable)
 * _G (callable)
 * }

Last but not least, this module has an ordered __pairs which can be more thorough than and. (Even reads nil keys!) The logical uniqueness of this iterator may be reason enough to Module:Buffer.

require'Module:Buffer'
Creates a new Module:Buffer object when the module returned by is called as a functioni.e., there is no 'main'.

Because are a pet peeve of this developer, this forwards arguments to ; if that op is not desired, you may chain a Buffer object function directly to the module and the self-action will be redirected to a new Buffer objecti.e require'Module:Buffer':_inHTML'table' is equivalent to require'Module:Buffer':_inHTML'table'.

The global variable is "magic" when passed as the first arg. Such enables the global functions and, if followed by a  string, will declare the new Buffer with that name in the global scope. If the argument following name is a table with no metatable, it and any other are forwarded to ; otherwise, as long as the first vararg is not nil or false, this passes them to. The _G passed may also gain a __call (details at _G object).

As a final note, if you text from a module which returns a Buffer object, it may be more efficient to create a new Buffer via chaining  after a require statement for the other module and use  at the point where you would append the required text. (Best add --:_in == indirect require Module:Buffer so future editors won't hunt for function ...:_in at the other module)

Buffer object
Get a Buffer as a via a function call on the Buffer object (as opposed to a call on the module). This is basically shorthand for, or, with no args,. However, if this Buffer is in raw mode or contains at least one  index, this reconstructs the Buffer by creating a new table, coercing it contents to strings and appending them sequentially to the temporary "buffer" via   (or a similar process).

Unconventionally, any -type position passed as  or   are treated as relative to length; that is,  is equivalent to  (which obviates the need to  Buffer just to use the ). Moreover, unlike table.concat, this automatically adjusts numerical positions to be within the range of the lowest and greatest indicies.

Note you may append a Buffer object without tostring coercion to an object via  (though not mw.html:wikitext because of type checking).

Buffer.last_concat
When strung without a (valid), the result is cached at. Until purged, future calls to return that Buffer as a string will return this index instead.

This should clear automatically whenever a Buffer object function changes the contents of a Buffer. You may manually purge the cache by setting this key to nil, as well as by passing nothing to.

Buffer:_
Appends a value to the Buffer. In rough terms, Buffer:_'string1':_'string2' is the same as. (It may help to imagine :_ as a .. that has stood up and is now casting a shadow.)

If passed an invalid value listed below, this is a no-op:
 * empty, or any table such that returns an empty string (or nil/false)
 * without a __tostring metamethod and which table[1] is nil or false
 * empty, or any table such that returns an empty string (or nil/false)
 * without a __tostring metamethod and which table[1] is nil or false

A table with no __tostring will pass through before insertion. An may be thrown if the table would cause table.concat to error. (Use instead for such tables.)

When passed pos of type, the argument is identical to pos for. In fact, assuming a valid value, is exactly the same as.

Just like with the position arguments of Buffer, any pos of type would be treated as relative to length.

Set raw to true to force append a value without tostring coercion, including invalid values. If given only two (non-self) arguments with the second being a boolean, then the second is read as raw instead.

Buffer:_nil
Removes the value buffered at pos. As with, a string pos string is treated as #Buffer + pos.

If a non-boolean replacement is provided, then it will replace the value indexed at pos. Passing a boolean as the second argument is a no-op.

When replacement is nil, the op is basically. As with the positional arguments of other Buffer methods, any numerical string pos is added to length, such that removes the last item. Note the only type check on replacement is a direct comparison to both booleans (nil is implied). Any other type, including strings, sets the Buffer to raw mode.

A pos that is omitted, nil, or false has the same effect as though  (or ) were passed. Given only one non-numerical argument which evaluates true but would return it as nil, this treats it as though it were passed as the second. If passed a non-numerical pos with any other argument, including nil, this is effectively a no op (though may still purge the cache).

Buffer:_all
Takes a table value, iterates through all number keys, appending each valid value to the end of the Buffer. In contrast to, this starts at the most negative key (down to ) and ends at the most positive index, continuing through any nil keys and includes non-integer number keys.

A table value that has no metatable will have its contents iterated by this function before moving on to the next value. All other data types are processed by.

By default, this ignores non-number keys unless nanKeys evaluates true. If so, non-number keys are processed after number keys. Keep in mind such keys are iterated in order, though an order may be imposed by wrapping each pair in a table indexed at a number key.

If given a  pair, defined as a number or number string indexed at a non-number key, then they will be passed as the value and pos arguments for. Thus,
 * {{luaself|:_all|args=({1,2,3,'... done',[3.5]=variable and 4 or {four='1',zero=1{{))}},true)}}

produces the same result as:

if variable then Buffer:_(4) else Buffer:_'four':_('zero',1)--vs :_all{four='1',zero=1}; less redundant would be end                          --   :_all{'four',zero=1}, but that doesn't demo string numbers Buffer:_'... done' --returns "1234... done" if variable evaluates true or "zero123four... done" if false|lang=lua}}
 * {{#tag:syntaxhighlight|Buffer:_(1):_(2):_(3)
 * }

If a non-number key points to a value that cannot be coerced into a {{luaref|tonumber|coerced into a number|y}} then the pair may be treated as, when functionName matches a Buffer object function and args is not boolean. If args is such that {{code|value[1]|lang=lua}} evaluates true, then this will pass the return of {{luaref|unpack|args=value, 1, table.maxn(value)}} to the named function; otherwise, the value is passed as is.

Buffer:_in
Passes any arguments to Module:Buffer to create a new Buffer object, sets an external reference to the parent Buffer and returns the child.

This does not append the child to the parent. (See )

Also, be aware that Buffer parent references are. Thus, if you were to (re-)set a local variable that is currently set to the parent, such could trigger immediate garbage collection on the parent.

Buffer:_out
Joins a Buffer with sep and appends result to its parent, which is returned. If no parent is found, this is a no-op and returns the same Buffer.

When given two or more arguments, this reads the first as opsthe number of :_out operations to perform. This applies the first sep in sep-list for the current Buffer, the second for its parent, the third for its grandparent, and so on.

If the last item is a, then any nil in sep-list default to ; any false mean "no-sep". The table may be the second arg (i.e. sep-list may be omitted). If it has other keys, then table[n] would apply instead of table[1], making these synonymous:

and.

The number 0 is "magic" when passed as the first arg (even by itself), joining and appending to the same Buffer after it has been. This is the only method by which a Buffer in raw mode may lose that status. Parent references are preserved.

Buffer:_str
Joins a Buffer with sep and returns the string.

This uses the same helper method as to handle multiple arguments, with which, if provided, this creates a new temporary Buffer to which this appends the results of the number of generations specified by , with each ancestor in front of its descendants. This then performs one additional concat op using the sep at ops + 1 and returns the result. If a parent-less Buffer is reached before ops, then the sep that follows its op number will separate the generations.

When no valid sep is given, this returns a string identical to what would append to the next ancestor, if those arguments were passed to Buffer:_out instead and one additional :_out op performed.

However, because this does not actually append child Buffers to their parent, the result may differ where this would insert the sep at ops + 1 and Buffer:_out would place the parent's sep between it and its child instead.

Buffer:_cc
Nils all keys of the table referenced by clear and unsets its metatable. If clear evaluates false, this simply purges the cache at.

If given a table to copy, this will duplicate all key-value pairs of copy into clear, cloning any table value recursively via Buffer:_cc(0, value). This returns the Buffer unless passed the number 0 as clear, which causes this to create a new table and return that instead. Passing true as copy is equivalent to passing the Buffer itself. If copy is not a table, then it will be set as the first item in clear as long as it is not false.

While this may resemble, there are several differences, namely that this:
 * Gives clear the same metatable as copy (or sets, if given) as opposed to a "clone" of the metatable.
 * Conserves attribute (though empty strings may replace some nil keys )
 * s values and iterates without invoking any __pairs.
 * Includes Buffer parent and raw attributes (stored externally)

To obtain the key-value pairs left as empty strings in the previous copy op in a table, simply call this again such with value such that  is true; call :getParent on this table returns clear (useful after leeaving the local scope which referenced clear).

Buffer:_parent
Resembling the reverse of, this calls on the Buffer's parent with the arguments given and appends the strung ancestor(s) to the current Buffer, which is returned.

The parent is unaffected by this operation and may still be retrieved via or re-appended again with this function.

Buffer:getParent
Returns parent Buffer, or, if none exists, creates a new Buffer and returns it as the adopted parent. As with, this does not automatically append the adoptive child to the new parent.

Pass a non-false  and this performs an op on the parent object.

If passed anything other than value (including nil), this requires that value names a function available to the parent object, which this calls and forwards the additional.

Pass only a table value which has no metatable and this forwards value to the parent which calls.

Given only a string starting with _ and naming a parent function, this calls it on the parent without arguments. Any other valid singular argument appends to the end of the parent Buffer.

Buffer:killParent
Unsets Buffer parent reference.

If passed any args, they are forwarded to the current parent, if one exists, via Buffer:getParent as a "parting gift". In either case, returns the current Buffer.

This is not necessary for since Buffer parent references are weak. Rather, use this when it is desirable to assign a new parent via Buffer:getParent or, for example, to prevent the inclusion of an ancestor when passing as ops for functions such as  (more useful when recycling Module:Buffer from another Module).

Buffer:stream
Switches a Buffer to stream mode. While streaming, the __call metamethod will append values to the end of the Buffer instead of the usual op.

Aside from that, there is only one other function:. Any args passed to Buffer:stream are forwarded to that function for a reason that should be evident when you finish reading this very short section.

No special action is needed to exit this mode. The normal call to string op is restored upon the use of any regular Buffer function or any operation which coerces the Buffer into a string (e.g. the  operator).

Stream-Buffer object
Stream-Buffer objects accept only one argument which they append if valid. That is, the op is a streamlined version of sans the pos and raw args.

This also exploits to append a series of string literals (and tables) with nothing between them (or only  if desired).

For example, both A and B will produce identical strings:


 * }
 * }

Stream-Buffer:each
Appends an undetermined number of valid values to the Buffer object.

If the above line gives you Déjà vu, that is because it is drawn from. However, unlike mw.html:wikitext, this does not stop at the first nil value.

HTML library extension
Upon the first call to, Module:Buffer clones the , adding Module:Buffer's __eq and __concat metamethods along with a few additional functions.

Objects with this modified metatable are referred to as Buffer-HTML objects. Yet, though dressed in bells and whistles, they are only named with Buffer as an adverb since they lack most Buffer object functions.

In contrast, the Element-Buffer (returned by the function call on a Buffer-HTML object) is a true Buffer object with specialized "tricks" allowing complex structures to be built via both mw.html and Buffer object methods as well as through a builder that perhaps marries the best of both worlds.

Buffer:_inHTML
Accepts the same parameters as to create and return a modified mw.html object. As with, this does not append the child object to the parent Buffer and instead sets a Buffer-style parent reference.

An exception to the above is when chaining this to an Element-Buffer and such produces a selfClosing tag; when both conditions are met, this appends the tag and returns to the same Buffer.

Unlike mw.html.create, if args has keys other than  and , it will pass for further processing through  (a cousin of  that handles mw.html functions). Moreover, if passed a table where mw.html.create expects tagName, this treats it as args instead.

Finally, this does not automatically set the  reference, making this an alternative to mw.html:tag

Buffer:getHTML
Available only after is used the first time.

Accepts the same arguments as, however this instead return the last Buffer-HTML object ("lastHTML") created, or, if available, the lastHTML passed to any of the following functions:
 * (except when passed 0)
 * (except when passed 0)
 * (except when passed 0)

Buffer:_html
Available only after is used the first time.

This (re-)appends the last Buffer-HTML object to the current Buffer object. The  and   args are generally the same as those in.

When called with no arguments on an Element-Buffer, this appends lastHTML without string coercion. Be warned however that if the Element-Buffer belongs to lastHTML or one of its tags, such will cause an infinite loop, which can be avoided by passing an explicit nil to append lastHTML as a string.

HTML object functions
Buffer-HTML objects may be used like any mw.html object. (In fact, merely replacing mw.html.create with require'Module:Buffer':_inHTML in an existing Module should produce the same output.)

Most mw.html functions are unchanged, except, , and are embedded in a wrapper function that checks whether they return a normal mw.html object. If so, switches the metatable to convert it to a Buffer-HTML object and sets a parent reference.

As a side bonus, the .. may be used on Buffer-mw.html objects directly (no needed).

Buffer-HTML object
Call this object as a function to return its .nodes index, which this converts to an object, granting it the same metatable as regular Module:Buffer objects (as well as several additional "tricks") and assigning the Buffer-HTML as its parent Buffer.

This takes one argument which is forwarded to its Element-Buffer. Tables pass to the specialized HTML builder. All other values are appended to its Element-Buffer via.

You cannot chain call regular Buffer functions on a Buffer-HTML object; however, since mw.html functions cannot read Buffer-style parent references, modified versions of methods that return the parent Buffer are available to Buffer-HTML without having to call into the Element-Buffer. For convenience, is called automatically prior to the op, though after the   reference has been set for.


 * 
 * getParent
 * killParent
 * _out
 * _str
 * _parent
 * _parent


 * }

In addition to the above, global functions may be available to Buffer-HTML if enabled; these functions are the same for all Module:Buffer objectsi.e. the self action is never redirected.

Element-Buffer object
Sharing the same metatable as with regular Buffer objects, Element-Buffers concatenate the same way when called to produce a string analogous to the JavaScript DOM "innerHTML" property. In other words, when strung, it is generally the contents of the Buffer-HTML object without the "outerHTML" or tag.

There are exceptions to this "innerHTML" behavior. For instance, as appended to another object via, an Element-Buffer and its Buffer-HTML are interchangeable (though appending the former via only includes the inner result).

Also, using the concatenation operator .. on an Element-Buffer includes its tag in a manner depending on if it is selfClosing:
 * For most tags, the conjoined string is placed inside the tag, e.g. Buffer:_inHTML'p' 'inner text' .. 1 returns ' inner text1 '.
 * For selfClosers, the .. op redirects to its Buffer-HTML, e.g. insert _add{selfClosing=true} in the above example before .. 1 to produce '1'.

You may use most Buffer object functions normally, however if there is a Buffer-HTML version, it instead behaves as though chained on the outer HTML object. You may also chain any mw.html object function. Unless otherwise indicated, such returns a wrapper method that merely redirects the self-action to the outside Buffer-HTML.

As a final note, Element-Buffers are in permanent raw mode since it is expected that some mw.html method (e.g. :tag and :node) may or will append non-string elements.

Element-Buffer:done
When passed nothing, this should behave just like as called the "outer" HTML objectreturning , if available, or Buffer-HTML if not.

However, this has been re-designed to accept ops, the number of :done operations to perform. Thus, Element–Buffer:done(4) is equivalent to Buffer–HTML:done:done:done:done.

Pass 0 (zero) as dones to return to the Element-Buffer's direct HTML container.

Finally, keep in mind that Buffer-HTML objects use the original mw.html:done (albeit in a light wrapper).

Element-Buffer:tag
This uses the same helper method as to handle arguments and produce new Buffer-HTML objects, selectively passing args to  when it contains keys not used by.

As may be expected, this differs from Buffer:_inHTML in that this actually appends the tag and will set a mw.html-style parent reference. This also lacks the other function's "auto-done" feature for selfClosing tags.

As with the other Element-Buffer remake of an mw.html method, the features described here do not apply to the version used by Buffer-HTML objects.

Element-Buffer:_add
Takes a table as its only argument. This then thoroughly iterates all number keys from lowest† to highest using this module's custom __pairs method. Most values append as wikitext if valid. If a table is indexed at a number key, this recursively iterates the table before moving on to the next key.

After processing all number key-value pairs, this then iterates the other (non-number) keys. For those naming a core Buffer object function, this selectively unpacks args in a manner described at when that function is passed the nanKey parameter (excepting that this does not read numbers as pos, i.e. treats them the same way as strings).

This also accepts keys naming HTML and global functions as well as mw.html arguments. Thus, Element–Buffer:_add{ tag = 'br', 'text'} appends a BR tag after the text and Element–Buffer:_add{ {tag = 'br' }, 'text'} appends the BR before the text. Note however that how this handles args for such keys depends on the particular function or argument named:



args.argName
Element–Buffer:_add{ arg = value }

The effect of passing args with keys such as  and   is the same as though args were passed to. This also takes one additional arg, i.e., which value replaces the original tagName of the HTML object (or, if false, removes the tag).

Note that these are the only keys for which a boolean arg would result in an op. (For Buffer object functions that do not no-op when passed only a boolean, place the boolean in an args table for unpacking.)

args.cssName
Element–Buffer:_add{ cssName = cssValue }

A non-number key and value pair may default as the  and   parameters for  when the key matches none of the three argName keys nor the name of any available function for Buffer and mw.html objects.

This sends non-boolean cssValue though prior to forwarding it to mw.html:css. Because this is the default, any typoed key goes to mw.html:css as cssName. Names of functions not yet loaded also end up there.

For convenience, any  character in the key string is automatically substituted with the   character; thus border_bottom_style = is equivalent to ['border-bottom-style'] =.

The form Element-Buffer:_add{ css = { cssName = cssValue } } also works (or to clear a previously set value; see example at args.htmlFunction for more details).

args.tag†
Element–Buffer:_add{ tag = tagName }

Element–Buffer:_add{ tag = { tagName, args–list } }

Set the key  to a string and this calls  with it as the tagName argument. This then raw inserts the returned mw.html object ("tag"), emulating the effect of minus parent references, which are unnecessary.

Pair the args.tag key with a table value and this calls mw.html.create with table[1] as tagName (or nil if ), appending it as described above for string values, but also pointing  to the Element-Buffer's parent as well as temporarily setting the tag as the parent Buffer of. This then treats tag.nodes as a pseudo-Element-Buffer, recursing tag.nodes as "self" and the table as args, though only iterating keys not equal to 1 (or less).[*]

Note this appends normal mw.html objects. That said, most Buffer functions named in args-list should still work as though the tag and  were Buffer objects.

Upon completing a recursive iteration for args.tag, this checks if the tag is selfClosing, in which case, this sets tag.nodes to nil. Likewise, if its tagName property evaluates false, this nils tag.styles and tag.attributes. Such presumes those properties will not be modified afterwards, so use mw.html:tag outside of Element-Buffer:_add if such is not the case.

args.done†
Element–Buffer:_add{ done = wikitext }

Element–Buffer:_add{ done = { ops, args–list } }

Similar to args.tag, this treats the first index of the table as the ops argument of. After calling that function, this then iterates all subsequent keys in a recursive call on the Element-Buffer of the Buffer-HTML object returned.

args.allDone
Element–Buffer:_add{ allDone = wikitext }

Element–Buffer:_add{ allDone = { args–list } }

Similar to the previous two, except that the first index is not used as an argument; thus, the entire table is iterated.

args.globalFunction‡
Element–Buffer:_add{ globalFunction = name }

Element–Buffer:_add{ globalFunction = { name, save, args–list } }

Element–Buffer:_add{ _B = { var, args–list } }

If the global functions have been loaded and a key matches one, this calls the function with the first two arguments ed from the paired args-list table. This then recursively iterates the list, excluding keys less than or equal to 2, with whatever object is returned as self. However, if the returned object has a metatable and  exists, the self will be object.nodes instead.

Because takes only one argument, args._B only unpacks the first index and starts the iteration after that key.

If neither of the first two keys evaluate true, this assumes the paired value is a string for use as the name argument for the function matching its key. In that case, the current call stack's self (an Element-Buffer or tag.nodes if this was called indirectly) is ''self' for the global function.

args.htmlFunction
Element–Buffer:_add{ htmlFunction = object }

Element–Buffer:_add{ htmlFunction = { arg-list, name = value } }

If args.key matches an that does not have its own args section, this checks if the associated object is table. If not a table, or if  evaluates true, this calls the mw.html function with the object as the only argument.

For table objects without an object.nodes, this iterates the table (non-recursively), repeatedly calling the named mw.html function with one or two arguments depending on key's type in each loop. Non-number key-value pairs are both passed as arguments. For numeric indices, only the value is passed. Boolean values are a no-op.

Unlike with most implementions of Module:Buffer's __pairs, this first loops through non-number keys, followed by number keys (still ordered from lowest to highest). Thus, something like Element–Buffer:_add{ attr = { 'width', width = 20 } } is equivalent to Element–Buffer:attr( 'width', 20 ):attr( 'width' ), setting then unsetting the width attribute and returning the Element-Buffer for a net no-op (though the practical purpose of such is a mystery for this developer).

For  only, this auto-replaces underscores with hypens for string keysi.e., you may save four keystrokes/pair by typing   instead of. This does not apply to strings indexed at number keys.

{{note label|skip1|†|| Unlike with tables passed directly, recursive iterations for functions marked with {{sup|†}} start at the smallest number greater than {{code|lang=lua|1}} instead of negative infinity.

{{note label|skip2|‡|| The iteration may start after {{code|lang=lua|2}} for some global functions.}}
 * }

Loadable convenience extensions
The methods here are loaded on demand or depend on subroutines which need initialization.

These methods can greatly simplify the structure of the modules which employ them by doing, in a fluent manner, many tasks which may otherwise force an awkward interruption in Buffer call chains.

Global functions
Methods such as and  traverse a node tree in only one direction. While fine for returning to an ancestor, they do not provide navigation to a non-ancestor (often necessary for templates with co-dependent parameters). Yet, repeated breaks in call chains to set local variables for several nodes of the same branch can look choppy if not confusing for nodes many generations removed from its declaration statement.

Templates with several conditionally-appended nodes with similar, but not identical, parts may present another conundrum for coders who must decide between having awkward call chain interruptions to store potentially repeated components as local variables or constructing a somewhat redundant module that is more susceptible to maintenance errors by future editors who may patch one code segment but miss the sibling buried within a convoluted nesting of.

This module's global functions and added syntactic sugar for the _G object were formulated to simplify such node trees with multi-conditional or repeating structures by providing concise in-chain variable declaration. The extension is enabled by passing your global table to the moduleeither in the initial call to require'Module:Buffer' (more instructions in that section) or to which forwards arguments to Module:Buffer.

Buffer:_G
Pass  and   to assign the object passed as save to a global variable via.

Pass only name and this substitutes self for save to assign the Buffer object to  instead. Give an explicit nil as save to unset a global. This returns the Buffer object as well as any argument given after name.

This is a no-op when name is nil or a boolean, or, when save (eventually) evaluates true and also returns true.

If the named global already exists, this "backs up" the old value by moving it to the meta __index of the global table, setting a new metatable if none exists. Retrieving the old value requires unsetting the new one via (more details in that section). If overwritten a third time, the first value is discarded and the second takes its place in the back up.

If a metaglobal variable exists but the global is nil, this sets the global without unsetting the metaglobal (i.e. does not back up a nil global). An exception is when this is given an explicit nil as save and only the metaglobal exists; thus, passing nil twice for the same name, unsets the key in both the global table and its metaindex.

Buffer:_R
This with the global table as the first argument and   and   as the second and third, respectively, returning the Buffer object for call chaining. This is a no-op if name is nil or a boolean.

Note that Buffer methods use a local variable  as a proxy for the global table _G; though not a global index, the string 'new_G' is a "magic word" that changes the destination for future save for this and Buffer:_G.

Pass a table as  (same place as save) to set as the new new_G. Any table such that var._G == _G is treated as a (former) new_G object. This of former proxies and  with the  __call method on non-new_G tables. Then, this, if third parameter  equals:
 * nil backs up the current proxy as the metaindex of the next (though this no-ops if var equals new_G to avoid cyclical indexing).
 * false leaves the metaindex intact (replacing the current proxy without back-up)
 * true unsets the metaindex of the next proxy
 * any other value sets that value as the metaindex of the next proxy. (Note new_G._G is not set until it is returned by )

To omit or to pass nil/false as var has the same effect as. Pass true instead and this treats it as though passed as metaindex, creating a new proxy without backing up the incumbent.

Buffer:_2
This returns the value indexed at key  in the global table. If it does not exist, this forwards both arguments to and returns the saved value (which may be itself).

In other words, is roughly equivalent to _G[name] = _G[name] or save or save==nil and Buffer.

The string 'new_G' will return the Module:Buffer local variable, used as a proxy for the global variable _G. Given more than one argument, this forwards arguments to to assign another proxy global before returning the (newly-deposed) proxy. This then sets  to the original _G object for call chaining. (See &sect; chain call in _G object).

Buffer:_B
Takes only one argument and returns that argument.

Assuming the only X declared is and new_G equals _G, then  and  are equivalent.

When passed a variable that does not exist, this returns the Buffer nil object:

Buffer-nil object
The Buffer-nil object is unique. Calling this as a function returns nothing (in contrast, calling an empty Buffer object returns an empty string). This does however have the Module:Buffer __concat metamethod, which treats this the same way as any invalid object (i.e. ignores it).

Appending this via or  has the same effect as appending nil. Passing this to returns nil instead of the string 'nil'.

The only real Buffer method in its meta __index is, however, any non-numerical key string retrieves a function that only returns the Buffer nil object for call chaining. In a sense, you can think of Buffer:_B(var):... as an if var~=nil then var:... block around the following chain that ends in the next :_B.

If, the clone will be a normal Buffer object.

_G object
The first _G variable passed to this module is given a __call metamethod that self-s and returns in a manner that depends on whether it was called directly or from a chain. Saving a new_G object globally via a chain call can prevent conflict. The follow example has a hypothetical "Module:X" that may overwrite globals declared by your module or unwittingly discard your new_G when it passes _G to Module:Buffer (passing _G to this module resets new_G to the global table even when the global functions are already enabled):


 * } This module conserves any pre-existing metatable and alters no metamethod other than __call.
 * } This module conserves any pre-existing metatable and alters no metamethod other than __call.





direct call
When called, the _G object self-sets any string passed as  with whatever is passed as. This returns v, or nil if omitted (unlike with rawset, an explicit nil is not necessary to unset a variable with direct calls).

Note that k must be a string to declare or unset a global in this op. Tables passed as the first argument are treated as though this were executed via a call chain (discussed shortly). Passing k which is not one of those two types will throw an error.

chain call
When used in a call chain, this rawsets the key-value pair in the chained object and returns that object. The _G object may chain itself when returning _G is desired for another op instead of v.

In contrast to the direct op, the in-chain op will index non-string k values. Moreover, this only unsets object[k] when passed an explicitly nil v.

If v is omitted in-chain, this uses the chained object as the missing argument; thus, (chained)  has identical effect and return to.
 * }

The same __call method is given to new_G objects created by Buffer:_R, however the direct call only works if its metaindex is the _G object. Any table such that  points to the _G object may chain save to itself regardless of metaindex.

Though the behavior of the chain op when v is omitted may be a dead ringer to that of Buffer:_G when save is omitted and new_G is the chained object, mind that the Buffer object function sets keys in new_G variable rather than the chained (Buffer) object; in other words, this is unaffected by Buffer:_R reassigning new_G to another table. Also, this does not have the back up behavior of Buffer:_G.

Buffer:_var
Raw appends a Buffer-variable object, which may appear as a different value each time the object (or its container) is converted to a string.

Initialize a Buffer-variable object by passing as  a:
 * number - which, when strung the first time, appears as that number and reappears as  the next time it is strung.
 * string - that transforms into the next ASCII character via.
 * table - to return the first (non-nil) item, then the second, and so on as determined by, looping back to the first item after reaching the last. (Note the change argument does not apply to table-based Buffer-variables.)
 * custom function - to be set as the _build and __tostring method of a variable-object, though instructions for coding such functions are beyond the scope of this manual.

Re-append the same variable object by passing true as the only argument. For non-table-based variables, you may specify change to append a sister object which transforms the value at the rate specified. Changes are cumulative. Thus, if the original is re-strung after a sister, its value will differ from that of its last appearance by the sum of the original and sister rates and vice versa.

Apply a change without appending a new variable object to the Buffer by passing false. The shift is effective immediately and may affect previously appended variable objects not yet finalized. Pass only false (i.e., omit change) to produce the same effect as stringing the original once. Note that the false-change is the only change table-based Buffer variables will honor.

Pass nothing to append a version which simply repeats the result of the last stringing. While generally identical in effect to the object generated by, the Buffer-variable will return nothing if strung before any of its sisters.

If passed an explicit nil as the first argument, this is no-op. If passed a boolean before any Buffer-variable has been initialized, this is also a no-op. Note that any op disables future caching at for all Buffer objects in your module (and in any module which may require it).

Basic usage
Buffer:functionName( ... )

You may directly chain any function from the following libraries on Buffer objects:


 * • 3
 * }

Functions from these libraries added to the Module:Buffer metatable on-demand and placed within a wrapper method that strings the Buffer object for the first argument and then forwards the remaining arguments.

Thus, the following are equivalent: Buffer:nowiki( ... ) and

If a name exists in both the string and mw.ustring libraries, the string version takes precedence. You may prefix the letter u on any mw.ustring functione.g. Buffer:ulen returns the number of unicode characters and Buffer:len returns the number of bytes.

Buffer:gsub and Buffer:ugsub have a slightly different wrapper which substitutes the  argument of  and  when it evaluates false or is omitted with an empty string (otherwise the originals would throw an error). This saves a few keystrokes when removing characters via Buffer:gsub'[pattern]' as opposed to Buffer:gsub( '[pattern]', '' ). All other arguments are handled the same as with the other on-demand methods.

Library functions which take a non-string as the first argument are not supported.

Empty Buffer interface
To obtain the first return value as a Buffer object (as opposed to whatever type the original normally returns), simply chain the imported method immediately after creating the new Buffer via Buffer:_in. Only empty Buffer objects which have a parent object will append the result of their parent in this manner.

This syntactic sugar allows two things:
 * For appending additional objects after the op via Buffer object methods.
 * For chaining multiple methods not chainable to stringse.g., this: Buffer:_in:uformat( ... ):_in:toNFD:encode'[<>#]':match'^(.-)==='
 * vs. the following which has the same order of operations albeit harder to see: mw.text.encode( mw.ustring.toNFD( Buffer:uformat( ... ) ), '[<>#]' ):match'^(.-)==='





Special case: Element-Buffer
empty–Element–Buffer:functionName( ... )

Element–Buffer:_in:functionName( ... )

The 'empty' behavior is different when chained to empty Element-Buffer or an empty child Buffer of an Element-Buffer.

Library methods chained to an empty Buffer which parent is an Element-Buffer object will instead string the grandparent Buffer-HTML object for use as the first argument before appending the result to the new Buffer. This interface is provided because Buffer-HTML objects, which are not true Buffer objects, are unable to load these functions, making this the only chainable option for Scribunto methods that includes the outer tag of non-empty Element-Buffers.

Chained on an empty Element-Buffer, these methods will string the Buffer object which created its HTML tree via and append the result to the Element-Buffer.
 * }

Modified .. operator
All "true" Buffer objectse.g., the regular, stream, and element varietiesshare the same. Some Buffer-like classes, namely s and the, also have this same metamethod.

The extended operator does not append to Buffer objects. In other words, Buffers generally remain the same as before the op excepting those effects that apply whenever Buffers are strung (See, , and ).

with non-tables
Buffer .. value

value .. Buffer

Any non-table  may be joined a Buffer object with the concatenation operator .. without error.

With the exception of Element-Buffers (which are a special case), the op passes each object, ordered left-to-right, to which inserts validated items in a new table, which this returns through.

Concatenating an invalid value and a Buffer has generally the same effect as unless such involves:
 * the Buffer-nil object which produces an empty string (instead of nil)
 * Element-Buffer objects which returns the string of the parent Buffer-HTML

with tables
Buffer .. table

table .. Buffer

The same general operation applies for tables as with non-tablesi.e., validated values are inserted left-to-right into a new table to be joined by table.concat. In fact, tables which have a metatable (including Buffer objects which are not an Element-Buffer) are forwarded to Buffer:_ and processed the same way as non-tables.

Given a  for which  returns nil or false, this instead forwards the table to, which iterates every value indexed at a number key in sequential order, inserting those which are valid in the new table.

As a reminder, Buffer:_ validates tables with metatables that lack a __tostring method through table.concat, which throws an error on sequences containing one or more value that is neither a string nor a number. Such accounts for nearly all cases of breaking errors involving this op.

Note that the valKey parameter of Buffer:_all is not triggered.

for Element-Buffers
Element-Buffer .. value

value .. Element-Buffer

Element-Buffer .. Element-Buffer

To recap and expand upon &sect; Element-Buffer-object, the behavior of this op depends on whether its parent Buffer-HTML is or if the other value is also an Element-Buffer. Also, the final result always includes the outer HTML object (i.e., the tag) in some manner.

For Element-Buffers of "open" tags, this op creates a table with a metaindex that references the parent of the Element-Buffer. The table is then effectively a "mirror" of the parent Buffer-HTML object except that it contains an empty table at  the index of the Element-Buffer within its parent. This then populates the mirror's inner table with the string of the Element-Buffer and the other value, validated left-to-right, in a manner not unlike what this does with the temporary table it creates when concatenating non-element Buffers to another value. This then returns the mirror table through the __tostring metamethod of the mw.html library, yielding a string which resembles that of the parent tag but with value inserted in front of or behind the original inner text depending on whether value was to the left or right of the   operator, respectively.

When the selfClosing property of the parent evaluates true, this operates on the parent instead of the Element-Bufferi.e., the resulting string will have value on the outside as opposed to within the tag (placing it inside would be pointless since selfClosing tags do not show inner contents).

If both operated objects are Element-Buffers, this mirrors the parent of the first. The inner table of the mirror is then populated by inserting the string of the first Element-Buffer followed by the string the parent Buffer-HMTL of the second. The resulting string would be as though the parent of the second were the last node of the first parent. Note that this Element-to-Element rule does not apply when the first Buffer belongs to a selfClosing tag (in which case, this behaves as though the selfClosing parent were to the left of the operator, returning a string with the selfClosing tag inside the tag of the second Element-Buffer in front the latter's inner contents.)

Finally, this combines an Element-Buffer and a table value which has no metatable by passing the table as args for with the mirror of the Element-Buffer as the "self". This avoids permanently changing the parent Buffer-HTML by setting a new table at  or   in the mirror the first time methods such as, , , etc. attempt to access those tables, copying the original's via the recursive form of. Note however that permanent changes may be made to other objects whenever methods such as via or  are keyed to navigate beyond the mirror or "sandbox".

require'Module:Buffer'.__pairs
require'Module:Buffer'.__pairs( table, flag, ext )

Returns two values: an iterator function and the. This is intended for use in the.

One distinctive feature of this pairs method is that it splits keys into two groups: s and non-numbers. This indexes each group of keys in its own "map" object, traversed by its own iterator functioni.e, iterating both sets of keys requires two separate for loops. Numeric keys are served in an orderly fashion as with except that those which are negative, non-consecutive, and non-integer may be included. Moreover, this can find some keys paired with explicitly nil values.

The  argument selects the iterator method returned for that loop. When flag is an explicit nil or omitted, this returns an iterator for number keys. If given any non-nil flag (i.e., false or any value that evaluates true), this returns a method for looping non-numeric keys. Because both sets are mapped at the same time, you may avoid a redundant mapping op in a subsequent loop by passing an explicit nil or false as flagi.e., omitting flag or passing true indicate that re-mapping is desired.

This automatically selects certain tables for "mapless" iteration. Typically, mapless differs from mapped only in that it uses fewer server resources, though, as explained in the next section on mapping, it may "miss" keys in some cases.

Mapping behavior may be modified or extended by. To disable mapless iteration for the table, you may pass false as ext. If not nil or false, ext must be a pairs method that takes the table as its only argument and returns a function that may iterate its keys for mapping purposes. Note that re-mapping avoidance via flag does not apply if ext is explicitly given, though a nil ext does not disqualify a table from mapless iteration.

Mapping process
Tables are mapped in two stages.

The initial stage is a which inserts integers between 1 and #table in the number key map. Because nothing is checked in this step, this may map keys which the numeric map iterator would pair with nil values or with values from the table's.

The second stage explores the table's keys with an and  as the default expression-list, or, if ext evaluates true, the expression returned by ext( table ). This ignores keys already mapped in the first stage and checks if any unmapped key is a number before indexing it in the appropriate map group. Upon completion, if any new number key were found in the second stage, this runs the numeric map through. No order is imposed on the non-numeric map.

Alternatively, a table may qualify for "mapless" iteration if is not nil, and  returns nil. If either flag or ext are not nil, or if the table was previously mapped, such permanently disqualifies a table for mapless processing.

As a side note, if mapless numeric iteration occurs, this returns iterator, table, nil. In other words, you may use to confirm that the table qualifies for mapless iteration when it has a third explicit return (for debugging).

Iterators
One of four functions may be provided in the  returned by this pairs method, depending on which group of keys (numeric or non-numeric) and which iteration process (map-based or mapless) is indicated.

When  is nil or unspecified, map iterators will return the key object referenced by the first index of the relevant map along with the value it indexes. If passed the first mapped key, these iterators then return the second index mapped, which if passed in turn may retrieve the third and so on until the last mapped key has been served.

For numeric iteration, the mapless method returns 1, table[1] when key is unspecified. If a key is given, it returns key + 1, table[ key + 1 ] unless key is greater or equal to the length of the table, upon which it returns nil. For non-numeric keys, the mapless "iterator" is actually a no-op (empty) function which takes nothing, does nothing, and returns nothingprovided only to prevent an error when the for loop expects a function.

As mentioned (using different words), key-value pairs are served independently of whether or not table[key] exists and retrieved without using.

For example, take a look at table x as declared in the following statement:. Table x has a length equal to 8. With ipairs, the for loop stops after the first pair. In contrast, this module's __pairs method will loop all 8 keys declaredi.e., (1, 1), (2, nil), ... (7, nil), (8, 8). That said, this only iterates two keys if table x were declared as { 1, [8] = 8} instead even though such is indistinguishable to Finally, the loop would continue to include any keys set to nil after the mapping process.

You may assign these iterators to a local variable to use them directly. If an unmapped table is given to a map iterator, it will forward the table to this pairs method for immediate mapping. Though no map table is produced for the mapless iteration, the pairs method does cache the length of the table at a map reference, which the iterator compares against key to determine when to stop. Unlike the map methods, the mapless iterator does not call the pairs method when such has been bypassed and instead compares key to the value returned by the, which may be unstable if the loop includes code that sets or unsets indicies within the table. Also, the mapless method will throw an error if given a table that has been mapped (when it attempts to compare key to a map object).

Tips and style recommendations

 * If joining Buffer with a string immediately after, place a space between 'string' and the separator and use double/single quote marks to . (i.e.   instead of   or  )
 * Saving Module:Buffer locally, e.g., though fine, is often unnecessary since all Buffer objects can create new buffers via.

For 


 * Treat :_ as though it were a .. op. Wrapping strings with unnecessary is akin to ( 'string1' ) .. ( 'string2' ) .. ( 'string3' ).
 * To insert an empty string as a placeholder for a separator without setting raw, pass a table containing only an empty string, like so:.
 * Raw appending a non-table is pointless since no other Scribunto type can tostring differently afterwards. However, this developer believes you are smart enough that raw and type(v)=='table' is a waste of server resources. (Such checks are why takes twice as much time to append a list of strings as  despite their near-identical roles in an Element-Stream-Buffer).

For and 
 * Something like Buffer_str(2, false, A, false, {C, B}) will use variable  as the parent's separator, or   instead if A is nil, or   if both A and B are nil.

For 


 * Appending values in multiple locations is one of the primary reasons why the nanKeys argument exists. While passing a boolean directly will cause an error, you can do something like...
 * this: {{code|1=Buffer:_all({condition and {_nil={'0', 'replacement'},Front=1,getParent='from child'{{))}}}, true)|lang=lua}}
 * versus: Buffer:_nil('0', condition and 'replacement' or false):_(condition and 'Front', 1):getParent(condition and 'from child'):_B(child).

For 


 * If the table reference passed as clear was appended raw in multiple positions, this is akin to performing at all positions simultaneously. (May be easier than trying to come up with a  pattern)
 * Inserting a named empty table is raw as a placeholder to be populated later via this function may be easier than calculating pos argument of.