Module:Uri

From RationalWiki
Jump to navigation Jump to search

Written for RationalWiki template uses.

Usage

{{#invoke:uri|wiki|some text part of a link}}

This works like using magic words for URL data, except it automatically applies {{#urlencode:...|WIKI}} to text without a #, or to any text before #, while applying {{#anchorencode:...}} to any text after the first #. Suitable for templates that turn some text into a link made to some other wiki.


--[[

This module is intended to make the most basic MW URI functionality
template-friendly. Encoding/descoding strings for URL and the like.

]]

local uri = {}

--[[ Encode string for use as part of wiki URL. ]]
function uri.wiki(frame)
	local str = mw.text.trim(frame.args[1] or '')
	local i = string.find(str, '#', 1, false)
	if i == nil then
		return mw.uri.encode(str, "WIKI")
	end
	if i == 1 then
		return mw.uri.anchorEncode(str)
	end
	local n = string.len(str)
	return mw.uri.encode(string.sub(str, 1, i), "WIKI") .. mw.uri.anchorEncode(string.sub(str, i+1, n))
end

return uri