Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Slovak municipality

Permanently protected module
From Wikipedia, the free encyclopedia

-- Project "[[:wikidata:Q132395970|Module:SK]]"

local sk = {}

sk.error_place="Unknown place."
sk.error_code="Unknown password."
sk.decimal_separator="."
sk.thousands_separator=","
sk.percentage_delimer=""
sk.percent_first_value=true
sk.year_suffix=""
sk.minus="−"
sk.open_th="<th>"
sk.open_td="<td>"
sk.none_value="–"
sk.religion_table_language="en"
sk.population_notice_title="Note on population"
sk.population_notice="The difference values of population numbers in the table \"Population statistic\" and in the sections \"Ethnicity\" & \"Religion\" is caused by the use of various statistical methods."
sk.religion_table_percent_plus=1
sk.table_class="wikitable sortable"
sk.religion_header='<tr><th>Religion</th><th>Number</th><th>Fraction</th></tr>'
sk.etno_header='<tr><th>Ethnicity</th><th>Number</th><th>Fraction</th></tr>'
sk.religion_caption_ref="{{cite web| title=Population - Basic results| publisher=Statistical Office of the Slovak republic|url=LINK| accessdate=2025-11-03|website= www.scitanie.sk}}"
sk.religion_caption='Census YEAR (1+ %)REFERENCE'
sk.etno_caption='Census YEAR (1+ %)REFERENCES'
sk.etno_relig_interval_notice="Sum of %s–%s."
sk.etno_relig_text=true
sk.etno_relig_text_content_etno="In year %s was  %s people by ethnicity %s."
sk.etno_relig_text_content_relig="In year %s was %s people by religion %s."
sk.etno_relig_text_item_etno="DELIMER NUMBER as NAME"
sk.etno_relig_text_item_relig="DELIMER NUMBER from NAME"
sk.etno_relig_text_delimer=", "
sk.etno_relig_text_delimer_end=" and "
sk.etno_relig_links_table=true
sk.etno_relig_links_text=true

