پرش به محتوا

پودمان:Footnotes/تمرین

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[نمایش] [ویرایش] [تاریخچه] [پاکسازی]

این پودمان {{sfn}}، {{harvard citation}} و انواع آن الگوها را پیاده‌سازی می‌کند..

-- این پودمان به‌دقت بومی‌سازی شده‌است. هنگام به‌روزرسانی دقت کنید
local num_con = require('Module:Numeral converter').convert
local getArgs = require('Module:Arguments').getArgs

f = {
	args_default = {
		bracket_left = '',
		bracket_right = '',
		bracket_year_left = '',
		bracket_year_right = '',
		postscript = '',
		page = '',
		pages = '',
		location = '',
		page_sep = 'ص.',
		pages_sep = 'صص.',
		ref = '',
		P1 = '',
		P2 = '',
		P3 = '',
		P4 = '',
		P5 = '',
		andOthers = 'و دیگران',
		amp = 'و',
		comma = '،',
	}
}

function trim( str )
	if str == nil then
		return nil
	end
	return mw.ustring.match (str, '^%s*(.-)%s*$')
end    

local function is_year (param)
	return mw.ustring.match (param, '^%d%d%d%d?%l?$') or mw.ustring.match (param,'^n%.d%.%l?$') or mw.ustring.match (param,'^nd%l?$') or mw.ustring.match (param,'^c%. %d%d%d%d?%l?$')
end


function core( args )
	local result

	if args.P5 ~= '' then
		if is_year (args.P5) then
			result = table.concat({args.P1, ' ', args.andOthers, ' ', args.bracket_year_left, args.P5, args.bracket_year_right})
		else
			args.P5 = ''														-- when P5 not a year don't include in anchor
			result = table.concat ({args.P1, ' ', args.andOthers})						-- and don't render it
		end
	elseif args.P4 ~= '' then
		if is_year (args.P4) then
			result = table.concat ({args.P1, args.comma, ' ', args.P2, ' ', args.amp, ' ', args.P3, ' ', args.bracket_year_left, args.P4, args.bracket_year_right})	-- three names and a year
		else
			result = table.concat ({args.P1, ' ', args.andOthers})						-- four names
		end

	elseif args.P3 ~= '' then
		if is_year (args.P3) then
			result = table.concat ({args.P1, ' ', args.amp, ' ', args.P2, ' ', args.bracket_year_left, args.P3, args.bracket_year_right})	-- two names and a year
		else
			result = table.concat ({args.P1, args.comma, ' ', args.P2, ' ', args.amp, ' ', args.P3})	-- three names
		end
			
	elseif args.P2 ~= '' then
		if is_year (args.P2) then
			result = table.concat ({args.P1, ' ', args.bracket_year_left, args.P2, args.bracket_year_right})	-- one name and year
		else
			result = table.concat ({args.P1, ' ', args.amp, ' ', args.P2})				-- two names
		end
		
	else
		result = args.P1														-- one name
	end

	if ('.' == result:sub(-1)) and ('' == args.page) and ('' == args.pages) and ('' == args.location) then
		args.postscript = ''		-- prevent double periods when date is 'n.d.'
	end
	
	if args.ref ~= 'none' then
		if args.ref ~= '' then
			result = '[[#' .. mw.uri.anchorEncode(args.ref) .. '|' .. result .. ']]'
		else
			result = '[[#CITEREF' .. mw.uri.anchorEncode(args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5) .. '|' .. result .. ']]'
		end
	end

	if args.page ~= '' then
		result = result .. args.comma .. ' ' .. args.page_sep .. ' ' .. args.page
	elseif args.pages ~= '' then
		result = result .. args.comma .. ' ' .. args.pages_sep .. ' ' .. args.pages
	end      

	if args.location ~= '' then
		result = result .. args.comma .. ' ' .. args.location
	end

	result = args.bracket_left .. result .. args.bracket_right .. args.postscript
	return result
