Module:Plain text

From Viki
Jump to navigation Jump to search

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

 1 --converts text with wikilinks to plain text, e.g "[[foo|gah]] is [[bar]]" to "gah is bar"
 2 --removes anything enclosed in tags that isn't nested, mediawiki strip markers (references etc), files, italic and bold markup
 3 local p = {}
 4 
 5 function p.main(frame)
 6 	local text = frame.args[1]
 7 	local encode = require('Module:yesno')(frame.args.encode)
 8 	return p._main(text, encode)
 9 end
10 
11 function p._main(text, encode)
12 	if not text then return end
13 	text = mw.text.killMarkers(text)
14 		:gsub(' ', ' ') --replace nbsp spaces with regular spaces
15 		:gsub('<br ?/?>', ', ') --replace br with commas
16 		:gsub('<span.->(.-)</span>', '%1') --remove spans while keeping text inside
17 		:gsub('<i.->(.-)</i>', '%1') --remove italics while keeping text inside
18 		:gsub('<b.->(.-)</b>', '%1') --remove bold while keeping text inside
19 		:gsub('<em.->(.-)</em>', '%1') --remove emphasis while keeping text inside
20 		:gsub('<strong.->(.-)</strong>', '%1') --remove strong while keeping text inside
21 		:gsub('<.->.-<.->', '') --strip out remaining tags and the text inside
22 		:gsub('<.->', '') --remove any other tag markup
23 		:gsub('%[%[%s*[Ff][Ii][Ll][Ee]%s*:.-%]%]', '') --strip out files
24 		:gsub('%[%[%s*[Ii][Mm][Aa][Gg][Ee]%s*:.-%]%]', '') --strip out use of image:
25 		:gsub('%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:.-%]%]', '') --strip out categories
26 		:gsub('%[%[[^%]]-|', '') --strip out piped link text
27 		:gsub('([^%[])%[[^%[%]][^%]]-%s', '%1') --strip out external link text
28 		:gsub('^%[[^%[%]][^%]]-%s', '') --strip out external link text
29 		:gsub('[%[%]]', '') --then strip out remaining [ and ]
30 		:gsub("'''''", "") --strip out bold italic markup
31 		:gsub("'''?", "") --not stripping out '''' gives correct output for bolded text in quotes
32 		:gsub('----+', '') --remove ---- lines
33 		:gsub("^%s+", "") --strip leading
34 		:gsub("%s+$", "") --and trailing spaces
35 		:gsub("%s+", " ") --strip redundant spaces
36 	if encode then
37 		return mw.text.encode(text)
38 	else
39 		return text
40 	end
41 end
42 
43 return p