İçeriğe atla

Modül:İlknot liste

Hukukipedia sitesinden

Bu modül için bir Modül:İlknot liste/belge belgelendirmesi oluşturabilirsiniz

--------------------------------------------------------------------------------
--                           Modül:İlknot liste                               --
--                                                                            --
-- Bu modül ilknotlarda kullanılmak üzere listeler üretir ve biçimlendirir.   --
-- Özellikle {{hakkında}}, {{yönlendirme}} ve varyantlarında kullanıldığı     --
-- gibi "X için Y sayfasına bakınız" ifadelerinin listesini uygular.          --
-- Ayrıca listeleri bu bağlaçlarla biçimlendirmek için andList ve orList      --
-- yardımcı fonksiyonları tanıtılmıştır.                                      --
--------------------------------------------------------------------------------

local mArguments -- gecikmeli başlatma (lazy initialization)
local mFormatLink = require('Modül:Format link')
local mHatnote = require('Modül:İlknot')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local p = {}

--------------------------------------------------------------------------------
-- Liste dizgeleştirme yardımcı fonksiyonları
--------------------------------------------------------------------------------

local stringifyListDefaultOptions = {
	conjunction = "ve",
	separator = ",",
	altSeparator = ";",
	space = " ",
	formatted = false
}

local function searchDisp(haystack, needle)
	return string.find(
		string.sub(haystack, (string.find(haystack, '|') or 0) + 1), needle
	)
end

local function stringifyList(list, options)
	checkType("stringifyList", 1, list, "table")
	if #list == 0 then return nil end
	checkType("stringifyList", 2, options, "table", true)
	options = options or {}
	for k, v in pairs(stringifyListDefaultOptions) do
		if options[k] == nil then options[k] = v end
	end
	local s = options.space
	if options.formatted then
		list = mFormatLink.formatPages(
			{categorizeMissing = mHatnote.missingTargetCat}, list
		)
	end
	
	local separator = options.separator
	for k, v in pairs(list) do
		if searchDisp(v, separator) then
			separator = options.altSeparator
			break
		end
	end
	
	local conjunction = s .. options.conjunction .. s
	
	-- Türkçede "ve" bağlacından önce virgül kullanılmadığı için 
	-- İngilizcedeki "Oxford virgülü" kuralı devre dışı bırakılmıştır.
	-- if #list == 2 and searchDisp(list[1], "§") or #list > 2 then
	-- 	conjunction = separator .. conjunction
	-- end
	
	return mw.text.listToText(list, separator .. s, conjunction)
end

function p.conjList (conj, list, fmt)
	return stringifyList(list, {conjunction = conj, formatted = fmt})
end

function p.andList (...) return p.conjList("ve", ...) end
function p.orList (...) return p.conjList("veya", ...) end

--------------------------------------------------------------------------------
-- İçin bakınız (For see)
--------------------------------------------------------------------------------

local forSeeDefaultOptions = {
	andKeyword = 've',
	title = mw.title.getCurrentTitle().text,
	otherText = 'diğer kullanımları',
	forSeeForm = '%s için %s sayfasına bakınız.',
}

local function punctuationCollapse (text)
 	return text:match("[.?!]('?)%1(%]?)%2%.$") and text:sub(1, -2) or text
end

function p.forSeeArgsToTable (args, from, options)
	checkType("forSeeArgsToTable", 1, args, 'table')
	checkType("forSeeArgsToTable", 2, from, 'number', true)
	from = from or 1
	checkType("forSeeArgsToTable", 3, options, 'table', true)
	options = options or {}
	for k, v in pairs(forSeeDefaultOptions) do
		if options[k] == nil then options[k] = v end
	end
	
	local maxArg = 0
	for k, v in pairs(args) do
		if type(k) == 'number' and k > maxArg then maxArg = k end
	end
	
	local forTable = {}
	local i = from
	local terminated = false
	
	if options.extratext and i > maxArg then return nil end
	
	repeat
		local forRow = {}
		forRow.use = args[i]
		if not args[i] then terminated = true end
		
		forRow.pages = {}
		table.insert(forRow.pages, args[i + 1])
		
		while args[i + 2] == options.andKeyword do
			if args[i + 3] then
				table.insert(forRow.pages, args[i + 3])
			end
			i = i + 2
		end
		i = i + 2
		table.insert(forTable, forRow)
	until terminated or i > maxArg

	return forTable
end

function p.forSeeTableToString (forSeeTable, options)
	checkType("forSeeTableToString", 1, forSeeTable, "table", true)
	checkType("forSeeTableToString", 2, options, "table", true)
	options = options or {}
	for k, v in pairs(forSeeDefaultOptions) do
		if options[k] == nil then options[k] = v end
	end
	
	local strList = {}
	if forSeeTable then
		for k, v in pairs(forSeeTable) do
			local useStr = v.use or options.otherText
			
			-- Karakter setine duyarlı ilk harf büyütme işlemi
			useStr = mw.getContentLanguage():ucfirst(useStr)

			local pagesStr =
				p.andList(v.pages, true) or
				mFormatLink._formatLink{
					categorizeMissing = mHatnote.missingTargetCat,
					link = mHatnote.disambiguate(options.title)
				}
				
			-- Kardinalite (Çoğul/Tekil) kontrolü ve dinamik dize ataması
			local currentForm = options.forSeeForm
			if v.pages and #v.pages > 1 then
				currentForm = string.gsub(currentForm, "sayfasına", "sayfalarına")
			end
			
			local forSeeStr = string.format(currentForm, useStr, pagesStr)
			forSeeStr = punctuationCollapse(forSeeStr)
			table.insert(strList, forSeeStr)
		end
	end
	if options.extratext then table.insert(strList, punctuationCollapse(options.extratext..'.')) end
	
	return table.concat(strList, ' ')
end

function p._forSee (args, from, options)
	local forSeeTable = p.forSeeArgsToTable(args, from, options)
	return p.forSeeTableToString(forSeeTable, options)
end

function p.forSee (frame, from, options)
	mArguments = require('Modül:Bağımsız değişkenler')
	return p._forSee(mArguments.getArgs(frame), from, options)
end

return p