function sk.etno_table(args)
	local all_translates
	local caption
	local caption_notice
	local caption_ref_first
	local caption_ref_second
	local dataset
	local dataset_group
	local etno_relig_content
	local etno_relig_item_head
	local etno_relig_item_content
	local etno_relig_items
	local etno_relig_items_content
	local item_count
	local label_raw
	local lau=args.args[1]
	local lau_length
	local lua_length
	local max_value
	local oend
	local ostart
	local percent
	local place
	local place_values
	local prefix_key
	local ref_url
	local ref_url_second
	local select_prefix
	local translate
	local translate_key
	local translate_key_term
	local translates
	local translates_links
	local type_percent
	local wiki
	local year
	
	if lau == "" then
		lau=sk.get_nuts_lua()
	end
	
	-- Select translates --
	all_translates=mw.ext.data.get("Translates of Population ethnicities of Slovak municipalities.tab")
	
	translates={}
	translates_links={}
	
	select_prefix=sk.religion_table_language..":"
	for i,items in ipairs(all_translates["data"]) do
		prefix_key=string.sub(items[1], 1,3);
		if prefix_key == select_prefix then
			translates[items[1]]=items[2]
			translates_links[items[1]]=items[3]
		end
	end
	
	-- Datasets
	
	lau_length=string.len(lau)
	if lau_length == 12 then
		dataset_group=tonumber(string.sub(lau,4,4));
	
		if not dataset_group then
			return ""
		end
		
		if dataset_group < 3 then
			dataset=mw.ext.data.get("Population ethnicities of Slovak municipalities (SK01 and SK02; last census).tab")
		else
			if dataset_group == 3 then
				dataset=mw.ext.data.get("Population ethnicities of Slovak municipalities (SK03, last census).tab")
			else
				dataset=mw.ext.data.get("Population ethnicities of Slovak municipalities (SK04, last census).tab")
			end
		end
	else
		dataset=mw.ext.data.get("Population ethnicities of Slovak supmunicipalities (last census).tab")
	end
	
	-- Place value statistic
	
	place_values=false
	
	for i,items in ipairs(dataset["data"]) do
		if items[1] == lau then
			place_values=items
			break
		end
	end
	
	if place_values == false then
		return ""
	end
	
	-- Place statistic
	
	place={}
	place['values']={}
	place['percents']={}
	
	for i,items in ipairs(dataset["schema"]["fields"]) do
		if i > 2 then
			label_raw=items["title"]
			if not label_raw then
				label_raw=items["title"]["sk"]
			end
			
			type_percent=false
			translate_key=sk.religion_table_language..":"..label_raw
			translate_key_term=translate_key
			ostart,oend=string.find(translate_key_term, " %(%%%)")
			if ostart then
				type_percent=true
				translate_key_term=string.sub(translate_key_term, 1, ostart-1)
			end
			
			--translate=translates[translate_key_term]
			translate=string.sub(translate_key_term, 4)
	
			if translate then
				if type_percent then
					place['percents'][translate]=place_values[i]
				else
					table.insert(place['values'],{tostring(translate),place_values[i]})
				end
			end
		end
	end

	-- Sort by value
	table.sort(place['values'], function (v, w) if v[2] == w[2] then return v[1]>w[1] end return v[2]>w[2] end)
	
	-- Table construction
	
	ostart,oend=string.find(dataset["sources"], "the year ")
	year=string.sub(dataset["sources"], ostart+9, ostart+12);
	
	lau_length=string.len(lau)
	
	if lau == "SK0422_0425" or lau == "SK_CAP" then
		if lau == "SK_CAP" then
			ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-ethnicity/KR/SK010/OK"
			ref_url_second="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-another-ethnicity/KR/SK010/OK"
			
		else
			ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-ethnicity/KR/SK042/OK"
			ref_url_second="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-another-ethnicity/KR/SK042/OK"
		end
	else
		if(lau_length == 3) then
			ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-ethnicity/SR/SK0/SR"
			ref_url_second="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-another-ethnicity/SR/SK0/SR"
		else
			if(lau_length == 5) then
				ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-ethnicity/KR/"..lau.."/KR"
				ref_url_second="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-another-ethnicity/KR/"..lau.."/KR"
			else
				if(lau_length == 6) then
					ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-ethnicity/OK/"..lau.."/OK"
					ref_url_second="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-another-ethnicity/OK/"..lau.."/OK"
				else
					if(lau_length == 12) then
						ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-ethnicity/OB/"..lau.."/OB"
						ref_url_second="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-another-ethnicity/OB/"..lau.."/OB"
					end
				end
			end
		end
	end
	
	caption_ref_first=string.gsub(sk.religion_caption_ref, "LINK", ref_url)
	caption_ref_first=args:callParserFunction("#tag", "ref", caption_ref_first)
	
	caption_ref_second=string.gsub(sk.religion_caption_ref, "LINK", ref_url_second)
	caption_ref_second=args:callParserFunction("#tag", "ref", caption_ref_second)
	
	caption_notice=""
	
	if lau == "SK0422_0425" or lau == "SK_CAP" then
		if lau == "SK0422_0425" then
			caption_notice=string.format(sk.etno_relig_interval_notice, "SK0422", "SK0425")
		else
			caption_notice=string.format(sk.etno_relig_interval_notice, "SK0101", "SK0105")
		end
		caption_notice=args:callParserFunction("#tag", "ref", caption_notice)
	end
	
	caption=string.gsub(sk.etno_caption, "REFERENCES", caption_ref_first..caption_ref_second..caption_notice)
	caption=string.gsub(caption, "YEAR", year)
	
	wiki='<table class="'..sk.table_class..'"><caption>'..caption..'</caption>'..sk.etno_header
	max_value=-1
	item_count=0;
	for i, v in ipairs(place['values']) do
		if v[2] > max_value then
			max_value=v[2]
		end
		
		if v[2] ~= 0 then
			item_count=item_count+1
		end
		
		percent=place['percents'][v[1]]
		if not percent then
			percent=0
		end
		if percent >= sk.religion_table_percent_plus then
			if percent then
				percent=sk.localised_number(tostring(percent))
			else
				percent=""
			end
			
			if sk.percent_first_value then
				percent=percent..sk.percentage_delimer.."%"
			else
				percent="%"..sk.percentage_delimer..percent
			end
			
			if sk.etno_relig_links_table then
				translate=translates_links[sk.religion_table_language..":"..v[1]]
				if translate == "" then
					translate=translates[sk.religion_table_language..":"..v[1]]
				end
			else
				translate=translates[sk.religion_table_language..":"..v[1]]
			end
			wiki=wiki.."<tr><td>"..translate.."</td><td>"..sk.localised_number(tostring(v[2])).."</td><td>"..percent.."</td></tr>"
		end
	end
	
	item_count=item_count-1 -- suma out
	
	translate=translates[sk.religion_table_language..":Spolu"]
	wiki=wiki.."<tr><td>"..translate.."</td><td>"..sk.localised_number(max_value).."</td></tr></table>"
	
	-- Etno/relig text
	if sk.etno_relig_text == false then
		wiki=wiki.."\n'''"..sk.population_notice_title.."''': ''"..sk.population_notice.."''\n\n"
		do return wiki end
	end
	
	etno_relig_items="";
	
	for i, v in ipairs(place['values']) do -- v = {place, count}
		if v[2] > 0 and i > 1 then -- first is suma of all that it's skeeping
			if i > 2  then
				item_count=item_count-1
				if item_count == 1 then
					etno_relig_item_head=sk.etno_relig_text_delimer_end
				else
					etno_relig_item_head=sk.etno_relig_text_delimer
				end
			else
				etno_relig_item_head=""
			end
			
			etno_relig_item_content=sk.etno_relig_text_item_etno
			
			if sk.etno_relig_links_text then
				translate=translates_links[sk.religion_table_language..":"..v[1]]
				if translate == "" then
					translate=translates[sk.religion_table_language..":"..v[1]]
				end
			else
				translate=translates[sk.religion_table_language..":"..v[1]]
			end
			
			etno_relig_item_content=string.gsub(etno_relig_item_content, "DELIMER", etno_relig_item_head)
			etno_relig_item_content=string.gsub(etno_relig_item_content, "NUMBER", sk.localised_number(tostring(v[2])))
			etno_relig_item_content=string.gsub(etno_relig_item_content, "NAME", translate)
			
			etno_relig_items=etno_relig_items..etno_relig_item_content
		end
	end
	
	wiki=wiki..string.format(sk.etno_relig_text_content_etno, year, sk.localised_number(max_value),etno_relig_items)
	wiki=wiki.."\n'''"..sk.population_notice_title.."''': ''"..sk.population_notice.."''\n\n"
	return wiki
