Module:DecodeEncode

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Edit] [Purge] Documentation

Implements Lua functions mw.text.decode, mw.text.encode in a module.

Usage

{{#invoke:DecodeEncode|decode|s=Source text}}Source text

See also

 1 local p = {}
 2 
 3 function _getBoolean( boolean_str )
 4 	-- from: module:String; adapted
 5 	-- requires an explicit true
 6 	local boolean_value
 7 
 8 	if type( boolean_str ) == 'string' then
 9 		boolean_str = boolean_str:lower()
10 		if boolean_str == 'true' or boolean_str == 'yes' or boolean_str == '1' then
11 			boolean_value = true
12 		else
13 			boolean_value = false
14 		end
15 	elseif type( boolean_str ) == 'boolean' then
16 		boolean_value = boolean_str
17 	else
18 		boolean_value = false
19 	end
20 	return boolean_value
21 end
22 
23 function p.decode( frame )
24 	local s
25 	local subset_only 
26 
27 	s = frame.args['s'] or ''
28 	subset_only = _getBoolean(frame.args['subset_only'] or false)
29 
30 	return p._decode( s, subset_only )
31 end
32 
33 function p._decode( s, subset_only )
34 	local ret = nil;
35 
36     s = mw.ustring.gsub( s, ' ', ' ' ) -- Workaround for bug:   gets properly decoded in decode, but   doesn't.
37 
38 	ret = mw.text.decode( s, not subset_only )
39 
40 	return ret
41 end
42 
43 function p.encode( frame )
44 	local s
45 	local charset
46 
47 	s = frame.args['s'] or ''
48 	charset = frame.args['charset']
49 
50 	return p._encode( s, charset )
51 end
52 
53 function p._encode( s, charset )
54 	-- example: charset = '_&©−°\\\"\'\=' -- do escape with backslash not %;
55 	local ret
56 
57 	if charset ~= (nil or '') then
58 		ret = mw.text.encode( s, charset )
59 	else
60 		-- use default: chartset = '<>&"\' ' (outer quotes = lua required; space = NBSP)
61 		ret = mw.text.encode( s )
62 	end 
63 	
64 	return ret
65 end
66 
67 return p