Modul:SK: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
preuzeto s hr.wikipedije
 
mNema sažetka uređivanja
 
Nije prikazano 9 međuinačica
Redak 1: Redak 1:
local sk = {}
local p = {}


sk.error_place="Nepoznato mjesto."
p.db = {}
sk.error_code="Nepoznata šifra."
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>"


function sk.get_nuts_lua()
--------------------------------------------------------------------
local place_info
-- 1) Učitavanje data modula
local nuts_lau
--------------------------------------------------------------------
code=mw.wikibase.getEntityIdForCurrentPage()
if code == nil then
return nill
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 == nill 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 == nill 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 == nill then
first=""
end
first_length=string.len(first)


if(first_length <= 4) then
local modules = {
return first..second
    "Modul:SK/Data1",
end
    "Modul:SK/Data2",
    "Modul:SK/Data3",
    "Modul:SK/Data4",
    "Modul:SK/Data5",
    "Modul:SK/Data6",
    "Modul:SK/Data7",
}


Output=string.sub(first, -3, -1)
for _, name in ipairs(modules) do
    local ok, raw = pcall(require, name)
    if ok and type(raw) == "string" then
        for line in raw:gmatch("[^\n]+") do


if(first_length > 3) then
            -- NOVI FORMAT: 6 polja
Output=string.sub(first, -6, -4)..sk.thousands_separator..Output
            local code, naziv, pop, area, year, density =
end
                line:match("([^|]+)|([^|]+)|([^|]+)|([^|]+)|([^|]+)|([^|]+)")
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
            if code then
return Output
                p.db[code] = {
else
                    naziv = naziv,
return Output..second
                    pop = tonumber(pop),
end
                    area = tonumber(area),
                    year = tonumber(year),
                    density = density, -- ostaje string jer koristiš decimalni zarez
                }
            end
        end
    end
end
end


function sk.localised_number_strict(number) -- NNNN is as N.NNNN
--------------------------------------------------------------------
local number2
-- 2) Format funkcije
local offset
--------------------------------------------------------------------
local offset_end
local first
local second
local first_length
local Output


number2=tostring(number)
function p.format(code, mode)
offset, offset_end=string.find(number2, "%.")
    local d = p.db[code]
if offset == nill then
    if not d then return "" end
first=number
second=""
else
first=string.sub(number2, 1, offset-1)
second=sk.decimal_separator..string.sub(number2, offset+1, offset+2)
end


first_length=string.len(first)
    if mode == "p" then return d.naziv end
    if mode == "a" then return d.pop end
    if mode == "d" then return d.area end
    if mode == "g" then return d.year end
    if mode == "y" then return d.density end


if(first_length <= 3) then
    return ""
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
end


function sk.delta_progress(count_a, count_b) -- count_a < count_b
--------------------------------------------------------------------
if count_a == 0 or count_b == 0 then
-- 3) Glavna funkcija (POPRAVLJENA)
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)
function p.sk(frame)
index=1
     local args = frame.args
while 1 do
    local mode = args[1]
item=data.data[index]
    local code = args[2]
