Module:Module wikitext/sandbox
Appearance
| This is the module sandbox page for Module:Module wikitext (diff). |
| Any breaking changes to this template, including moving it or nominating it for deletion, must be communicated in advance to Twinkle's users and maintainers at Wikipedia talk:Twinkle. The standard installation of Twinkle relies on this template. Thank you. |
Usage
[edit]This is an auxiliary module used to allow module pages to display wikitext. Use it by setting this module's text value to whatever content you want to display. The _addText helper method may be useful. For example, to tag a module with {{db-g7}}, put require('Module:Module wikitext')._addText('{{db-g7}}') at the top of it.
--- This module provides functions related to adding wikitext to
-- module pages and transcluding wikitext.
-- @module module_wikitext
-- @alias p
-- @release stable
local p = {}
p.text = ''
--- Fetches the current module wikitext value
--
-- @return The current module wikitext
function p.main()
return p.text
end
--- Adds module wikitext.
--
-- @param {string} text Module wikitext to add
-- @param preprocessFrame frame to use or false to skip preprocessing
-- @usage require("Module:Module wikitext")._addText(text)
function p._addText(text, preprocessFrame)
if preprocessFrame ~= false then
text = (preprocessFrame or mw.getCurrentFrame()):preprocess(text)
end
p.text = p.text .. text
return p
end
function p:addText(text, preprocessFrame)
if preprocessFrame ~= false then
text = (preprocessFrame or mw.getCurrentFrame()):preprocess(text)
end
self.text = self.text .. text
return self
end
--- Adds a new line
function p._newLine()
p.text = p.text .. '\n\n'
return p
end
function p:newLine()
self.text = self.text .. '\n\n'
return self
end
--- Package data for wrapping
--
-- @table p.packageData
-- @field package Package to use
-- @field prepend Text to prepend to string outputs
-- @field append Text to append to string outputs
p.packageData = {}
--- Wrapper function for packages using module wikitext
--
-- @param {table} package Module package
-- @param {string} prepend Text to prepend instead of p.text
-- @param {string} append Text to append
-- @return Metatable that invokes package wrapper and wraps metatable
function p.wrapper(package, prepend, append)
p.packageData = {
package = package,
prepend = prepend or p.text or '',
append = append or ''
}
return setmetatable({}, {
__index = function( _, key )
mw.logObject(p.packageData)
if type(p.packageData.package[key]) == 'function' then
-- This passthrough function adds the module wikitext
-- to any string output, necessary for stuff like TfD
return function(...)
local output = p.packageData.package[key](...)
if type(output) == 'string' then
return p.packageData.prepend .. output .. p.packageData.append
end
return output
end
end
return p.packageData.package[key]
end
})
end
return p