Module:Buffer/doc
![]() | This is a documentation subpage for Module:Buffer. It may contain usage information, categories and other content that is not part of the original module page. |
Usage
This module was originally developed to optimize string concatenation in Module:Asbox but can be used in any module.
The interface for Module:Buffer objects is similar to that of mw.html
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 (though better to use
Buffer:_
since the concatenation operator is over 10 times slower). It can also be inserted into mw.html
objects via
mw.html:node
(though not mw.html:wikitext because of type checking). (See also #usage with string/mw.text libraries)
require('Module:Buffer')
require('Module:Buffer')
( ... )
require('Module:Buffer')
( _G, ... )
Use the module directly as a function to create a new Module:Buffer object (i.e. there is no 'main').
Some functions are available only if Module:Buffer is initialized with the global variable
_G
prior to the creation of any Buffer objects (see #advanced functions for more detail).
If initialized with _G then any
varargs
will be passed to
Buffer:_G
( \ )
. If initialized without then any varargs will by passed to
Buffer:_
( ... )
. You may also omit initialization and use any Buffer object function directly on the module—i.e
require('Module:Buffer'):function{...}
is equivalent to require('Module:Buffer')():function{...}
.
Saving the module locally, e.g. local Buffer =
require'Module:Buffer'
, though perfectly valid, is often unnecessary since all Buffer objects can create new buffers via
Buffer:_in
Buffer
Calling the Buffer object created by this module (as opposed to the module itself) is basically shorthand for
table.concat
, or with no args, ( Buffer, ... )
tostring
.
( Buffer )
However, if Buffer contains values inserted via
Buffer:_
that are raw (see section) or are out-of-sequence (i.e. numerical keys ignored by
ipairs
), then the result would be the same as ''empty-buffer'':_all
( Buffer )
( ... )
—which would iterate through all keys in numerical order, converting each item to a string before concatenating.
As a side note, if no args are passed , the the result of the concatenation are cached by the Buffer,
Buffer:_
Buffer:_( value, pos, raw )
Buffer:_( value, raw )
Inserts a value
if it is valid. The pos
argument is similar to that of
table.insert
, except in two cases: 1) the first
( table, pos, value )
valid values
===require('Module:Buffer')():_
Each time you call buffer()
it will return a new buffer object of type table, which you may save like so:
local buffer = require('Module:Buffer') local string1, string2 =
Buffer:_inHTML
advanced functions
usage with string/mw.text libraries
Example
For example, take the following snippet from the p.templatepage() function of Module:Asbox.
content = buffer(page.text ~= 'Stub' and require('Module:Asbox stubtree').subtree{args = {pagename = page.text}}) :_in'\n== About this template ==\nThis template is used to identify a':_(args.subject):_'stub':_(args.qualifier):_out' ' :_'. It uses {{[[Template:Asbox|asbox]]}}, which is a meta-template designed to ease the process of creating and maintaining stub templates.\n=== Usage ===\nTyping <code>{{' :_(page.text == 'Stub' and 'stub' or page.text) :_'}}</code> produces the message shown at the beginning, and adds the article to the following categor' :_(#stubCats > 1 and 'ies' or 'y') :_':\n'()
That snippet is basically equivalent to:
content = page.text ~= 'Stub' and require('Module:Asbox stubtree').subtree{args = {pagename = page.text}} or '' content = content .. '\n== About this template ==\nThis template is used to identify a' if args.subject then content = content .. ' ' .. args.subject end content = content .. ' stub' if args.qualifier then content = content .. ' ' .. args.qualifier end content = content .. '. It uses {{[[Template:Asbox|asbox]]}}, which is a meta-template designed to ease the process of creating and maintaining stub templates.\n=== Usage ===\nTyping <code>{{' .. (page.text == 'Stub' and 'stub' or page.text) .. '}}</code> produces the message shown at the beginning, and adds the article to the following categor' .. (#stubCats > 1 and 'ies' or 'y') .. ':\n'