Difference between revisions of "Module:Delink/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Mr. Stradivarius
(a few tweaks)
blackwiki>Mr. Stradivarius
(attempt to simplify the p._delinkWikilink function)
Line 3: Line 3:
 
local getArgs = require('Module:Arguments').getArgs
 
local getArgs = require('Module:Arguments').getArgs
 
local yesno = require('Module:Yesno')
 
local yesno = require('Module:Yesno')
 +
 +
-- Often-used functions and variables
 +
local htmlDecode = mw.text.decode
 +
local uriDecode = mw.uri.decode
 +
local isKnownLanguageTag = mw.language.isKnownLanguageTag
 +
local namespaces = mw.site.namespaces
  
 
p = {}
 
p = {}
Line 15: Line 21:
 
end
 
end
 
return yesno(val)
 
return yesno(val)
end
 
 
function p._delinkReversePipeTrick(s)
 
if s:find('^%[%[|.*[|\n]') or s == '[[|]]' then -- Check for newlines or multiple pipes.
 
return s
 
else
 
return s:match('%[%[|(.*)%]%]')
 
end
 
end
 
 
function p._delinkPipeTrick(s)
 
-- s the tile area, without bracket nor pipe
 
-- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
 
 
-- First, remove the text before the first colon, if any.
 
s = s:gsub('^(.-:)', '')
 
 
-- Next up, brackets and commas.
 
if s:find('%(.-%)$') then -- Brackets trump commas.
 
s = s:match('(.-) ?%(.-%)$')
 
elseif s:find(',') then -- If there are no brackets, display only the text before the first comma.
 
s = s:match('(.-),.*$')
 
end
 
return s
 
 
end
 
end
  
 
function p._delinkWikilink(s)
 
function p._delinkWikilink(s)
 
-- s is a string starting with '[[' and ending with ']]'. It does not contain any other ']]' strings.
 
-- s is a string starting with '[[' and ending with ']]'. It does not contain any other ']]' strings.
 +
 +
local linkText = s:sub(3, -3)
  
 
-- Deal with nested links
 
-- Deal with nested links
local nested = '[[' .. s:sub(3):gsub('%[%[.-%]%]$', p._delinkWikilink)
+
if linkText:find('%[%[') then
if nested ~= s then
+
return '[[' .. s:sub(3):gsub('%[%[.-%]%]$', p._delinkWikilink)
return nested
 
 
end
 
end
  
-- Deal with the reverse pipe trick.
+
local titleArea, display = linkText:match('^(.-)|(.*)$')
if s:find('^%[%[|') then
+
 
return p._delinkReversePipeTrick(s)
+
-- Process links with display areas. Pipe tricks aren't processed here, as we need to know more about the link title first.
 +
if display then
 +
display = htmlDecode(display, true) -- decode HTML entities.
 +
if titleArea == '' then
 +
-- We are dealing with a reverse pipe trick.
 +
if display:find('[|\n]') or s == '[[|]]' then
 +
-- The link is invalid.
 +
return s
 +
else
 +
return display
 +
end
 +
elseif display ~= '' then
 +
-- We are dealing with a normal piped link.
 +
return display
 +
end
 
end
 
end
  
local decoded = mw.uri.decode(s, 'PATH') -- decode percent-encoded entities. Leave underscores and plus signs.
+
titleArea = titleArea or linkText
decoded = mw.text.decode(decoded, true) -- decode HTML entities.
 
  
-- Check for bad titles. To do this we need to find the
+
-- Decode percent-encoded and HTML-encoded characters.
-- title area of the link, i.e. the part before any pipes.
+
titleArea = uriDecode(titleArea, 'PATH')
local titlearea, display = decoded:match('^%[%[([^|%]]*)|?(.*)%]%]$')
+
titleArea =  htmlDecode(titleArea, true)
local temptitlearea, fragment = titlearea:match('^(.-)#(.*)$')
+
 
titlearea = temptitlearea or titlearea
+
-- Find the fragment, if any.
 +
local titleAreaNoFragment, fragment = titleArea:match('^(.-)#(.*)$')
 +
titleAreaNoFragment = titleAreaNoFragment or titleArea
  
 
-- Check for bad characters.
 
-- Check for bad characters.
if titlearea:find('[%[%]<>{}%%%c\n]') then
+
if titleAreaNoFragment:find('[%[%]<>{}%%%c\n]') then
 
return s
 
return s
 
end
 
