Dualcoding con @bachoseven per la pagina "/storia" più qualche miglioria

main-old
Antonio De Lucreziis 3 years ago
parent e3b71962e5
commit b2a9355d1d

@ -1,16 +1,25 @@
module git.phc.dm.unipi.it/phc/website module git.phc.dm.unipi.it/phc/website
go 1.13 go 1.18
require ( require (
github.com/alecthomas/chroma v0.9.4 github.com/alecthomas/chroma v0.9.4
github.com/gofiber/fiber/v2 v2.34.0 github.com/gofiber/fiber/v2 v2.34.0
github.com/gofiber/redirect/v2 v2.1.23 github.com/gofiber/redirect/v2 v2.1.23
github.com/joho/godotenv v1.4.0 github.com/joho/godotenv v1.4.0
github.com/klauspost/compress v1.15.6 // indirect
github.com/litao91/goldmark-mathjax v0.0.0-20210217064022-a43cf739a50f github.com/litao91/goldmark-mathjax v0.0.0-20210217064022-a43cf739a50f
github.com/yuin/goldmark v1.4.4 github.com/yuin/goldmark v1.4.4
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
) )
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/klauspost/compress v1.15.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.37.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
)

@ -52,6 +52,8 @@ github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 h1:0SJnXjE4jDClMW6grE0xpNhwpqbPwkBTn8zpVw5C0SI= github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 h1:0SJnXjE4jDClMW6grE0xpNhwpqbPwkBTn8zpVw5C0SI=
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01/go.mod h1:TwKQPa5XkCCRC2GRZ5wtfNUTQ2+9/i19mGRijFeJ4BE= github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01/go.mod h1:TwKQPa5XkCCRC2GRZ5wtfNUTQ2+9/i19mGRijFeJ4BE=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f h1:KK6mxegmt5hGJRcAnEDjSNLxIRhZxDcgwMbcO/lMCRM=
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

@ -1,7 +1,6 @@
package main package main
import ( import (
"encoding/json"
"html/template" "html/template"
"git.phc.dm.unipi.it/phc/website/articles" "git.phc.dm.unipi.it/phc/website/articles"
@ -45,7 +44,7 @@ func main() {
"/": "home.html", "/": "home.html",
"/link": "link.html", "/link": "link.html",
"/login": "login.html", "/login": "login.html",
"/storia": "storia.html", "/utenti": "utenti.html",
} }
for route, view := range actuallyStaticRoutes { for route, view := range actuallyStaticRoutes {
@ -62,19 +61,18 @@ func main() {
return err return err
} }
c.Type("json") return c.JSON(utenti)
return json.NewEncoder(c).Encode(utenti)
}) })
app.Get("/utenti", func(c *fiber.Ctx) error { app.Get("/storia", func(c *fiber.Ctx) error {
utenti, err := GetUtenti() storia, err := GetStoria()
if err != nil { if err != nil {
return err return err
} }
c.Type("html") c.Type("html")
return renderer.Render(c, "utenti.html", util.H{ return renderer.Render(c, "storia.html", util.H{
"Utenti": utenti, "Storia": storia,
}) })
}) })

