Module:JsonEscape

From Tensura: Reincarnated Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:JsonEscape/doc

local p = {}

function p.escape( frame )
	local text = frame.args[1] or ''

	-- Convert [[Page|Display]] and [[Page]] wikilinks to plain text
	text = text:gsub( '%[%[([^%]|]+)|([^%]]+)%]%](%a*)', function( page, display, suffix )
		return '(link to ' .. page .. ' article, displayed as ' .. display .. suffix .. ')'
	end )
	text = text:gsub( '%[%[([^%]]+)%]%](%a*)', function( page, suffix )
		return '(link to ' .. page .. ' article, displayed as ' .. page .. suffix .. ')'
	end )

	if #text > 1000 then
		return '(values exceeds 1000 characters...)'
	end

	-- jsonEncode a string returns it wrapped in quotes with proper escaping;
	-- strip the surrounding quotes since JsonEscape only needs the inner content
	local encoded = mw.text.jsonEncode( text )
	return encoded:sub( 2, -2 )
end

return p