end
  
-- Check for categories, interwikis, and files.
+
-- Find the interwiki and the title. Actually, only the prefix before the first
local colonprefix = titlearea:match('^(.-):') or '' -- Get the text before the first colon.
+
-- comma is counted as the interwiki, so the "title" may contain another interwiki
local ns = mw.site.namespaces[colonprefix] -- see if this is a known namespace
+
-- prefix and/or a namespace name, but it's close enough for our purposes.
if mw.language.isKnownLanguageTag(colonprefix)
+
local interwiki, title = titleAreaNoFragment:match('^(.-):(.*)$') or ''
 +
title = title or titleAreaNoFragment
 +
 
 +
-- Check for unescaped categories, interwikis, and files. If any are found,
 +
-- return the blank string, as nothing would be displayed.
 +
local ns = namespaces[interwiki]
 +
if isKnownLanguageTag(interwiki)
 
or ns and (ns.id == 6 or ns.id == 14)
 
or ns and (ns.id == 6 or ns.id == 14)
 
then
 
then
Line 79: Line 83:
  
 
-- Remove the colon if the link is using the [[Help:Colon trick]].
 
-- Remove the colon if the link is using the [[Help:Colon trick]].
if titlearea:sub(1, 1) ==  ':' then
+
titleArea = titleArea:match('^:(.*)$') or titleArea
titlearea = titlearea:sub(2)
 
end
 
  
 
-- Deal with links using the [[Help:Pipe trick]].
 
-- Deal with links using the [[Help:Pipe trick]].
 
if display == '' then
 
if display == '' then
return p._delinkPipeTrick(titlearea)
+
if fragment then
end
+
-- Fragments in a pipe trick are invalid, so return the input string.
 
+
return s
-- Find the display area of the wikilink
+
end
if not display then -- Find if we're dealing with a piped link.
+
-- Pipe tricks don't display interwikis, so we only need the title text here.
-- Remove new lines from the display of multiline piped links,
+
-- We need to remove parentheses and commas. Parentheses have priority.
-- where the pipe is before the first new line.
+
local pipeTrickText = title:match('^(.-) ?%(.-%)$')
titlearea  = titlearea:gsub('\n', '')
+
if pipeTrickText then
 +
return pipeTrickText
 +
else
 +
-- If there are no parentheses, display only the text before the first comma.
 +
pipeTrickText = title:match('(.-),.*$') or title
 +
return pipeTrickText
 +
end
 
end
 
end
  
return display or titlearea
+
-- If we haven't returned any text yet, display the title area.
 +
return titleArea
 
end
 
end
  

Revision as of 14:52, 18 April 2014

Documentation for this module may be created at Module:Delink/sandbox/doc

-- This module de-links most wikitext. 

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

-- Often-used functions and variables
local htmlDecode = mw.text.decode
local uriDecode = mw.uri.decode
local isKnownLanguageTag = mw.language.isKnownLanguageTag
local namespaces = mw.site.namespaces

p = {}

local function whitespaceYesno(val)
	-- Like yesno, but trims whitespace from vals and removes blank strings.
	if type(val) == 'string' then
		val = val:match('^%s*(.-)%s*$')
		if val == '' then
			return nil
		end
	end
	return yesno(val)
end

