Aller au contenu

Module:ugly hacks

Avy amin'i Wikibolana — Rakibolana malagasy malalaka

La documentation pour ce module peut être créée à Module:ugly hacks/doc

local m_str_utils = require("Module:string utilities")

local gsplit = m_str_utils.gsplit
local ufind = m_str_utils.find
local ugsub = m_str_utils.gsub
local umatch = m_str_utils.match
local usub = m_str_utils.sub

local export = {}

function export.explode(frame)
	local wanted_index = tonumber(frame.args[3])
 
	local count = 1
	for item in gsplit(frame.args[1], frame.args[2], true) do
		if count == wanted_index then
			return item
		end
		count = count + 1	
	end
	
	return ""
end

function export.substr(frame)
	return usub(frame.args[1] or "", tonumber(frame.args[2]) or 1, tonumber(frame.args[3]) or -1)
end

function export.find(frame)
	return ufind(frame.args[1] or "", frame.args[2] or "", 1, true) or ""
end

function export.find_pattern(frame)
	return ufind(frame.args[1] or "", frame.args[2] or "", 1, false) or ""
end

function export.replace(frame)
	return (ugsub(frame.args[1] or "", frame.args[2] or "", frame.args[3] or ""))
end

function export.match(frame)
	return (umatch(frame.args[1] or "", frame.args[2] or ""))
end

function export.is_valid_page_name(frame)
	return mw.title.new(frame.args[1]) and "valid" or ""
end

return setmetatable({ }, {
	__index = function(self, key)
		local m_debug = require('Module:debug')
		local frame = mw.getCurrentFrame()
		local pframe = frame:getParent()
		local tname = pframe and pframe:getTitle()

		m_debug.track('ugly hacks/' .. key)
		if pframe then
			m_debug.track('ugly hacks/' .. key .. '/from ' .. tname)
		else
			mw.log(debug.traceback('ugly hacks: parent frame not available'))
		end
		return export[key]	
	end
})