@ -800,7 +800,7 @@ form .field-set input {
} }
.page-storia .history-container .events .spacer { .page-storia .history-container .events .spacer {
height: 4rem; height: calc(var(--size) * 1rem);
} }
@keyframes rotate { @keyframes rotate {

@ -3,40 +3,127 @@ package main
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"math"
"sort"
"strconv"
"strings"
"git.phc.dm.unipi.it/phc/website/util"
) )
type byEventDateDescending []*GenericEvent
func (m byEventDateDescending) Len() int {
return len(m)
}
func (m byEventDateDescending) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m byEventDateDescending) Less(i, j int) bool {
return m[i].Date > m[j].Date
}
type Macchinista struct { type Macchinista struct {
Name string `json:"username"` Uid string `json:"uid"`
Uid string `json:"password"` FullName string `json:"fullName"`
Date string `json:"date"` EntryDate string `json:"entryDate"`
Type string `json:"type"` ExitDate string `json:"exitDate"`
} }
type GenericEvent struct { type GenericEvent struct {
// Campi principali
Type string `json:"type"`
Date string `json:"date"`
// Altri campi utilizzati ogni tanto
Title string `json:"title"` Title string `json:"title"`
Description string `json:"description"` Description string `json:"description"`
Date string `json:"date"`
Type string `json:"type"` // Se il tipo di evento è riferito ad un utente usiamo anche questo campo apposta
Uid string `json:"uid"`
FullName string `json:"fullName"`
// Se il campo è uno "spacer" questa è la sua dimensione
Size int `json:"size"`
} }
type EventsDB struct { type HistoryDB struct {
Macchinisti map[string]*Macchinista `json:"macchinisti"` Macchinisti []*Macchinista `json:"macchinisti"`
GenericEvent map[string]*GenericEvent `json:"eventi,omitempty"` GenericEvent []*GenericEvent `json:"eventi"`
} }
func GetEvents() ([]EventsDB, error) { func GetRawHistory() (*HistoryDB, error) {
var events []EventsDB var rawHistory HistoryDB
eventsJsonData, err := ioutil.ReadFile("./storia.json") historyDB, err := ioutil.ReadFile("./storia.json")
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := json.Unmarshal(eventsJsonData, &events); err != nil { if err := json.Unmarshal(historyDB, &rawHistory); err != nil {
return nil, err return nil, err
} }
return events, nil return &rawHistory, nil
} }
// TODO: Logica per ordinare gli eventi func getDateYear(date string) int {
year, err := strconv.Atoi(strings.Split(date, "/")[0])
if err != nil {
panic(err) // Tanto nel caso si nota in fase di sviluppo visto che "storia.json" è statico
}
return year
}
func withSpacers(events []*GenericEvent) []*GenericEvent {
// crea un nuovo slice con capacità già almeno "len(events)"
newEvents := make([]*GenericEvent, 0, len(events))
for i := 0; i < len(events)-1; i++ {
newEvents = append(newEvents, events[i])
delta := util.Abs(getDateYear(events[i+1].Date) - getDateYear(events[i].Date))
if delta > 1 {
logDelta := int(math.Log(float64(delta)))
newEvents = append(newEvents, &GenericEvent{
Type: "spacer",
Size: logDelta,
})
}
}
newEvents = append(newEvents, events[len(events)-1])
return newEvents
}
func GetStoria() ([]*GenericEvent, error) {
history, err := GetRawHistory()
if err != nil {
return nil, err
}
events := history.GenericEvent[:]
for _, m := range history.Macchinisti {
if m.ExitDate != "" {
events = append(events, &GenericEvent{
Type: "exit-macchinista",
Uid: m.Uid,
FullName: m.FullName,
Date: m.ExitDate,
})
}
if m.EntryDate != "" {
events = append(events, &GenericEvent{
Type: "entry-macchinista",
Uid: m.Uid,
FullName: m.FullName,
Date: m.EntryDate,
})
}
}
sort.Sort(byEventDateDescending(events))
return withSpacers(events), nil
}

@ -1,22 +1,44 @@
[
"macchinisti":
{ {
"nome": "Francesco Minnocci", "macchinisti": [
{
"uid": "minnocci", "uid": "minnocci",
"date": "2022/02", "fullName": "Francesco Minnocci",
"type": "entry" "entryDate": "2022/02"
},
{
"uid": "manicastri",
"fullName": "Francesco Manicastri",
"entryDate": "2022/05"
},
{
"uid": "delucreziis",
"fullName": "Antonio De Lucreziis",
"entryDate": "2019/??"
}, },
{ {
"nome": "Francesco Caporali",
"uid": "caporali", "uid": "caporali",
"date": "201?/??", "fullName": "Francesco Caporali",
"type": "exit" "entryDate": "2018/??",
} "exitDate": "2022/03"
}, },
"eventi":
{ {
"uid": "dachille",
"fullName": "Letizia D'Achille",
"entryDate": "2018/??",
"exitDate": "2022/03"
}
],
"eventi": [
{
"type": "simple",
"title": "Nuovo sito del PHC", "title": "Nuovo sito del PHC",
"description": "TBA", "description": "TBA",
"date": "2022/??" "date": "2022/??"
},
{
"type": "simple",
"title": "Apertura del PHC",
"date": "1992"
} }
] ]
}

