close
Jump to content

Module:Module wikitext/sandbox

From Wikipedia, the free encyclopedia
--- 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