end

function sk.religion_table(args)
	local all_translates
	local caption
	local caption_notice
	local caption_ref
	local caption_ref_first
	local dataset
	local dataset_group
	local item_count
	local label_raw
	local lau=args.args[1]
	local lau_length
	local max_value
	local oend
	local ostart
	local percent
	local place
	local place_values
	local prefix_key
	local ref_url
	local ref_url_second
	local select_prefix
	local translate
	local translate_key
	local translate_key_term
	local translates
	local translates_links
	local type_percent
	local wiki
	local year
	
	if lau == "" then
		lau=sk.get_nuts_lua()
	end
	
	-- Select translates --
	all_translates=mw.ext.data.get("Translates of Religious belief of Slovak municipalities.tab")
	
	translates={}
	translates_links={}
	
	select_prefix=sk.religion_table_language..":"
	for i,items in ipairs(all_translates["data"]) do
		prefix_key=string.sub(items[1], 1,3);
		if prefix_key == select_prefix then
			translates[items[1]]=items[2]
			translates_links[items[1]]=items[3]
		end
	end
	
	-- Datasets
	lau_length=string.len(lau)
	if lau_length == 12 then
		dataset_group=string.sub(lau,4,4);
	
		if not dataset_group then
			return ""
		end
		if tonumber(dataset_group) > 2 then
			dataset=mw.ext.data.get("Religious belief of Slovak municipalities (SK03 and SK04; last census).tab")
		else
			dataset=mw.ext.data.get("Religious belief of Slovak municipalities (SK01 and SK02; last census).tab")
		end
	else
		dataset=mw.ext.data.get("Religious belief of Slovak supmunicipalities (last census).tab")
	end
	
	-- Place value statistic
	
	place_values=false
	
	for i,items in ipairs(dataset["data"]) do
		if items[1] == lau then
			place_values=items
			break
		end
	end
	
	if place_values == false then
		return "hodnoty miest"
	end
	
	-- Place statistic
	
	place={}
	place['values']={}
	place['percents']={}
	
	for i,items in ipairs(dataset["schema"]["fields"]) do
		if i > 2 then
			label_raw=items["title"]
			if not label_raw then
				label_raw=items["title"]["sk"]
			end
			
			type_percent=false
			translate_key=sk.religion_table_language..":"..label_raw
			translate_key_term=translate_key
			ostart,oend=string.find(translate_key_term, " %(%%%)")
			if ostart then
				type_percent=true
				translate_key_term=string.sub(translate_key_term, 1, ostart-1)
			end
			translate_key_term=string.gsub(translate_key_term, " %(abs%.%)", "")
			
			--translate=translates[translate_key_term]
			translate=string.sub(translate_key_term, 4)
	
			if translate then
				if type_percent then
					place['percents'][translate]=place_values[i]
				else
					table.insert(place['values'],{translate,place_values[i]})
				end
			end
		end
	end
	
	-- Sort by value
	
	table.sort(place['values'], function (v, w) if v[2] == w[2] then return v[1]>w[1] end return v[2]>w[2] end)
	
	-- Table construction
	
	ostart,oend=string.find(dataset["sources"], "the year ")
	year=string.sub(dataset["sources"], ostart+9, ostart+12);
	
	lau_length=string.len(lau)

	if lau == "SK0422_0425" or lau == "SK_CAP" then
		if lau == "SK_CAP" then
			ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-religious-belief/KR/SK010/OK"
			ref_url_second="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-religious-belief/KR/SK010/OK"
			
		else
			ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-religious-belief/KR/SK042/OK"
			ref_url_second="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-religious-belief/KR/SK042/OK"
		end
	else
		if(lau_length == 3) then
			ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-religious-belief/SR/SK0/SR"
		else
			if(lau_length == 5) then
				ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-religious-belief/KR/"..lau.."/KR"
			else
				if(lau_length == 6) then
					ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-religious-belief/OK/"..lau.."/OK"
				else
					if(lau_length == 12) then
						ref_url="https://www.scitanie.sk/en/population/basic-results/structure-of-population-by-religious-belief/OB/"..lau.."/OB"
					end
				end
			end
		end
	end

	caption_ref_first=string.gsub(sk.religion_caption_ref, "LINK", ref_url)
	caption_ref=args:callParserFunction("#tag", "ref", caption_ref_first)
	
	caption=string.gsub(sk.religion_caption, "REFERENCE", caption_ref)
	caption=string.gsub(caption, "YEAR", year)
	
	caption_notice=""
	
	if lau == "SK0422_0425" or lau == "SK_CAP" then
		if lau == "SK0422_0425" then
			caption_notice=string.format(sk.etno_relig_interval_notice, "SK0422", "SK0425")
		else
			caption_notice=string.format(sk.etno_relig_interval_notice, "SK0101", "SK0105")
		end
		caption_notice=args:callParserFunction("#tag", "ref", caption_notice)
	end
	
	wiki='<table class="'..sk.table_class..'"><caption>'..caption..caption_notice..'</caption>'..sk.religion_header
	max_value=-1
	item_count=0;
	for i, v in ipairs(place['values']) do
		if v[2] > max_value then
			max_value=v[2]
		end
		
		if v[2] ~= 0 then
			item_count=item_count+1
		end
		
		percent=place['percents'][v[1]]
		if not percent then
			percent=0
		end
		if percent >= sk.religion_table_percent_plus then
			if percent then
				percent=sk.localised_number(tostring(percent))
			else
				percent=""
			end
			
			if sk.percent_first_value then
				percent=percent..sk.percentage_delimer.."%"
			else
				percent="%"..sk.percentage_delimer..percent
			end
			
			if sk.etno_relig_links_table then
				translate=translates_links[sk.religion_table_language..":"..v[1]]
				if translate == "" then
					translate=translates[sk.religion_table_language..":"..v[1]]
				end
			else
				translate=translates[sk.religion_table_language..":"..v[1]]
			end
			wiki=wiki.."<tr><td>"..translate.."</td><td>"..sk.localised_number(tostring(v[2])).."</td><td>"..percent.."</td></tr>"
		end
	end
	
	item_count=item_count-1 -- suma out
	
	translate=translates[sk.religion_table_language..":Spolu"]
	wiki=wiki.."<tr><td>"..translate.."</td><td>"..sk.localised_number(max_value).."</td></tr></table>"
	
	-- Etno/relig text
	if sk.etno_relig_text == false then
		do return wiki end
	end
	
	etno_relig_items="";
	
	for i, v in ipairs(place['values']) do -- v = {place, count}
		if v[2] > 0 and i > 1 then -- first is suma of all that it's skeeping
			if i > 2  then
				item_count=item_count-1
				if item_count == 1 then
					etno_relig_item_head=sk.etno_relig_text_delimer_end
				else
					etno_relig_item_head=sk.etno_relig_text_delimer
				end
			else
				etno_relig_item_head=""
			end
			
			etno_relig_item_content=sk.etno_relig_text_item_relig
			
			translate=translates[sk.religion_table_language..":m:"..v[1]]
			if not translate then
				if sk.etno_relig_links_text then
					translate=translates_links[sk.religion_table_language..":"..v[1]]
					if translate == "" then
						translate=translates[sk.religion_table_language..":"..v[1]]
					end
				else
					translate=translates[sk.religion_table_language..":"..v[1]]
				end
			end
			
			etno_relig_item_content=string.gsub(etno_relig_item_content, "DELIMER", etno_relig_item_head)
			etno_relig_item_content=string.gsub(etno_relig_item_content, "NUMBER", sk.localised_number(tostring(v[2])))
			etno_relig_item_content=string.gsub(etno_relig_item_content, "NAME", translate)
			
			etno_relig_items=etno_relig_items..etno_relig_item_content
		end
	end
	
	wiki=wiki..string.format(sk.etno_relig_text_content_relig, year, sk.localised_number(max_value),etno_relig_items)
	return wiki