if item == nil then
break
end
if item[1] == nuts_lau then
break
end
index=index+1
end
if item == nil then
error(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 == nill 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
error(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)
    -- Ako je code prazan, ili sadrži vitlaste zagrade (nije obrađen), uzmi iz parenta (članka)
local style=args.args[1] -- format
    if not code or code == "" or code:find("{") then
local nuts_lau=args.args[2]  -- nuts / LAU, default: automatic
        local parentArgs = frame:getParent().args
local code
        code = parentArgs[1]
local data
    end
local index
local info
local item
local i_start
local i_start2
local i_end
local last_acces
local last_update
local place_info
local url_population
local url_area
local year
-- 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 == nill 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
    -- Ako i dalje nemamo code, vrati prazno (npr. na stranici samog predloška)
if string.len(nuts_lau) == 12 then
    if not mode or not code or code == "" or code:find("{") then  
data=mw.ext.data.get("Statistics of Slovak municipalities.tab")
        return ""  
else
    end
data=mw.ext.data.get("Statistics of Slovak supmunicipalities.tab")
   
end
    -- Očisti razmake (trim)
    code = code:match("^%s*(.-)%s*$")
-- get NUTS/LUA for page
   
if style == "l" or style == "u" or style == "y" then
    return p.format(code, mode)
else
item=sk.get_line_data(data, nuts_lau)
if item == nil then
error(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" then return sk.localised_number(item[2]) end
if style == "P" then return url_population end
if style == "o" then return sk.localised_number_strict(item[2]) end
if style == "a" then return sk.localised_number(item[3]) end
if style == "A" then return url_area end
if style == "d" then return sk.localised_number(item[2]/item[3]) 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
error(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", sk.localised_number_strict(item[2]))
Output=string.gsub(Output, "%%A", url_area)
Output=string.gsub(Output, "%%d", sk.localised_number(item[2]/item[3]))
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
end


return sk
return p  -- (ili 'return h' ovisno o tome koji modul uređuješ)

Posljednja izmjena od 26. ožujak 2026. u 13:01

Dokumentacija modula
== About ==

Get statistic data about Slovak municipalities.

This template is a sibling of Predložak:SK.

This template use the data from CC-BY database.

Original first author: meta.wikimedia.org/wiki/User:Dušan Kreheľ

Reference implementation is on hrwiki.

Parameters

First paramer

Meaning Parameter Code Code output
Population p {{#invoke:SK|sk|p|SK518581}} Lešť (vojenský obvod)
Population
(source URL)
P {{#invoke:SK|sk|P|SK0417524395}}<br>{{#invoke:SK|sk|P|SK}}<br>{{#invoke:SK|sk|P|SK0106}}

Population
(ex. for table)
p {{#invoke:SK|sk|o|SK0417524395}}
Population Id r {{#invoke:SK|sk|r|SK0417524395}}
Area a {{#invoke:SK|sk|a|SK0417524395}}
Area
(source URL)
A {{#invoke:SK|sk|A|SK0417524395}}
Area Id b {{#invoke:SK|sk|b|SK0417524395}}
Population density
(as population/area)
d {{#invoke:SK|sk|d}}
Data year y {{#invoke:SK|sk|y}}
Last acces l {{#invoke:SK|sk|l}}
Last update u {{#invoke:SK|sk|u}}
Is slovak municipality
(none support %i)
i {{#invoke:SK|sk|i|SK0417524395}}
Get NUTS/LAU code
(none support %e)
e

You can used both the format string like printf() with parameters from table.

Code: {{#invoke:SK|sk|%p (source: %P)|SK0417524395}}
Output:

Second parameter

It is a STUN or LAU code for place sup-place (ex. SK0417524395). If none, then code is automatic based on the actual page.


local p = {}

p.db = {}

--------------------------------------------------------------------
-- 1) Učitavanje data modula
--------------------------------------------------------------------

local modules = {
    "Modul:SK/Data1",
    "Modul:SK/Data2",
    "Modul:SK/Data3",
    "Modul:SK/Data4",
    "Modul:SK/Data5",
    "Modul:SK/Data6",
    "Modul:SK/Data7",
}

for _, name in ipairs(modules) do
    local ok, raw = pcall(require, name)
    if ok and type(raw) == "string" then
        for line in raw:gmatch("[^\n]+") do

            -- NOVI FORMAT: 6 polja
            local code, naziv, pop, area, year, density =
                line:match("([^|]+)|([^|]+)|([^|]+)|([^|]+)|([^|]+)|([^|]+)")

            if code then
                p.db[code] = {
                    naziv = naziv,
                    pop = tonumber(pop),
                    area = tonumber(area),
                    year = tonumber(year),
                    density = density, -- ostaje string jer koristiš decimalni zarez
                }
            end
        end
    end
end

--------------------------------------------------------------------
-- 2) Format funkcije
--------------------------------------------------------------------

function p.format(code, mode)
    local d = p.db[code]
    if not d then return "" end

    if mode == "p" then return d.naziv end
    if mode == "a" then return d.pop end
    if mode == "d" then return d.area end
    if mode == "g" then return d.year end
    if mode == "y" then return d.density end

    return ""
end

--------------------------------------------------------------------
-- 3) Glavna funkcija (POPRAVLJENA)
--------------------------------------------------------------------

function p.sk(frame)
    local args = frame.args
    local mode = args[1]
    local code = args[2]

    -- Ako je code prazan, ili sadrži vitlaste zagrade (nije obrađen), uzmi iz parenta (članka)
    if not code or code == "" or code:find("{") then
        local parentArgs = frame:getParent().args
        code = parentArgs[1]
    end

    -- Ako i dalje nemamo code, vrati prazno (npr. na stranici samog predloška)
    if not mode or not code or code == "" or code:find("{") then 
        return "" 
    end
    
    -- Očisti razmake (trim)
    code = code:match("^%s*(.-)%s*$")
    
    return p.format(code, mode)
end

return p  -- (ili 'return h' ovisno o tome koji modul uređuješ)