Module:Delink
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Delink/doc
1 -- This module de-links most wikitext.
2
3 require('Module:No globals')
4
5 local p = {}
6
7 local getArgs
8
9 local function delinkReversePipeTrick(s)
10 if s:match("^%[%[|.*[|\n]") then -- Check for newlines or multiple pipes.
11 return s
12 else
13 return s:match("%[%[|(.*)%]%]")
14 end
15 end
16
17 local function delinkPipeTrick(s)
18 local linkarea, display = "", ""
19 -- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
20
21 -- First, remove the text before the first colon, if any.
22 if s:match(":") then
23 s = s:match("%[%[.-:(.*)|%]%]")
24 -- If there are no colons, grab all of the text apart from the square brackets and the pipe.
25 else
26 s = s:match("%[%[(.*)|%]%]")
27 end
28
29 -- Next up, brackets and commas.
30 if s:match("%(.-%)$") then -- Brackets trump commas.
31 s = s:match("(.-) ?%(.-%)$")
32 elseif s:match(",") then -- If there are no brackets, display only the text before the first comma.
33 s = s:match("(.-),.*$")
34 end
35 return s
36 end
37
38 local function delinkWikilink(s)
39 local result = s
40 -- Deal with the reverse pipe trick.
41 if result:match("%[%[|") then
42 return delinkReversePipeTrick(result)
43 end
44
45 result = mw.uri.decode(result, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
46 result = mw.text.decode(result, true) -- decode HTML entities.
47
48 -- Check for bad titles. To do this we need to find the
49 -- title area of the link, i.e. the part before any pipes.
50 local titlearea
51 if result:match("|") then -- Find if we're dealing with a piped link.
52 titlearea = result:match("^%[%[(.-)|.*%]%]")
53 else
54 titlearea = result:match("^%[%[(.-)%]%]")
55 end
56 -- Check for bad characters.
57 if mw.ustring.match(titlearea, "[%[%]<>{}%%%c\n]") then
58 return s
59 end
60
61 -- Check for categories, interwikis, and files.
62 local colonprefix = result:match("%[%[(.-):.*%]%]") or "" -- Get the text before the first colon.
63 local ns = mw.site.namespaces[colonprefix] -- see if this is a known namespace
64 if mw.language.isKnownLanguageTag(colonprefix)
65 or ( ns and ( ns.canonicalName == "File" or ns.canonicalName == "Category" ) ) then
66 return ""
67 end
68
69 -- Remove the colon if the link is using the [[Help:Colon trick]].
70 if result:match("%[%[:") then
71 result = "[[" .. result:match("%[%[:(.*%]%])")
72 end
73
74 -- Deal with links using the [[Help:Pipe trick]].
75 if mw.ustring.match(result, "^%[%[[^|]*|%]%]") then
76 return delinkPipeTrick(result)
77 end
78
79 -- Find the display area of the wikilink
80 if result:match("|") then -- Find if we're dealing with a piped link.
81 result = result:match("^%[%[.-|(.+)%]%]")
82 -- Remove new lines from the display of multiline piped links,
83 -- where the pipe is before the first new line.
84 result = result:gsub("\n", "")
85 else
86 result = result:match("^%[%[(.-)%]%]")
87 end
88
89 return result
90 end
91
92 local function delinkURL(s)
93 -- Assume we have already delinked internal wikilinks, and that
94 -- we have been passed some text between two square brackets [foo].
95
96 -- If the text contains a line break it is not formatted as a URL, regardless of other content.
97 if s:match("\n") then
98 return s
99 end
100
101 -- Check if the text has a valid URL prefix and at least one valid URL character.
102 local valid_url_prefixes = {"//", "http://", "https://", "ftp://", "gopher://", "mailto:", "news:", "irc://"}
103 local url_prefix
104 for i,v in ipairs(valid_url_prefixes) do
105 if mw.ustring.match(s, '^%[' .. v ..'[^"%s].*%]' ) then
106 url_prefix = v
107 break
108 end
109 end
110
111 -- Get display text
112 if not url_prefix then
113 return s
114 end
115 s = s:match("^%[" .. url_prefix .. "(.*)%]") -- Grab all of the text after the URL prefix and before the final square bracket.
116 s = s:match('^.-(["<> ].*)') or "" -- Grab all of the text after the first URL separator character ("<> ).
117 s = mw.ustring.match(s, "^%s*(%S.*)$") or "" -- If the separating character was a space, trim it off.
118
119 local s_decoded = mw.text.decode(s, true)
120 if mw.ustring.match(s_decoded, "%c") then
121 return s
122 else
123 return s_decoded
124 end
125 end
126
127 local function delinkLinkClass(s, pattern, delinkFunction)
128 if not type(s) == "string" then
129 error("Attempt to de-link non-string input.", 2)
130 end
131 if not ( type(pattern) == "string" and mw.ustring.sub(pattern, 1, 1) == "^" ) then
132 error('Invalid pattern detected. Patterns must begin with "^".', 2)
133 end
134 -- Iterate over the text string, and replace any matched text. using the
135 -- delink function. We need to iterate character by character rather
136 -- than just use gsub, otherwise nested links aren't detected properly.
137 local result = ""
138 while s ~= '' do
139 -- Replace text using one iteration of gsub.
140 s = mw.ustring.gsub(s, pattern, delinkFunction, 1)
141 -- Append the left-most character to the result string.
142 result = result .. mw.ustring.sub(s, 1, 1)
143 s = mw.ustring.sub(s, 2, -1)
144 end
145 return result
146 end
147
148 function p._delink(args)
149 local text = args[1] or ""
150 if args.refs == "yes" then
151 -- Remove any [[Help:Strip markers]] representing ref tags. In most situations
152 -- this is not a good idea - only use it if you know what you are doing!
153 text = mw.ustring.gsub(text, "UNIQ%w*%-ref%-%d*%-QINU", "")
154 end
155 if not (args.comments == "no") then
156 text = text:gsub("<!%-%-.-%-%->", "") -- Remove html comments.
157 end
158 if not (args.wikilinks == "no") then
159 text = delinkLinkClass(text, "^%[%[.-%]%]", delinkWikilink) -- De-link wikilinks.
160 end
161 if not (args.urls == "no") then
162 text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs.
163 end
164 if not (args.whitespace == "no") then
165 -- Replace single new lines with a single space, but leave double new lines
166 -- and new lines only containing spaces or tabs before a second new line.
167 text = mw.ustring.gsub(text, "([^\n \t][ \t]*)\n([ \t]*[^\n \t])", "%1 %2")
168 text = text:gsub("[ \t]+", " ") -- Remove extra tabs and spaces.
169 end
170 return text
171 end
172
173 function p.delink(frame)
174 if not getArgs then
175 getArgs = require('Module:Arguments').getArgs
176 end
177 return p._delink(getArgs(frame, {wrappers = 'Template:Delink'}))
178 end
179
180 return p