end


function sk.table_list(args)
	local places=args.args[1]
	local items=mw.text.split(places,";", true)
	local output=""
	local data=false
	local data_sup=false
	local custom_text=false
	local label
	local lau
	local record
	local population
	local lang=mw.language.getContentLanguage()
	
	for i,item in ipairs(items) do
		label=""
		
		custom_text=string.find(item, "::")
		if custom_text then
			label=string.sub(item, custom_text+2)
			lau=string.sub(item, 1, custom_text-1)
		end
		
		
		if string.len(lau) == 12 then
			if not data then
				data=mw.ext.data.get("Statistics of Slovak municipalities.tab")
			end
			record=sk.get_line_data(data, lau)
		else
			if not data_sup then
				data_sup=mw.ext.data.get("Statistics of Slovak supmunicipalities.tab")
			end
			record=sk.get_line_data(data_sup, lau)
		end
		
		population=record[2]
		area=record[3]
		
		output=output.."<tr><td>"..label.."</td><td>"..sk.localised_number(area).."</td><td>"..lang:formatNum(population).."</td></tr>\n"
	end
	
	return output
end

function sk.get_nuts_lua()
	local place_info
	local nuts_lau
	
	code=mw.wikibase.getEntityIdForCurrentPage()
	if code == nil then
		return nil
	end
	
	-- Source of 2 codes: https://commons.wikimedia.org/w/index.php?title=Data:Statistics_of_Slovak_supmunicipalities.tab&oldid=1001162094
	if code == "Q1780"  then return "SK_CAP"	  end
	if code == "Q25409" then return "SK0422_0425" end
	
	if code == "Q214"   then return "SK0" end
	if code == "SK"     then return "SK0" end
	
	while 1 do
		place_info=mw.wikibase.getBestStatements(code,"P605")
		if place_info[1] == nil then
			break
		end
		nuts_lau=place_info[1]["mainsnak"]['datavalue']['value']
		if nuts_lau == nil then
		else
			return nuts_lau
		end
		break
	end
	
	while 1 do
		place_info=mw.wikibase.getBestStatements(code,"P782")
		if place_info[1] == nil then
			break
		end
		nuts_lau=place_info[1]["mainsnak"]['datavalue']['value']
		if nuts_lau == nil then
		else
			return nuts_lau
		end
		break
	end
	
	return nil