end

function corefa( args )
	local result

	if args.P5 ~= '' then
		if is_year (args.P5) then
			result = table.concat({args.P1, ' ', args.andOthers, ' ', args.bracket_year_left, num_con("fa", args.P5), args.bracket_year_right})
		else
			args.P5 = ''														-- when P5 not a year don't include in anchor
			result = table.concat ({args.P1, ' ', args.andOthers})						-- and don't render it
		end
	elseif args.P4 ~= '' then
		if is_year (args.P4) then
			result = table.concat ({args.P1, args.comma, ' ', args.P2, ' ', args.amp, ' ', args.P3, ' ', args.bracket_year_left, num_con("fa", args.P4), args.bracket_year_right})	-- three names and a year
		else
			result = table.concat ({args.P1, ' ', args.andOthers})						-- four names
		end

	elseif args.P3 ~= '' then
		if is_year (args.P3) then
			result = table.concat ({args.P1, ' ', args.amp, ' ', args.P2, ' ', args.bracket_year_left, num_con("fa", args.P3), args.bracket_year_right})	-- two names and a year
		else
			result = table.concat ({args.P1, args.comma, ' ', args.P2, ' ', args.amp, ' ', args.P3})	-- three names
		end
			
	elseif args.P2 ~= '' then
		if is_year (args.P2) then
			result = table.concat ({args.P1, ' ', args.bracket_year_left, num_con("fa", args.P2), args.bracket_year_right})	-- one name and year
		else
			result = table.concat ({args.P1, ' ', args.amp, ' ', args.P2})				-- two names
		end
		
	else
		result = args.P1														-- one name
	end

	if ('.' == result:sub(-1)) and ('' == args.page) and ('' == args.pages) and ('' == args.location) then
		args.postscript = ''		-- prevent double periods when date is 'n.d.'
	end
	
	if args.ref ~= 'none' then
		if args.ref ~= '' then
			result = '[[#' .. mw.uri.anchorEncode(args.ref) .. '|' .. result .. ']]'
		else
			result = '[[#CITEREF' .. mw.uri.anchorEncode(args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5) .. '|' .. result .. ']]'
		end
	end

	if args.page ~= '' then
		result = result .. args.comma .. ' ' .. args.page_sep .. ' ' .. num_con("fa", args.page)
	elseif args.pages ~= '' then
		result = result .. args.comma .. ' ' .. args.pages_sep .. ' ' .. num_con("fa", args.pages)
	end      

	if args.location ~= '' then
		result = result .. args.comma .. ' ' .. args.location
	end

	result = args.bracket_left .. result .. args.bracket_right .. args.postscript
	return result
end

function f.harvard_core( frame )
	local args = {}
	local pArgs = getArgs(frame)

	args.bracket_left = pArgs.BracketLeft or ''
	args.bracket_right = pArgs.BracketRight or ''
	args.bracket_year_left = pArgs.BracketYearLeft or ''
	args.bracket_year_right = pArgs.BracketYearRight or ''
	args.postscript = pArgs.Postscript or ''
	if 'none' == args.postscript then
		args.postscript = ''
	end

	args.page = pArgs.p or pArgs.Page or pArgs.page or pArgs['ص'] or ''
	args.pages = pArgs.pp or pArgs.Pages or pArgs.pages or pArgs['صص'] or ''
	args.location = pArgs.Location or pArgs.loc or pArgs['محل'] or ''
	args.page_sep = trim(pArgs.PageSep) or ''
	args.pages_sep = trim( pArgs.PagesSep) or ''
	args.ref = pArgs.REF or '{{{REF}}}'
	args.P1 = trim( pArgs.P1 ) or ''
	args.P2 = trim( pArgs.P2 ) or ''
	args.P3 = trim( pArgs.P3 ) or ''
	args.P4 = trim( pArgs.P4 ) or ''
	args.P5 = trim( pArgs.P5 ) or ''
	args.andOthers = trim( pArgs.AndOthers ) or 'و دیگران'
	args.amp = trim( pArgs.amp ) or 'و'
	args.comma = trim( pArgs.comma ) or '،'

	return core( args )
