Module:Ημερολόγια

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
Documentation icon Τεκμηρίωση module[δημιουργία]
p ={}

local df = require("Module:DateFunctions")
--επιστρέφει το κείμενο σε div με σφάλμα
--και εντάσσει τη σελίδα σε κατηγορία με σφάλματα
errorhere = function(errstring)
    return '<div class="error">' .. errstring .. '</div>[[Κατηγορία:Σελίδες με σφάλματα στο ημερολόγιο]]'
end

--επιστρέφει true αν είναι αλφαριθμητικό και έχει έστω και έναν χαρακτήρα
exeikati = function(onearg)
	if onearg == nil then 
		return false 
	end
	if type(onearg) == 'string' then
    	return  (mw.ustring.len(onearg) > 0)
	else
		return false
	end	
end

monthtable = function (poiosmhnas)
    local currentdays = {}
    local tmpnum = 0
    local monthnum = tonumber(poiosmhnas)
    local monthname = df['μήνες'][monthnum]['όνομα']
    local monthg = df['μήνες'][monthnum]['γεν']
    local etos = os.date('*t').year
    local firstday = df.get_day_of_week (1, monthnum, etos)
    if firstday == 7 then
    	firstday = 1
	else
		firstday = firstday + 1
	end
	
    local monthdays = df['μήνες'][monthnum]['ημ'] 
    --φέρνει 29 για το φλεβάρη από το: Module:DateFunctions
    if monthnum == 2 and (not df.isleapyear(etos)) then monthdays = 28 end
    for xcounter = 1,7 do
        if firstday > xcounter then 
            currentdays[xcounter] = '|colspan="1"| &nbsp;'
        else
            tmpnum = (xcounter - firstday + 1)
            currentdays[xcounter] = '| [[' .. tmpnum .. ' ' .. monthg .. '|' .. tmpnum ..']]'
        end
    end
    for xcounter = 8,28 do
        tmpnum = (xcounter - firstday + 1)
        currentdays[xcounter] = '| [[' .. tmpnum .. ' ' .. monthg .. '|' .. tmpnum ..']]'
    end
    for xcounter = 29,35 do
        if monthdays + firstday > xcounter then 
            tmpnum = (xcounter - firstday + 1)
            currentdays[xcounter] = '| [[' .. tmpnum .. ' ' .. monthg .. '|' .. tmpnum ..']]'
        else
            currentdays[xcounter] = '|colspan="1"| &nbsp;'
        end
    end
 
    newtable = [=[{| style="float:right; margin-left: 1em; text-align:center; background-color:#f9f9f9; border:1px solid #aaaaaa;font-size:90%;"
|- style="background:#ccccff;"
|colspan="7"|'''[[]=] ..  monthname  ..  [=[]]''' 
|- style="background:#ccccff;"
|width="14%"| Κυρ.
|width="14%"| Δευτ.
|width="14%"| Τρ.
|width="14%"| Τετ.
|width="14%"| Πέμ.
|width="14%"| Παρ.
|width="14%"| Σάβ.
|-]=]
    local daytoshow = 0
    for ycounter = 1,5 do
        for xcounter = 1,7 do
            --if ycounter < 2 then
                --daytoshow = xcounter
            --else
            daytoshow = (((ycounter - 1 ) * 7) + xcounter)
            --mw.log(type(daytoshow))
            newtable = newtable .. '\n' .. currentdays[daytoshow] .. '\n'
        end
        newtable = newtable .. '\n' .. '|-'
        --αν έχει 28 ημέρες και τις έχεις ήδη δείξει βγες
        if (monthdays + firstday) < 29 and (daytoshow > 27) then break end
    end
    newtable = newtable .. [=[
 
|colspan="7"| &nbsp;
|- style="background:#ccccff"
|colspan="7"| [[]=] .. etos .. ']]\n' .. [=[
|}]=]
    return newtable
end
 
--συνάρτηση που δίνει τον πίνακα του μήνα (με το τρέχον έτος του server)
p['πίνακαςμήνα'] = function (frame)
    local lemmaargs = frame:getParent().args
    if lemmaargs[1] == nil then return errorhere('Δεν δόθηκε η παράμετρος μήνας!') end
    return frame:preprocess(monthtable(lemmaargs[1]))
end

--συνάρτηση που δίνει τις ημέρες του μήνα (με το τρέχον έτος του server)
--σε σειρά οριζόντια
p['πίνακαςημερώνμήνα'] = function (frame)
    local lemmaargs = frame:getParent().args
    if lemmaargs[1] == nil then return errorhere('Δεν δόθηκε η παράμετρος μήνας!') end
    local etos = os.date('*t').year
    local monthnum = tonumber(poiosmhnas)
    local monthname = df['μήνες'][monthnum]['όνομα']
    local monthgen = df['μήνες'][monthnum]['γεν']
    local monthdays = df['μήνες'][monthnum]['ημ']
    -- αν δεν τις βάζουμε όλες του φλεβάρη...  
	--if monthnum == 2 and df.isleapyear(etos) then monthdays = 29 end	

    local oneline = '<center>'
    for xcounter = 1, monthdays do
        oneline = oneline .. ' [[' .. xcounter .. ' ' .. monthgen .. '|' .. xcounter .. ']] '
        if monthdays > xcounter then  oneline = oneline .. '|' end
    end
    oneline = oneline .. '</center>'
    return frame:preprocess(oneline)    
end

p['5μήνες']  = function (frame)
    local lemmaargs = frame:getParent().args
    if lemmaargs['μ'] == nil then return errorhere('Δεν δόθηκε η παράμετρος μήνας!') end
    local output = '<center>'
    local month = tonumber(lemmaargs['μ'])
    firstmonth = month-3
    
    for xcounter = 1,2 do
        output = output .. '[[' .. df.getmonthname(df.realmonthnum(firstmonth + xcounter)) .. ']] | '
    end
    output = output .. '<b>[[' .. df.getmonthname(df.realmonthnum(firstmonth + 3)) .. ']]</b>'
    for xcounter = 4,5 do
        output = output .. ' | [[' .. df.getmonthname(df.realmonthnum(firstmonth + xcounter)) .. ']]'
    end
	output = output .. '</center>'
    return frame:preprocess(output)             

end

--###############################
--το σκεπτικό είναι να μετατρέπουμε σε ακέραιο αριθμό το έτος.
--Το πρόβλημα είναι ότι στους υπολογισμούς προκύπτουν και έτη μηδέν.
--Αυτά δεν μπορούν απλά να μετατραπούν σε έτος 1 μ.Χ. ή 1 π.Χ.
--(Θα πρέπει να γνωρίζουμε αν προήλθαν από πρόσθεση ή αφαίρεση)

--Μάλλον καλύτερη προσέγγιση είναι:
--Αντιστοιχούμε όλα τα έτη μ.Χ. στον αντίστοιχο ακέραιο αριθμό 
--και τα έτη π.Χ. σε αρνητικό στον οποίο προσθέτουμε 1 
--Έτσι έχουμε
--(..., 235 π.Χ., ..., 3 π.Χ., 2 π.Χ, 1 π.Χ.)
--(..., -234, ..., -2, -1, 0)
--για τα υπόλοιπα (δεκαετίες, αιώνες, χιλιετίες)
--χρησιμοποιούμε ένα οποιοδήποτε πραγματικό έτος τους 
--για να πάρουμε την ονομασία τους

centurystring = function(etosasnum)
    --αν είναι αρνητικός αριθμός
    --τα έτη 0 μέχρι -99 είναι στον 1ο π.Χ. αιώνα
    --αυτό το παίρνουμε επειδή μετατρέπονται σε 100 μέχρι 199    
    if etosasnum < 1 then
        return tostring(math.floor((100+math.abs(etosasnum))/100)) .. 'ος αιώνας π.Χ.'
    end
    --αλλιώς προσθέτουμε 99
    --ώστε τα έτη 1 μέχρι 100
    --να μας δώσουν 100 μέχρι 199
    return tostring(math.floor((etosasnum+99)/100)) .. 'ος αιώνας'
end

decadestring = function(etosasnum)
    --ας μην έχουμε δεκαετίες π.Χ. για την ώρα
    --για αυτές θα χρειαστεί άλλη προσέγγιση αφού πρέπει να περιλμβάνουν 2 έτη στον τίτλο
    --ας τις περιορίσουμε επίσης στο διάστημα 1700 μέχρι τώρα 
    if etosasnum < 1 then return nil end
    return tostring(10 * math.floor(etosasnum/10) )
end

yearstring = function(etosasnum)
    if etosasnum < 1 then
        return tostring(math.abs(etosasnum) + 1) .. ' π.Χ.'
    end
    return tostring(etosasnum)
end

--[=[Δοκιμές στη κονσόλα αποσφαλμάτωσης (debug console) στο κάτω μέρος
με
=p.aiones(1)
=p.aiones(1999)
=p.aiones(0)  --δηλαδή 1 π.Χ.
κλπ.
]=]
p.aiones = function(etosasnum)
    local output = '[[' .. centurystring(etosasnum - 200) .. '|◄]]' 
    output = output .. ' [[' .. centurystring(etosasnum - 100) .. ']]' 
    output = output .. ' [[' .. centurystring(etosasnum) .. ']]' 
    local tempstr = centurystring(etosasnum + 100)
    --ΠΡΟΣΟΧΗ!! expensive!!
    --ίσως να ελέγχουμε αν είναι μετά από τώρα και να μην το εμφανίζουμε
    --θεωρώντας ότι δεν θα υπάρχει
    if mw.title.new(tempstr).exists then
    	output = output .. ' [[' .. tempstr .. ']]' 
	    tempstr = centurystring(etosasnum + 200)
	    --ΠΡΟΣΟΧΗ!! expensive!!
	    if mw.title.new(tempstr).exists then
	    	output = output .. ' [[' .. tempstr .. '|►]]' 
    	end
    end
    return output
end

--[=[Δοκιμές στη κονσόλα αποσφαλμάτωσης (debug console) στο κάτω μέρος
με
=p.dekaeties(1)
=p.dekaeties(1999)
=p.dekaeties(0)  --δηλαδή 1 π.Χ. --εεε... χμμμμ... Όχι ακόμα!
κλπ.
]=]
p.dekaeties = function(etosasnum)
    --Πρέπει να αποφασιστεί τι θα κάνουμε
    --1. Να ελέγχουμε αν υπάρχει (expensive)
    --2. Να το βάζουμε έτσι κι αλλιώς (θα έχει κόκκινα που μπορεί κάποιος χρήστης να τα δημιουργήσει μόνο και μόνο επειδή έκανε κλικ)
    --3. Να έχουμε ένα όριο προς τα πίσω πριν από το οποίο δεν θα δημιουργήσουμε δεκαετίες (υπάρχουν όμως ήδη ακόμα και π.Χ.)
    --Η τρέχουσα προσέγγιση είναι η 2
    --Για την ώρα δεν επιστρέφει πριν το 30 μ.Χ.
    if etosasnum < 30 then return nil end
    local output = '[[Δεκαετία ' .. decadestring(etosasnum - 20) .. '|◄]]' 
    output = output .. ' [[Δεκαετία ' .. decadestring(etosasnum - 10) .. ']]' 
    output = output .. ' [[Δεκαετία ' .. decadestring(etosasnum) .. ']]' 
    local tempstr = 'Δεκαετία ' .. decadestring(etosasnum + 10)
    --ΠΡΟΣΟΧΗ!! expensive!!
    --ίσως να ελέγχουμε αν είναι μετά από τώρα και να μην το εμφανίζουμε
    --θεωρώντας ότι δεν θα υπάρχει
    if mw.title.new(tempstr).exists then
    	output = output .. ' [[Δεκαετία ' .. tempstr .. ']]' 
	    tempstr = 'Δεκαετία ' .. decadestring(etosasnum + 20)
	    --ΠΡΟΣΟΧΗ!! expensive!!
	    if mw.title.new(tempstr).exists then
	    	output = output .. ' [[Δεκαετία ' .. tempstr .. '|►]]' 
    	end
    end
    return output
end

--[=[Δοκιμές στη κονσόλα αποσφαλμάτωσης (debug console) στο κάτω μέρος
με
=p.eth(1)
=p.eth(1999)
=p.eth(0)  --δηλαδή 1 π.Χ.
κλπ.
]=]
p.eth = function(etosasnum)
    local output = '[[' .. yearstring(etosasnum - 10) .. '|◄◄]]' 
    output = output .. ' [[' .. yearstring(etosasnum - 5) .. '|◄]]' 
    output = output .. ' [[' .. yearstring(etosasnum - 4) .. ']]' 
    output = output .. ' [[' .. yearstring(etosasnum - 3) .. ']]' 
    output = output .. ' [[' .. yearstring(etosasnum - 2) .. ']]' 
    output = output .. ' [[' .. yearstring(etosasnum - 1) .. ']]' 
    output = output .. ' [[' .. yearstring(etosasnum) .. ']]' 
    local tempstr = yearstring(etosasnum + 1)
    --ΠΡΟΣΟΧΗ!! expensive!!
    --καλύτερα ίσως να ελέγχουμε αν είναι μετά από σήμερα και να μην το εμφανίζουμε
    --θεωρώντας ότι δεν θα υπάρχει
    if mw.title.new(tempstr).exists then
    	output = output .. ' [[' .. tempstr .. ']]' 
	    tempstr = yearstring(etosasnum + 2)
	    --ΠΡΟΣΟΧΗ!! expensive!!
	    if mw.title.new(tempstr).exists then
	    	output = output .. ' [[' .. tempstr .. ']]' 
	        tempstr = yearstring(etosasnum + 3)
	        --ΠΡΟΣΟΧΗ!! expensive!!
	        if mw.title.new(tempstr).exists then
	        	output = output .. ' [[' .. tempstr .. ']]' 
	            tempstr = yearstring(etosasnum + 4)
	            --ΠΡΟΣΟΧΗ!! expensive!!
	            if mw.title.new(tempstr).exists then
	            	output = output .. ' [[' .. tempstr .. ']]' 
	                tempstr = yearstring(etosasnum + 5)
	                --ΠΡΟΣΟΧΗ!! expensive!!
	                if mw.title.new(tempstr).exists then
	                	output = output .. ' [[' .. tempstr .. '|►]]' 
	                    tempstr = yearstring(etosasnum + 10)
	                    --ΠΡΟΣΟΧΗ!! expensive!!
	                    if mw.title.new(tempstr).exists then
	                    	output = output .. ' [[' .. tempstr .. '|►►]]' 
                    	end
                	end
            	end
        	end
    	end
    end
    return output
end
--για χρήση στο πρότυπο έτος και έτος π.Χ.
--εμπεριέχει και την κατηγορία
--δεν εμφανίζει σωστά το div
p['πρότυποέτος'] = function (frame)
	PAGENAME = mw.title.getCurrentTitle().text
    --αν έχει π.Χ. να το αφαιρούμε και να γίνει αρνητικός αριθμός
    local startx, stopx
    startx, stopx = mw.ustring.find(PAGENAME, ' π.Χ.')
    if startx == nil then
    	etosasnum = tonumber(PAGENAME)
    --αν έχει στον τίτλο το π.Χ.
    --τον μετατρέπω σε αρνητικό +1
    --το 1 π.Χ. θα γίνει 0, το 2 π.Χ. θα γίνει -1
	else
		etosasnum = 1-tonumber(mw.ustring.sub(PAGENAME,1,startx-1))
	end
	--δεν επεξεργάζεται το <div> με preprocess
    local output = ''
    output = p.aiones(etosasnum)
    --η "εδώ" προσέγγιση είναι να μην εμφανίζονται δεκαετίες πριν το 1900
    --(ίσως να γίνεται έλεγχος για το αν υπάρχουν αλλά είναι expensive!!!-)
    --Επίσης μπορεί να αφαιρεθεί εξ ολοκλήρου η εμφάνιση των δεκαετιών
    --Αν χρειαστεί μπορούμε να τις προσθέσουμε στο πινακάκι
    --if etosasnum >= 1900 then
        --output = output .. '<br/>' .. p.dekaeties(etosasnum)
    --end
    output = output .. '<br/>' .. p.eth(etosasnum)
    --Κατηγορία χ αιώνας
    --output = output .. ' [[Κατηγορία:' .. centurystring(etosasnum) .. '|#' .. mw.ustring.format(etosasnum,'%5d') .. ']]'
    local preprocessed = frame:preprocess(output)
    
    local boiled = '<div class="metadata" style="border: 1px solid #ccd2d9;  background: white; padding: 0.5em 1em 0.5em 1em; text-align: center;">'
    boiled = boiled .. preprocessed .. '</div> [[Κατηγορία:' .. centurystring(etosasnum) .. '|#' .. mw.ustring.format(etosasnum,'%5d') .. ']]'
    return boiled
    --return frame:preprocess(output)
end

--###############################

return  p