Jump to content

Module:Biglist

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Johnuniq (talk | contribs) at 06:39, 14 February 2016 (generate a big list of similar items to reduce template include size exceeded problems). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- Make a big list of items with an emulated template call for each item so the
-- page is not added to [[:Category:Pages where template include size is exceeded]].

local function collection()
	-- Return a table to hold items.
	return {
		n = 0,
		add = function (self, item)
			self.n = self.n + 1
			self[self.n] = item
		end,
		join = function (self, sep)
			return table.concat(self, sep)
		end,
	}
end

local function message(msg, nocat)
	-- Return formatted message text for an error.
	-- Can append "#FormattingError" to URL of a page with a problem to find it.
	-- This should not be called because there is no validity checking.
	local anchor = '<span id="FormattingError" />'
	local category = nocat and '' or '[[Category:Biglist errors]]'
	return anchor ..
		'<strong class="error">Error: ' ..
		msg ..
		'</strong>' ..
		category .. '\n'
end

local function urlencode(text)
	-- Return equivalent of {{urlencode:text}}.
	local function byte(char)
		return string.format('%%%02X', string.byte(char))
	end
	return text:gsub('[^ %w%-._]', byte):gsub(' ', '+')
end

local function replace2(text, template)
	-- Return template after substituting text and urlencoded text.
	local plain = text:gsub('%%', '%%%%')  -- for gsub, '%' has a special meaning in replacement
	local encoded = urlencode(text):gsub('%%', '%%%%')
	return template:gsub('<TXT>', plain):gsub('<UENC>', encoded)
end

local function clean(text, default)
	-- Return text, if not empty, after trimming leading/trailing whitespace.
	-- Otherwise return default which may be nil.
	if text then
		text = text:match("^%s*(.-)%s*$")
		if text ~= '' then
			return text
		end
	end
	return default
end

local function make_list(args, formatter, template)
	-- Return a list of formatted items.
	-- Input is a string of multiple lines, one item per line.
	local text = args.list or args[1] or ''
	local prefix = clean(args.prefix) or ''
	local comment = clean(args.comment)
	local results = collection()
	for line in string.gmatch(text .. '\n', '[\t ]*(.-)[\t\r ]*\n') do
		-- Skip line if empty or a comment.
		if line ~= '' then
			if not (comment and line:sub(1, #comment) == comment) then
				results:add(prefix .. formatter(line, template))
			end
		end
	end
	return results:join('\n')
end

local templates = {
	-- Equivalent of {{userlinks|text}}.
	userlinks = [=[
<span class="plainlinks userlinks">[[User:<TXT>|<TXT>]] <span class="plainlinks">([[User talk:<TXT>|talk]] '''·''' [[Special:Contributions/<TXT>|contribs]]<span class="sysop-show"> '''·''' [[Special:DeletedContributions/<TXT>|deleted contribs]]</span> '''·''' [//en.wikipedia.org/w/index.php?title=Special:Log&user=<UENC> logs] '''·''' [//en.wikipedia.org/w/index.php?title=Special:AbuseLog&wpSearchUser=<UENC> edit filter log]<span class="sysop-show"> '''·''' [[Special:Block/<TXT>|block user]]</span> '''·''' [//en.wikipedia.org/w/index.php?title=Special:Log&type=block&page=User:<UENC> block log])</span></span>]=],
	-- Equivalent of how {{search|topic}} is used.
	topicsearch = [=[
[[<TXT>]] – <span class="plainlinks">([//en.wikipedia.org/w/index.php?title=Special:Search&search=<UENC> wp]  [http://www.google.com/search?q=site%3Awikipedia.org+<TXT> gwp] [http://www.google.com/search?q=<TXT> g] [http://www.bing.com/search?q=site%3Awikipedia.org+<TXT> bwp] [http://www.bing.com/search?q=<TXT> b] | [http://www.britannica.com/search?query=<TXT> eb] [http://www.google.com/custom?sitesearch=1911encyclopedia.org&q=<TXT> 1911] [http://www.bartleby.com/cgi-bin/texis/webinator/65search?query=<TXT> co] [http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fwww.pcmag.com%2Fencyclopedia_term%2F+<TXT> gct] [http://scienceworld.wolfram.com/search/index.cgi?as_q=<TXT> sw] [https://archive.org/search.php?query=<UENC> arc] [http://babel.hathitrust.org/cgi/ls?field1=ocr;q1=<UENC>;a=srchls;lmt=ft ht])</span>]=],
}

local function main(formatter, tname)
	local template = templates[tname]
	if template then
		return function (frame)
			local args = frame.args
			local success, result = pcall(make_list, args, formatter, template)
			if success then
				return result
			end
			return message(result, clean(args.nocat))
		end
	else
		return function (frame)
			local args = frame.args
			return message('Unknown template name "' .. tname .. '"', clean(args.nocat))
		end
	end
end

return {
	userlinks = main(replace2, 'userlinks'),
	topicsearch = main(replace2, 'topicsearch'),
}