end

function f.harvard_citation( frame )
	local args = f.args_default
	local pArgs = getArgs(frame)

	args.bracket_left = '('
	args.bracket_right = ')'
	args.page = pArgs.p or pArgs.page or pArgs['ص'] or ''
	args.pages = pArgs.pp or pArgs.pages or pArgs['صص'] or ''
	args.location = pArgs.loc or pArgs['محل'] or ''
	args.ref = pArgs.ref or pArgs.Ref or ''
	args.P1 = trim( pArgs[1] ) or ''
	args.P2 = trim( pArgs[2] ) or ''
	args.P3 = trim( pArgs[3] ) or ''
	args.P4 = trim( pArgs[4] ) or ''
	args.P5 = trim( pArgs[5] ) or ''
	args.andOthers = trim( pArgs.AndOthers ) or args.andOthers
	args.amp = trim( pArgs.amp ) or args.amp
	args.comma = trim( pArgs.comma ) or args.comma
	args.page_sep = trim( pArgs.PageSep ) or args.page_sep
	args.pages_sep = trim( pArgs.PagesSep ) or args.pages_sep

	return core( args )
end

function f.harvard_citation_no_bracket( frame )
	local args = f.args_default
	local pArgs = getArgs(frame)

	args.page = pArgs.p or pArgs.page or pArgs['ص'] or ''
	args.pages = pArgs.pp or pArgs.pages or pArgs['صص'] or ''
	args.location = pArgs.loc or pArgs['محل'] or ''
	args.ref = pArgs.ref or pArgs.Ref or ''
	args.P1 = trim( pArgs[1] ) or ''
	args.P2 = trim( pArgs[2] ) or ''
	args.P3 = trim( pArgs[3] ) or ''
	args.P4 = trim( pArgs[4] ) or ''
	args.P5 = trim( pArgs[5] ) or ''
	args.andOthers = trim( pArgs.AndOthers ) or args.andOthers
	args.amp = trim( pArgs.amp ) or args.amp
	args.page_sep = trim( pArgs.PageSep ) or args.page_sep
	args.pages_sep = trim( pArgs.PagesSep ) or args.pages_sep
	args.comma = trim( pArgs.comma ) or args.comma

	return core( args )
end

function f.sfn( frame )
	local args = f.args_default
	local pArgs = getArgs(frame)
	for k, v in pairs( frame.args ) do											-- for {{sfnp}}, override default with values provided in the #invoke:
		args[k] = v	   
	end

	args.postscript = pArgs.postscript or pArgs.ps or '.'
	if 'none' == args.postscript then
		args.postscript = ''
	end
	args.page = pArgs.p or pArgs.page or pArgs['ص'] or ''
	args.pages = pArgs.pp or pArgs.pages or pArgs['صص'] or ''
	args.location = pArgs.loc or pArgs['محل'] or ''
	args.ref = pArgs.ref or pArgs.Ref or ''
	args.P1 = trim( pArgs[1] ) or ''
	args.P2 = trim( pArgs[2] ) or ''
	args.P3 = trim( pArgs[3] ) or ''
	args.P4 = trim( pArgs[4] ) or ''
	args.P5 = trim( pArgs[5] ) or ''
	args.andOthers = trim( pArgs.AndOthers ) or args.andOthers
	args.amp = trim( pArgs.amp ) or args.amp
	args.comma = trim( pArgs.comma ) or args.comma
	args.page_sep = trim( pArgs.PageSep ) or args.page_sep
	args.pages_sep = trim( pArgs.PagesSep ) or args.pages_sep

	local result = core( args )
	local name = 'FOOTNOTE' .. args.P1 .. args.P2 .. 
	args.P3 .. args.P4 .. args.P5 .. args.page .. args.pages .. args.location

	result = frame:extensionTag{ name = 'ref', args = {name=name}, content=result }

	return result
