Module:Redirect template
From TechInfoDepot
Jump to navigationJump to search
This module implements {{redirect template}} and {{redirect template/core}}. Please see the template pages for documentation.
The above documentation is transcluded from Module:Redirect template/doc. (edit | history) Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages. Subpages of this module. |
1 require('Module:No globals')
2
3 local p = {}
4
5 -- key is beginning of arg name. value is table with namespace number and link
6 -- alternatively, a function taking the namespace number and returning a validity
7 -- can be used
8 local namespaceCategories = {
9 all = { function() return true end },
10 main = { 0, '[[wp:mainspace|main]]' },
11 help = { 12, '[[wp:help namespace|help]]' },
12 portal = { 100, '[[wp:portal|portal]]' },
13 talk = { function(n) return n > 0 and n%2 == 1 end, '[[Help:Using talk pages|talk]]' },
14 template = { 10, '[[wp:template namespace|template]]' },
15 techInfoDepot = { 4, '[[wp:project namespace|TechInfoDepot project]]' },
16 category = { 14, '[[wp:categorization|category]]' },
17 user = { 2, '[[wp:user pages|user]]' },
18 }
19
20 -- Don't convert blank category to nil
21 local function valueFunc(key, val)
22 if type(val) == 'string' then
23 val = val:match('^%s*(.-)%s*$')
24 if val == '' and key ~= 'category' and key ~= 'embed' then
25 return nil
26 end
27 end
28 return val
29 end
30
31 local function getPrettyName(args)
32 for k in pairs(namespaceCategories) do
33 if args[k .. ' category'] then
34 return "'''[[:Category:" .. args[k .. ' category'] .. "|" .. args.name .. "]]''': "
35 end
36 end
37 return "'''" .. args.name .. "''': "
38 end
39
40 function p.core(frame, args)
41 if not args then
42 args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Redirect template/core', valueFunc = valueFunc})
43 end
44 local namespace = mw.title.getCurrentTitle().namespace
45 local otherCategory = args['other category'] and (args.category or string.format('[[Category:%s]]', args['other category']))
46 local embedPossible = args.embed == nil or args.embed == 'yes'
47
48 --- XXX: this is a HORRIBLE HACK. kill it with fire as soon as https://bugzilla.wikimedia.org/show_bug.cgi?id=12974 is fixed
49 local beCompatibleWithBug12974 = args.info and (args.info:find('^[:;#*]', 1) == 1 or args.info:find('{|', 1, true) == 1) and '\n' or ' '
50
51 local retval = string.format('*%sThis is a redirect%s%s.%s%s\n',
52 embedPossible and args.name and getPrettyName(args) or '',
53 args.from and (' from ' .. args.from) or '',
54 args.to and (' to ' .. args.to) or '',
55 args.info and beCompatibleWithBug12974 or '',
56 args.info or ''
57 )
58 for k,v in pairs(namespaceCategories) do
59 if args[k .. ' category'] then
60 if type(v[1]) == 'function' and v[1](namespace) or v[1] == namespace then
61 retval = retval .. (args.category or string.format('[[Category:%s]]', args[k .. ' category']))
62 elseif args['other category'] then
63 retval = retval .. otherCategory
64 else
65 retval = retval .. frame:expandTemplate{title = 'Incorrect redirect template', args = {v[2]}}
66 end
67 end
68 end
69 return retval
70 end
71
72 function p.main(frame)
73 local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Redirect template', valueFunc = valueFunc})
74 local retval = p.core(frame, args)
75 if mw.title.getCurrentTitle().namespace == 0 then
76 if args.printworthy == 'yes' then
77 return retval .. (args.category or '[[Category:Printworthy redirects]]')
78 elseif args.printworthy == 'no' then
79 return retval .. (args.category or '[[Category:Unprintworthy redirects]]')
80 end
81 end
82 return retval
83 end
84
85 return p