function p._delinkWikilink(s)
	-- s is a string starting with '[[' and ending with ']]'. It does not contain any other ']]' strings.
	
	local linkText = s:sub(3, -3)

	-- Deal with nested links
	if linkText:find('%[%[') then
		return '[[' .. s:sub(3):gsub('%[%[.-%]%]$', p._delinkWikilink)
	end

	local titleArea, display = linkText:match('^(.-)|(.*)$')

	-- Process links with display areas. Pipe tricks aren't processed here, as we need to know more about the link title first.
	if display then
		display = htmlDecode(display, true) -- decode HTML entities.
		if titleArea == '' then
			-- We are dealing with a reverse pipe trick.
			if display:find('[|\n]') or s == '[[|]]' then
				 -- The link is invalid.
				return s
			else
				return display
			end
		elseif display ~= '' then
			-- We are dealing with a normal piped link.
			return display
		end
	end

	titleArea = titleArea or linkText

	-- Decode percent-encoded and HTML-encoded characters.
	titleArea = uriDecode(titleArea, 'PATH')
	titleArea =  htmlDecode(titleArea, true)

	-- Find the fragment, if any.
	local titleAreaNoFragment, fragment = titleArea:match('^(.-)#(.*)$')
	titleAreaNoFragment = titleAreaNoFragment or titleArea

	-- Check for bad characters.
	if titleAreaNoFragment:find('[%[%]<>{}%%%c\n]') then
		return s
	end

	-- Find the interwiki and the title. Actually, only the prefix before the first
	-- comma is counted as the interwiki, so the "title" may contain another interwiki
	-- prefix and/or a namespace name, but it's close enough for our purposes.
	local interwiki, title = titleAreaNoFragment:match('^(.-):(.*)$') or ''
	title = title or titleAreaNoFragment

	-- Check for unescaped categories, interwikis, and files. If any are found,
	-- return the blank string, as nothing would be displayed.
	local ns = namespaces[interwiki]
	if isKnownLanguageTag(interwiki)
		or ns and (ns.id == 6 or ns.id == 14)
	then
		return ''
	end

	-- Remove the colon if the link is using the [[Help:Colon trick]].
	titleArea = titleArea:match('^:(.*)$') or titleArea

	-- Deal with links using the [[Help:Pipe trick]].
	if display == '' then
		if fragment then
			-- Fragments in a pipe trick are invalid, so return the input string.
			return s
		end
		-- Pipe tricks don't display interwikis, so we only need the title text here.
		-- We need to remove parentheses and commas. Parentheses have priority.
		local pipeTrickText = title:match('^(.-) ?%(.-%)$')
		if pipeTrickText then
			return pipeTrickText
		else
			-- If there are no parentheses, display only the text before the first comma.
			pipeTrickText = title:match('(.-),.*$') or title
			return pipeTrickText
		end
	end

	-- If we haven't returned any text yet, display the title area.
	return titleArea
end

function p._delinkURL(s)
	-- Assume we have already delinked internal wikilinks, and that
	-- we have been passed some text between two square brackets [foo].

	-- If the text contains a line break it is not formatted as a URL, regardless of other content.
	if s:find('\n') then
		return s
	end

	-- Check if the text has a valid URL prefix and at least one valid URL character.
	local valid_url_prefixes = {'//', 'http://', 'https://', 'ftp://', 'gopher://', 'mailto:', 'news:', 'irc://'} 
	local url_prefix
	for i,v in ipairs(valid_url_prefixes) do
		if s:find('^%[' .. v ..'[^"%s].*%]' ) then
			url_prefix = v
			break
		end
	end

	-- Get display text
	if not url_prefix then
		-- Deal with nested links or send back original string.
		return '[' .. s:sub(2):gsub('%[.-%]', p._delinkURL)
	end
	s = s:match('^%[' .. url_prefix .. '(.*)%]') -- Grab all of the text after the URL prefix and before the final square bracket.
	s = s:match('^.-(["<> [].*)') or '' -- Grab all of the text after the first URL separator character ("<> ).
	s = s:match('^%s*(%S.*)$') or '' -- If the separating character was a space, trim it off.

	local s_decoded = mw.text.decode(s, true)
	if s_decoded:find('%c') then
		return s
	else    
		return s_decoded
	end
end

function p._delink(args)
	local text = args[1] or ''
	if whitespaceYesno(args.refs) then
		-- Remove any [[Help:Strip markers]] representing ref tags. In most situations 
		-- this is not a good idea - only use it if you know what you are doing!
		text = mw.ustring.gsub(text, 'UNIQ%w*%-ref%-%d*%-QINU', '')
	end
	if whitespaceYesno(args.comments) ~= false then
		text = text:gsub('<!%-%-.-%-%->', '') -- Remove html comments.
	end
	if whitespaceYesno(args.wikilinks) ~= false then
		text = text:gsub('%[%[.-%]%]', p._delinkWikilink) -- De-link wikilinks.
	end
	if whitespaceYesno(args.urls) ~= false then
		text = text:gsub('%[.-%]', p._delinkURL) -- De-link URLs.
	end
	if whitespaceYesno(args.whitespace) ~= false then
		-- Replace single new lines with a single space, but leave double new lines
		-- and new lines only containing spaces or tabs before a second new line.
		text = text:gsub('([^\n \t][ \t]*)\n([ \t]*[^\n \t])', '%1 %2')
		text = text:gsub('[ \t]+', ' ') -- Remove extra tabs and spaces.
	end
	return text
end

function p.delink(frame)
	local args = frame:getParent().args
	return p._delink(args)
end

return p