Module:γενικέςσυναρτήσεις

Από Βικιλεξικό
Μετάβαση στην πλοήγηση Πήδηση στην αναζήτηση

Documentation for this module may be created at Module:γενικέςσυναρτήσεις/τεκμηρίωση

p = {}

--αντιγραφή από το w:Module:Redirect 
-- Gets a mw.title object, using pcall to avoid generating script errors if we
-- are over the expensive function count limit (among other possible causes).
local function _getTitle(...)
	local success, titleObj = pcall(mw.title.new, ...)
	if success then
		return titleObj
	else
		return nil
	end
end

-- Function to load corresponding unit
function p.load_lang_unit(base, lang, unit_name)
	if (base or '') == '' or (lang or '') == '' or (unit_name or '') == '' then
		return nil
	end
	
	return require('Module:' .. base .. '/' .. lang .. '/' .. unit_name);
end

function p.split(pString, pPattern)
   local localtable = {}
   local localpattern = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(localpattern, 1)
   while s do
       if s ~= 1 or cap ~= "" then
           table.insert(localtable,cap)
       end
	   last_end = e+1
	   s, e, cap = pString:find(localpattern, last_end)
   end
   if last_end <= #pString then
       cap = pString:sub(last_end)
	   table.insert(localtable, cap)
   end
   return localtable
end

--βρες το όνομα του Προτύπου που κάλεσε το Module
--αλλά μόνο αν κληθηκε από σελίδα που κάλεσε το πρότυπο
function p.getTemplateName(frame)
	if type(frame.args) == 'table' and type(frame.getParent) == 'function' then
	    local templateobj = mw.title.new(frame:getParent():getTitle())
	    if templateobj.namespace == 10 then -- NS_TEMPLATE
		    return templateobj.text:match( "^%s*(.-)%s*$" )
	    else
	        return nil
	    end
	else
	    return nil
    end
end

return p