Module:Transclude

From About PCs Wiki
Jump to navigation Jump to search

Invoke procedure proc: {{#invoke:Transclude|proc}} · use module: local t = require('Module:Transclude')

Subpages

Module Usage

{{raw:yazz:Hr}} Help for interwiki transclusion ("scary" transclusion)


-- this module is for everything transcluded

local p = {} -- p stands for package

function p.call(frame)
	local parms_invoke   = frame.args              -- parameters per #invoke 
	local parms_template = frame:getParent().args  -- parameters per template

	local template = parms_invoke['$template'] or parms_template['$template'] or 'missing template' -- name of template
	local raw      = parms_invoke['$raw']      or parms_template['$raw'] or 'raw:'                  -- expand mode raw or not
	local wiki     = parms_invoke['$wiki']     or parms_template['$wiki'] or 'templatewiki'         -- name of remote wiki
	--if 1 then return c .. table.concat(a, ", ")  end
	local i,v
	local _, expand_template = '', ''
	
	-- all parameternames including $ are excluded from transclusion
	for i,v in pairs(parms_invoke) do
		-- parameter name i may not contain the string $
		if not string.match(i, ('%$')) then
			expand_template = expand_template .. '|' .. i .. '=' .. trim(v)
		end
	end
	for i,v in pairs(parms_template) do 
		-- parameter name i may not contain the string $
		if not string.match(i, ('%$')) then
			expand_template = expand_template .. '|' .. i .. '=' .. trim(v)
		end
	end
	-- clean all $ parameters
	wiki     = trim(wiki)
	template = trim(template)
	
	if raw ~= 'raw:' then raw = '' end -- raw must be empty or 'raw:'
	
	expand_template = '{{' .. raw .. wiki .. ':' .. template .. expand_template .. '}}'
	--if 1 then return 'alle Parameter: <nowiki>' .. expand_template .. '</nowiki><br>template: <nowiki>' .. template .. '</nowiki><br>prefix: ' .. wiki end
	--return frame:preprocess(expand_template)
	return expand_template
end

function trim(s)
  -- removes whitespace on both ends of a string
  if type(s) ~= 'string' then return s end  -- return unchanged in case no string
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function empty (self)
	-- there is no intrinsic function for checking if a table is empty
	-- returns true when table is empty
	-- or no table
    for _, _ in pairs(self) do
        return false
    end
    return true
end

function tidy( val)
	-- return false when emptiy: '' is not false
	-- trims string and check if empty
	if type(val) == 'string' then
		val = val:match('^%s*(.-)%s*$')
		if val == '' then
			return false
		else
			return val
		end
	else
		return val
	end
end

return p