پودمان:Find country names in string

پودمان به طور دائم حفاظت‌شده است
از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

local countries = require('Module:Find country names in string/countries').countries
local frame = mw.getCurrentFrame()

-- Escapes spaces in a string
-- Used to find exact country names in input
-- i.e. !"mali" in "somalia" or !"Congo" in "Democratic Republic of Congo"
function replace_spaces (s)
	s = mw.ustring.gsub(s, '%s', '%%s')
	--s = '%^' .. s .. '%$'
	return s
end

function p._isRedirect(title)
	return mw.title.getCurrentTitle(title).isRedirect
end

-- Lists all wikilinked country names in /valid countries subpage (content mode should be converted to wikitext)
function p.doc(frame)
	local text = 'فهرست نام معتبر کشورها که توسط این الگو شناسایی می‌شوند به شرح زیر است.' ..
		 '\n\nدر صورتی که عنوان مقالهٔ مورد تأیید این الگو تغییر کرده باشد (به صفحهٔ دیگری تغییرمسیر یافته باشد)،' ..
		 ' نماد تغییرمسیر در جلوی نام آن کشور نمایش می‌یابد. در این صورت لازم است که [[' ..
		 'پودمان:Find country names in string/countries]] ویرایش شود و نام آن کشور در آن به‌روزرسانی شود.' ..
		 '\n\nدر صورتی که قادر به ویرایش این پودمان نیستید، لطفاً در بحث پودمان یا در [' .. '[ویکی‌پدیا:قهوه‌خانه/فنی]]' ..
		 ' درخواست دهید تا ویرایش مورد نظر در پودمان انجام شود.' ..
		 '\n\nدقت کنید که در صورت تغییر عنوان مقالهٔ یک کشور، باید رده‌های مرتبط با آن کشور نیز به عنوان جدید منتقل شوند.' ..
		 ' بنابراین پس از ایجاد تغییر در پودمان عنوان رده‌ها را نیز اصلاح کنید تا از بروز خطا در صفحهٔ آن رده‌ها جلوگیری شود.\n'
	local li, ob, cb, nl, sp, i = '* ', '[[', ']]', '\n', ' ', 1
	local redirect_icon, list = '{' .. '{آیکون|تغییرمسیر}}', frame:expandTemplate{title = 'چندستونه', args = {'4'}}
	while (i <= #countries) do
		list = list .. nl .. li .. ob .. countries[i] .. (p._isRedirect(countries[i]) and cb .. sp .. redirect_icon or cb)
		i = i + 1
	end
	text =  text .. list
			
	return text
end

function p.main(frame)
	local args = getArgs(frame, {
		valueFunc = function (key, value)										-- Do not trim whitespace from separator parameter (args[3])
			if key == 3 then
				return value
			elseif value then
				value = mw.text.trim(value)
				if value ~= '' then
					return value
				end
			end
			return nil
		end
	})
	return p._main(args)
end

function p._main(args)
	local args = frame:getParent().args
	local arg = args[1] or 1													-- Use args[1] (must be and integer) as the index of the country name to return,
	local tracking = {}
																				-- and if args[1] is not provided, return the first country name
	
	local input = args[2] or mw.title.getCurrentTitle().text					-- Input can be a string in args[2] and if not provided, will be the page name
	local j = 1
	local names = {}															-- Table to store country names in alphabetical order
	if yesno(args['روابط']) then 
		local match_num = 0
		repeat
			local c1_found = false
			local c2_found = false
			local country1 = ''
			local country2 = ''
			if (match_num == 0) then
				country1, country2 = mw.ustring.match(input, 'روابط (.-) و (.*)')
			elseif (match_num == 1) then
				local part1, part2, part3 = mw.ustring.match(input, 'روابط (.-) و (.-) و (.*)')
				if part1 and part2 then
					country1 = part1..' و '..part2
				else
					country1 = ''
				end
				country2 = part3
			elseif (match_num == 2) then
				local part1, part2, part3 = mw.ustring.match(input, 'روابط (.-) و (.-) و (.*)')
				country1 = part1
				if part2 and part3 then
					country2 = part2..' و '..part3
				else
					country2 = ''
				end
			elseif (match_num == 3) then
				local part1, part2, part3, part4 = mw.ustring.match(input, 'روابط (.-) و (.-) و (.-) و (.*)')
				if part1 and part2 then
					country1 = part1..' و '..part2
				else
					country1 = ''
				end
				if part3 and part4 then
					country2 = part3..' و '..part4
				else
					country2 = ''
				end
			end
			
			match_num = match_num + 1
			
			for _, v in pairs(countries) do
				if v == country1 then
					table.insert(names, v)								-- If a match is found in input from "countries" table, store it and ...
					c1_found = true
				end
			end
			for _, v in pairs(countries) do
				if v == country2 then
					table.insert(names, v)
					c2_found = true
				end
			end
		until ((c1_found and c2_found) or (match_num > 3))
	else
		while (j <= #countries) do
			if mw.ustring.match(input, replace_spaces(countries[j]), 1) then
				table.insert(names, countries[j])									-- If a match is found in input from "countries" table, store it and ...
			end
			j = j + 1																-- ... continue the loop to find more country names
		end
	end
	-- Value of "output" depends on args['all']
	--	if args['all'] == true
	--		"output" will be a string of all countries found in "input"
	--		separated by comma (or user-defined separator)
	--  else
	--		output will be the first (or args[1]th)
	--  	country name in "names" table
	local output
	if yesno(args['همه']) then
		output = #names > 1 and table.concat(names, args[3] or '، ') or names[1]
	else
		output = names[tonumber(arg)]
	end
	
	if #names == 0 then
		table.insert(tracking, '[[رده:الگوی نام کشور از رشته بدون استخراج نام کشور|{{نام‌صفحه}}]]')
		if output then
			output = output .. table.concat(tracking, '')
		end
	end
	
	return output
end

return p