Module:Sandbox/Naypta/Autolink

From blackwiki
Jump to navigation Jump to search

This module automatically generates either an external link or an internal link, dependent on whether it is given a link with a protocol or not.

Understand that using this module will mean that links to things like the redirects in Category:Redirects from domain names will result in external links being generated. This may not always be the desired result, so it should only be used where a wikilink that looks like a URL is implausible for the value the module is passed.

Usage

{{#invoke:Sandbox/Naypta|link|your link goes here|link title}}

link title is an optional parameter, which if used will be set as the piped link for internal links like this, or the link title for external links like this.



local p = {}

function p.link( frame )
    if (frame.args[1] == nil)
    then
    	error("No link provided")
    end
    
	if (mw.ustring.match( frame.args[1], "^%w+:%/%/" ) ~= nil)
	then
		return "[" .. frame.args[1] .. generateEnd( frame.args[2], true )
	else
		return "[[" .. frame.args[1] .. generateEnd( frame.args[2], false )
	end
end

function generateEnd( linkName, isEL )
	endBit = "]"
    if (linkName ~= nil)
    then
    	endBit = linkName .. endBit
    end
    
    if (isEL == true)
    then
    	endBit = " " .. endBit
    elseif (linkName ~= nil)
	then
		endBit = "|" .. endBit .. "]"
	else
		endBit = endBit .. "]"
    end
	return endBit
end

return p