@ -0,0 +1,17 @@
package util
import (
"golang.org/x/exp/constraints"
)
type Numeric interface {
constraints.Float | constraints.Integer
}
func Abs[T Numeric](a T) T {
if a < 0 {
return -a
}
return a
}

@ -12,68 +12,39 @@
<div class="history-container"> <div class="history-container">
<div class="timeline-bar"></div> <div class="timeline-bar"></div>
<div class="events"> <div class="events">
<!-- {{ range .Events }} --> {{ range .Storia }}
<!-- {{ if (.IsMacchinista) }} --> {{ if eq .Type "simple" }}
<!-- {{ if (.IsEntry) }} -->
<!-- <i class="fa-solid fa-sign-in"></i> -->
<!-- {{ end }} -->
<!-- {{ if (.IsExit) }} -->
<!-- {{ end }} -->
<!-- {{ end }} -->
<!-- {{ if (.IsGeneric) }} -->
<!-- <div class="description">{{ .Description }}</div> -->
<!-- {{ if (.IsAnniversary) }} -->
<!-- {{ end }} -->
<!-- {{ end }} -->
<!-- {{ end }} -->
<div class="event"> <div class="event">
<div class="title">Nuovo sito del PHC</div> <div class="title">
<div class="date">2022/??</div> {{ .Title }}
</div> </div>
<div class="spacer"></div> <div class="date">{{ .Date }}</div>
<div class="event">
<div class="title">Ingresso di <a href="#">Francesco Manicastri</a></div>
<div class="date">2022/05</div>
</div>
<div class="event">
<div class="title">Ingresso di <a href="#">Francesco Minnocci</a></div>
<div class="date">2022/02</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Ingresso di <a href="#">Antonio De Lucreziis</a></div>
<div class="date">201?/??</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Ingresso di <a href="#">Francesco Caporali</a></div>
<div class="date">201?/??</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Ingresso di <a href="#">Letizia D'Achille</a></div>
<div class="date">201?/??</div>
</div> </div>
<div class="spacer"></div> {{ end }}
{{ if eq .Type "entry-macchinista" }}
<div class="event"> <div class="event">
<div class="title">Ingresso di <a href="#">Giorgio Mossa</a></div> <div class="title">
<div class="date">201?/??</div> Ingresso di
<i class="fa-solid fa-sign-in"></i>
<a href="{{ $.Config.UserPagesBaseUrl }}{{ .Uid }}">{{ .FullName }}</a>
</div> </div>
<div class="event"> <div class="date">{{ .Date }}</div>
<div class="title">Ingresso di <a href="#">Cristiano Ricci</a></div>
<div class="date">201?/??</div>
</div> </div>
<div class="spacer"></div> {{ end }}
<div class="spacer"></div> {{ if eq .Type "exit-macchinista" }}
<div class="event"> <div class="event">
<div class="title">Prima versione del sito "Poisson"</div> <div class="title">
<div class="date">~1996/06/17</div> Uscita di
<i class="fa-solid fa-sign-out"></i>
<a href="{{ $.Config.UserPagesBaseUrl }}{{ .Uid }}">{{ .FullName }}</a>
</div> </div>
<div class="spacer"></div> <div class="date">{{ .Date }}</div>
<div class="event">
<div class="title">Apertura del PHC</div>
<div class="date">~1994</div>
</div> </div>
{{ end }}
{{ if eq .Type "spacer" }}
<div class="spacer" style="--size: {{ .Size }}"></div>
{{ end }}
{{ end }}
</div> </div>
</div> </div>
</section> </section>

Loading…
Cancel
Save