Module:If preview

From Viki
Jump to navigation Jump to search

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

 1 local p = {}
 2 
 3 local cfg = mw.loadData('Module:If preview/configuration')
 4 
 5 --[[
 6 main
 7 
 8 This function returns either the first argument or second argument passed to
 9 this module, depending on whether the page is being previewed.
10 
11 ]]
12 function p.main(frame)
13 	if cfg.preview then
14 		return frame.args[1] or ''
15 	else
16 		return frame.args[2] or ''
17 	end
18 end
19 
20 --[[
21 pmain
22 
23 This function returns either the first argument or second argument passed to
24 this module's parent (i.e. template using this module), depending on whether it
25 is being previewed.
26 
27 ]]
28 function p.pmain(frame)
29 	return p.main(frame:getParent())
30 end
31 
32 
33 local function warning_text(warning)
34 	return mw.ustring.format(
35 		cfg.warning_infrastructure,
36 		cfg.templatestyles,
37 		warning
38 	)
39 end
40 
41 function p._warning(args)
42 	
43 	local warning = args[1] and args[1]:match('^%s*(.-)%s*$') or ''
44 	if warning == '' then
45 		return warning_text(cfg.missing_warning)
46 	end
47 	
48 	if not cfg.preview then return '' end
49 	
50 	return warning_text(warning)
51 end
52 
53 --[[
54 warning
55 
56 This function returns a "preview warning", which is the first argument marked
57 up with HTML and some supporting text, depending on whether the page is being previewed.
58 
59 disabled since we'll implement the template version in general
60 
61 ]]
62 --function p.warning(frame)
63 --	return p._warning(frame.args)
64 --end
65 
66 --[[
67 warning, but for pass-through templates like {{preview warning}}
68 ]]
69 function p.pwarning(frame)
70 	return p._warning(frame:getParent().args)
71 end
72 
73 return p