end

function sk.is_slovak(args)
	local nuts_lau=args.args[1]
	if nuts_lau == nil or nuts_lau == "" then
		nuts_lau=sk.get_nuts_lua()
		if nuts_lau == nil then
			return ""
		end
	end
	
	if string.sub(nuts_lau, 1, 2) == "SK" then
		return "1"
	end
	return ""
end

function sk.localised_number(number)
	local number2
	local offset
	local offset_end
	local first
	local second
	local first_length
	local Output

	number2=tostring(number)
	offset, offset_end=string.find(number2, "%.")
	if offset == nil then
		first=number
		second=""
	else
		first=string.sub(number2, 1, offset-1)
		second=sk.decimal_separator..string.sub(number2, offset+1, offset+2)
	end

	if first == nil then
		first=""
	end
	
	first_length=string.len(first)

	if(first_length <= 4) then
		return first..second
	end

	Output=string.sub(first, -3, -1)

	if(first_length > 3) then
		Output=string.sub(first, -6, -4)..sk.thousands_separator..Output
	end
	if(first_length > 6) then
		Output=string.sub(first, -9, -7)..sk.thousands_separator..Output
	end
	if(first_length > 9) then
		Output=string.sub(first, -12, -10)..sk.thousands_separator..Output
	end

	if second == nil then
		return Output
	else
		return Output..second
	end
