From 7ef603e0d5189eff56e6b62b7aa687b3cec2d287 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Fri, 24 May 2024 23:40:24 -0600 Subject: [PATCH] fixup! templates: render static pages using templ Signed-off-by: Sumner Evans --- internal/application.go | 16 +- internal/archive.go | 359 ++++++++++++++----------------- internal/templates/archive.templ | 141 ++++++++++++ website/templates/archive.html | 78 ------- 4 files changed, 309 insertions(+), 285 deletions(-) create mode 100644 internal/templates/archive.templ delete mode 100644 website/templates/archive.html diff --git a/internal/application.go b/internal/application.go index 66f3149..8c580a6 100644 --- a/internal/application.go +++ b/internal/application.go @@ -109,7 +109,7 @@ func (a *Application) Start() { noArgs := func(r *http.Request) map[string]any { return nil } // Static pages - staticPages2 := map[string]struct { + staticPages := map[string]struct { title string pageName partials.PageName content templ.Component @@ -120,8 +120,9 @@ func (a *Application) Start() { "GET /rules": {"Rules", partials.PageNameRules, templates.Rules()}, "GET /register": {"Register", partials.PageNameRegister, templates.Register()}, "GET /faq": {"FAQ", partials.PageNameFAQ, templates.FAQ()}, + "GET /archive": {"Archive", partials.PageNameArchive, templates.Archive(archiveInfo)}, } - for path, pageInfo := range staticPages2 { + for path, pageInfo := range staticPages { router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() user, err := a.GetLoggedInTeacher(r) @@ -136,17 +137,6 @@ func (a *Application) Start() { }) } - // Static pages - staticPages := map[string]struct { - Template string - ArgGenerator func(r *http.Request) map[string]any - }{ - "/archive": {"archive.html", a.GetArchiveTemplate}, - } - for path, templateInfo := range staticPages { - router.HandleFunc("GET "+path, a.ServeTemplate(a.Log, templateInfo.Template, templateInfo.ArgGenerator)) - } - // Redirect pages redirects := map[string]string{ "/register/teacher": "/register/teacher/createaccount", diff --git a/internal/archive.go b/internal/archive.go index 7c8a73b..74ac427 100644 --- a/internal/archive.go +++ b/internal/archive.go @@ -1,227 +1,198 @@ package internal import ( - "net/http" + "github.com/ColoradoSchoolOfMines/mineshspc.com/internal/templates" ) -type Link struct { - URL string - Title string -} - -type WinningTeam struct { - Place string - Name string - School string - Location string -} - -type CompetitionResult struct { - Name string - Shortname string - Teams []WinningTeam -} - -type YearInfo struct { - Year int - RecapParagraphs []string - Links []Link - Results []CompetitionResult -} - -func (a *Application) GetArchiveTemplate(*http.Request) map[string]any { - return map[string]any{ - "YearInfo": []YearInfo{ +var archiveInfo = []templates.YearInfo{ + { + Year: 2024, + RecapParagraphs: []string{ + "The 2024 competition returned to an in-person only competition, but we also had an open division. We gave a separate set of prizes for teams consisting of only first-time competitors. We did not award prizes for the open division.", + "The in-person competition had 27 teams while the open division had 31 teams.", + }, + Links: []templates.Link{ + {URL: "/static/2024-solutions.pdf", Title: "Solution Sketch Slides"}, + {URL: "https://sumnerevans.com/posts/school/2024-hspc/", Title: "Competition Recap and Solution Sketches"}, + {URL: "https://mines-hspc.kattis.com/contests/mines-hspc24/problems", Title: "Problems"}, + }, + Results: []templates.CompetitionResult{ { - Year: 2024, - RecapParagraphs: []string{ - "The 2024 competition returned to an in-person only competition, but we also had an open division. We gave a separate set of prizes for teams consisting of only first-time competitors. We did not award prizes for the open division.", - "The in-person competition had 27 teams while the open division had 31 teams.", - }, - Links: []Link{ - {"/static/2024-solutions.pdf", "Solution Sketch Slides"}, - {"https://sumnerevans.com/posts/school/2024-hspc/", "Competition Recap and Solution Sketches"}, - {"https://mines-hspc.kattis.com/contests/mines-hspc24/problems", "Problems"}, - }, - Results: []CompetitionResult{ - { - Name: "Overall Winners", - Shortname: "Overall", - Teams: []WinningTeam{ - {"1st", "Innovation Center 1", "Innovation Center SVVSD", "Longmont"}, - {"2nd", "Sigma Scripters", "Arapahoe High School", "Centennial"}, - {"3nd", "CyberRebels2", "Columbine High School", "Littleton"}, - }, - }, - { - Name: "First-Time Team Winners", - Shortname: "FirstTime", - Teams: []WinningTeam{ - {"1st", "Loopy Groupies", "Chatfield Senior High School", "Littleton"}, - {"2nd", "Lorem Ipsum", "Warren Tech", "Lakewood"}, - {"3nd", "the cows(mooooooooooooo)", "Cherry Creek High School", "Greenwood Village"}, - }, - }, + Name: "Overall Winners", + Shortname: "Overall", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "Innovation Center 1", School: "Innovation Center SVVSD", Location: "Longmont"}, + {Place: "2nd", Name: "Sigma Scripters", School: "Arapahoe High School", Location: "Centennial"}, + {Place: "3nd", Name: "CyberRebels2", School: "Columbine High School", Location: "Littleton"}, }, }, { - Year: 2023, - RecapParagraphs: []string{ - "The 2023 competition again featured two divisions: beginner and advanced. As with 2022, it was a hybrid competition, but we awarded prizes for both in-person and remote winners in both divisions.", - "The advanced division featured 31 teams, while the beginner division had 34 teams.", - }, - Links: []Link{ - {"/static/2023-solutions.pdf", "Solution Sketch Slides"}, - {"https://sumnerevans.com/posts/school/2023-hspc/", "Competition Recap and Solution Sketches"}, - {"https://mines23advanced.kattis.com/problems", "Advanced Problems"}, - {"https://mines23beginner.kattis.com/problems", "Beginner Problems"}, - }, - Results: []CompetitionResult{ - { - Name: "Advanced In-Person", - Shortname: "AdvancedInPerson", - Teams: []WinningTeam{ - {"1st", "Code Rats", "Futures Lab", "Fort Collins, Colorado"}, - {"2nd", "The Spanish Inquisition", "Regis Jesuit High School", "Aurora, Colorado"}, - {"3nd", "CA is 202", "Colorado Academy", "Denver, Colorado"}, - }, - }, - { - Name: "Beginner In-Person", - Shortname: "BeginnerInPerson", - Teams: []WinningTeam{ - {"1st", "Spaghetti Code and Meatballs", "Warren Tech", "Lakewood, Colorado"}, - {"2nd", "Innovation Center 1", "Innovation Center SVVSD", "Longmont, Colorado"}, - {"3nd", "Team LuLo", "Colorado Academy", "Denver, Colorado"}, - }, - }, - { - Name: "Advanced Remote", - Shortname: "AdvancedRemote", - Teams: []WinningTeam{ - {"1st", "River Hill Team #1", "River Hill High School", "Clarksville, Maryland"}, - {"2nd", "CreekCyberBruins", "Cherry Creek High School", "Greenwood Village, Colorado"}, - {"3nd", "JMS", "Bergen County Academies", "Bergen County, New Jersey"}, - }, - }, - { - Name: "Beginner Remote", - Shortname: "BeginnerRemote", - Teams: []WinningTeam{ - {"1st", "Wormhole", "Voice of Calling NPO", "Northridge, California"}, - {"2nd", "Lineup", "Voice of Calling NPO", "Northridge, California"}, - {"3nd", "River Hill Team #2", "River Hill High School", "Clarksville, Maryland"}, - }, - }, + Name: "First-Time Team Winners", + Shortname: "FirstTime", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "Loopy Groupies", School: "Chatfield Senior High School", Location: "Littleton"}, + {Place: "2nd", Name: "Lorem Ipsum", School: "Warren Tech", Location: "Lakewood"}, + {Place: "3nd", Name: "the cows(mooooooooooooo)", School: "Cherry Creek High School", Location: "Greenwood Village"}, }, }, + }, + }, + { + Year: 2023, + RecapParagraphs: []string{ + "The 2023 competition again featured two divisions: beginner and advanced. As with 2022, it was a hybrid competition, but we awarded prizes for both in-person and remote winners in both divisions.", + "The advanced division featured 31 teams, while the beginner division had 34 teams.", + }, + Links: []templates.Link{ + {URL: "/static/2023-solutions.pdf", Title: "Solution Sketch Slides"}, + {URL: "https://sumnerevans.com/posts/school/2023-hspc/", Title: "Competition Recap and Solution Sketches"}, + {URL: "https://mines23advanced.kattis.com/problems", Title: "Advanced Problems"}, + {URL: "https://mines23beginner.kattis.com/problems", Title: "Beginner Problems"}, + }, + Results: []templates.CompetitionResult{ { - Year: 2022, - RecapParagraphs: []string{ - "The 2022 competition was the first to feature two divisions: a beginner division and an advanced division. It was also the first hybrid competition with both remote and in-person contestants.", - "The advanced division had 26 teams, while the beginner division had 39 teams. Due to the number of teams, we decided to give awards to first place through fourth place.", - }, - Links: []Link{ - {"https://sumnerevans.com/posts/school/2022-hspc/", "Competition Recap and Solution Sketches"}, - {"https://mines22advanced.kattis.com/problems", "Advanced Problems"}, - {"https://mines22beginner.kattis.com/problems", "Beginner Problems"}, - }, - Results: []CompetitionResult{ - { - Name: "Advanced", - Teams: []WinningTeam{ - {"1st", "Pen A Team", "PEN Academy", "Cresskill, New Jersey"}, - {"2nd", "Cherry Creek Cobras", "Cherry Creek High School", "Greenwood Village, Colorado"}, - {"3nd", "River Hill Team 1", "River Hill High School", "Clarksville, Maryland"}, - {"4th", "The Spanish Inquisition", "Regis Jesuit High School", "Aurora, Colorado"}, - }, - }, - { - Name: "Beginner", - Teams: []WinningTeam{ - {"1st", "LLL", "Future Forward at Bollman", "Thornton, Colorado"}, - {"2nd", "Error 404: Name not found", "Colorado Academy", "Denver, Colorado"}, - {"3nd", "Liberty 1", "Liberty Common School", "Fort Collins, Colorado"}, - {"4th", "Cool Cats", "Arvada West High School", "Arvada, Colorado"}, - }, - }, + Name: "Advanced In-Person", + Shortname: "AdvancedInPerson", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "Code Rats", School: "Futures Lab", Location: "Fort Collins, Colorado"}, + {Place: "2nd", Name: "The Spanish Inquisition", School: "Regis Jesuit High School", Location: "Aurora, Colorado"}, + {Place: "3nd", Name: "CA is 202", School: "Colorado Academy", Location: "Denver, Colorado"}, }, }, { - Year: 2021, - RecapParagraphs: []string{ - "The 2021 competition was an all-remote competition featuring 55 teams from across the nation.", - }, - Links: []Link{ - {"https://sumnerevans.com/posts/school/2021-hspc/", "Competition Recap and Solution Sketches"}, - {"https://mines21.kattis.com/problems", "Problems"}, - }, - Results: []CompetitionResult{ - { - Teams: []WinningTeam{ - {"1st", "River Hill HS Team 1", "River Hill High School", "Clarksville, Maryland"}, - {"2nd", "PEN A Team", "PEN Academy", "Cresskill, New Jersey"}, - {"3nd", "River Hill HS Team 2", "River Hill High School", "Clarksville, Maryland"}, - }, - }, + Name: "Beginner In-Person", + Shortname: "BeginnerInPerson", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "Spaghetti Code and Meatballs", School: "Warren Tech", Location: "Lakewood, Colorado"}, + {Place: "2nd", Name: "Innovation Center 1", School: "Innovation Center SVVSD", Location: "Longmont, Colorado"}, + {Place: "3nd", Name: "Team LuLo", School: "Colorado Academy", Location: "Denver, Colorado"}, }, }, { - Year: 2020, - RecapParagraphs: []string{ - "Due to COVID, the 2020 competition was the first all-remote HSPC competition. The competition featured 30 teams.", + Name: "Advanced Remote", + Shortname: "AdvancedRemote", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "River Hill Team #1", School: "River Hill High School", Location: "Clarksville, Maryland"}, + {Place: "2nd", Name: "CreekCyberBruins", School: "Cherry Creek High School", Location: "Greenwood Village, Colorado"}, + {Place: "3nd", Name: "JMS", School: "Bergen County Academies", Location: "Bergen County, New Jersey"}, }, - Links: []Link{ - {"https://sumnerevans.com/posts/school/2020-hspc/", "Competition Recap and Solution Sketches"}, - {"https://mines20.kattis.com/problems", "Problems"}, - }, - Results: []CompetitionResult{ - { - Teams: []WinningTeam{ - {"1st", "Installation Wizards", "STEM School Highlands Ranch", "Highlands Ranch, Colorado"}, - {"2nd", "i", "STEM School Highlands Ranch", "Highlands Ranch, Colorado"}, - {"3nd", "Sun Devils", "Kent Denver", "Denver, Colorado"}, - }, - }, + }, + { + Name: "Beginner Remote", + Shortname: "BeginnerRemote", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "Wormhole", School: "Voice of Calling NPO", Location: "Northridge, California"}, + {Place: "2nd", Name: "Lineup", School: "Voice of Calling NPO", Location: "Northridge, California"}, + {Place: "3nd", Name: "River Hill Team #2", School: "River Hill High School", Location: "Clarksville, Maryland"}, }, }, + }, + }, + { + Year: 2022, + RecapParagraphs: []string{ + "The 2022 competition was the first to feature two divisions: a beginner division and an advanced division. It was also the first hybrid competition with both remote and in-person contestants.", + "The advanced division had 26 teams, while the beginner division had 39 teams. Due to the number of teams, we decided to give awards to first place through fourth place.", + }, + Links: []templates.Link{ + {URL: "https://sumnerevans.com/posts/school/2022-hspc/", Title: "Competition Recap and Solution Sketches"}, + {URL: "https://mines22advanced.kattis.com/problems", Title: "Advanced Problems"}, + {URL: "https://mines22beginner.kattis.com/problems", Title: "Beginner Problems"}, + }, + Results: []templates.CompetitionResult{ { - Year: 2019, - RecapParagraphs: []string{ - "The second ever CS@Mines High School Programming Competition featured 22 teams from all around Colorado and from as far as Steamboat Springs.", + Name: "Advanced", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "Pen A Team", School: "PEN Academy", Location: "Cresskill, New Jersey"}, + {Place: "2nd", Name: "Cherry Creek Cobras", School: "Cherry Creek High School", Location: "Greenwood Village, Colorado"}, + {Place: "3nd", Name: "River Hill Team 1", School: "River Hill High School", Location: "Clarksville, Maryland"}, + {Place: "4th", Name: "The Spanish Inquisition", School: "Regis Jesuit High School", Location: "Aurora, Colorado"}, }, - Links: []Link{ - {"https://sumnerevans.com/posts/school/2019-hspc/", "Competition Recap and Solution Sketches"}, - {"https://mines19.kattis.com/problems", "Problems"}, + }, + { + Name: "Beginner", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "LLL", School: "Future Forward at Bollman", Location: "Thornton, Colorado"}, + {Place: "2nd", Name: "Error 404: Name not found", School: "Colorado Academy", Location: "Denver, Colorado"}, + {Place: "3nd", Name: "Liberty 1", School: "Liberty Common School", Location: "Fort Collins, Colorado"}, + {Place: "4th", Name: "Cool Cats", School: "Arvada West High School", Location: "Arvada, Colorado"}, }, - Results: []CompetitionResult{ - { - Teams: []WinningTeam{ - {"1st", "STEM Team 1", "STEM School Highlands Ranch", "Highlands Ranch, Colorado"}, - {"2nd", "IntrospectionExceptions", "Colorado Academy", "Lakewood, Colorado"}, - {"3nd", "Team 2", "?", "?"}, - }, - }, + }, + }, + }, + { + Year: 2021, + RecapParagraphs: []string{ + "The 2021 competition was an all-remote competition featuring 55 teams from across the nation.", + }, + Links: []templates.Link{ + {URL: "https://sumnerevans.com/posts/school/2021-hspc/", Title: "Competition Recap and Solution Sketches"}, + {URL: "https://mines21.kattis.com/problems", Title: "Problems"}, + }, + Results: []templates.CompetitionResult{ + { + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "River Hill HS Team 1", School: "River Hill High School", Location: "Clarksville, Maryland"}, + {Place: "2nd", Name: "PEN A Team", School: "PEN Academy", Location: "Cresskill, New Jersey"}, + {Place: "3nd", Name: "River Hill HS Team 2", School: "River Hill High School", Location: "Clarksville, Maryland"}, }, }, + }, + }, + { + Year: 2020, + RecapParagraphs: []string{ + "Due to COVID, the 2020 competition was the first all-remote HSPC competition. The competition featured 30 teams.", + }, + Links: []templates.Link{ + {URL: "https://sumnerevans.com/posts/school/2020-hspc/", Title: "Competition Recap and Solution Sketches"}, + {URL: "https://mines20.kattis.com/problems", Title: "Problems"}, + }, + Results: []templates.CompetitionResult{ { - Year: 2018, - RecapParagraphs: []string{ - "The first ever CS@Mines High School Programming Competition featured 22 teams.", + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "Installation Wizards", School: "STEM School Highlands Ranch", Location: "Highlands Ranch, Colorado"}, + {Place: "2nd", Name: "i", School: "STEM School Highlands Ranch", Location: "Highlands Ranch, Colorado"}, + {Place: "3nd", Name: "Sun Devils", School: "Kent Denver", Location: "Denver, Colorado"}, }, - Links: []Link{ - {"https://mines18.kattis.com/problems", "Problems"}, + }, + }, + }, + { + Year: 2019, + RecapParagraphs: []string{ + "The second ever CS@Mines High School Programming Competition featured 22 teams from all around Colorado and from as far as Steamboat Springs.", + }, + Links: []templates.Link{ + {URL: "https://sumnerevans.com/posts/school/2019-hspc/", Title: "Competition Recap and Solution Sketches"}, + {URL: "https://mines19.kattis.com/problems", Title: "Problems"}, + }, + Results: []templates.CompetitionResult{ + { + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "STEM Team 1", School: "STEM School Highlands Ranch", Location: "Highlands Ranch, Colorado"}, + {Place: "2nd", Name: "IntrospectionExceptions", School: "Colorado Academy", Location: "Lakewood, Colorado"}, + {Place: "3nd", Name: "Team 2", School: "?", Location: "?"}, }, - Results: []CompetitionResult{ - { - Teams: []WinningTeam{ - {"1st", "The Crummies", "Warren Tech", "Arvada, Colorado"}, - {"2nd", "The Bean Beans", "Colorado Academy", "Lakewood, Colorado"}, - {"3nd", "Warriors", "Arapahoe High School", "Centennial, Colorado"}, - }, - }, + }, + }, + }, + { + Year: 2018, + RecapParagraphs: []string{ + "The first ever CS@Mines High School Programming Competition featured 22 teams.", + }, + Links: []templates.Link{ + {URL: "https://mines18.kattis.com/problems", Title: "Problems"}, + }, + Results: []templates.CompetitionResult{ + { + Teams: []templates.WinningTeam{ + {Place: "1st", Name: "The Crummies", School: "Warren Tech", Location: "Arvada, Colorado"}, + {Place: "2nd", Name: "The Bean Beans", School: "Colorado Academy", Location: "Lakewood, Colorado"}, + {Place: "3nd", Name: "Warriors", School: "Arapahoe High School", Location: "Centennial, Colorado"}, }, }, }, - } + }, } diff --git a/internal/templates/archive.templ b/internal/templates/archive.templ new file mode 100644 index 0000000..1d91545 --- /dev/null +++ b/internal/templates/archive.templ @@ -0,0 +1,141 @@ +package templates + +import "strconv" + +type Link struct { + URL templ.SafeURL + Title string +} + +type WinningTeam struct { + Place string + Name string + School string + Location string +} + +type CompetitionResult struct { + Name string + Shortname string + Teams []WinningTeam +} + +type YearInfo struct { + Year int + RecapParagraphs []string + Links []Link + Results []CompetitionResult +} + +func accordionHeadingID(year int, shortname string) string { + return "heading" + strconv.Itoa(year) + shortname +} + +func accordionCollapseCSSID(year int, shortname string) string { + return "#" + accordionCollapseID(year, shortname) +} + +func accordionCollapseID(year int, shortname string) string { + return "winners" + strconv.Itoa(year) + shortname +} + +func trophyColor(place int) string { + switch place + 1 { + case 1: + return "#FFD700" + case 2: + return "#C0C0C0" + default: + return "#CD7F32" + } +} + +css trophyColorStyle(place int) { + color: { trophyColor(place) }; +} + +templ Archive(years []YearInfo) { +
+ +
+
+ for _, year := range years { +
+
+

{ strconv.Itoa(year.Year) }

+ for _, p := range year.RecapParagraphs { +

{ p }

+ } +
+
+
+ for _, r := range year.Results { +
+

+ +

+
+
+
+ for i, t := range r.Teams { +
  • + + + { t.Place } + + { t.Name } +

    + { t.School } • { t.Location } +

    +
  • + } +
    +
    + } +
    +
    +
    +
    + for i, link := range year.Links { + if i > 0 { +  •  + } + { link.Title } + } +
    +
    +
    + } +
    +} diff --git a/website/templates/archive.html b/website/templates/archive.html deleted file mode 100644 index 1abae20..0000000 --- a/website/templates/archive.html +++ /dev/null @@ -1,78 +0,0 @@ -{{ define "title" }}Archive{{ end }} - -{{ define "content" }} -
    - -
    - -
    - {{- range $y := .Data.YearInfo -}} -
    -
    -

    {{ .Year }}

    - {{- range .RecapParagraphs -}}

    {{ . }}

    {{- end -}} -
    -
    -
    - {{- range $i, $r := .Results -}} -
    -

    - -

    -
    -
    - {{- range $j, $t := .Teams -}} -
  • - - {{ if eq $j 0 }} - - {{ else if eq $j 1 }} - - {{ else }} - - {{ end }} - {{ .Place }} - - {{ .Name }} -

    - {{ .School }} • {{ .Location }} -

    -
  • - {{- end -}} -
    -
    -
    - {{- end -}} -
    -
    -
    -
    -
    - {{ $length := len .Links }} - {{- range $i, $r := .Links -}} - {{- if $i -}} -  •  - {{- end -}} - {{- .Title -}} - {{- end -}} -
    -
    - {{- end -}} -
    -{{ end }}