Module:Sandbox/Ahecht/url-access

From English Wikipedia @ Freddythechick
local p = {}
local cfg = mw.loadData("Module:Citation/CS1/Configuration")

local function safe_for_url( str )
	return str:gsub( '[%[%]\n]', {	
		['['] = '[',
		[']'] = ']',
		['\n'] = ' ' } );
end

local function external_link( URL, label, source, access)
	local base_url;
	
	if not is_set( label ) then
		base_url = URL
	else
		local domain, path = URL:match ('^([/%.%-%+:%a%d]+)([/%?#].*)$');		-- split the url into scheme plus domain and path
		if path then															-- if there is a path portion
			path = path:gsub ('[%[%]]', {['[']='%5b',[']']='%5d'});				-- replace '[' and ']' with their percent encoded values
			URL = table.concat ({domain, path});								-- and reassemble
		end

		base_url = table.concat({ "[", URL, " ", safe_for_url (label), "]" });	-- assemble a wikimarkup url
	end
	
	if is_set (access) then														-- access level (subscription, registration, limited)
		base_url = substitute (cfg.presentation['ext-link-access-signal'], {cfg.presentation[access].class, cfg.presentation[access].title, base_url});	-- add the appropriate icon
	end
	
	return base_url;
end