end

function sk.delta_progress(count_a, count_b) -- count_a < count_b
	if count_a == 0 or count_b == 0 then
		return "–"
	end
	
	local value
	
	value=tostring(count_b/(count_a/100)-100)
	if string.sub(value, 1, 1) == "-" then
		value_string=string.sub(value, 2)
		value=sk.minus..sk.localised_number(value_string)
	else
		value="+"..sk.localised_number(value)
	end
	
	if sk.percent_first_value then
		value=value..sk.percentage_delimer.."%"
	else
		value="%"..sk.percentage_delimer..value
	end
	
	return value
end

function sk.get_line_data(data, nuts_lau)
	index=1
	while 1 do
		item=data.data[index]
		if item == nil then
			break
		end
		if item[1] == nuts_lau then
			break
		end
		index=index+1
	end
	
	if item == nil then
		mw.addWarning(sk.error_place)
	end
	
	return item
end

function sk.constant_code(code)
	if code == nil then
		return nil
	end
	
	if string.len(code) == 2 then
		-- Source of 2 codes: https://commons.wikimedia.org/w/index.php?title=Data:Statistics_of_Slovak_supmunicipalities.tab&oldid=1001162094
		if code == "BA"     then return "SK_CAP"      end
		if code == "KE"     then return "SK0422_0425" end
		
		if code == "SK"     then return "SK0" end
	end
	return code
end

