پودمان: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 country1, country2 = mw.ustring.match(input, 'روابط (.+) و (.+)')
		while (j <= #countries) do
			if country1 == countries[j] then
				table.insert(names, countries[j])								-- If a match is found in input from "countries" table, store it and ...
			elseif country2 == countries[j] then
				table.insert(names, countries[j])
			end
			j = j + 1																-- ... continue the loop to find more country names
		end	
	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, '[[رده:الگوی نام کشور از رشته بدون استخراج نام کشور|{{نام‌صفحه}}]]')
		output = output .. table.concat(tracking, '')
	end
	
	return output
end

return p