mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-19 11:08:48 +02:00
feat:#57 fixed bug
This commit is contained in:
@ -18,6 +18,7 @@ package v1
|
||||
|
||||
import (
|
||||
"golang.org/x/net/html"
|
||||
"log/slog"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -39,6 +40,10 @@ func findFirstTable(node *html.Node) *html.Node {
|
||||
// Find the first <span> element with the specified class attribute value
|
||||
func findFirstSpanWithClass(node *html.Node, classValue string) *html.Node {
|
||||
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if the current node is a <span> element with the specified class attribute value
|
||||
if node.Type == html.ElementNode && node.Data == "span" {
|
||||
if hasClassAttribute(node, classValue) {
|
||||
@ -66,20 +71,45 @@ func hasClassAttribute(node *html.Node, classValue string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type dayTable struct {
|
||||
day string
|
||||
table []*html.Node
|
||||
}
|
||||
|
||||
// Get Tables with days
|
||||
func getEventTables(node *html.Node) [][]*html.Node {
|
||||
var eventTables [][]*html.Node
|
||||
func getEventTables(node *html.Node, dayLabels []string) map[string][]*html.Node {
|
||||
// Create a map to store the tables with the corresponding day from the dayLabels
|
||||
dayTablesMap := make(map[string][]*html.Node)
|
||||
|
||||
tables := findTables(node)
|
||||
// get all tables with events
|
||||
for events := range tables {
|
||||
rows := findTableRows(tables[events])
|
||||
// check that a first row exists
|
||||
if len(rows) > 0 {
|
||||
rows = rows[1:]
|
||||
eventTables = append(eventTables, rows)
|
||||
|
||||
// Ensure we have the same number of tables as day labels
|
||||
if len(tables) != len(dayLabels) {
|
||||
// Handle the case where the number of tables doesn't match the dayLabels (log error or return early)
|
||||
slog.Error("Number of tables does not match number of day labels")
|
||||
return dayTablesMap // Returning empty map
|
||||
}
|
||||
|
||||
// Iterate over dayLabels and their corresponding tables
|
||||
for i, day := range dayLabels {
|
||||
rows := findTableRows(tables[i])
|
||||
|
||||
// check that rows exist and skip the header
|
||||
if len(rows) > 1 {
|
||||
rows = rows[1:] // Skip header row
|
||||
// Add the event rows to the map entry for this day
|
||||
dayTablesMap[day] = rows
|
||||
}
|
||||
}
|
||||
return eventTables
|
||||
|
||||
// Remove days that have no events (empty slices)
|
||||
for day, eventTable := range dayTablesMap {
|
||||
if len(eventTable) == 0 {
|
||||
delete(dayTablesMap, day)
|
||||
}
|
||||
}
|
||||
|
||||
return dayTablesMap
|
||||
}
|
||||
|
||||
// Get Tables with days
|
||||
|
Reference in New Issue
Block a user