Module:Get entries from dab

From Wikipedia, the free encyclopedia

-- this module implements [[template:get entries from dab]]
local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local page_name = args[1] or ''
	local page_content = ''
	local entries = {}
	if page_name ~= '' then
		local title = mw.title.new(page_name)
		if title then
			if title.redirectTarget then title = title.redirectTarget end
			page_content = title:getContent()
		end
		page_content = mw.ustring.gsub(page_content, '[\r\n]==*%s*[Ss]ee also.*', '')
		page_content = mw.ustring.gsub(page_content, '([\r\n]%*)([^%s])', '%1 %2')
	end
	local k = 2
	args[2] = (args[2] or '') == '' and '.' or args[2]
	
	while (args[k] or '') ~= '' do
		local pattern = args[k]
		if pattern ~= '.' then
			for s in mw.ustring.gmatch(page_content, '[\r\n]%*[^\r\n]*[%s%[]' .. pattern .. '[%s%|%],][^\r\n]*') do
				table.insert(entries, s)
			end
		else
			for s in mw.ustring.gmatch(page_content, '[\r\n]%*[^\r\n]*') do
				table.insert(entries, s)
			end
		end
		k = k + 1
	end
	return frame:preprocess(table.concat(entries, '\n'))
end

return p