Module:Sandbox/Ahecht/url-access

From blackwiki
< Module:Sandbox‎ | Ahecht
Revision as of 09:15, 27 September 2020 by Blackwikiadmin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/Ahecht/url-access/doc

local p = {}
local cfg = mw.loadData("Module:Citation/CS1/Configuration")

local function safe_for_url( str )
	return str:gsub( '[%[%]\n]', {	
		['['] = '&#91;',
		[']'] = '&#93;',
		['\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