-- Reference data are for population
function sk.population_table(args)
	local style=args.args[1] -- format
	local nuts_lau=args.args[2]  -- nuts / LAU, default: automatic
	local count_years
	local count_years_stop
	local data
	local head
	local index
	local item
	local i_start
	local i_end
	local text
	local one_step_year
	local one_step_values
	local value
	local value_string
	local year
	local years={}
	
	nuts_lau=sk.constant_code(nuts_lau)
	
	-- get NUTS/LUA for page
	if nuts_lau == nil or nuts_lau == "" then
		nuts_lau=sk.get_nuts_lua()
		if nuts_lau == nil then
			return ""
		end
	end

	-- select date line
	if string.len(nuts_lau) == 12 then
		data=mw.ext.data.get("Population statistic of Slovak municipalities (some years).tab")
	else
		data=mw.ext.data.get("Population statistic of Slovak supmunicipalities (some years).tab")
	end

	-- get years
	head=""
	text=data.sources
	i_start, i_end=string.find(text, "years: ")
	offset=i_start+7
	count_years=0
	while 1 do 
		year=string.sub(text, offset, offset+3)
		count_years=count_years+1
		years[count_years]=year
		if string.sub(text, offset+4, offset+4) == " " then
		    offset=offset+7
			break
		end
		offset=offset+6
	end
	year=string.sub(text, offset, offset+3)
	count_years=count_years+1
	years[count_years]=year
	count_years_stop=count_years+1
	
	head=""
	for i=2,count_years_stop do
		year_index=i-1
		while 1 do 
			if year_index+1 < count_years_stop then
				if (years[year_index]-years[year_index+1]) == -1 then
					break
				end
			end
			head=head..sk.open_th..years[year_index]..sk.year_suffix.."</th>"
			break
		end
	end
	
	if style == "h" then
		return head
	end

	-- values
	item=sk.get_line_data(data, nuts_lau)
	if item == nil then
		mw.addWarning(sk.error_place)
	end
	-- get values
	content=""
	
	if style == "d" or style == "Y" or style == "V" or style == "P" then
		one_step_year=""
		one_step_values=""
		one_step_progress=""
		
		content="<td></td>\n"
		for i=3,count_years_stop do
			year_index=i-1
			while 1 do
				-- check the first item with delta = 1
				if year_index+1 < count_years_stop then
					if (years[year_index]-years[year_index+1]) == -1 then
						one_step_year=one_step_year..sk.open_th..years[year_index]..sk.year_suffix.."</th>"
						one_step_values=one_step_values..sk.open_td..sk.localised_number(item[i]).."</td>"
						one_step_progress=one_step_progress.."<td></td>"
						break
					end
				end
				
				-- check the second item with delta = 1
				if year_index > 1 then
					if (years[year_index]-years[year_index-1]) == 1 then
						one_step_year=one_step_year..sk.open_th..years[year_index]..sk.year_suffix.."</th>"
						one_step_values=one_step_values..sk.open_td..sk.localised_number(item[i]).."</td>"
						one_step_progress=one_step_progress..sk.open_td..sk.delta_progress(item[i-1], item[i]).."</td>"
						
						content=content..sk.open_td..sk.delta_progress(item[i-2], item[i]).."</td>\n"
						break
					end
				end
				
				content=content..sk.open_td..sk.delta_progress(item[i-1], item[i]).."</td>\n"
				break
			end
		end
		
		if style == "Y" then
			return one_step_year
		end
		if style == "V" then
			return one_step_values
		end
		if style == "P" then
			return one_step_progress
		end

		
		return content
	end
	
	content=""
	for i=2,count_years_stop do
		year_index=i-1
		while 1 do 
			if year_index+1 < count_years_stop then
				if (years[year_index]-years[year_index+1]) == -1 then
					break
				end
			end
			content=content..sk.open_td..sk.localised_number(item[i]).."</td>"
			break
		end
	end
		
	return content
end