end
--[[
The following function is idential to f.sfn with the exception that
it guarantees the output <ref> tag will have dir=ltr which is useful
for references copied from English Wikipedia
]]
function f.sfnltr( frame )
	local args = f.args_default
	local pArgs = getArgs(frame)
	for k, v in pairs( frame.args ) do											-- for {{sfnp}}, override default with values provided in the #invoke:
		args[k] = v
	end

	args.postscript = pArgs.postscript or pArgs.ps or '.'
	if 'none' == args.postscript then
		args.postscript = ''
	end
	args.page = pArgs.p or pArgs.page or pArgs['ص'] or ''
	args.pages = pArgs.pp or pArgs.pages or pArgs['صص'] or ''
	args.location = pArgs.loc or pArgs['محل'] or ''
	args.ref = pArgs.ref or pArgs.Ref or ''
	args.P1 = trim( pArgs[1] ) or ''
	args.P2 = trim( pArgs[2] ) or ''
	args.P3 = trim( pArgs[3] ) or ''
	args.P4 = trim( pArgs[4] ) or ''
	args.P5 = trim( pArgs[5] ) or ''
	args.andOthers = trim( pArgs.AndOthers ) or args.andOthers
	args.amp = trim( pArgs.amp ) or args.amp
	args.comma = trim( pArgs.comma ) or args.comma
	args.page_sep = trim( pArgs.PageSep ) or args.page_sep
	args.pages_sep = trim( pArgs.PagesSep ) or args.pages_sep

	local result = core( args )
	local name = 'FOOTNOTE' .. args.P1 .. args.P2 .. 
	args.P3 .. args.P4 .. args.P5 .. args.page .. args.pages .. args.location
	
	result = num_con("en", result)

	result = frame:extensionTag{ name = 'ref', args = {name=name,dir="ltr"}, content=result }

	return result
end

function f.sfnfa( frame )
	local args = f.args_default
	local pArgs = getArgs(frame)
	for k, v in pairs( frame.args ) do											-- for {{sfnp}}, override default with values provided in the #invoke:
		args[k] = v	   
	end

	args.postscript = pArgs.postscript or pArgs.ps or '.'
	if 'none' == args.postscript then
		args.postscript = ''
	end
	args.page = pArgs.p or pArgs.page or pArgs['ص'] or ''
	args.pages = pArgs.pp or pArgs.pages or pArgs['صص'] or ''
	args.location = pArgs.loc or pArgs['محل'] or ''
	args.ref = pArgs.ref or pArgs.Ref or ''
	args.P1 = trim( pArgs[1] ) or ''
	args.P2 = trim( pArgs[2] ) or ''
	args.P3 = trim( pArgs[3] ) or ''
	args.P4 = trim( pArgs[4] ) or ''
	args.P5 = trim( pArgs[5] ) or ''
	args.andOthers = trim( pArgs.AndOthers ) or args.andOthers
	args.amp = trim( pArgs.amp ) or args.amp
	args.comma = trim( pArgs.comma ) or args.comma
	args.page_sep = trim( pArgs.PageSep ) or args.page_sep
	args.pages_sep = trim( pArgs.PagesSep ) or args.pages_sep
	
	args.P1 = num_con("en", args.P1)
	args.P2 = num_con("en", args.P2)
	args.P3 = num_con("en", args.P3)
	args.P4 = num_con("en", args.P4)
	args.P5 = num_con("en", args.P5)

	local result = corefa( args )
	local name = 'FOOTNOTE' .. args.P1 .. args.P2 .. 
	args.P3 .. args.P4 .. args.P5 .. args.page .. args.pages .. args.location

	result = frame:extensionTag{ name = 'ref', args = {name=name}, content=result }

	return result
end

return f