יחידה:דגל

מתוך ויקיפדיה, האנציקלופדיה החופשית

הערה: היחידה נמצאת בבנייה, אין להשתמש בה עד לאישורה באולם הדיונים

  • א' ריק:
  • ב' אוגנדה: אוגנדהאוגנדה
  • ג' אלסקה: אלסקהאלסקה

-- This module implements {{תבנית:דגל1}}
local getArgs = require('Module:Arguments').getArgs
local inArray = require('Module:TableTools').inArray
local DateModule = require('Module:תאריך')

local function getData()
	local json_file = mw.title.new('דגל/list.json','Module'):getContent()
	local data = mw.text.jsonDecode(json_file)	
	return data
end

local function getFlag(entity, startDate)
	local filename = ""
	local data = getData()
	local flags = {}
	for k,v in pairs(data) do
		local aliases = v["aliases"]
		table.insert(aliases, k)
		if inArray(aliases, entity) then
			flags = v["flags"]
		end
	end
	for k,v in pairs(flags) do
		local flagStartDate, flagEndDate
		if v["start"] then
			flagStartDate = DateModule.newFromWikitext(v["start"])
		end
		if v["end"] then
			flagEndDate = DateModule.newFromWikitext(v["end"])
		end
		if startDate then
			if flagStartDate and flagEndDate then
				isAfterStart = DateModule.le(flagStartDate, startDate, true)
				isBeforeEnd = DateModule.le(startDate, flagEndDate, true)
				if isAfterStart and isBeforeEnd then
					filename = v["file"]
				end
			end
		else
			if flagStartDate and not flagEndDate then
				filename = v["file"]
			end
		end
	end
	if filename == "" then
		filename = "No flag.svg"
	end
	return filename
end

local function buildHtml(title, filename, options)
	local fileStr = ""
	local root = mw.html.create('span')
	root
		:addClass('nowrap')
	
	local iconSpan = root:tag('span')
	iconSpan
		:addClass('flagicon')
		
	local captionSpan = iconSpan:tag('span')
	captionSpan
		:css('display', 'none')
		:wikitext(title)
		
	fileStr = fileStr .. '[[קובץ:'
	fileStr = fileStr .. filename
	fileStr = fileStr .. '|' .. 'קישור=' .. title
	fileStr = fileStr .. '|' .. 'גבול'
	if options["size"] then
		fileStr = fileStr .. '|' .. options["size"] .. 'px'
	else
		fileStr = fileStr .. '|' .. '22x16px'
	end
	fileStr = fileStr .. '|' .. title
	fileStr = fileStr .. ']]'
	
	iconSpan
		:wikitext(fileStr)
		
	if options["showTitle"] then
		iconSpan
			:wikitext(" ")
		local titleStr = ""
		titleStr = titleStr .. '[['
		titleStr = titleStr .. title
		titleStr = titleStr .. '|' .. mw.ustring.gsub(title, " *[(].*[)]","")
		titleStr = titleStr .. ']]'
		local titleSpan = iconSpan:tag('span')
		titleSpan
			:css('font-size', options["fontSize"])
			:wikitext(titleStr)
	end

	return root
end

--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
local p = {}

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	local entityParam = args.entity or args[1]
	if entityParam == nil then
		return ''
	end
	local iconSizeParam = args.size or args[2]
	local showTitleParam = args.title or args[3]
	local fontSizeParam = args.font or args[4] or "inherit"
	local dateParam = args.date or args[5]
	if dateParam ~= nil then
		dateObj = DateModule.newFromWikitext(dateParam)
	end
	local caption = args.caption or args[6] or entityParam
	local flag = getFlag(entityParam, dateObj)
	local options = {
		["size"] = iconSizeParam,
		["showTitle"] = showTitleParam,
		["fontSize"] = fontSizeParam
	}
	html = buildHtml(caption, flag, options)
	
	return html
end
	
return p