function sk.sk(args)
	local style=args.args[1] -- format
	local nuts_lau=args.args[2]  -- nuts / LAU, default: automatic
	local code
	local data
	local index
	local info
	local item
	local i_start
	local i_start2
	local i_end
	local lang
	local last_acces
	local last_update
	local place_info
	local url_population
	local url_area
	local year
	
	lang=mw.language.getContentLanguage()
	
	-- select date line
	
	nuts_lau=sk.constant_code(nuts_lau)
	
	if nuts_lau == nil or nuts_lau == "" then
		nuts_lau=sk.get_nuts_lua()
		if nuts_lau == nil then
			return ""
		end
	end
	
	if style == "i" then
		local parameters={args={nuts_lau}}
		return sk.is_slovak(parameters)
	end
	
	if style == "e" then
		i_start, i_end=string.find(nuts_lau, "_")
		if i_start == nil then
			return nuts_lau
		else
			return ""
		end
	end

	-- select date line
	if string.len(nuts_lau) == 12 then
		data=mw.ext.data.get("Statistics of Slovak municipalities.tab")
	else
		data=mw.ext.data.get("Statistics of Slovak supmunicipalities.tab")
	end
	
	-- get NUTS/LUA for page
	if style == "l" or style == "u" or style == "y" then
	else
		item=sk.get_line_data(data, nuts_lau)
		
		if item == nil then
			mw.addWarning(sk.error_place)
		end
	end
	
	
	-- year
	i_start, i_end=string.find(data.sources, "year: ")
	year=string.sub(data.sources, i_start+6, i_start+9)
	
	-- last acces
	i_start, i_end=string.find(data.sources, "acces: ")
	if i_start == nil then
		i_start, i_end=string.find(data.sources, "access: ")
		last_acces=string.sub(data.sources, i_start+8, i_start+17)
	else
		last_acces=string.sub(data.sources, i_start+7, i_start+16)
	end
	
	
	-- last update
	i_start, i_end=string.find(data.sources, "update: ")
	last_update=string.sub(data.sources, i_start+8, i_start+17)
	
	-- url_population
	i_start, i_end=string.find(data.sources, "http")
	i_start2, i_end=string.find(string.sub(data.sources, i_start), " ")
	i_start2=i_start+i_start2-1
	url_population=string.sub(data.sources, i_start, i_start2)
	
	-- url_area
	i_start, i_end=string.find(data.sources, "http")
	info=string.sub(data.sources, i_start+1)
	i_start, i_end=string.find(info, "http")
	i_start2, i_end=string.find(string.sub(info, i_start), " ")
	i_start2=i_start+i_start2-1
	url_area=string.sub(info, i_start, i_start2)
	
	-- population_id
	i_start, i_end=string.find(url_population, "/vbd_dem/")
	info=string.sub(url_population, i_start+9)
	i_start, i_end=string.find(info, "/")
	population_id=string.sub(info, 1, i_start-1)
	
	-- area_id
	i_start, i_end=string.find(url_area, "/vbd_dem/")
	info=string.sub(url_area, i_start+9)
	i_start, i_end=string.find(info, "/")
	area_id=string.sub(info, 1, i_start-1)
	
	-- render
	i_start, i_end = string.find(style, "%%")
	if i_start == nil then
		if style == "p" or style == "o" or style == "d" then
			if item[2] then
				if style == "p" then return sk.localised_number(item[2]) end
				if style == "o" then return lang:formatNum(item[2]) end
				if style == "d" then
					if item[2] ~= 0 then
						if item[3] and item[3] ~= 0 then
							return sk.localised_number(item[2]/item[3])
						end
					end
				end
			end
			return sk.none_value
		end

		if style == "a" then
			if item[3] then
				return sk.localised_number(item[3])
			else
				return sk.none_value
			end
		end
		
		if style == "P" then return url_population end
		if style == "A" then return url_area end
		if style == "y" then return year end
		if style == "l" then return last_acces end
		if style == "u" then return last_update end
		if style == "r" then return population_id end
		if style == "b" then return area_id end
		mw.addWarning(sk.error_code)
	else
		Output=style
		Output=string.gsub(Output, "%%p", sk.localised_number(item[2]))
		Output=string.gsub(Output, "%%P", url_population)
		Output=string.gsub(Output, "%%a", sk.localised_number(item[3]))
		Output=string.gsub(Output, "%%o", lang:formatNum(item[2]))
		Output=string.gsub(Output, "%%A", url_area)
		local density;
		if item[2] == nil or item[2] == 0 then
			density=sk.none_value;
		else
			density=sk.localised_number(item[2]/item[3])
		end
		Output=string.gsub(Output, "%%d", density)
		Output=string.gsub(Output, "%%y", year)
		Output=string.gsub(Output, "%%l", last_acces)
		Output=string.gsub(Output, "%%u", last_update)
		Output=string.gsub(Output, "%%r", population_id)
		Output=string.gsub(Output, "%%b", area_id)
		
		return Output
	end
end

return sk